fix for strict.lua over stripped bytecode

This commit is contained in:
Cosmin Apreutesei
2015-04-27 06:08:40 +03:00
parent 242ffbca5d
commit ea1ff57dd1
+9 -9
View File
@@ -5,6 +5,8 @@
-- (even assigning nil will do) in a main chunk before being used
-- anywhere or assigned to inside a function.
--
-- modified for better compatibility with LuaJIT, see:
-- http://www.freelists.org/post/luajit/strictlua-with-stripped-bytecode
local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget
@@ -16,15 +18,10 @@ end
mt.__declared = {}
local function what ()
local d = getinfo(3, "S")
return d and d.what or "C"
end
mt.__newindex = function (t, n, v)
if not mt.__declared[n] then
local w = what()
if w ~= "main" and w ~= "C" then
local info = getinfo(2, "S")
if info and info.linedefined > 0 then
error("assign to undeclared variable '"..n.."'", 2)
end
mt.__declared[n] = true
@@ -33,8 +30,11 @@ mt.__newindex = function (t, n, v)
end
mt.__index = function (t, n)
if not mt.__declared[n] and what() ~= "C" then
error("variable '"..n.."' is not declared", 2)
if not mt.__declared[n] then
local info = getinfo(2, "S")
if info and info.linedefined > 0 then
error("variable '"..n.."' is not declared", 2)
end
end
return rawget(t, n)
end