-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsandbox.lua
More file actions
296 lines (271 loc) · 7.93 KB
/
sandbox.lua
File metadata and controls
296 lines (271 loc) · 7.93 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---@class SandBox
local M = {}
M.__index = M
function M:make_env()
local env = {}
self.env = 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.select = select
env.setmetatable = setmetatable
env.tonumber = tonumber
env.tostring = tostring
env.type = type
env.xpcall = xpcall
env.load = self:make_load()
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 = {
[1] = self:make_preload(),
[2] = self:make_searcher(),
}
env.package.searchpath = self:make_packagesearchpath()
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
env.debug = {}
env.debug.getinfo = self:make_debuggetinfo()
env.debug.setmetatable = self:make_debugsetmetatable()
env.debug.traceback = debug.traceback
env.debug.upvalueid = debug.upvalueid
env.io = {}
env.io.stderr = io.stderr
env.io.type = io.type
env.require = self:make_require()
return env
end
local function split(str, sep)
local result = {}
for part in string.gmatch(str, '([^' .. sep .. ']+)') do
result[#result+1] = part
end
return result
end
---@private
function M:make_searcher()
return function (name)
local path, err = self.env.package.searchpath(name, self.env.package.path)
if not path then
return err
end
local f = assert(loadfile(path, 't', self.env))
return f
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
self.require = 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
return self.require
end
function M:make_debuggetinfo()
local debug_getinfo = debug.getinfo
local type = type
return function (f, ...)
if type(f) == 'number' then
f = f + 1
end
local result = debug_getinfo(f, ...)
if not result then
return nil
end
result.func = nil
return result
end
end
function M:make_debugsetmetatable()
local type = type
local debug_setmetatable = debug.setmetatable
return function (t, mt)
if type(t) == 'table'
or type(t) == 'userdata' then
return setmetatable(t, mt)
else
return debug_setmetatable(t, mt)
end
end
end
function M:make_packagesearchpath()
local io_open = io.open
local string_gsub = string.gsub
return function (name, path, sep, rep)
local configs = split(self.env.package.config, '\r\n')
sep = sep or '.'
rep = rep or configs[1]
name = string_gsub(name, '%' .. sep, rep)
local pp
if self.prefix_name then
pp = string_gsub(self.prefix_name, '%' .. configs[3], name)
pp = string_gsub(pp, '%' .. sep, rep)
end
local msg = ''
local paths = split(path, configs[2])
for i = 1, #paths do
local filepath = string_gsub(paths[i], '%' .. configs[3], name)
if pp then
filepath = pp .. rep .. filepath
end
local f = io_open(filepath, 'r')
if f then
f:close()
return filepath
end
msg = msg .. ("\n\tno file '%s'"):format(filepath)
end
return nil, msg
end
end
function M:make_load()
local load = load
return function (chunk, chunkname, mode, env)
return load(chunk, chunkname, 't', env)
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:make_env()
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