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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
if not modules then modules = { } end modules ['l-lua'] = {
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"
}
-- compatibility hacks ... try to avoid usage
local major, minor = string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$")
_MAJORVERSION = tonumber(major) or 5
_MINORVERSION = tonumber(minor) or 1
_LUAVERSION = _MAJORVERSION + _MINORVERSION/10
-- lpeg
if not lpeg then
lpeg = require("lpeg")
end
-- basics:
if loadstring then
local loadnormal = load
function load(first,...)
if type(first) == "string" then
return loadstring(first,...)
else
return loadnormal(first,...)
end
end
else
loadstring = load
end
-- table:
-- At some point it was announced that i[pairs would be dropped, which makes
-- sense. As we already used the for loop and # in most places the impact on
-- ConTeXt was not that large; the remaining ipairs already have been replaced.
-- Hm, actually ipairs was retained, but we no longer use it anyway (nor
-- pairs).
--
-- Just in case, we provide the fallbacks as discussed in Programming
-- in Lua (http://www.lua.org/pil/7.3.html):
if not ipairs then
-- for k, v in ipairs(t) do ... end
-- for k=1,#t do local v = t[k] ... end
local function iterate(a,i)
i = i + 1
local v = a[i]
if v ~= nil then
return i, v --, nil
end
end
function ipairs(a)
return iterate, a, 0
end
end
if not pairs then
-- for k, v in pairs(t) do ... end
-- for k, v in next, t do ... end
function pairs(t)
return next, t -- , nil
end
end
-- The unpack function has been moved to the table table, and for compatiility
-- reasons we provide both now.
if not table.unpack then
table.unpack = _G.unpack
elseif not unpack then
_G.unpack = table.unpack
end
-- package:
-- if not package.seachers then
--
-- package.searchers = package.loaders -- 5.2
--
-- elseif not package.loaders then
--
-- package.loaders = package.searchers
--
-- end
if not package.loaders then -- brr, searchers is a special "loadlib function" userdata type
package.loaders = package.searchers
end
-- moved from util-deb to here:
local print, select, tostring = print, select, tostring
local inspectors = { }
function setinspector(inspector) -- global function
inspectors[#inspectors+1] = inspector
end
function inspect(...) -- global function
for s=1,select("#",...) do
local value = select(s,...)
local done = false
for i=1,#inspectors do
done = inspectors[i](value)
if done then
break
end
end
if not done then
print(tostring(value))
end
end
end
--
local dummy = function() end
function optionalrequire(...)
local ok, result = xpcall(require,dummy,...)
if ok then
return result
end
end
-- 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 = string.gsub, string.format
local package = package
local searchers = package.searchers or package.loaders
local libpaths = nil
local clibpaths = nil
local libhash = { }
local clibhash = { }
local libextras = { }
local clibextras = { }
-- 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 function cleanpath(path) -- hm, don't we have a helper for this?
return path
end
local helpers = package.helpers or {
libpaths = function() return { } end,
clibpaths = function() return { } end,
cleanpath = cleanpath,
trace = false,
report = function(...) print(format(...)) end,
}
package.helpers = helpers
local function getlibpaths()
return libpaths or helpers.libpaths(libhash)
end
local function getclibpaths()
return clibpaths or helpers.clibpaths(clibhash)
end
package.libpaths = getlibpaths
package.clibpaths = getclibpaths
local function addpath(what,paths,extras,hash,...)
local pathlist = { ... }
local cleanpath = helpers.cleanpath
local trace = helpers.trace
local report = helpers.report
--
local function add(path)
local path = cleanpath(path)
if not hash[path] then
if trace then
report("extra %s path: %s",what,path)
end
paths [#paths +1] = path
extras[#extras+1] = path
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, extras
end
function package.extralibpath(...)
libpaths, libextras = addpath("lua", getlibpaths(), libextras, libhash,...)
end
function package.extraclibpath(...)
clibpaths, clibextras = addpath("lib",getclibpaths(),clibextras,clibhash,...)
end
-- function package.extralibpath(...)
-- libpaths = getlibpaths()
-- local pathlist = { ... }
-- local cleanpath = helpers.cleanpath
-- local trace = helpers.trace
-- local report = helpers.report
-- --
-- local function add(path)
-- local path = cleanpath(path)
-- if not libhash[path] then
-- if trace then
-- report("extra lua path: %s",path)
-- end
-- libextras[#libextras+1] = path
-- libpaths [#libpaths +1] = path
-- 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
-- end
-- function package.extraclibpath(...)
-- clibpaths = getclibpaths()
-- local pathlist = { ... }
-- local cleanpath = helpers.cleanpath
-- local trace = helpers.trace
-- local report = helpers.report
-- --
-- local function add(path)
-- local path = cleanpath(path)
-- if not clibhash[path] then
-- if trace then
-- report("extra lib path: %s",path)
-- end
-- clibextras[#clibextras+1] = path
-- clibpaths [#clibpaths +1] = path
-- 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
-- end
if not searchers[-2] then
-- use package-path and package-cpath
searchers[-2] = searchers[2]
end
searchers[2] = function(name)
return helpers.loaded(name)
end
searchers[3] = nil -- get rid of the built in one
local function loadedaslib(resolved,rawname)
-- local init = "luaopen_" .. string.match(rawname,".-([^%.]+)$")
local init = "luaopen_"..gsub(rawname,"%.","_")
if helpers.trace then
helpers.report("calling loadlib with '%s' with init '%s'",resolved,init)
end
return package.loadlib(resolved,init)
end
local function loadedbylua(name)
if helpers.trace then
helpers.report("locating '%s' using normal loader",name)
end
return true, searchers[-2](name) -- the original
end
local function loadedbypath(name,rawname,paths,islib,what)
local trace = helpers.trace
local report = helpers.report
if trace then
report("locating '%s' as '%s' on '%s' paths",rawname,name,what)
end
for p=1,#paths do
local path = paths[p]
local resolved = filejoin(path,name)
if trace then -- mode detail
report("checking for '%s' using '%s' path '%s'",name,what,path)
end
if isreadable(resolved) then
if trace then
report("lib '%s' located on '%s'",name,resolved)
end
if islib then
return true, loadedaslib(resolved,rawname)
else
return true, loadfile(resolved)
end
end
end
end
local function notloaded(name)
if helpers.trace then
helpers.report("? unable to locate library '%s'",name)
end
end
helpers.loadedaslib = loadedaslib
helpers.loadedbylua = loadedbylua
helpers.loadedbypath = loadedbypath
helpers.notloaded = notloaded
-- alternatively we could set the package.searchers
function helpers.loaded(name)
local thename = gsub(name,"%.","/")
local luaname = addsuffix(thename,"lua")
local libname = addsuffix(thename,os.libsuffix or "so") -- brrr
local libpaths = getlibpaths()
local clibpaths = getclibpaths()
local done, result = loadedbypath(luaname,name,libpaths,false,"lua")
if done then
return result
end
local done, result = loadedbypath(luaname,name,clibpaths,false,"lua")
if done then
return result
end
local done, result = loadedbypath(libname,name,clibpaths,true,"lib")
if done then
return result
end
local done, result = loadedbylua(name)
if done then
return result
end
return notloaded(name)
end
|