1
votes

I have a C++ function, called lua_tcall, which extends the abilities of lua_pcall. When lua_tcall is called, it expects that the top value of the stack is a Lua function, much like lua_pcall does. I would like lua_tcall to be able to create a thread, and use the function on the top of the global stack as the thread's function. How do I push the function from the global state into the lua_State* that I get back from lua_newthread?

2
lua_pcall does not expect the top item on the stack to be a function (unless you're calling it with zero arguments). The location of the function on the stack is -(1 + nargs). So what exactly is it that you expect to be on the stack with your lua_tcall? - Nicol Bolas
lua_tcall is a template function that I wrote (C++). It expects the top value to be a Lua function. There are overloads to push different numbers of parameters onto the stack, and pop 0 or 1 return values. Following the notation in the Lua manual, the contract is [-1, 0, -] (errors are caught and thrown as exceptions) - Sean Edwards

2 Answers

2
votes

To transfer a function from one lua_State to another, you must use something global to both states to store the object as an intermediary. You could use a global variable or a registry entry.

2
votes

To transfer values between states, lua_xmove is better than using a shared intermediary.