【发布时间】:2015-09-02 11:12:16
【问题描述】:
我已经为这个简单的事情尝试了很多替代方案,但无法让它发挥作用。我希望用户在第一步中从 Lua 定义一个表:
a={["something"]=10} -- key=something, value=10
然后,在第二步中,用户将从 Lua 中调用一个用 C++ 设计的函数:
b=afunction(a) -- afunction will be designed in C++
C++ 代码:
int lua_afunction(lua_State* L)
{
int nargs = lua_gettop(L);
if(nargs>1) throw "ERROR: Only 1 argument in the form of table must be supplied.";
int type = lua_type(L, 1);
if(type!=LUA_TTABLE) throw "ERROR: Argument must be a table";
//Until here it works as expected
lua_pushnil(L); //does not work with or without this line
const char* key=lua_tostring(L,-2);
double val=lua_tonumber(L,-1);
return 0;
}
从代码lua_type(L,1) 可以看出,堆栈的底部是表本身。我假设在表的顶部,键将驻留在其顶部,值将位于其顶部。因此堆栈的高度为 3,其中 idx=-1 为值,idx=-2 为键。但是,似乎我既无法读取密钥(“某物”)也无法读取值(10)。任何想法表示赞赏。
【问题讨论】: