l-lua.lmt /size: 2522 b    last modification: 2025-02-21 11:03
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-- compatibility hacks and helpers
10
11LUAMAJORVERSION = status.lua_version_major
12LUAMINORVERSION = status.lua_version_minor
13LUAVERSION      = status.lua_version
14LUAFORMAT       = status.lua_format
15
16if not loadstring      then loadstring      = load              end
17if not table.unpack    then table.unpack    = _G.unpack         end
18if not unpack          then _G.unpack       = table.unpack      end
19if not package.loaders then package.loaders = package.searchers end -- brr, searchers is a special "loadlib function" userdata type
20
21do
22
23    local print, select, tostring, type, next = print, select, tostring, type, next
24
25    local inspectors = { }
26
27    function setinspector(kind,inspector) -- global function
28        inspectors[kind] = inspector
29    end
30
31    function inspect(...) -- global function
32        for s=1,select("#",...) do
33            local value = select(s,...)
34            if value == nil then
35                print("nil")
36            else
37                local done  = false
38                -- type driven (table)
39                local kind      = type(value)
40                local inspector = inspectors[kind]
41                if inspector then
42                    done = inspector(value)
43                    if done then
44                        break
45                    end
46                end
47                -- whatever driven (token, node, ...)
48                for kind, inspector in next, inspectors do
49                    done = inspector(value)
50                    if done then
51                        break
52                    end
53                end
54                if not done then
55                    print(tostring(value))
56                end
57            end
58        end
59    end
60
61end
62
63do
64
65    local xpcall = xpcall
66
67    local dummy = function() end
68
69    function optionalrequire(...)
70        local ok, result = xpcall(require,dummy,...)
71        if ok then
72            return result
73        end
74    end
75
76end
77
78do
79
80    local flush   = io.flush
81    local execute = os.execute
82    local popen   = io.popen
83
84    function os.execute(...) flush() return execute(...) end
85    function io.popen  (...) flush() return popen  (...) end
86
87end
88
89os.setenv("engine",string.lower(status.luatex_engine or "unknown"))
90