要从 Lua 访问 C 结构体的字段,您需要一个函数作为__index
元方法,因为您需要访问用户数据对象,如果__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
这对于属性来说效果很好,但是对于访问由同一类型的所有用户数据共享的方法来说,这很不舒服(而且效率低下)。一个__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
但现在我们两者都想要!
元方法可以在 Lua 中链接起来,所以我们可以尝试设置一个__index
作为后备功能__index
table (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
table 作为第一个参数。因此,我们无法访问原始用户数据(本例中的表),并且实际上无法查找任何字段。所以那是行不通的。
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()
函数链接this https://stackoverflow.com/questions/29957701/how-to-store-a-value-type-in-a-userdata/29967658#29967658答案确实如此(此外,它还可以让您为所有方法设置值,例如luaL_setfuncs()
).