1if 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
10
11
12
13
14
15
16
17
18
19
20local next, type, tonumber = next, type, tonumber
21
22
23
24LUAMAJORVERSION, LUAMINORVERSION = string.match(_VERSION,"^[^%d]+(%d+)%.(%d+).*$")
25
26LUAMAJORVERSION = tonumber(LUAMAJORVERSION) or 5
27LUAMINORVERSION = tonumber(LUAMINORVERSION) or 1
28LUAVERSION = LUAMAJORVERSION + LUAMINORVERSION/10
29
30LUAFORMAT = status and status.lua_format or 0
31
32if LUAVERSION < 5.2 and jit then
33
34
35
36 MINORVERSION = 2
37 LUAVERSION = 5.2
38end
39
40
41
42
43
44
45
46
47
48if not lpeg then
49 lpeg = require("lpeg")
50end
51
52
53
54
55
56
57
58
59if loadstring then
60
61 local loadnormal = load
62
63 function load(first,...)
64 if type(first) == "string" then
65 return loadstring(first,...)
66 else
67 return loadnormal(first,...)
68 end
69 end
70
71else
72
73 loadstring = load
74
75end
76
77
78
79
80
81
82
83
84
85
86
87
88if not ipairs then
89
90
91
92
93 local function iterate(a,i)
94 i = i + 1
95 local v = a[i]
96 if v ~= nil then
97 return i, v
98 end
99 end
100
101 function ipairs(a)
102 return iterate, a, 0
103 end
104
105end
106
107if not pairs then
108
109
110
111
112 function pairs(t)
113 return next, t
114 end
115
116end
117
118
119
120
121if not table.unpack then
122
123 table.unpack = _G.unpack
124
125elseif not unpack then
126
127 _G.unpack = table.unpack
128
129end
130
131
132
133
134
135
136
137
138
139
140
141
142
143if not package.loaders then
144
145 package.loaders = package.searchers
146
147end
148
149
150
151local print, select, tostring = print, select, tostring
152
153local inspectors = { }
154
155function setinspector(kind,inspector)
156 inspectors[kind] = inspector
157end
158
159function inspect(...)
160 for s=1,select("#",...) do
161 local value = select(s,...)
162 if value == nil then
163 print("nil")
164 else
165 local done = false
166
167 local kind = type(value)
168 local inspector = inspectors[kind]
169 if inspector then
170 done = inspector(value)
171 if done then
172 break
173 end
174 end
175
176 for kind, inspector in next, inspectors do
177 done = inspector(value)
178 if done then
179 break
180 end
181 end
182 if not done then
183 print(tostring(value))
184 end
185 end
186 end
187end
188
189
190
191local dummy = function() end
192
193function optionalrequire(...)
194 local ok, result = xpcall(require,dummy,...)
195 if ok then
196 return result
197 end
198end
199
200local flush = io.flush
201
202if flush then
203
204 local execute = os.execute if execute then function os.execute(...) flush() return execute(...) end end
205 local exec = os.exec if exec then function os.exec (...) flush() return exec (...) end end
206 local spawn = os.spawn if spawn then function os.spawn (...) flush() return spawn (...) end end
207 local popen = io.popen if popen then function io.popen (...) flush() return popen (...) end end
208
209end
210
211
212
213FFISUPPORTED = type(ffi) == "table" and ffi.os ~= "" and ffi.arch ~= "" and ffi.load
214
215if not FFISUPPORTED then
216
217
218
219
220
221 local okay ; okay, ffi = pcall(require,"ffi")
222
223 FFISUPPORTED = type(ffi) == "table" and ffi.os ~= "" and ffi.arch ~= "" and ffi.load
224
225end
226
227if not FFISUPPORTED then
228 ffi = nil
229elseif not ffi.number then
230 ffi.number = tonumber
231end
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254if LUAVERSION > 5.3 then
255
256
257end
258
259if status and os.setenv then
260 os.setenv("engine",string.lower(status.luatex_engine or "unknown"))
261end
262 |