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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
if not modules then modules = { } end modules ['l-package'] = {
version = 1.001,
comment = "companion to luat-lib.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- Code moved from data-lua and changed into a plug-in.
-- We overload the regular loader. We do so because we operate mostly in
-- tds and use our own loader code. Alternatively we could use a more
-- extensive definition of package.path and package.cpath but even then
-- we're not done. Also, we now have better tracing.
--
-- -- local mylib = require("libtest")
-- -- local mysql = require("luasql.mysql")
local type = type
local gsub, format, find = string.gsub, string.format, string.find
local P, S, Cs, lpegmatch = lpeg.P, lpeg.S, lpeg.Cs, lpeg.match
local package = package
local searchers = package.searchers or package.loaders
-- dummies
local filejoin = file and file.join or function(path,name) return path .. "/" .. name end
local isreadable = file and file.is_readable or function(name) local f = io.open(name) if f then f:close() return true end end
local addsuffix = file and file.addsuffix or function(name,suffix) return name .. "." .. suffix end
-- local separator, concatinator, placeholder, pathofexecutable, ignorebefore = string.match(package.config,"(.-)\n(.-)\n(.-)\n(.-)\n(.-)\n")
--
-- local config = {
-- separator = separator, -- \ or /
-- concatinator = concatinator, -- ;
-- placeholder = placeholder, -- ? becomes name
-- pathofexecutable = pathofexecutable, -- ! becomes executables dir (on windows)
-- ignorebefore = ignorebefore, -- - remove all before this when making lua_open
-- }
local function cleanpath(path) -- hm, don't we have a helper for this?
return path
end
local pattern = Cs((((1-S("\\/"))^0 * (S("\\/")^1/"/"))^0 * (P(".")^1/"/"+P(1))^1) * -1)
local function lualibfile(name)
return lpegmatch(pattern,name) or name
end
local offset = luarocks and 1 or 0 -- todo: also check other extras
local helpers = package.helpers or {
cleanpath = cleanpath,
lualibfile = lualibfile,
trace = false,
report = function(...) print(format(...)) end,
builtin = {
["preload table"] = searchers[1+offset], -- special case, built-in libs
["path specification"] = searchers[2+offset],
["cpath specification"] = searchers[3+offset],
["all in one fallback"] = searchers[4+offset], -- special case, combined libs
},
methods = {
},
sequence = {
"already loaded",
"preload table",
"qualified path", -- beware, lua itself doesn't handle qualified paths (prepends ./)
"lua extra list",
"lib extra list",
"path specification",
"cpath specification",
"all in one fallback",
"not loaded",
}
}
package.helpers = helpers
local methods = helpers.methods
local builtin = helpers.builtin
-- extra tds/ctx paths ... a bit of overhead for efficient tracing
local extraluapaths = { }
local extralibpaths = { }
local luapaths = nil -- delayed
local libpaths = nil -- delayed
local oldluapath = nil
local oldlibpath = nil
local nofextralua = -1
local nofextralib = -1
local nofpathlua = -1
local nofpathlib = -1
local function listpaths(what,paths)
local nofpaths = #paths
if nofpaths > 0 then
for i=1,nofpaths do
helpers.report("using %s path %i: %s",what,i,paths[i])
end
else
helpers.report("no %s paths defined",what)
end
return nofpaths
end
local function getextraluapaths()
if helpers.trace and #extraluapaths ~= nofextralua then
nofextralua = listpaths("extra lua",extraluapaths)
end
return extraluapaths
end
local function getextralibpaths()
if helpers.trace and #extralibpaths ~= nofextralib then
nofextralib = listpaths("extra lib",extralibpaths)
end
return extralibpaths
end
local function getluapaths()
local luapath = package.path or ""
if oldluapath ~= luapath then
luapaths = file.splitpath(luapath,";")
oldluapath = luapath
nofpathlua = -1
end
if helpers.trace and #luapaths ~= nofpathlua then
nofpathlua = listpaths("builtin lua",luapaths)
end
return luapaths
end
local function getlibpaths()
local libpath = package.cpath or ""
if oldlibpath ~= libpath then
libpaths = file.splitpath(libpath,";")
oldlibpath = libpath
nofpathlib = -1
end
if helpers.trace and #libpaths ~= nofpathlib then
nofpathlib = listpaths("builtin lib",libpaths)
end
return libpaths
end
package.luapaths = getluapaths
package.libpaths = getlibpaths
package.extraluapaths = getextraluapaths
package.extralibpaths = getextralibpaths
local hashes = {
lua = { },
lib = { },
}
local function registerpath(tag,what,target,...)
local pathlist = { ... }
local cleanpath = helpers.cleanpath
local trace = helpers.trace
local report = helpers.report
local hash = hashes[what]
--
local function add(path)
local path = cleanpath(path)
if not hash[path] then
target[#target+1] = path
hash[path] = true
if trace then
report("registered %s path %s: %s",tag,#target,path)
end
else
if trace then
report("duplicate %s path: %s",tag,path)
end
end
end
--
for p=1,#pathlist do
local path = pathlist[p]
if type(path) == "table" then
for i=1,#path do
add(path[i])
end
else
add(path)
end
end
return paths
end
helpers.registerpath = registerpath
function package.extraluapath(...)
registerpath("extra lua","lua",extraluapaths,...)
end
function package.extralibpath(...)
registerpath("extra lib","lib",extralibpaths,...)
end
-- lib loader (used elsewhere)
local function loadedaslib(resolved,rawname) -- todo: strip all before first -
local base = gsub(rawname,"%.","_")
-- so, we can do a require("foo/bar") and initialize bar
-- local base = gsub(file.basename(rawname),"%.","_")
local init = "luaopen_" .. gsub(base,"%.","_")
if helpers.trace then
helpers.report("calling loadlib with '%s' with init '%s'",resolved,init)
end
return package.loadlib(resolved,init)
end
helpers.loadedaslib = loadedaslib
-- wrapped and new loaders
local function loadedbypath(name,rawname,paths,islib,what)
local trace = helpers.trace
for p=1,#paths do
local path = paths[p]
local resolved = filejoin(path,name)
if trace then
helpers.report("%s path, identifying '%s' on '%s'",what,name,path)
end
if isreadable(resolved) then
if trace then
helpers.report("%s path, '%s' found on '%s'",what,name,resolved)
end
if islib then
return loadedaslib(resolved,rawname)
else
return loadfile(resolved)
end
end
end
end
helpers.loadedbypath = loadedbypath
local function loadedbyname(name,rawname)
if find(name,"^/") or find(name,"^[a-zA-Z]:/") then
local trace=helpers.trace
if trace then
helpers.report("qualified name, identifying '%s'",what,name)
end
if isreadable(name) then
if trace then
helpers.report("qualified name, '%s' found",what,name)
end
return loadfile(name)
end
end
end
helpers.loadedbyname = loadedbyname
methods["already loaded"] = function(name)
return package.loaded[name]
end
methods["preload table"] = function(name)
return builtin["preload table"](name)
end
methods["qualified path"]=function(name)
return loadedbyname(addsuffix(lualibfile(name),"lua"),name)
end
methods["lua extra list"] = function(name)
return loadedbypath(addsuffix(lualibfile(name),"lua" ),name,getextraluapaths(),false,"lua")
end
methods["lib extra list"] = function(name)
return loadedbypath(addsuffix(lualibfile(name),os.libsuffix),name,getextralibpaths(),true, "lib")
end
methods["path specification"] = function(name)
getluapaths() -- triggers list building and tracing
return builtin["path specification"](name)
end
methods["cpath specification"] = function(name)
getlibpaths() -- triggers list building and tracing
return builtin["cpath specification"](name)
end
methods["all in one fallback"] = function(name)
return builtin["all in one fallback"](name)
end
methods["not loaded"] = function(name)
if helpers.trace then
helpers.report("unable to locate '%s'",name or "?")
end
return nil
end
local level = 0
local used = { }
helpers.traceused = false
function helpers.loaded(name)
local sequence = helpers.sequence
level = level + 1
for i=1,#sequence do
local method = sequence[i]
if helpers.trace then
helpers.report("%s, level '%s', method '%s', name '%s'","locating",level,method,name)
end
local result, rest = methods[method](name)
if type(result) == "function" then
if helpers.trace then
helpers.report("%s, level '%s', method '%s', name '%s'","found",level,method,name)
end
if helpers.traceused then
used[#used+1] = { level = level, name = name }
end
level = level - 1
return result, rest
end
end
-- safeguard, we never come here
level = level - 1
return nil
end
function helpers.showused()
local n = #used
if n > 0 then
helpers.report("%s libraries loaded:",n)
helpers.report()
for i=1,n do
local u = used[i]
helpers.report("%i %a",u.level,u.name)
end
helpers.report()
end
end
function helpers.unload(name)
if helpers.trace then
if package.loaded[name] then
helpers.report("unloading, name '%s', %s",name,"done")
else
helpers.report("unloading, name '%s', %s",name,"not loaded")
end
end
package.loaded[name] = nil
end
-- overloading require does not work out well so we need to push it in
-- front ..
table.insert(searchers,1,helpers.loaded)
|