lua-users home
lua-l archive

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


> However, what I REALLY want is to have a way to make lua_load NOT add
> the implied function.  Is there a way to do this?

No.  Lua 4 did run a loaded chunk at once.  The Lua 5 "two step" approach is
cleaner.  For example it is perfect for a "dump to bytecode" (to simply
compile a chunk) but there are other reasons as well.  If you don't need the
chunk as a function then simply run it:

    luaL_loadfile(L, my_file_name);
    lua_call(L, 0, 0);

(No error checking here...)

Loading and running the script "function main() ... end" *does* do
something:  It results in a function that is stored in the "main" field in
the globals.  If you need it from C later on, here's what you do (assuming
that "main" is stored in the C globals, which is the default for
luaL_loadfile):

    /* retrieve global "main" */
    lua_pushstring(L, "main");
    lua_gettable(L, LUA_GLOBALSINDEX);

The function is now on top of the stack (if it was found of course).  Now
let's run it:

    /* push a value arg and run ... */
    lua_pushnumber(L, 4);
    lua_call(L, 1, 0);

> If I'm stuck with this, what is the most efficient way to then call
> main from my C code, passing a value and being (hopefully) very
> efficient.

Manipulating Lua from C as in the example above is really REALLY fast!  I'm
sure you'll appreciate it when you play around with it some more.  I know I
do!  :-)

--
Wim