-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsandbox.lua
218 lines (203 loc) · 6.03 KB
/
sandbox.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
---@class SandBox
local M = {}
M.__index = M
local function make_env()
local env = {}
env._G = env
env._VERSION = _VERSION
env.assert = assert
env.collectgarbage = collectgarbage
env.error = error
env.getmetatable = getmetatable
env.ipairs = ipairs
env.next = next
env.pairs = pairs
env.pcall = pcall
env.print = print
env.rawequal = rawequal
env.rawget = rawget
env.rawlen = rawlen
env.rawset = rawset
--env.require = require
env.select = select
env.setmetatable = setmetatable
env.tonumber = tonumber
env.tostring = tostring
env.type = type
env.xpcall = xpcall
env.coroutine = {}
env.coroutine.close = coroutine.close
env.coroutine.create = coroutine.create
env.coroutine.isyieldable = coroutine.isyieldable
env.coroutine.resume = coroutine.resume
env.coroutine.running = coroutine.running
env.coroutine.status = coroutine.status
env.coroutine.wrap = coroutine.wrap
env.coroutine.yield = coroutine.yield
env.math = {}
env.math.abs = math.abs
env.math.acos = math.acos
env.math.asin = math.asin
env.math.atan = math.atan
env.math.ceil = math.ceil
env.math.cos = math.cos
env.math.deg = math.deg
env.math.exp = math.exp
env.math.floor = math.floor
env.math.fmod = math.fmod
env.math.huge = math.huge
env.math.log = math.log
env.math.max = math.max
env.math.maxinteger = math.maxinteger
env.math.min = math.min
env.math.mininteger = math.mininteger
env.math.modf = math.modf
env.math.pi = math.pi
env.math.rad = math.rad
env.math.random = math.random
env.math.randomseed = math.randomseed
env.math.sin = math.sin
env.math.sqrt = math.sqrt
env.math.tan = math.tan
env.math.tointeger = math.tointeger
env.math.type = math.type
env.math.ult = math.ult
env.os = {}
env.os.clock = os.clock
env.os.date = os.date
env.os.difftime = os.difftime
env.os.time = os.time
env.package = {}
env.package.config = package.config
env.package.loaded = {}
env.package.preload = {}
env.package.path = '?.lua;?/init.lua'
env.package.searchers = {}
env.package.searchpath = package.searchpath
env.string = {}
env.string.byte = string.byte
env.string.char = string.char
env.string.dump = string.dump
env.string.find = string.find
env.string.format = string.format
env.string.gmatch = string.gmatch
env.string.gsub = string.gsub
env.string.len = string.len
env.string.lower = string.lower
env.string.match = string.match
env.string.pack = string.pack
env.string.packsize = string.packsize
env.string.rep = string.rep
env.string.reverse = string.reverse
env.string.sub = string.sub
env.string.unpack = string.unpack
env.string.upper = string.upper
env.table = {}
env.table.concat = table.concat
env.table.insert = table.insert
env.table.move = table.move
env.table.pack = table.pack
env.table.remove = table.remove
env.table.sort = table.sort
env.table.unpack = table.unpack
env.utf8 = {}
env.utf8.char = utf8.char
env.utf8.charpattern = utf8.charpattern
env.utf8.codepoint = utf8.codepoint
env.utf8.codes = utf8.codes
env.utf8.len = utf8.len
env.utf8.offset = utf8.offset
return env
end
---@private
function M:make_searcher()
local searchers = package.searchers
local prefix_name = self.prefix_name
return function (name)
if prefix_name then
name = prefix_name .. '.' .. name
end
local msg = ''
for _, searcher in ipairs(searchers) do
local f, extra = searcher(name)
if type(f) == 'function' then
return f, extra
elseif type(f) == 'string' then
msg = msg .. f
end
end
return msg
end
end
---@private
function M:make_preload()
local preload = self.env.package.preload
return function (name)
assert(type(preload) == "table", "'package.preload' must be a table")
if preload[name] == nil then
return ("\n\tno field package.preload['%s']"):format(name)
end
return preload[name]
end
end
---@private
function M:make_require(searchers, loaded)
local env = self.env
searchers = searchers or env.package.searchers
loaded = loaded or env.package.loaded
local function search_loader(name)
local msg = ''
for _, searcher in ipairs(searchers) do
local f, extra = searcher(name)
if type(f) == 'function' then
return f, extra
elseif type(f) == 'string' then
msg = msg .. f
end
end
error(("module '%s' not found:%s"):format(name, msg))
end
return function (name)
assert(type(name) == "string", ("bad argument #1 to 'require' (string expected, got %s)"):format(type(name)))
local p = loaded[name]
if p ~= nil then
return p
end
local f, extra = search_loader(name)
if debug.getupvalue(f, 1) == '_ENV' then
debug.setupvalue(f, 1, env)
end
local res = f(name, extra)
if res == nil then
res = true
end
loaded[name] = res
return res
end
end
---@package
---@param sandbox_name string
---@param prefix_name? string
function M:init(sandbox_name, prefix_name)
---@type string
self.name = sandbox_name
---@type string?
self.prefix_name = prefix_name
---@type table
self.env = make_env()
self.env.package.searchers[1] = self:make_preload()
self.env.package.searchers[2] = self:make_searcher()
self.env.require = self:make_require()
end
function M:require(name)
local require = self:make_require(package.searchers, package.loaded)
return require(name)
end
---@param name string
---@param prefix_name? string
---@return SandBox
return function (name, prefix_name)
local sandbox = setmetatable({}, M)
sandbox:init(name, prefix_name)
return sandbox
end