Re: How do I add a "local" using the c api
David Given <dg <at> cowlark.com>
2009-04-01 04:21:45 GMT
Tom Miles wrote:
[...]
> When I create a state, I load an associated script file. I want to add
> some variables into the state the script is running in that are only
> visible to that script. I can add globals using lua_setglobal(L,
> "myvar"), or even lua_setfield(L, LUA_GLOBALSINDEX, "myvar"), but there
> doesn't seem to be an equivelant for locals.
Something that nobody's said yet which might clarify matters for you is
this...
You *cannot* add a local variable to an already compiled script. [*]
This is because locals do not actually exist in the compiled bytecode
--- they're just slots on the stack that the compiler has special
knowledge of, and, AFAIK, you can't change the bytecode after it's been
compiled. They're not looked up by name and I think that if you strip
the debugging information from your bytecode debug.getlocal() can't find
out what they're called.
The lua_Reader trick is a way of inserting boilerplate at compile time.
This will work, but be aware that it's not really modifying the state
after it's been compiled. Think of it as the equivalent of a #include at
the top of the script.
Using a shared environment table is usually a way of sharing values
between *C* functions (and you still need a table lookup anyway).
Setting upvalues is another way of sharing data between chunks, but to
(Continue reading)