-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequire.lua
More file actions
124 lines (99 loc) · 2.67 KB
/
require.lua
File metadata and controls
124 lines (99 loc) · 2.67 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
-- -*- coding: utf8 -*-
-- Copyright (c) 2014 Odd Straaboe <oddstr13 at openshell dot no>
-- License: MIT - http://opensource.org/licenses/MIT
-- Filename: require.lua
_G.package = {}
_G.package.cpath = ""
_G.package.loaded = {}
_G.package.loadlib = function() error("NotImplemented: package.loadlib") end
_G.package.path = table.concat({
"?",
"?.lua",
"?/init.lua",
"/lib/?",
"/lib/?.lua",
"/lib/?/init.lua",
"/rom/apis/?",
"/rom/apis/?.lua",
"/rom/apis/?/init.lua",
"/rom/apis/turtle/?",
"/rom/apis/turtle/?.lua",
"/rom/apis/turtle/?/init.lua",
"/rom/apis/command/?",
"/rom/apis/command/?.lua",
"/rom/apis/command/?/init.lua",
}, ";")
_G.package.preload = {}
_G.package.seeall = function(module) error("NotImplemented: package.seeall") end
_G.module = function(m) error("NotImplemented: module") end
local _package_path_loader = function(name)
local fname = name:gsub("%.", "/")
for pattern in package.path:gmatch("[^;]+") do
local fpath = pattern:gsub("%?", fname)
if fs.exists(fpath) and not fs.isDir(fpath) then
local apienv = {}
setmetatable(apienv, {__index = _G})
local apifunc, err = loadfile(fpath)
local ok
if apifunc then
setfenv(apifunc, apienv)
ok, err = pcall(apifunc)
end
if not apifunc or not ok then
error("error loading module '" .. name .. "' from file '" .. fpath .. "'\n\t" .. err)
end
local api = {}
if type(err) == "table" then
api = err
end
for k,v in pairs( apienv ) do
api[k] = v
end
return api
end
end
end
_G.package.loaders = {
function(name)
if package.preload[name] then
return package.preload[name]
else
return "\tno field package.preload['" .. name .. "']"
end
end,
function(name)
local _errors = {}
local fname = name:gsub("%.", "/")
for pattern in package.path:gmatch("[^;]+") do
local fpath = pattern:gsub("%?", fname)
if fs.exists(fpath) and not fs.isDir(fpath) then
return _package_path_loader
else
table.insert(_errors, "\tno file '" .. fpath .. "'")
end
end
return table.concat(_errors, "\n")
end
}
_G.require = function(name)
if package.loaded[name] then
--return package.loaded[name]
end
local _errors = {}
for _, searcher in pairs(package.loaders) do
local loader = searcher(name)
if type(loader) == "function" then
local res = loader(name)
if res ~= nil then
package.loaded[name] = res
end
if package.loaded[name] == nil then
package.loaded[name] = true
end
return package.loaded[name]
elseif type(loader) == "string" then
table.insert(_errors, loader)
end
end
error("module '" .. name .. "' not found:\n" .. table.concat(_errors, "\n"))
end