Remember, that:
function luaFoo() ... end
equivalent to this, in Lua:
luaFoo = function() ... end
So your question ultimately boils down to this: "I have a value in the global table. How do I push it onto the stack?" The fact that this value is a function does not matter; the value of a function in Lua is no different from an integer value that is no different from a string value. Obviously, you can do different things with them, but you just want to copy them. This works the same regardless of value.
The function you are looking for is lua_getglobal .
Regarding the second question, you can do this in one of two ways. You can either get the function value registered in the global table, or simply reregister it with lua_pushcfunction . Since you are not using upvalues, re-registering it actually has no drawbacks.
Oh, and one more thing in code style. Lua does not require ; at the end of the instructions. You can do this (to make C-native programmers feel more comfortable), but this is not necessary.
source share