Reputation: 3484
As the global variables are stored in the table named _ENV
, they could be found by the name as the keys of the table.
How does Lua find the right value for a specific local variable which has been assigned before?
EDIT:
Here is the demo code:
do
local a=1;
local b=2;
local c=3;
sum = a+c;
end
How does Lua know the value of variable a
is 1?
Upvotes: 0
Views: 581
Reputation: 539
Like all interpreted languages, lua runs on top of a "stack", which is quite literally what it sounds like. A big, tall memory structure which stores the entire memory of the lua script. (The term "stack overflow" comes from this stack running out of memory)
During lua compilation, lua looks for local variables, and allocates them a space on the stack:
local a = "abc" -- Item #1
local b = "abc" -- Item #2
local c = "abc" -- Item #3
When you run this script, lua's memory looks something like this:
+-------+
| "abc" |
+-------+
| "abc" |
+-------+
| "abc" |
+-------+
This is also lexically scoped, when you enter a do end
scope (or any scope), Lua creates a new area of the stack, and discards that area when the lexical scope is left.
local a = "abc"
do
local b = "xyz"
end
Would look something like this:
Before: In "do": After:
+-------+ +-------+ +-------+
| "abc" | | "abc" | | "abc" |
+-------+ +-------+ +-------+
+-------+
| "xyz" |
+-------+
Interestingly, lua also has no "global variables". Every variable is on the stack, and is simply a table lookup.
The way this is performed is through upvalues; A lua scope can access locals in the scope above it. However, when you load in a brand new lua chunk (function) with load
, it has no upvalues. To implement globals, all chunks have one upvalue, _ENV
. All "global" variables are really just a index on this table.
Since _ENV is always at the very bottom of the stack, it's easy for Lua to look up globals. (It is slower, however)
+------+
| _ENV |
+------+
+-------+
| "abc" |
+-------+
Final example:
local a = "abc" -- Item #1
b = "xyz" -- _G["b"] = "xyz"
c = b -- _G["c"] = _G["b"]
local z = "awesome" -- Item #2
To clarify: All operations in lua are on this stack. When you want to call a function- add two numbers, or even look up a table, all of these operations are performed on the lua stack. If you're interested in how this works, check out the lua C api.
Upvotes: 1
Reputation: 2793
Normaly local defined variables and functions makes sense inside a do ... end
or function(...) ... end
. For example my QLua on android dont print anything except nil
when doing it without above rules...
Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
> local a=1
> local b=2
> print(a,b)
nil nil
> do local a=1 local b=2 print(a,b) end
1 2
Upvotes: 0
Reputation: 2531
Acording to LUA Reference:
Any variable name is assumed to be global unless explicitly declared as a local (see §3.3.7). Local variables are lexically scoped: local variables can be freely accessed by functions defined inside their scope (see §3.5).
so:
function printA()
print(a)
end
a = 1
print(a) -- 1
local a = 2
print(a) -- 2
do
print(a) -- 2
local a = 3
print(a) -- 3
end
print(a) -- 2
printA() -- 1
Upvotes: 0