How can I create a pointer to existing data using the LuaJIT FFI?

后端 未结 3 997
情深已故
情深已故 2021-01-19 14:50

I know there are examples of creating pointers using the LuaJIT FFI, but most of these aren\'t pointed to existing data. One such example of this is here: How to pass a poin

3条回答
  •  终归单人心
    2021-01-19 15:31

    I was able to do that with a C function that copies the cdata pointer like this

    void cdataToPointer(void *cdata, void **pointer) {
        *pointer = cdata;
    }
    
    // ...
    
    void *mycdata = NULL;
    
    lua_pushlightuserdata(L, &mycdata);
    lua_setglobal(L, "__TEMP_USERDATA__");
    
    luaL_dostring(L,
         "local ffi = require'ffi'\n"
         "ffi.cdef[[\n"
         "  void cdataToPointer(void *cdata, void **pointer);\n"
         "]]\n"
         "ffi.C.cdataToPointer(mycdata, __TEMP_USERDATA__)\n");
    

提交回复
热议问题