Simple question. Where are local variables stored?
I'm fairly sure global variables, tables and functions are stored in _G but I'm not sure where to find local variables. There's no particular reason to this, I was just curious
local variable storage
Started by Hydrotronics, Oct 08 2016 12:37 PM
6 replies to this topic
#1
Posted 08 October 2016 - 12:37 PM
#2
Posted 08 October 2016 - 12:42 PM
Under LuaJ (the implementation which ComputerCraft uses) when each function gets called it allocates an array of LuaValues to represent a local variable. When you do:
You can Cobalt's implementation of the VM here which is mostly the same as LuaJ's: note the stack variable: this stores all the locals.
local a, b = 1, 2 return a + bIt will store 1 in slot 0, 2 in slot 1 and then get slots 0 and 1 and add them together.
You can Cobalt's implementation of the VM here which is mostly the same as LuaJ's: note the stack variable: this stores all the locals.
Edited by SquidDev, 08 October 2016 - 12:42 PM.
#3
Posted 08 October 2016 - 12:50 PM
Oh ok, I'd assume we can't access stack in-game
#4
Posted 08 October 2016 - 01:38 PM
I'd assume not; certainly there's no accessible equivalent in regular Lua.
Furthermore, within ComputerCraft at least, globals you declare aren't going into _G. The bios script each system runs on boot starts the shell with a unique environment table, and all globals go into there. The shell and multishell scripts load their APIs into the current global environment, which means you can't find those through _G for example - this is why other APIs can't access shell / multishell (because they don't get access to any specific shell's environment table).
If you want the current environment table, getfenv() can return it to you.
Furthermore, within ComputerCraft at least, globals you declare aren't going into _G. The bios script each system runs on boot starts the shell with a unique environment table, and all globals go into there. The shell and multishell scripts load their APIs into the current global environment, which means you can't find those through _G for example - this is why other APIs can't access shell / multishell (because they don't get access to any specific shell's environment table).
If you want the current environment table, getfenv() can return it to you.
#5
Posted 08 October 2016 - 02:19 PM
oh cool. How would I use getfenv()? I tried using it in the lua prompt but I kept being returned with table: 68953f
#6
Posted 08 October 2016 - 02:27 PM
LemonJelly, on 08 October 2016 - 12:50 PM, said:
Oh ok, I'd assume we can't access stack in-game
LemonJelly, on 08 October 2016 - 02:19 PM, said:
oh cool. How would I use getfenv()? I tried using it in the lua prompt but I kept being returned with table: 68953f
local env = getfenv() assert(env == _ENV) assert(env.print == print) for k, v in pairs(env) do print(k, v) endIt is worth noting that in later versions you should use _ENV rather than getfenv() as the latter is being phased out. You may note when iterating over the environment that it doesn't have any members: this is because it uses metatables to lookup in the parent environment if it can't find it in this one: try getmetatable(_ENV).__index.
#7
Posted 08 October 2016 - 04:04 PM
okie! Ta
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











