【问题标题】:Where and when load a library in luajit ffi在 luajit ffi 中加载库的位置和时间
【发布时间】:2013-05-03 02:00:33
【问题描述】:
我正在 C++ 引擎和 Lua 之间制作一个包装器,我正在使用 LuaJIT,因此我使用 ffi 作为这两者之间的“包装器”,因为引擎有很多不同的部分,我在想它将它们分成文件然后要求它们会很好,但是,在阅读了一些关于 LuaJIT 的信息后,我发现对于外部库,您必须加载库,所以我提出了这个:何时何地我应该加载库?在“胶水”代码(统一所有模块的代码)中?在每个人中?还是将其保留为单个文件会更好?
另外,为了确定加载库的速度有多慢?
【问题讨论】:
标签:
shared-libraries
ffi
luajit
【解决方案1】:
您可以创建一个加载库的“核心”模块:engine/core.lua
local ffi = require'ffi'
local C = ffi.load('engine') -- loads .so/.dll
ffi.cdef[[
/* put common function/type definitions here. */
]]
-- return native module handle
return C
然后您将为每个引擎部件创建一个模块:engine/graphics.lua
local ffi = require'ffi' -- still need to load ffi here.
local C = require'engine.core'
-- load other dependent parts.
-- If this module uses types from other parts of the engine they most
-- be defined/loaded here before the call to the ffi.cdef below.
require'engine.types'
ffi.cdef[[
/* define function/types for this part of the engine here. */
]]
local _M = {}
-- Add glue functions to _M module.
function _M.glue_function()
return C.engine_function()
end
return _M
“engine.core”模块中的代码只会被执行一次。将引擎分成多个部分的最大问题是处理跨类型依赖关系。为了解决这个添加'typedef struct name name;'用于在多个部分中使用的类型的“engine.core”模块。