lua-users home
lua-l archive

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


2010/8/10 <wowbagger58-lua@yahoo.com>
> I earlier asked about calling a Lua function from C/C++.
>
>
> I appreciate the feedback.
>
> However,
>
>
> 1) I was already familiar with the INCOMPLETE example associated with the below link:
>
> http://www.lua.org/manual/5.1/manual.html#lua_call
>
>
> 2) I was also aware that luaL_loadstring() does not execute the Lua chunk.
>
>
> 3) As for the example in Patrick Rapin's documentation, it is NOT an example of calling a Lua function. It's an example of calling a varargs
>
> expression (for which the Lua text was helpful).
>
>
> I realized what must be wrong with my program.
>
> I discovered that my program works fine with a Lua script consisting of a varargs expression.
>
> However, it does not work if the script consists of a Lua function.
>
> I realized that a Lua function is different.
>
> A Lua script might contain several functions.
>
> Surely, the individual functions must be "registered" so that they may later be pushed onto the stack via calls to lua_getfield().
>
> When I call lua_setfield() after loading a chunk consisting of a varargs expression, a table entry is made associated with the chunk (i.e.,
> the chunk is "registered").
>
> But, if I load a chunk containing one or more Lua functions, how do I cause the individual functions to be "registered"?

You have to run the chunk (with lua_call/lua_pcall). Any global
variable assignment (including global function definition) will go to
the chunk environment, which you may replace before running it with
lua_setfenv.