lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
access them from bytecode you'll have to trick the compiler into
generating references to them --- which means you have to have the local
statements in your source, so I doubt it will help.



[*] As far as I know. *crosses fingers*

- --
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│
│ "All power corrupts, but we need electricity." --- Diana Wynne Jones,
│ _Archer's Goon_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFJ0uvVf9E0noFvlzgRAjSjAJoD7eQgUub9xi8rr7/BPN1okziXHgCdEw7n
mUKamy+bNvH4pi7HdRe8i2Y=
=7hTg
-----END PGP SIGNATURE-----