簡體   English   中英

從Lua調用C嵌套函數指針

[英]Calling C nested function pointer from Lua

我有以下C結構,其中包含函數指針:

struct db {
    struct db_impl *impl;
    void (*test)(struct db *self); // How to invoke it from Lua??
};
void (*db_test)(void); // this I can invoke from Lua

struct db * get_db() {
    // create and init db
    struct db * db = init ...
    db->test = &db_real_impl; // db_real_impl is some C function
    return db;
}

因此,初始化后的測試函數指針指向某個函數。 現在,我需要使用FFI庫從Lua調用該函數,但是它失敗並顯示錯誤: 'void' is not callable

local db = ffi.C.get_db()
db.test(db)  -- fails to invoke
-- Error message: 'void' is not callable

ffi.C.db_test()  -- this works fine

在C中,代碼為:

struct db *db = get_db();
db->test(db);

在Lua中,我能夠輕松調用自由函數指針,但無法從struct調用函數指針。 如何從Lua調用它?

似乎在以下位置指出了一種解決方案: http : //lua-users.org/lists/lua-l/2015-07/msg00172.html

ffi.cdef[[
    typedef void (*test)(struct db *);
]]

local db = get_db()
local call = ffi.cast("test", db.test)
call(db)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM