LuaJit - 获取模块/包的元表并将其分配给用户数据。

假设我有一个自定义结构体 vector2_t 的元表,它在模块 mymod 中,如下所示:

local mymod = {}

local ffi = require("ffi")
local C = ffi.C

ffi.cdef[[
    typedef struct
    {
        double x;
        double y;
    } vector2_t;
]]

local vector2_t

vector2_t = ffi.metatype("vector2_t", {
    __eq = function(lhs, rhs)
        if lhs.x == rhs.x and lhs.y == rhs.y
        then return true else return false end
    end
    -- Member functions follow...
})

vcmp.vector2 = vector2_t

-- 我使用这种方法是因为脚本以字符串形式集成到 C/C++ 中,而不是作为文件存在,我需要一个常量名称,不是由文件名决定的

package.preload["mymod"] = function(mod) return mymod end

在另一个脚本中,我有这个回调/事件侦听器函数,它必须接收一个 vector2_t 作为参数:

local mymod =  require "mymod"

local some_pos = mymod.vector2(32, 29.7)

-- 那个 pos 参数必须是 mymod.vector2_t 的实例
function position_update(pos)
    print("New Pos: " .. pos.x .. ", " .. pos.y .. "\n")
    some_pos.x = pos.x
    some_pos.y = pos.y
end

现在,我必须从 C/C++ 中调用该回调/事件侦听器函数,并将该 vector2_t 实例(以及其关联的元表)作为该函数参数传递。

typedef struct
{
    double x;
    double y;
} vector2_t;

void call_position_update(lua_State* L, double x, double y)
{
    // 检索函数
    lua_getglobal(L, "position_update");

    // 验证它
    if (!lua_isfunction(L, lua_gettop(L)))
    {
        lua_pop(L, 1);
        return;
    }

    // 创建 vector2_t 的实例
    vector2_t *pos = (vector2_t *)lua_newuserdata(L, sizeof(vector2_t));

    // 将值分配给新实例
    pos->x = x;
    pos->y = y;

    // 如何将元表 vector2_t 放到堆栈上?
    // 有点追溯到模块…

    // 从模块中获取元表
    luaL_getmetatable(L, "vector2_t");
    // 将元表分配给已经在堆栈上的 vector2_t 实例
    lua_setmetatable(L, -2);

    // 我假设 vector2_t 实例已经在堆栈中,所以没有其他需要推出的东西

    // 用一个参数调用该函数,该参数应该是堆栈上的 vector2_t
    if (!lua_pcall(L, 1, 0, 0))
    {
        printf("Error calling function 'position_update': %s\n", lua_tostring(_Lua, -1));
    }
}

我有点迷失方向,不知道如何将 vector2_t 的实例作为函数参数传递。非常抱歉发布了这么多代码,但我想确保我正确解释了。

点赞
用户646619
用户646619

cdatauserdata 是两个完全不同的东西。它们之间唯一的交互方式是你可以通过 FFI 获取到 void* 指针来操作 userdata。要注意的是,cdata 对象没有 C API。混合使用它们肯定会给你带来很多麻烦。

请尽可能使用 Lua C API 或 FFI 中的一个,不要混合使用。


直接回答您的问题:

how to pas [sic] an instance of that vector2_t as the function parameter

如果要将它传递给 Lua C API 函数,它会像其他值一样在堆栈上传递。请注意,它将是一个 cdata 类型的对象,而不是 userdata 对象。特别要注意的是,你无法获取它的指针。

How do I get the meta table vector2_t on to the stack?

由于你在第一个脚本中没有将元表暴露给外部脚本,所以你不能将元表 vector2_t 放到堆栈上。luaL_getmetatable 只适用于使用 luaL_newmetatable 创建的元表。

2014-09-08 13:17:38