lua-users home
lua-l archive

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


I'd like someone to point me to a sample source on how to push function on the stack.

const
char* code = "function
() print('hello world') end";


How would one go about pushing code as a function? And also, how do I later retrieve it? A snippet of code would be greatly appreciated.

See loadstring:
http://www.lua.org/manual/5.1/manual.html#luaL_loadstring

You'll just have to prefix your sample code with "return " to make it
a valid chunk.

Then, compile it:

 luaL_loadstring(L, code);

And run the chunk to get your hello function on top of the stack:

 lua_pcall(L, 0, 1, 0);