Reputation: 85
I have a c function, used to return a table to lua, but the table is created from a string, not lua_newtable, how to do this?
int GetTable(lua_State* L)
{
//this string is generated at runtime, so i can not use lua_newtable
const char* TableFromStr = "{a = 123, b = 456, d = {x = 1, y = 9} }";
//i want to push the table to the top stack
luaL_loadstring(L, TableFromStr);
//return 1 table, lua code can get the table
return 1;
}
Upvotes: 1
Views: 307
Reputation: 7198
You have to use luaL_dostring
https://www.lua.org/manual/5.1/manual.html#luaL_dostring
If you're using Lua 5.1 make sure luaL_dostring
is correctly defined. See: http://lua-users.org/lists/lua-l/2006-04/msg00218.html
In Lua 5.1,
luaL_dostring
is defined asluaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)
and so it ignores returns.
Try this:
#undef luaL_dostring #define luaL_dostring(L,s) \ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
It's possible that you will also have to prepend your string with return
Upvotes: 3