l-lua.lua /size: 6405 b    last modification: 2021-10-28 13:50
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-- 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
20local next, type, tonumber = next, type, tonumber
21
22-- compatibility hacks and helpers
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    -- we want loadstring cum suis to behave like 5.2
33    --
34    MINORVERSION = 2
35    LUAVERSION   = 5.2
36end
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
46if not lpeg then
47    lpeg = require("lpeg")
48end
49
50-- if utf8 then
51--     utf8lua = utf8
52--     utf8    = nil
53-- end
54
55-- basics:
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-- 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
86if 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
103end
104
105if 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
114end
115
116-- The unpack function has been moved to the table table, and for compatiility
117-- reasons we provide both now.
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-- 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
141if not package.loaders then -- brr, searchers is a special "loadlib function" userdata type
142
143    package.loaders = package.searchers
144
145end
146
147-- moved from util-deb to here:
148
149local print, select, tostring = print, select, tostring
150
151local inspectors = { }
152
153function setinspector(kind,inspector) -- global function
154    inspectors[kind] = inspector
155end
156
157function 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
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-- new
210
211FFISUPPORTED = type(ffi) == "table" and ffi.os ~= "" and ffi.arch ~= "" and ffi.load
212
213if 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
223end
224
225if not FFISUPPORTED then
226    ffi = nil
227elseif not ffi.number then
228    ffi.number = tonumber
229end
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
252if LUAVERSION > 5.3 then
253 -- collectgarbage("collect")
254 -- collectgarbage("generational") -- crashes on unix
255end
256
257if status and os.setenv then
258    os.setenv("engine",string.lower(status.luatex_engine or "unknown"))
259end
260