Tom Leese
Tom Leese

Reputation: 19699

Lua get and execute function from C

If I have some lua code like this:

doSomething(function()
                print("Hello!")
            end)

How can I make it that, using the C API, I create C Lua function for doSomething that can then save the function passed to it to be executed a later date?

Upvotes: 2

Views: 1071

Answers (1)

hroptatyr
hroptatyr

Reputation: 4809

It's just a normal entry on the stack. Check it with lua_isfunction() then use luaL_ref() to generate a reference (so that the garbage collector doesn't steal it), and then maybe lua_topointer() to store a pointer to this object, depends what you want to do with it.

When you're finished it's just a luaL_unref().

Upvotes: 3

Related Questions