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
30if LUAVERSION < 5.2 and jit then
31
32
33
34 MINORVERSION = 2
35 LUAVERSION = 5.2
36end
37
38
39
40
41
42
43
44
45
46if not lpeg then
47 lpeg = require("lpeg")
48end
49
50
51
52
53
54
55
56
57if 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
69else
70
71 loadstring = load
72
73end
74
75
76
77
78
79
80
81
82
83
84
85
86if not ipairs then
87
88
89
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
96 end
97 end
98
99 function ipairs(a)
100 return iterate, a, 0
101 end
102
103end
104
105if not pairs then
106
107
108
109
110 function pairs(t)
111 return next, t
112 end
113
114end
115
116
117
118
119if not table.unpack then
120
121 table.unpack = _G.unpack
122
123elseif not unpack then
124
125 _G.unpack = table.unpack
126
127end
128
129
130
131
132
133
134
135
136
137
138
139
140
141if not package.loaders then
142
143 package.loaders = package.searchers
144
145end
146
147
148
149local print, select, tostring = print, select, tostring
150
151local inspectors = { }
152
153function setinspector(kind,inspector)
154 inspectors[kind] = inspector
155end
156
157function inspect(...)
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
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
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
185end
186
187
188
189local dummy = function() end
190
191function optionalrequire(...)
192 local ok, result = xpcall(require,dummy,...)
193 if ok then
194 return result
195 end
196end
197
198local flush = io.flush
199
200if 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
207end
208
209
210
211FFISUPPORTED = type(ffi) == "table" and ffi.os ~= "" and ffi.arch ~= "" and ffi.load
212
213if not FFISUPPORTED then
214
215
216
217
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
223end
224
225if not FFISUPPORTED then
226 ffi = nil
227elseif not ffi.number then
228 ffi.number = tonumber
229end
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252if LUAVERSION > 5.3 then
253
254
255end
256
257if status and os.setenv then
258 os.setenv("engine",string.lower(status.luatex_engine or "unknown"))
259end
260 |