l-lua.lua /size: 6405 b    last modification: 2021-10-28 13:50
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
l-lua
'
]
=
{
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
-- potential issues with 5.3:
10 11
-- i'm not sure yet if the int/float change is good for luatex
12 13
-- math.min
14
-- math.max
15
-- tostring
16
-- tonumber
17
-- utf.*
18
-- bit32
19 20
local
next
,
type
,
tonumber
=
next
,
type
,
tonumber
21 22
-- compatibility hacks and helpers
23 24
LUAMAJORVERSION
,
LUAMINORVERSION
=
string
.
match
(
_VERSION
,
"
^[^%d]+(%d+)%.(%d+).*$
"
)
25 26
LUAMAJORVERSION
=
tonumber
(
LUAMAJORVERSION
)
or
5
27
LUAMINORVERSION
=
tonumber
(
LUAMINORVERSION
)
or
1
28
LUAVERSION
=
LUAMAJORVERSION
+
LUAMINORVERSION
/
10
29 30
if
LUAVERSION
<
5
.
2
and
jit
then
31
--
32
-- we want loadstring cum suis to behave like 5.2
33
--
34
MINORVERSION
=
2
35
LUAVERSION
=
5
.
2
36
end
37 38
-- this is lmtx only:
39 40
-- if lua and lua.openfile then
41
-- io.open = lua.openfile
42
-- end
43 44
-- lpeg
45 46
if
not
lpeg
then
47
lpeg
=
require
(
"
lpeg
"
)
48
end
49 50
-- if utf8 then
51
-- utf8lua = utf8
52
-- utf8 = nil
53
-- end
54 55
-- basics:
56 57
if
loadstring
then
58 59
local
loadnormal
=
load
60 61
function
load
(
first
,
...
)
62
if
type
(
first
)
=
=
"
string
"
then
63
return
loadstring
(
first
,
...
)
64
else
65
return
loadnormal
(
first
,
...
)
66
end
67
end
68 69
else
70 71
loadstring
=
load
72 73
end
74 75
-- table:
76 77
-- At some point it was announced that i[pairs would be dropped, which makes
78
-- sense. As we already used the for loop and # in most places the impact on
79
-- ConTeXt was not that large; the remaining ipairs already have been replaced.
80
-- Hm, actually ipairs was retained, but we no longer use it anyway (nor
81
-- pairs).
82
--
83
-- Just in case, we provide the fallbacks as discussed in Programming
84
-- in Lua (http://www.lua.org/pil/7.3.html):
85 86
if
not
ipairs
then
87 88
-- for k, v in ipairs(t) do ... end
89
-- for k=1,#t do local v = t[k] ... end
90 91
local
function
iterate
(
a
,
i
)
92
i
=
i
+
1
93
local
v
=
a
[
i
]
94
if
v
~
=
nil
then
95
return
i
,
v
--, nil
96
end
97
end
98 99
function
ipairs
(
a
)
100
return
iterate
,
a
,
0
101
end
102 103
end
104 105
if
not
pairs
then
106 107
-- for k, v in pairs(t) do ... end
108
-- for k, v in next, t do ... end
109 110
function
pairs
(
t
)
111
return
next
,
t
-- , nil
112
end
113 114
end
115 116
-- The unpack function has been moved to the table table, and for compatiility
117
-- reasons we provide both now.
118 119
if
not
table
.
unpack
then
120 121
table
.
unpack
=
_G
.
unpack
122 123
elseif
not
unpack
then
124 125
_G
.
unpack
=
table
.
unpack
126 127
end
128 129
-- package:
130 131
-- if not package.seachers then
132
--
133
-- package.searchers = package.loaders -- 5.2
134
--
135
-- elseif not package.loaders then
136
--
137
-- package.loaders = package.searchers
138
--
139
-- end
140 141
if
not
package
.
loaders
then
-- brr, searchers is a special "loadlib function" userdata type
142 143
package
.
loaders
=
package
.
searchers
144 145
end
146 147
-- moved from util-deb to here:
148 149
local
print
,
select
,
tostring
=
print
,
select
,
tostring
150 151
local
inspectors
=
{
}
152 153
function
setinspector
(
kind
,
inspector
)
-- global function
154
inspectors
[
kind
]
=
inspector
155
end
156 157
function
inspect
(
...
)
-- global function
158
for
s
=
1
,
select
(
"
#
"
,
...
)
do
159
local
value
=
select
(
s
,
...
)
160
if
value
=
=
nil
then
161
print
(
"
nil
"
)
162
else
163
local
done
=
false
164
-- type driven (table)
165
local
kind
=
type
(
value
)
166
local
inspector
=
inspectors
[
kind
]
167
if
inspector
then
168
done
=
inspector
(
value
)
169
if
done
then
170
break
171
end
172
end
173
-- whatever driven (token, node, ...)
174
for
kind
,
inspector
in
next
,
inspectors
do
175
done
=
inspector
(
value
)
176
if
done
then
177
break
178
end
179
end
180
if
not
done
then
181
print
(
tostring
(
value
)
)
182
end
183
end
184
end
185
end
186 187
--
188 189
local
dummy
=
function
(
)
end
190 191
function
optionalrequire
(
...
)
192
local
ok
,
result
=
xpcall
(
require
,
dummy
,
...
)
193
if
ok
then
194
return
result
195
end
196
end
197 198
local
flush
=
io
.
flush
199 200
if
flush
then
201 202
local
execute
=
os
.
execute
if
execute
then
function
os
.
execute
(
...
)
flush
(
)
return
execute
(
...
)
end
end
203
local
exec
=
os
.
exec
if
exec
then
function
os
.
exec
(
...
)
flush
(
)
return
exec
(
...
)
end
end
204
local
spawn
=
os
.
spawn
if
spawn
then
function
os
.
spawn
(
...
)
flush
(
)
return
spawn
(
...
)
end
end
205
local
popen
=
io
.
popen
if
popen
then
function
io
.
popen
(
...
)
flush
(
)
return
popen
(
...
)
end
end
206 207
end
208 209
-- new
210 211
FFISUPPORTED
=
type
(
ffi
)
=
=
"
table
"
and
ffi
.
os
~
=
"
"
and
ffi
.
arch
~
=
"
"
and
ffi
.
load
212 213
if
not
FFISUPPORTED
then
214 215
-- Maybe we should check for LUATEXENGINE but that's also a bit tricky as we still
216
-- can have a weird ffi library laying around. Checking for presence of 'jit' is
217
-- also not robust. So for now we hope for the best.
218 219
local
okay
;
okay
,
ffi
=
pcall
(
require
,
"
ffi
"
)
220 221
FFISUPPORTED
=
type
(
ffi
)
=
=
"
table
"
and
ffi
.
os
~
=
"
"
and
ffi
.
arch
~
=
"
"
and
ffi
.
load
222 223
end
224 225
if
not
FFISUPPORTED
then
226
ffi
=
nil
227
elseif
not
ffi
.
number
then
228
ffi
.
number
=
tonumber
229
end
230 231
-- if not bit32 then -- and utf8 then
232
-- -- bit32 = load ( [[ -- replacement code with 5.3 syntax so that 5.2 doesn't bark on it ]] )
233
-- bit32 = require("l-bit32")
234
-- end
235 236
-- We need this due a bug in luatex socket loading:
237 238
-- local loaded = package.loaded
239
--
240
-- if not loaded["socket"] then loaded["socket"] = loaded["socket.core"] end
241
-- if not loaded["mime"] then loaded["mime"] = loaded["mime.core"] end
242
--
243
-- if not socket.mime then socket.mime = package.loaded["mime"] end
244
--
245
-- if not loaded["socket.mime"] then loaded["socket.mime"] = socket.mime end
246
-- if not loaded["socket.http"] then loaded["socket.http"] = socket.http end
247
-- if not loaded["socket.ftp"] then loaded["socket.ftp"] = socket.ftp end
248
-- if not loaded["socket.smtp"] then loaded["socket.smtp"] = socket.smtp end
249
-- if not loaded["socket.tp"] then loaded["socket.tp"] = socket.tp end
250
-- if not loaded["socket.url"] then loaded["socket.url"] = socket.url end
251 252
if
LUAVERSION
>
5
.
3
then
253
-- collectgarbage("collect")
254
-- collectgarbage("generational") -- crashes on unix
255
end
256 257
if
status
and
os
.
setenv
then
258
os
.
setenv
(
"
engine
"
,
string
.
lower
(
status
.
luatex_engine
or
"
unknown
"
)
)
259
end
260