Lua userdata:无法同时访问arrays和方法

我有这个人的问题: Lua userdata数组访问和方法

其中当我设置userdata的metatable的__index时,它总是调用getter,而不是我没有为meta-events声明的其他方法。 上面链接的解决方案是在Lua中,我尝试了一个看似不优雅的C实现,但无论如何,它创建了一个新问题,因为我的新方法不能再接受参数,我得到了这个错误:

attempt to call method 'asTable' (a table value)

在这个Lua声明中:

print_r(c:asTable() )

这是我设置一切的方式:

 //Methods, many of which are overridden Lua meta-events (with the underscores) static const struct luaL_reg vallib_m [] = { {"asTable", PushLuaTable}, //these functions are not called {"asCopy", CopyLuaVal}, {"__newindex", SetLuaVal}, {"__index", GetLuaVal}, {"__tostring", ValToString}, {"__gc", GarbageCollectVal}, {"__metatable", HideMetaTable}, {NULL, NULL} }; //Static library functions static const struct luaL_reg vallib_f [] = { {"specialprint", PrintVals}, {NULL, NULL} }; int luaopen_custom(lua_State *L) { luaL_newmetatable(L, "custom.Value"); lua_pushstring(L, "__index"); lua_pushvalue(L, -2); /* pushes the metatable */ lua_settable(L, -3); /* metatable.__index = metatable */ luaL_register(L, NULL, vallib_m); luaL_register(L, "special", vallib_f); return 0; } 

然后在我的getter中,默认情况下调用(通过__index),我首先检查我打算调用的其他事件,并将控制权转移给它们,如下所示。 请注意,我从堆栈中删除包含函数名称的参数。

 //TODO: this is a tentative fix, I would rather do this with metatables //checking for methods if (lua_isstring(L, 2)) { field = luaL_checkstring(L, 2); if (unlikely(!field)) { reporter->Warning("Fail in getter -- bad string as method attempt"); return LUA_FAILURE; } if (strcmp(field, "asTable") == 0) { lua_remove(L, 2); //delete string "asTable" return PushLuaTable(L); } else if (strcmp(field, "asCopy") == 0) { lua_remove(L, 2); //delete string "asCopy" return CopyLuaVal(L); } //... other methods. else { //Insert string back into stack?? } } 

无论传递多少个参数,它都不会将我的方法视为函数,如果有任何括号或冒号,则抛出错误。 (它可以通过c.asTable访问,它适用于不带参数的方法但我计划添加一些,但无论如何,语法与方法不一致。

在任何情况下,最好不要通过我的C getter调用这些函数,而是用metatables来解决这个问题。 如果可以,请提供一个使用C API的示例 – Lua中已经有StackOverflow解决方案,但我无法将它们转换为C.

要从Lua访问C结构的字段,您需要一个函数作为__index元方法,因为您需要访问userdata对象,如果__index是一个表,您将无法获取它:

 -- Lua example code; obj* should be userdatas ... -- an example __index function local function anIndex( o, k ) print( "accessing", k, "in", o ) return 1 end local obj = {} local meta = { __index = anIndex } setmetatable( obj, meta ) print( obj ) --> table: 0xfcb060 print( obj.x ) --> accessing x in table: 0xfcb060 --> 1 

这适用于属性,但是访问相同类型的所有userdatas共享的方法会让人感到不舒服(并且效率低下)。 __index表会更好:

 -- an example method local function aMethod( o ) print( "calling aMethod on", o ) return 2 end local obj2 = {} local methods = { aMethod = aMethod } local meta2 = { __index = methods } setmetatable( obj2, meta2 ) print( obj2 ) --> table: 0xfcada0 print( obj2:aMethod() ) --> calling aMethod on table: 0xfcada0 --> 2 

但是现在我们想要两个!

Metamethods可以在Lua中链接,因此我们可以尝试将__index函数设置为__index表的回退(在本例中为methods ):

 setmetatable( methods, meta ) print( obj2 ) --> table: 0xfcada0 print( obj2.x ) --> accessing x in table: 0xfcade0 --> 1 print( obj2:aMethod() ) --> calling aMethod on table: 0xfcada0 --> 2 

但是如果你仔细观察,你会发现__index函数获得的对象与obj2不同…

 print( methods ) --> table: 0xfcade0 

它将methods表作为第一个参数。 因此,我们无法访问原始userdata(此示例中的表),我们实际上无法查找任何字段。 所以那不行。

 setmetatable( methods, nil ) -- let's undo this ... 

幸运的是,__ index函数可以执行任意操作,包括访问另一个表(例如,存储在upvalue中的表):

 local obj3 = {} local meta3 = { __index = function( o, k ) local v = methods[ k ] -- methods is an upvalue here if v == nil then print( "accessing", k, "in", o ) v = 1 end return v end } setmetatable( obj3, meta3 ) print( obj3 ) --> table: 0xfc23a0 print( obj3.x ) --> accessing x in table: 0xfc23a0 --> 1 print( obj3:aMethod() ) --> calling aMethod on table: 0xfc23a0 --> 2 

现在这很棒! 如果这种情况经常发生,我们可以编写一个帮助函数,为我们创建一个合适的__index函数。 作为参数传递的indexfunc仅涉及字段查找,并且根本不必处理方法。 生成的函数将执行以下操作:

 local function makeindex( methodstable, indexfunc ) return function( o, k ) local v = methodstable[ k ] if v == nil then v = indexfunc( o, k ) end return v end end local obj4 = {} local meta4 = { __index = makeindex( methods, anIndex ) } setmetatable( obj4, meta4 ) print( obj4 ) --> table: 0xfc92b0 print( obj4.x ) --> accessing x in table: 0xfc92b0 --> 1 print( obj4:aMethod() ) --> calling aMethod on table: 0xfc92b0 --> 2 

如果你试图将它转换为Lua C API,你会发现使用luaL_Reg数组而不是方法表和lua_CFunction指针而不是Lua函数的堆栈索引更方便。 这就是本答案中链接的moon_propindex()函数(另外它让你为所有方法设置值,如luaL_setfuncs() )。