lua-users home
lua-l archive

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


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

Shane Lee wrote:
[...]
> I believe I see what you mean. I included the C code in my earlier
> reply for your perusal, however I am not aware of any other methods of
> obtaining the results I require other than what I have already
> attempted.

Yep, that's what you're doing.

The thing to remember is that the lua_State, L, remembers all functions
defined by your scripts; and that a function()...end sequence in the script is
actually a *statement* that, when executed, defines a function.

So by running the script *once*, you define all your functions. Once defined,
you can call them; you just don't need to redefine them again.

[...]
> It does help tremendously. It tells me that I have absolutely no idea
> what I am doing :-P

Actually, most of your code is right; it's just, ahem, not quite in the right
order.

The only thing you need to do is to split the code up so that luaL_dofile() is
only called once, and everything from lua_getglobal() onwards is called three
times. That should work. You also need to use lua_pcall() instead of
lua_call(). If an error occurs in the Lua script, Lua will attempt to throw an
exception using an internal longjmp-based mechanism; lua_pcall() will catch
these. lua_call() doesn't, so if something goes wrong, Bad Stuff happens.

You also need to be aware that const char* strings on the Lua stack only
remain valid for as long as the Lua string is on the stack... so as soon as
you call lua_pop(), ret dies.

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│
│ "There does not now, nor will there ever, exist a programming language in
│ which it is the least bit hard to write bad programs." --- Flon's Axiom
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGzeh5f9E0noFvlzgRAsO7AKC7CH8V4Q9lXSDzdoihMIZWl6iaeQCfbImx
Qe4CbhRKO0GKlvhunsMkDXs=
=An4M
-----END PGP SIGNATURE-----