繁体   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