Reputation: 1869
Using the C
API, I have pushed a table on the Lua stack. How can I push a function on the stack that returns this table?
The closest I can find is lua_pushcclosure
, but that pushes a lua_CFunction
on the stack, not a real Lua function, and that is not acceptable in my case because I want to use setfenv
later, which only works with real Lua functions.
Regards
Upvotes: 2
Views: 1081
Reputation: 1869
Old question, but I now add the correct answer as suggested by Etan.
status = luaL_dostring(L, "return function (ppp) return function () return ppp end end");
if (!status) {
// push table here
status = lua_pcall(L, 1, LUA_MULTRET, 0);
}
Regarding the question making no sense, I am aware that setfenv has no effect.
My only requirement was that setfenv can be executed without causing a crash.
Upvotes: 0
Reputation: 473322
Well, you do the obvious. Since it must be a "real Lua function", you must create a real Lua function that returns the table.
Something like this:
local tbl = ...
return function() return tbl end
This is a Lua chunk that, when executed, will return a function that returns the first parameter the original chunk was called with.
So simply use luaL_loadstring
to load that string. Call the resulting chuck with your table; the return value will be the function you desire.
That being said, your question ultimately makes no sense. setfenv
will not accomplish anything of value here. Setting the environment of a function that returns a local table will not affect anything stored within that table.
Upvotes: 0