문제

I am making a wrapper between a C++ engine and Lua, I'm using LuaJIT and because of this I'm using ffi as the "wrapper" between these two, since the engine has much and different parts I was thinking it would be nice to divide them in files and then requiring them, however, after reading a bit about LuaJIT I found that for external libraries you have to load the library, so I came with this: When and where I should load the library? In the "glue" code (the one wich unifies all the modules)?, in everyone?, or it would be better to keep it as a single file? Also, for deciding this how much slow is loading a library?

도움이 되었습니까?

해결책

You can create a 'core' module that loads the library: 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

Then you would create a module for each of the engine parts: 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

The code in the 'engine.core' module will only be executed once. The biggest issue with separating the engine into parts will be to handle cross type dependencies. To solve this add 'typedef struct name name;' to the 'engine.core' module for types that are used in multiple parts.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top