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