-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
63 lines (52 loc) · 1.27 KB
/
main.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
local lua = require("lua.main")
-- Custom environment
local env = {
print = print,
getmetatable = getmetatable,
setmetatable = setmetatable,
type = type,
pcall = function(f, ...)
local data = {pcall(f, ...)}
if not data[1] then
return false, data[2].error_object
else
return unpack(data)
end
end,
error = function(object)
error {error_object=object}
end,
pairs = pairs,
ipairs = ipairs,
unpack = unpack,
io = io,
table = table,
string = string,
tostring = tostring,
tonumber = tonumber,
math = math
}
-- Custom require and dofile functions
env._G = env
env.require = function(fpath)
local fpath = fpath:gsub("%.", "/")
local f = io.open(fpath .. ".lua", "r")
local code = f:read("*a")
f:close()
local success, ret = pcall(lua.run, lua, code, env)
if not success then
error("In file " .. fpath .. ": " .. ret)
end
return ret
end
env.dofile = function(fpath)
local f = io.open(fpath, "r")
local code = f:read("*a")
f:close()
local success, ret = pcall(lua.run, lua, code, env)
if not success then
error("In file " .. fpath .. ": " .. ret)
end
return ret
end
lua:dofile("test.lua", env)