lua 文件編譯相關工具

  1 -- 編譯一個代碼文件
  2 -- loadfile (lua_State *L, const char *filename);
  3 -- 將一個文件加載爲lua代碼塊,僅編譯不執行,返回值爲編譯後的
  4 -- 代碼塊函數和錯誤信息。
  5 function COMPILE(file)
  6     local fun, err = loadfile(file);
  7     return fun;
  8 end
  9 
 10 -- 編譯並運行一個代碼文件
 11 function LOAD_RUN(file)
 12     return require(file);
 13 end
 14 
 15 -- 載入一個目錄下的全部代碼文件(list.lua指明瞭有哪些文件)
 16 function LOAD_PATH(path)
 17     -- 取得list.lua的地址
 18     local listPath = path .. "/" .. "list";
 19 
 20     -- 加載list,須要加載的全部文件名字
 21     local fileNames = LOAD_RUN(listPath);
 22 
 23     -- 進行加載
 24     local file = {};
 25     for i = 1, #fileNames do
 26         local bin = path .. "/" .. fileNames[i];
 27         file[fileNames[i]] = LOAD_RUN(bin);
 28     end
 29     return file;
 30 end
 31 
 32 -- 卸載目錄下的全部文件
 33 function UNLOAD_PATH(path)
 34    local listPath  = path .. "/" .. "list";
 35    local fileNames = LOAD_RUN(listPath);
 36    local file = {};
 37    for i = 1, #fileNames do
 38         -- 設置已經被加載的fileNames[i]爲空
 39        package.loaded[path .. "/" .. fileNames[i]] = nil;
 40    end
 41 
 42    -- 回收內存("collect"作一次完整的垃圾收集循環)
 43    collectgarbage("collect");
 44 end
 45 
 46 -- 從新載入某個文件
 47 function update(file)
 48     local pos = string.find(file,"[^%/.]*$");
 49     local module_name = string.sub(file, pos);
 50     local mod = package.loaded[module_name];
 51     if not mod then
 52         mod = package.loaded[module_name.."M"];
 53     end
 54 
 55     -- 若該文件爲模塊,且存在 destruct 方法,則先調用析構方法,再從新加載
 56     if type(mod) == "table" and type(mod.destruct) == "function" then
 57         mod.destruct();
 58     end
 59 
 60     package.loaded[file] = false;
 61     local ret = require(file);
 62 
 63     mod = package.loaded[module_name];
 64     if not mod then
 65         mod = package.loaded[module_name.."M"];
 66     end
 67 
 68     -- 若該文件爲模塊,且存在 destruct 方法,則先調用析構方法,再從新加載
 69     if type(mod) == "table" and type(mod.init) == "function" then
 70         mod.init();
 71     end
 72 
 73     -- 回收垃圾
 74     -- 無須開啓 gc,不然會致使客戶端啓動速度變慢,見 SLIMEC-7396
 75     --collectgarbage("collect");
 76     return ret;
 77 end
 78 
 79 -- 使用代碼更新指定文件
 80 function updateByScript(file, script)
 81     local pos = string.find(file,"[^%/.]*$");
 82     local module_name = string.sub(file, pos);
 83     local mod = package.loaded[module_name];
 84     if not mod then
 85         mod = package.loaded[module_name.."M"];
 86     end
 87 
 88     -- 若該文件爲模塊,且存在 destruct 方法,則先調用析構方法,再從新加載
 89     if type(mod) == "table" and type(mod.destruct) == "function" then
 90         mod.destruct();
 91     end
 92     package.loaded[file] = false;
 93 
 94     -- 載入
 95     local ret = loadstring(script)();
 96 
 97     -- 若該文件爲模塊,且存在 destruct 方法,則先調用析構方法,再從新加載
 98     mod = package.loaded[module_name];
 99     if not mod then
100         mod = package.loaded[module_name.."M"];
101     end
102 
103     if type(mod) == "table" and type(mod.init) == "function" then
104         mod.init();
105     end
106 
107     -- 回收垃圾
108     --collectgarbage("collect");
109 
110     return ret;
111 end
112 
113 -- 從新載入一個目錄下的全部代碼文件(list.lua指明瞭有哪些文件)
114 function RRLOAD_PATH(path)
115     local listPath  = path .. "/" .. "list";
116     local fileNames = LOAD_RUN(listPath);
117     local file      = {};
118     for i = 1, #fileNames do
119         package.loaded[path .. "/" .. fileNames[i]] = nil;
120         local bin = update(path .. "/" .. fileNames[i]);
121         file[fileNames[i]] = bin;
122     end
123     return file;
124 end
complier
相關文章
相關標籤/搜索