Posted 09 February 2016 - 10:36 PM
So I've been trying to make a simple system to prevent people from defining global variables, and I did it like this:
However, when I run it on a simple program,
It tells me there's an ArrayIndexOutOfBoundsException at line 5 in gman, which is the one setting the metatable on env. Does anyone know why this is happening and how to fix it?
Ah, I found two problems after looking at my code for a while. I got two variables backwards, and I got a stack overflow by doing env[var] = val in the __newindex metamethod.
args = {...}
if (#args == 1) then
func = loadfile(args[1])
env = {}
setmetatable(env, {__index = _G, __newindex = function(table, val, var) env[var] = val end})
setfenv(func, env)
func()
end
However, when I run it on a simple program,
a = 1 print(a)
It tells me there's an ArrayIndexOutOfBoundsException at line 5 in gman, which is the one setting the metatable on env. Does anyone know why this is happening and how to fix it?
Ah, I found two problems after looking at my code for a while. I got two variables backwards, and I got a stack overflow by doing env[var] = val in the __newindex metamethod.