[英]How to create a lua table from a string?
我有ac函数,用于将表返回给lua,但是表是从字符串创建的,而不是lua_newtable,如何做到这一点?
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;
}
你必须使用luaL_dostring
https://www.lua.org/manual/5.1/manual.html#luaL_dostring
如果你正在使用Lua 5.1,请确保正确定义了luaL_dostring
。 请参阅: http : //lua-users.org/lists/lua-l/2006-04/msg00218.html
在Lua 5.1中,
luaL_dostring
被定义为luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)
luaL_loadstring(L, s) || lua_pcall(L, 0, 0, 0)
所以它忽略了回报。
尝试这个:
#undef luaL_dostring #define luaL_dostring(L,s) \\ (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
您可能还需要在return
字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.