-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.lua
More file actions
39 lines (39 loc) · 1.14 KB
/
environment.lua
File metadata and controls
39 lines (39 loc) · 1.14 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
local current = getfenv()
local meta = {__index = current, __newindex = current}
local __loadonce = {}
local __loaded = {}
local t
t = setmetatable({
dofile_once = function(filename)
local result = nil
local cached = __loadonce[filename]
if cached ~= nil then
result = cached[1]
else
local f, err = setfenv(loadfile(filename), t)
if f == nil then return f, err end
local __newindex = meta.__newindex
meta.__newindex = nil
result = f()
meta.__newindex = __newindex
__loadonce[filename] = {result}
do_mod_appends(filename)
end
return result
end,
dofile = function(filename)
local f = __loaded[filename]
if f == nil then
f, err = setfenv(loadfile(filename), t)
if f == nil then return f, err end
__loaded[filename] = f
end
local __newindex = meta.__newindex
meta.__newindex = nil
local result = f()
meta.__newindex = __newindex
do_mod_appends(filename)
return result
end,
}, meta)
setfenv(3, t)