l-package.lua /size: 11 Kb    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
l-package
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to luat-lib.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
-- Code moved from data-lua and changed into a plug-in.
10 11
-- We overload the regular loader. We do so because we operate mostly in
12
-- tds and use our own loader code. Alternatively we could use a more
13
-- extensive definition of package.path and package.cpath but even then
14
-- we're not done. Also, we now have better tracing.
15
--
16
-- -- local mylib = require("libtest")
17
-- -- local mysql = require("luasql.mysql")
18 19
local
type
=
type
20
local
gsub
,
format
,
find
=
string
.
gsub
,
string
.
format
,
string
.
find
21
local
insert
,
remove
=
table
.
insert
,
table
.
remove
22 23
local
P
,
S
,
Cs
,
lpegmatch
=
lpeg
.
P
,
lpeg
.
S
,
lpeg
.
Cs
,
lpeg
.
match
24 25
local
package
=
package
26
local
searchers
=
package
.
searchers
or
package
.
loaders
27 28
-------.loaders = nil -- old stuff that we don't want
29
-------.seeall = nil -- old stuff that we don't want
30 31
-- dummies
32 33
local
filejoin
=
file
and
file
.
join
or
function
(
path
,
name
)
return
path
.
.
"
/
"
.
.
name
end
34
local
isreadable
=
file
and
file
.
is_readable
or
function
(
name
)
local
f
=
io
.
open
(
name
)
if
f
then
f
:
close
(
)
return
true
end
end
35
local
addsuffix
=
file
and
file
.
addsuffix
or
function
(
name
,
suffix
)
return
name
.
.
"
.
"
.
.
suffix
end
36 37
-- local separator, concatinator, placeholder, pathofexecutable, ignorebefore = string.match(package.config,"(.-)\n(.-)\n(.-)\n(.-)\n(.-)\n")
38
--
39
-- local config = {
40
-- separator = separator, -- \ or /
41
-- concatinator = concatinator, -- ;
42
-- placeholder = placeholder, -- ? becomes name
43
-- pathofexecutable = pathofexecutable, -- ! becomes executables dir (on windows)
44
-- ignorebefore = ignorebefore, -- - remove all before this when making lua_open
45
-- }
46 47
local
function
cleanpath
(
path
)
-- hm, don't we have a helper for this?
48
return
path
49
end
50 51
local
pattern
=
Cs
(
(
(
(
1
-
S
(
"
\\/
"
)
)
^
0
*
(
S
(
"
\\/
"
)
^
1
/
"
/
"
)
)
^
0
*
(
P
(
"
.
"
)
^
1
/
"
/
"
+
P
(
1
)
)
^
1
)
*
-1
)
52 53
local
function
lualibfile
(
name
)
54
return
lpegmatch
(
pattern
,
name
)
or
name
55
end
56 57
local
offset
=
luarocks
and
1
or
0
-- todo: also check other extras
58 59
local
helpers
=
package
.
helpers
or
{
60
cleanpath
=
cleanpath
,
61
lualibfile
=
lualibfile
,
62
trace
=
false
,
63
report
=
function
(
...
)
print
(
format
(
...
)
)
end
,
64
builtin
=
{
65
[
"
preload table
"
]
=
searchers
[
1
+
offset
]
,
-- special case, built-in libs
66
[
"
path specification
"
]
=
searchers
[
2
+
offset
]
,
67
[
"
cpath specification
"
]
=
searchers
[
3
+
offset
]
,
68
[
"
all in one fallback
"
]
=
searchers
[
4
+
offset
]
,
-- special case, combined libs
69
}
,
70
methods
=
{
71
}
,
72
sequence
=
{
73
"
already loaded
"
,
74
"
preload table
"
,
75
"
qualified path
"
,
-- beware, lua itself doesn't handle qualified paths (prepends ./)
76
"
lua extra list
"
,
77
"
lib extra list
"
,
78
"
path specification
"
,
79
"
cpath specification
"
,
80
"
all in one fallback
"
,
81
"
not loaded
"
,
82
}
83
}
84 85
package
.
helpers
=
helpers
86 87
local
methods
=
helpers
.
methods
88
local
builtin
=
helpers
.
builtin
89 90
-- extra tds/ctx paths ... a bit of overhead for efficient tracing
91 92
local
extraluapaths
=
{
}
93
local
extralibpaths
=
{
}
94
local
luapaths
=
nil
-- delayed
95
local
libpaths
=
nil
-- delayed
96
local
oldluapath
=
nil
97
local
oldlibpath
=
nil
98 99
local
nofextralua
=
-1
100
local
nofextralib
=
-1
101
local
nofpathlua
=
-1
102
local
nofpathlib
=
-1
103 104
local
function
listpaths
(
what
,
paths
)
105
local
nofpaths
=
#
paths
106
if
nofpaths
>
0
then
107
for
i
=
1
,
nofpaths
do
108
helpers
.
report
(
"
using %s path %i: %s
"
,
what
,
i
,
paths
[
i
]
)
109
end
110
else
111
helpers
.
report
(
"
no %s paths defined
"
,
what
)
112
end
113
return
nofpaths
114
end
115 116
local
function
getextraluapaths
(
)
117
if
helpers
.
trace
and
#
extraluapaths
~
=
nofextralua
then
118
nofextralua
=
listpaths
(
"
extra lua
"
,
extraluapaths
)
119
end
120
return
extraluapaths
121
end
122 123
local
function
getextralibpaths
(
)
124
if
helpers
.
trace
and
#
extralibpaths
~
=
nofextralib
then
125
nofextralib
=
listpaths
(
"
extra lib
"
,
extralibpaths
)
126
end
127
return
extralibpaths
128
end
129 130
local
function
getluapaths
(
)
131
local
luapath
=
package
.
path
or
"
"
132
if
oldluapath
~
=
luapath
then
133
luapaths
=
file
.
splitpath
(
luapath
,
"
;
"
)
134
oldluapath
=
luapath
135
nofpathlua
=
-1
136
end
137
if
helpers
.
trace
and
#
luapaths
~
=
nofpathlua
then
138
nofpathlua
=
listpaths
(
"
builtin lua
"
,
luapaths
)
139
end
140
return
luapaths
141
end
142 143
local
function
getlibpaths
(
)
144
local
libpath
=
package
.
cpath
or
"
"
145
if
oldlibpath
~
=
libpath
then
146
libpaths
=
file
.
splitpath
(
libpath
,
"
;
"
)
147
oldlibpath
=
libpath
148
nofpathlib
=
-1
149
end
150
if
helpers
.
trace
and
#
libpaths
~
=
nofpathlib
then
151
nofpathlib
=
listpaths
(
"
builtin lib
"
,
libpaths
)
152
end
153
return
libpaths
154
end
155 156
package
.
luapaths
=
getluapaths
157
package
.
libpaths
=
getlibpaths
158
package
.
extraluapaths
=
getextraluapaths
159
package
.
extralibpaths
=
getextralibpaths
160 161
local
hashes
=
{
162
lua
=
{
}
,
163
lib
=
{
}
,
164
}
165 166
local
function
registerpath
(
tag
,
what
,
target
,
...
)
167
local
pathlist
=
{
...
}
168
local
cleanpath
=
helpers
.
cleanpath
169
local
trace
=
helpers
.
trace
170
local
report
=
helpers
.
report
171
local
hash
=
hashes
[
what
]
172
--
173
local
function
add
(
path
)
174
local
path
=
cleanpath
(
path
)
175
if
not
hash
[
path
]
then
176
target
[
#
target
+
1
]
=
path
177
hash
[
path
]
=
true
178
if
trace
then
179
report
(
"
registered %s path %s: %s
"
,
tag
,
#
target
,
path
)
180
end
181
else
182
if
trace
then
183
report
(
"
duplicate %s path: %s
"
,
tag
,
path
)
184
end
185
end
186
end
187
--
188
for
p
=
1
,
#
pathlist
do
189
local
path
=
pathlist
[
p
]
190
if
type
(
path
)
=
=
"
table
"
then
191
for
i
=
1
,
#
path
do
192
add
(
path
[
i
]
)
193
end
194
else
195
add
(
path
)
196
end
197
end
198
end
199 200
local
function
pushpath
(
tag
,
what
,
target
,
path
)
201
local
path
=
helpers
.
cleanpath
(
path
)
202
insert
(
target
,
1
,
path
)
203
if
helpers
.
trace
then
204
helpers
.
report
(
"
pushing %s path in front: %s
"
,
tag
,
path
)
205
end
206
end
207 208
local
function
poppath
(
tag
,
what
,
target
)
209
local
path
=
remove
(
target
,
1
)
210
if
helpers
.
trace
then
211
if
path
then
212
helpers
.
report
(
"
popping %s path from front: %s
"
,
tag
,
path
)
213
else
214
helpers
.
report
(
"
no %s path to pop
"
,
tag
)
215
end
216
end
217
end
218 219
helpers
.
registerpath
=
registerpath
220 221
function
package
.
extraluapath
(
...
)
222
registerpath
(
"
extra lua
"
,
"
lua
"
,
extraluapaths
,
...
)
223
end
224
function
package
.
pushluapath
(
path
)
225
pushpath
(
"
extra lua
"
,
"
lua
"
,
extraluapaths
,
path
)
226
end
227
function
package
.
popluapath
(
)
228
poppath
(
"
extra lua
"
,
"
lua
"
,
extraluapaths
)
229
end
230 231
function
package
.
extralibpath
(
...
)
232
registerpath
(
"
extra lib
"
,
"
lib
"
,
extralibpaths
,
...
)
233
end
234
function
package
.
pushlibpath
(
path
)
235
pushpath
(
"
extra lib
"
,
"
lib
"
,
extralibpaths
,
path
)
236
end
237
function
package
.
poplibpath
(
)
238
poppath
(
"
extra lib
"
,
"
lua
"
,
extralibpaths
)
239
end
240 241
-- lib loader (used elsewhere)
242 243
local
function
loadedaslib
(
resolved
,
rawname
)
-- todo: strip all before first -
244
local
base
=
gsub
(
rawname
,
"
%.
"
,
"
_
"
)
245
-- so, we can do a require("foo/bar") and initialize bar
246
-- local base = gsub(file.basename(rawname),"%.","_")
247
local
init
=
"
luaopen_
"
.
.
gsub
(
base
,
"
%.
"
,
"
_
"
)
248
if
helpers
.
trace
then
249
helpers
.
report
(
"
calling loadlib with '%s' with init '%s'
"
,
resolved
,
init
)
250
end
251
return
package
.
loadlib
(
resolved
,
init
)
252
end
253 254
helpers
.
loadedaslib
=
loadedaslib
255 256
-- wrapped and new loaders
257 258
local
function
loadedbypath
(
name
,
rawname
,
paths
,
islib
,
what
)
259
local
trace
=
helpers
.
trace
260
for
p
=
1
,
#
paths
do
261
local
path
=
paths
[
p
]
262
local
resolved
=
filejoin
(
path
,
name
)
263
if
trace
then
264
helpers
.
report
(
"
%s path, identifying '%s' on '%s'
"
,
what
,
name
,
path
)
265
end
266
if
isreadable
(
resolved
)
then
267
if
trace
then
268
helpers
.
report
(
"
%s path, '%s' found on '%s'
"
,
what
,
name
,
resolved
)
269
end
270
if
islib
then
271
return
loadedaslib
(
resolved
,
rawname
)
272
else
273
return
loadfile
(
resolved
)
274
end
275
end
276
end
277
end
278 279
helpers
.
loadedbypath
=
loadedbypath
280 281
local
function
loadedbyname
(
name
,
rawname
)
282
if
find
(
name
,
"
^/
"
)
or
find
(
name
,
"
^[a-zA-Z]:/
"
)
then
283
local
trace
=
helpers
.
trace
284
if
trace
then
285
helpers
.
report
(
"
qualified name, identifying '%s'
"
,
what
,
name
)
286
end
287
if
isreadable
(
name
)
then
288
if
trace
then
289
helpers
.
report
(
"
qualified name, '%s' found
"
,
what
,
name
)
290
end
291
return
loadfile
(
name
)
292
end
293
end
294
end
295 296
helpers
.
loadedbyname
=
loadedbyname
297 298
methods
[
"
already loaded
"
]
=
function
(
name
)
299
return
package
.
loaded
[
name
]
300
end
301 302
methods
[
"
preload table
"
]
=
function
(
name
)
303
return
builtin
[
"
preload table
"
]
(
name
)
304
end
305 306
methods
[
"
qualified path
"
]
=
function
(
name
)
307
return
loadedbyname
(
addsuffix
(
lualibfile
(
name
)
,
"
lua
"
)
,
name
)
308
end
309 310
methods
[
"
lua extra list
"
]
=
function
(
name
)
311
return
loadedbypath
(
addsuffix
(
lualibfile
(
name
)
,
"
lua
"
)
,
name
,
getextraluapaths
(
)
,
false
,
"
lua
"
)
312
end
313 314
methods
[
"
lib extra list
"
]
=
function
(
name
)
315
return
loadedbypath
(
addsuffix
(
lualibfile
(
name
)
,
os
.
libsuffix
)
,
name
,
getextralibpaths
(
)
,
true
,
"
lib
"
)
316
end
317 318
methods
[
"
path specification
"
]
=
function
(
name
)
319
getluapaths
(
)
-- triggers list building and tracing
320
return
builtin
[
"
path specification
"
]
(
name
)
321
end
322 323
methods
[
"
cpath specification
"
]
=
function
(
name
)
324
getlibpaths
(
)
-- triggers list building and tracing
325
return
builtin
[
"
cpath specification
"
]
(
name
)
326
end
327 328
methods
[
"
all in one fallback
"
]
=
function
(
name
)
329
return
builtin
[
"
all in one fallback
"
]
(
name
)
330
end
331 332
methods
[
"
not loaded
"
]
=
function
(
name
)
333
if
helpers
.
trace
then
334
helpers
.
report
(
"
unable to locate '%s'
"
,
name
or
"
?
"
)
335
end
336
return
nil
337
end
338 339
local
level
=
0
340
local
used
=
{
}
341 342
helpers
.
traceused
=
false
343 344
function
helpers
.
loaded
(
name
)
345
local
sequence
=
helpers
.
sequence
346
level
=
level
+
1
347
for
i
=
1
,
#
sequence
do
348
local
method
=
sequence
[
i
]
349
if
helpers
.
trace
then
350
helpers
.
report
(
"
%s, level '%s', method '%s', name '%s'
"
,
"
locating
"
,
level
,
method
,
name
)
351
end
352
local
result
,
rest
=
methods
[
method
]
(
name
)
353
if
type
(
result
)
=
=
"
function
"
then
354
if
helpers
.
trace
then
355
helpers
.
report
(
"
%s, level '%s', method '%s', name '%s'
"
,
"
found
"
,
level
,
method
,
name
)
356
end
357
if
helpers
.
traceused
then
358
used
[
#
used
+
1
]
=
{
level
=
level
,
name
=
name
}
359
end
360
level
=
level
-
1
361
return
result
,
rest
362
end
363
end
364
-- safeguard, we never come here
365
level
=
level
-
1
366
return
nil
367
end
368 369
function
helpers
.
showused
(
)
370
local
n
=
#
used
371
if
n
>
0
then
372
helpers
.
report
(
"
%s libraries loaded:
"
,
n
)
373
helpers
.
report
(
)
374
for
i
=
1
,
n
do
375
local
u
=
used
[
i
]
376
helpers
.
report
(
"
%i %a
"
,
u
.
level
,
u
.
name
)
377
end
378
helpers
.
report
(
)
379
end
380
end
381 382
function
helpers
.
unload
(
name
)
383
if
helpers
.
trace
then
384
if
package
.
loaded
[
name
]
then
385
helpers
.
report
(
"
unloading, name '%s', %s
"
,
name
,
"
done
"
)
386
else
387
helpers
.
report
(
"
unloading, name '%s', %s
"
,
name
,
"
not loaded
"
)
388
end
389
end
390
package
.
loaded
[
name
]
=
nil
391
end
392 393
-- overloading require does not work out well so we need to push it in
394
-- front ..
395 396
table
.
insert
(
searchers
,
1
,
helpers
.
loaded
)
397 398
if
context
then
399
package
.
path
=
"
"
400
end
401