-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathursalibcontext.lua
executable file
·49 lines (45 loc) · 1.5 KB
/
ursalibcontext.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
local lib = ursa.lib
local ul = {}
ursalibcontext = ul
-- it may seem like we should be chdir'ing when things are pushed or popped, but we actually live "natively" in the root directory. We only chdir when we need to.
local context_base = {prefix = "", absolute = lib.getcwd()}
local context_top = {current = context_base}
function ul.context_stack_push(chunk)
--print("pushing stack", chunk.prefix, chunk.absolute)
context_top = {current = chunk, up = context_top}
end
function ul.context_stack_pop(chunk)
--print("popping stack")
assert(context_top.current == chunk)
context_top = context_top.up
end
function ul.context_stack_prefix()
if context_top.current.prefix == "" then return "" end
return context_top.current.prefix .. "/"
end
function ul.context_stack_chdir(func, ...)
local cops = lib.getcwd()
lib.chdir(context_top.current.absolute)
current_print_prefix = context_top.current.prefix
--print("CHDIR INTO", context_stack[#context_stack].absolute)
local rp = lib.return_pack(func(...))
current_print_prefix = ""
--print("CHDIR EXIT")
lib.chdir(cops)
return lib.return_unpack(rp)
end
function ul.context_stack_chdir_native(...)
ul.context_stack_push(context_base)
local rp = lib.return_pack(ul.context_stack_chdir(...))
ul.context_stack_pop(context_base)
return lib.return_unpack(rp)
end
function ul.context_stack_get()
return context_top.current
end
function ul.context_stack_snapshot_get()
return context_top
end
function ul.context_stack_snapshot_put(top)
context_top = top
end