Changes from Rio Lua
Soni L edited this page Feb 1, 2015
·
3 revisions
Clone this wiki locally
[WIP]
OP_PUSH/POPFENV, 5.1-like function/chunk envs but more powerful
Feature status: Not yet implemented (NYI)
-- setfenv() sets the default env and overrides all entries on the env stack to point to it, just like in Lua 5.1
setfenv(1, {print=print})
print "normal stuff"
do -- `do` pushes the current env on the env stack (duplicates it)
-- `setsenv` translates to an opcode (which implies it's not a function) that pushes the new env, and also pops the current env, so we can handle `do...end`; it stands for "set scope environment".
setsenv {print=function() -- function inherits current env, and in this case we haven't changed the env yet
print "not normal" -- so we can use the old print like this
-- env stacks are per chunk, functions create new chunks, thus they have a separate env stack.
end}
print() -- prints "not normal"
end -- `end` pops the current env
print "normal stuff, again"Stack underflow and stack overflow both result in an error, but the raw operators can only be accessed via bytecode, so it's unlikely to happen. The environment stack gets cleared when the function exits.
OP_ME
Feature status: Done
@()