2013-04-21 67 views
0

我正在使用Visual C++ 2012并试图为Lua编写一个c扩展。目前,我设计了一个函数原型:如何获得c中的lua参数?

lib.myfunc(number, {a=1,b=2,c=3},{d=4,e=5,...}) 

没有关于“MYFUNC”功能3个参数,第一个参数是一个数字的整数,第二和第三个参数表类型,我需要访问值按键(键'a','b','c'...)

我已阅读lua手册并为许多教程搜索,但我仍然无法获得它的工作。我想要一个示例C代码做这个工作,谢谢〜

回答

1

我真的不知道luabind,所以我不知道,如果他们提供任何自己的设施来做到这一点,但在Lua你会这样做是这样的:

int myLuaFunc(lua_State *L) 
{ 
    int arg1 = luaL_toint(L, 1); 
    luaL_checktype(L, 2, LUA_TTABLE); //Throws an error, if it's not a table 
    luaL_checktype(L, 3, LUA_TTABLE); 

    //Get values for the first table and push it on the stack 
    lua_getfield(L, 2, "keyname"); //Or use lua_gettable 
    //Assuming it's a string, get it 
    const char *tmpstr = lua_tostring(L, -1); 

    //..... Similariliy for all the other keys 
} 

你可能想参考Lua Reference Manual来描述我使用的功能。

+0

看起来像'luaL_toint'应该是'luaL_tointeger',但它仍然帮助我很多。谢谢 – 2013-04-21 15:44:53

+0

它实际上应该是luaL_checkint。 luaL_checkinteger返回lua_Integer,而luaL_checkint返回int。 – 2013-04-21 16:22:23