- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloadkit.lua
More file actions
Latest commit
137 lines (137 loc) · 3.27 KB
/
Copy pathloadkit.lua
File metadata and controls
137 lines (137 loc) · 3.27 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
localVERSION="1.1.0"
localinsert, remove
do
local_obj_0=table
insert, remove=_obj_0.insert, _obj_0.remove
end
localunpack=table.unpackor_G.unpack
localdirsep, pathsep, wildcard=unpack((function()
local_accum_0= { }
local_len_0=1
forcinpackage.config:gmatch("[^\n]") do
_accum_0[_len_0] =c
_len_0=_len_0+1
end
return_accum_0
end)())
localloaders
loaders=function()
returnpackage.loadersorpackage.searchers
end
localescape_pattern
do
localpunct="[%^$()%.%[%]*+%-?%%]"
escape_pattern=function(str)
return (str:gsub(punct, function(p)
return"%" ..p
end))
end
end
wildcard=escape_pattern(wildcard)
localmodsep=escape_pattern(".")
localmake_loader
make_loader=function(ext, handler, load_path)
ifload_path==nilthen
load_path=package.path
end
handler=handlerorfunction(_, _, ...)
return...
end
localsearch_paths
do
local_accum_0= { }
local_len_0=1
forpathinload_path:gmatch("[^" ..tostring(pathsep) .."]+") do
local_continue_0=false
repeat
do
localp=path:match("^(.-)%.lua$")
ifpthen
_accum_0[_len_0] =p.."." ..ext
else
_continue_0=true
break
end
end
_len_0=_len_0+1
_continue_0=true
untiltrue
ifnot_continue_0then
break
end
end
search_paths=_accum_0
end
returnfunction(name)
localname_path=name:gsub(modsep, dirsep)
localfile, file_path
for_index_0=1, #search_pathsdo
localsearch_path=search_paths[_index_0]
file_path=search_path:gsub(wildcard, name_path)
file=io.open(file_path)
iffilethen
break
end
end
iffilethen
localloaded= {
handler(file, name, file_path)
}
ifnot (io.type(file) =="closed file") then
file:close()
end
returnunpack(loaded)
end
end
end
localregistered_handlers= { }
localregister
register=function(ext, handler, pos)
ifpos==nilthen
pos=2
end
assert(ext, "missing extension")
assert(handler, "missing handler")
localreal_ext=ext:match("^[^:]*")
localloader_fn=make_loader(real_ext, handler)
localwrapped_loader
wrapped_loader=function(name)
localres, err=loader_fn(name)
ifres~=nilthen
returnfunction()
returnres
end
else
returnerror"could not load `" ..tostring(real_ext) .."` file"
end
end
insert(loaders(), pos, wrapped_loader)
registered_handlers[ext] =wrapped_loader
returntrue
end
localunregister
unregister=function(ext)
localloader_fn=registered_handlers[ext]
ifnot (loader_fn) then
returnnil, "can't find existing loader `" ..tostring(ext) .."`"
end
fori, linpairs(loaders()) do
ifl==loader_fnthen
remove(loaders(), i)
returntrue
end
end
returnnil, "loader `" ..tostring(ext) .."` is no longer in searchers"
end
localis_registered
is_registered=function(ext)
returnnotnotregistered_handlers[ext]
end
return {
VERSION=VERSION,
register=register,
unregister=unregister,
is_registered=is_registered,
make_loader=make_loader,
_registered_handlers=registered_handlers
}