preinit.lua
一定要在初始化lua环境函数 执行此lua脚本 _luaEnv.DoString("require 'preinit'");
1 if jit then
2 jit.off()
3 jit.flush()
4 end
5
6 local __g = _G
7 -- 导出全局变量
8 exports = {}
9 setmetatable(exports, {
10 __newindex = function(_, name, value)
11 rawset(__g, name, value)
12 end,
13
14 __index = function(_, name)
15 return rawget(__g, name)
16 end
17 })
18 -- 禁止创建未预期的全局变量
19 setmetatable(__g, {
20 __newindex = function(_, name, value)
21 local msg = "USE 'exports.%s = value' INSTEAD OF SET GLOBAL VARIABLE"
22 error(string.format(msg, name), 0)
23 end
24 })