forked from patrickschulz/openPCells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.lua
50 lines (43 loc) · 1.22 KB
/
load.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
-- luacheck: ignore _get_reader _generic_load _load_module load
function _get_reader(filename)
local file = io.open(filename, "r")
if not file then
return nil, string.format("could not open file '%s'", filename)
end
local chunksize = 1000
return function()
return file:read(chunksize)
end
end
function _generic_load(reader, chunkname, synerrmsg, semerrmsg, env)
env = env or _ENV
local func, msg = load(reader, chunkname, "t", env)
if not func then
if synerrmsg then
error(string.format("%s: %s", synerrmsg, msg))
else
error(msg)
end
end
local status, chunk = pcall(func)
if not status then
if semerrmsg then
error(string.format("%s: %s", semerrmsg, chunk))
else
error(chunk)
end
end
return chunk
end
function _load_module(modname)
if not modname then
error("no module name given")
end
local filename = string.format("%s/%s.lua", _get_opc_home(), modname)
local chunkname = string.format("@%s", modname)
local reader, msg = _get_reader(filename)
if not reader then
error(msg)
end
return _generic_load(reader, chunkname)
end