【问题标题】:Call c function from Lua-lanes从 Lua-lanes 调用 c 函数
【发布时间】:2022-01-04 14:28:33
【问题描述】:

我想使用通道从 lua 调用 c 函数。

int initApp(lua_State *L) {
    lua_createtable(L, 0, 0);
    
    lua_pushcfunction(L, get_appinfo);
    lua_setfield(L, -2, "get_appinfo");
    
    lua_setglobal(L, "App");
    
    return 0;
}
local function thread1(n)
    return App.get_appinfo(n)
end

function startlanes()
    local lanes = require 'lanes'
    package.preload['App'] = function() return App end
    package.loaded['App'] = App
    local app = require 'App'
    print(app, app.get_appinfo) --table: 0x1234    function: 0x5678
    --(1)
    f = lanes.gen('*', {required = {'App'}}, thread1) --Lua Error: module 'App' not found: no field package.preload['App']...
    --(2)
    --f = lanes.gen('App', thread1) -- Bad library name: App
    a = f(1)
    sleep(1)
end

当我运行变体 (1) 时,我得到 Lua Error: module 'App' not found: no field package.preload['App']...no file '/App.lua'...。当我运行变体 (2) 时,我得到 Bad library name: App

如何使用lanes 调用App.get_appinfo()?我可以将所有App 函数移动到包中,但它必须从内存加载,而不是从文件系统加载。我嵌入了所有 lua 包。

【问题讨论】:

    标签: lua lua-lanes


    【解决方案1】:
    1. 将 C 函数 initApp 暴露给 Lua,例如 _G.init_App
        lua_pushcfunction(L, initApp);
        lua_setglobal(L, "init_App");
    
    1. 在启动每条车道时将其作为参数传递
    2. 在通道内,调用 init_App(作为第二个参数接收) - 它将创建您的库
    local function thread1(n, init_App)
        init_App()  -- create global table App in the current lane
        return App.get_appinfo(n)
    end
    
    function startlanes()
        local lanes = require 'lanes'
        local f = lanes.gen('*', thread1)
        a = f(1, init_App)  -- pass your module loader as argument
        sleep(1)
    end
    

    【讨论】:

    • init_App 的外观如何?如何在这里定义/声明我的 c 函数?
    • 答案已更新。
    • 错误:lanes.lua:329: main: function 'init_App' not found in Lane #0x100862008 destination transfer database。
    • 看来我修好了。我必须将参数on_state_create=init_App 添加到lanes.configure。车道在每个状态下运行此功能。正是我要找的。无需传递给thread1
    • 叶戈尔,你是认真的吗?您可以更新您的答案,我接受它已解决。并从thread1 中删除init_App
    【解决方案2】:

    要调用 c 函数,我必须将 on_state_create 的函数传递给 lanes.configure

    //c code
    int init_App(lua_State *L) {
        lua_createtable(L, 0, 0);
        
        lua_pushcfunction(L, get_appinfo);
        lua_setfield(L, -2, "get_appinfo");
        
        lua_setglobal(L, "App");
        
        return 0;
    }
    
    int init(lua_State *L) {
        lua_pushcfunction(L, init_App);
        lua_setglobal(L, "init_App");
        luaL_dostring(L, "local lanes = require 'lanes'.configure{on_state_create=init_App}; local l = lanes.linda()");
        luaL_dostring(L, "startlanes()");
    }
    
    --lua code
    local function thread1(n)
        return App.get_appinfo(n)
    end
    
    function startlanes()
        local lanes = require 'lanes'
        local f = lanes.gen('*', thread1)
        a = f(1)
        sleep(1)
        print(a[1])
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 2013-02-19
      • 2015-12-30
      • 2015-07-14
      • 1970-01-01
      • 2021-04-15
      相关资源
      最近更新 更多