在两个 lua_State 之间传输数据

我有一个在第一个 lua_State 中的 C 函数。如何将该函数复制到第二个 lua_State 中?我尝试使用 lua_xmove,但它对我不起作用 (PANIC:在调用 Lua API 时出现未保护的错误,尝试索引函数值)

void lua_setfield (lua_State *L, int index, const char *k)
{
    if (lua_isfunction(L, lua_gettop(L)) && !strcmp(k, "myfunction"))
    {
            lua_pushvalue(L, lua_gettop(L));
            lua_xmove(L, myluastate, 1);
            o_lua_setfield(myluastate, index, k);
    }
    o_lua_setfield(L,index,k);
}

o_lua_setfield - 原始的 lua_setfield 函数

lua_setfield - hooked setfield 函数

myfunction - 我需要的函数

点赞