使用Lua C API从表中提取用户数据。

我使用lua c api在表格中循环变量,如下所示。

lua脚本:

array = {0,1,2,3}

lua c api:


} lua_pushnil(l); while(lua_next(l,-2)){

    int value =(int)lua_tonumber(l,-1);
    printf("%d \n",value);

    lua_pop(l,1);
} ```

我可以获得结果

0
1
2
3

然后我想将一些userdata对象放入表中,然后在c api中循环它们。

lua脚本

foo0 = Foo.new(“fred0”) foo1 = Foo.new(“fred0”) foo2 = Foo.new(“fred0”) foo3 = Foo.new(“fred0”) array = {foo0,foo1,foo2,foo3}


lua c api

extern“C” { #include“lua.h” #include“lauxlib.h” #include“lualib.h” }

#include #include

#include

class Foo { public: Foo(const std ::string&name):name(name) { std ::cout <<“Foo is born”<< std ::endl; }

std ::string Add(int a,int b)
{
    std ::stringstream ss;
    ss << name <<“:”<< a <<“+”<< b <<“=”<<(a + b);
    返回ss.str();
}

~Foo()
{
    std ::cout <<“Foo is gone”<< std ::endl;
}

std ::string name;

};

int l_Foo_constructor(lua_State * l) { const char * name = luaL_checkstring(l,1);

Foo ** udata =(Foo **)lua_newuserdata(l,sizeof(Foo *));
* udata = new Foo(name);

luaL_getmetatable(l,“luaL_Foo”);

lua_setmetatable(l,-2);

返回1;

}

Foo * l_CheckFoo(lua_State * l,int n) { 返回*(Foo **)luaL_checkudata(l,n,“luaL_Foo”); }

int l_Foo_add(lua_State * l) {

返回1;

}

int l_Foo_destructor(lua_State * l) { Foo * foo = l_CheckFoo(l,1); delete foo;

返回0;

}

void RegisterFoo(lua_State * l) { luaL_Reg sFooRegs [] = { {“new”,l_Foo_constructor}, {“add”,l_Foo_add}, {“__gc”,l_Foo_destructor}, {NULL,NULL} };

luaL_newmetatable(l,“luaL_Foo”);

luaL_register(l,NULL,sFooRegs);

lua_pushvalue(l,-1);

lua_setfield(l,-1,“__index”);

lua_setglobal(l,“Foo”);

}

int main() { lua_State * l = luaL_newstate(); luaL_openlibs(l); RegisterFoo(l);

int erred = luaL_dofile(l,“/Volumes/Work/CODE/Test/testStatic/testStatic/kami.txt”);
if(erred)
    std ::cout << “Lua error:”<< luaL_checkstring(l,-1)<< std ::endl;

lua_getglobal(l,“array”);
if(lua_isnil(l,-1)){
    //return std::vector();
}
lua_pushnil(l);

std ::vector <Foo *> v;
while(lua_next(l,-2)){

    Foo * foo = l_CheckFoo(l,-1); //此行不起作用
    //
    //
    //
    //
    //我不知道在这里该如何操作。
    //
    //
    //
    //
    //v.push_back(foo);
    lua_pop(l,1);
}

// for(Foo * theValue:v) // { // printf(“==>%s”,theValue->name.c_str()); // }

lua_close(l);

返回0;

}

```

如何从表中提取userdata? 请帮帮我,谢谢。

点赞
用户415752
用户415752

最后我自己解决了它。

l_CheckFoo(l, -1);

应该改为

lua_touserdata(l, -1);

void *hehe = lua_touserdata(l, -1);

Foo **haha = (Foo **)hehe;

Foo *f = *haha;

printf("%s \n", f->name.c_str());
2015-02-15 15:03:45