forked from HuiDesktop/HDTL-LuaRoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdtbase.lua
84 lines (71 loc) · 2.36 KB
/
hdtbase.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local ffi = require 'ffi'
local packageStack = {{'.', {}}}
local packageTop = 1
local sharedModules = {}
local stderr = io.stderr
_G['log'] = function(x)
stderr:write(tostring(x))
stderr:write('\n')
end
--[[
package stack:
[{
1: path(to load module relatively)
2: local modules(one shared module use this)
3: shared checker(function, f(name) = true refers to shared module)
}, ...]
notice that shared modules has a unique instance
]]
local function returnFalse() return false end
local req = require
local _M = {}
_M.genRequire = function(checkGlobal, path)
local localLibs = {}
return function(name)
if checkGlobal(name) then return _M.reqShared(name)
else
if localLibs[name] == nil then
local f, err = loadfile(path .. "/" .. name .. ".lua")
if err ~= nil then error(err) end
if f == nil then error('unknown error') end
localLibs[name] = f()
end
return localLibs[name]
end
end
end
local function push(path)
local f = loadfile(path .. "/hdtmodule.lua")
require = _M.genRequire(type(f) == "function" and f() or returnFalse, path)
_G['hdtLoadFFI'] = function(filename) return ffi.load(path .. '/' .. filename) end
end
_M.reqShared = function(name)
if sharedModules[name] == nil then
local f, err = loadfile("lua/" .. name .. '/init.lua')
if err ~= nil then
sharedModules[name] = req(name)
elseif f == nil then
error('unknown error')
else
local lastReq = require
local lastFFI = _G['hdtLoadFFI']
push("lua/" .. name)
log(string.format('model %s require = %s', name, tostring(require)))
sharedModules[name] = f()
require = lastReq
_G['hdtLoadFFI'] = lastFFI
end
end
return sharedModules[name]
end
_M.reqLocal = function(name)
if packageStack[packageTop][2][name] == nil then
local f, err = loadfile(packageStack[packageTop][1] .. "/" .. name ..
".lua")
if err ~= nil then error(err) end
if f == nil then error('unknown error') end
packageStack[packageTop][2][name] = f()
end
return packageStack[packageTop][2][name]
end
require = _M.genRequire(dofile("hdtmodule.lua"), '.')