c - Lua 用户数据 : Unable to have simultaneous array access and methods

标签 c api lua metatable lua-userdata

我遇到了这个人的问题: Lua userdata array access and methods

其中,当我设置用户数据的元表的 __index 时,它总是调用 getter,而不是我未为元事件声明的其他方法。上述链接的解决方案是在 Lua 中,我尝试了一个看起来不优雅的 C 实现,但无论如何,它产生了一个新问题,因为我的新方法不能再接受参数,我得到这个错误:

尝试调用方法“asTable”(表值)

关于这个 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;
}

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

//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 调用这些函数,而是使用元表来解决这个问题。如果可能,请提供一个使用 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

这对于属性来说工作得很好,但是对于访问所有相同类型的用户数据共享的方法来说是不舒服的(而且效率低下)。 __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 表的回退(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 表作为第一个参数。所以我们失去了对原始用户数据(本例中的表)的访问权限,并且我们实际上无法查找任何字段。所以那是行不通的。

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

幸运的是,__index 函数可以做任意事情,包括访问另一个表(例如存储在上值中的表):

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 函数的指针而不是堆栈索引。这就是 this 中链接的 moon_propindex() 函数。 answer 确实如此(此外,它还允许您为所有方法设置值,例如 luaL_setfuncs())。

关于c - Lua 用户数据 : Unable to have simultaneous array access and methods,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089303/

相关文章:

c++ - 结构中的可变长度字符串和导致时间旅行的未定义行为

c - 手动将协议(protocol)头与正文分离

rest - 如果我在Groovy&Grails中创建一个简单的 Controller 并将其用作Rest API Controller ,然后再扩展RestFulController,该怎么办?

javascript - 在 Facebook API 中输入错误代码 191 - 我做错了什么?

python - 无法使用 python PDFKIT 错误 : "No wkhtmltopdf executable found:" 创建 pdf

lua - 如何获取 Lua 中的主目录(在 GNU/Linux 系统上)?

c - 使用 mysql_stmt_prepare 时如何引用 char* 而不是使用 char[]

计算文件中 8 位符号的频率

Lua:尝试将 bool 值与数字进行比较

c - 是否可以在 Lua 中禁用堆栈回溯?