| 910 | } |
| 911 | |
| 912 | void EnableGlobalsProtection(lua_State *lua) { |
| 913 | const char *code = |
| 914 | "local dbg=debug\n" |
| 915 | "local mt = {}\n" |
| 916 | "setmetatable(_G, mt)\n" |
| 917 | "mt.__newindex = function (t, n, v)\n" |
| 918 | " if dbg.getinfo(2) then\n" |
| 919 | " local w = dbg.getinfo(2, \"S\").what\n" |
| 920 | " if w ~= \"user_script\" and w ~= \"C\" then\n" |
| 921 | " error(\"Script attempted to create global variable '\"..tostring(n)..\"'\", 2)\n" |
| 922 | " end\n" |
| 923 | " end\n" |
| 924 | " rawset(t, n, v)\n" |
| 925 | "end\n" |
| 926 | "mt.__index = function (t, n)\n" |
| 927 | " if dbg.getinfo(2) and dbg.getinfo(2, \"S\").what ~= \"C\" then\n" |
| 928 | " error(\"Script attempted to access nonexistent global variable '\"..tostring(n)..\"'\", 2)\n" |
| 929 | " end\n" |
| 930 | " return rawget(t, n)\n" |
| 931 | "end\n" |
| 932 | "debug = nil\n"; |
| 933 | |
| 934 | luaL_loadbuffer(lua, code, strlen(code), "@enable_strict_lua"); |
| 935 | lua_pcall(lua, 0, 0, 0); |
| 936 | } |
| 937 | |
| 938 | void LoadLibraries(lua_State *lua) { |
| 939 | auto load_lib = [](lua_State *lua, const char *libname, lua_CFunction func) { |