如何在使用luajit的变量参数的C函数中获取cdata?

这是代码。目标是打印消息。 在 PrintC 中,我想要得到 e,但是它变成了 cdata。如何解包或规避此问题?

extern "C"
{
    static int PrintC ( lua_State *L )
    {

         // does not work cdata
        //executor* e = ( executor* ) luaL_checkudata(L, 1, "cdata"); does n

        //luaL_checkudata(L, 1, "void *");

        if ( e->writelog )
        {
            int no = lua_gettop ( L ) ;

            for ( int i = 2; i <= no; i++ )
            {
                cout << lua_tostring (L,i);
            }

        }
        return 1;
    }

}

// initialised as
lua_pushcfunction ( L, PrintC );
lua_setglobal ( L, "PrintC" );
lua_pushinteger ( L, ( long ) this ); // this is in a class executor
lua_setglobal ( L, "p" );

p= ffi.cast("void *",p)
function   Print()
   return  PrintC(p)
end
点赞
用户646619
用户646619

你不能。Lua C API 和 LuaJIT FFI 是有意分开的,并且不能互相交互。

使用 FFI 重写 PrintC 的 Lua 函数,或者编写 Lua C API 绑定到你正在使用的库 p。也就是说,使用其中之一。

2015-06-01 16:09:37