luat-cnf.lua /size: 7737 b    last modification: 2021-10-28 13:50
1if not modules then modules = { } end modules ['luat-cnf'] = {
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
9local type, next, tostring, tonumber =  type, next, tostring, tonumber
10local format, concat, find, lower, gsub = string.format, table.concat, string.find, string.lower, string.gsub
11
12local report = logs.reporter("system")
13
14local allocate = utilities.storage.allocate
15
16texconfig.kpse_init    = false
17texconfig.shell_escape = 't'
18
19luatex       = luatex or { }
20local luatex = luatex
21
22texconfig.error_line      =      250
23texconfig.expand_depth    =    10000
24texconfig.half_error_line =      125
25texconfig.max_print_line  =   100000
26texconfig.max_strings     =   500000
27texconfig.hash_extra      =   250000
28texconfig.function_size   =    32768
29texconfig.properties_size =    10000
30texconfig.max_in_open     =     1000
31texconfig.nest_size       =     1000
32texconfig.param_size      =    25000
33texconfig.save_size       =   100000
34texconfig.stack_size      =    10000
35texconfig.buf_size        = 10000000
36texconfig.fix_mem_init    =  1000000
37
38local variablenames = {
39    error_line      = false,
40    half_error_line = false,
41    max_print_line  = false,
42    max_in_open     = false,
43    expand_depth    = true,
44    hash_extra      = true,
45    nest_size       = true,
46    max_strings     = true,
47    param_size      = true,
48    save_size       = true,
49    stack_size      = true,
50    function_size   = true,
51    properties_size = true,
52    fix_mem_init    = true,
53}
54
55local stub = [[
56
57-- checking
58
59storage = storage or { }
60luatex  = luatex  or { }
61
62-- we provide our own file handling
63
64texconfig.kpse_init    = false
65texconfig.shell_escape = 't'
66---------.start_time   = tonumber(os.getenv("SOURCE_DATE_EPOCH")) -- not used in context
67
68-- as soon as possible
69
70luatex.starttime = os.gettimeofday()
71
72-- this will happen after the format is loaded
73
74function texconfig.init()
75
76    -- development
77
78    local builtin, globals = { }, { }
79
80    libraries = { -- we set it here as we want libraries also 'indexed'
81        basiclua = {
82            -- always
83            "string", "table", "coroutine", "debug", "file", "io", "lpeg", "math", "os", "package",
84            -- bonus
85            "bit32", "utf8",
86        },
87        basictex = {
88            -- always
89            "callback", "font", "lua", "node", "status", "tex", "texconfig", "texio", "token",
90            -- not in luametatex
91            "img", "pdf", "lang",
92            -- in luametatex
93            "language",
94        },
95        extralua = {
96            -- not in luametatex
97            "unicode", "utf", "gzip",  "zip", "zlib",
98            -- in luametatex
99            "xzip", "xmath", "xcomplex", "xdecimal", "basexx",
100            -- maybe some day in luametatex
101            "lz4", "lzo",
102            -- always (mime can go)
103            "lfs","socket", "mime", "md5", "sha2", "fio", "sio",
104        },
105        extratex = {
106            -- not in luametatex
107            "kpse",
108            -- always
109            "pdfe", "mplib",
110            -- in luametatex
111            "pdfdecode", "pngdecode",
112        },
113        obsolete = {
114            "epdf",
115            "fontloader", -- can be filled by luat-log
116            "kpse",
117        },
118        functions = {
119            "assert", "pcall", "xpcall", "error", "collectgarbage",
120            "dofile", "load","loadfile", "require", "module",
121            "getmetatable", "setmetatable",
122            "ipairs", "pairs", "rawequal", "rawget", "rawset", "next",
123            "tonumber", "tostring",
124            "type", "unpack", "select", "print",
125        },
126        builtin = builtin, -- to be filled
127        globals = globals, -- to be filled
128    }
129
130    for k, v in next, _G do
131        globals[k] = tostring(v)
132    end
133
134    local function collect(t,fnc)
135        local lib = { }
136        for k, v in next, t do
137            if fnc then
138                lib[v] = _G[v]
139            else
140                local keys = { }
141                local gv = _G[v]
142                local tv = type(gv)
143                if tv == "table" then
144                    for k, v in next, gv do
145                        keys[k] = tostring(v) -- true -- by tostring we cannot call overloads functions (security)
146                    end
147                end
148                lib[v] = keys
149                builtin[v] = keys
150            end
151        end
152        return lib
153    end
154
155    libraries.basiclua  = collect(libraries.basiclua)
156    libraries.basictex  = collect(libraries.basictex)
157    libraries.extralua  = collect(libraries.extralua)
158    libraries.extratex  = collect(libraries.extratex)
159    libraries.functions = collect(libraries.functions,true)
160    libraries.obsolete  = collect(libraries.obsolete)
161
162    -- shortcut and helper
163
164    local setbytecode  = lua.setbytecode
165    local getbytecode  = lua.getbytecode
166    local callbytecode = lua.callbytecode or function(i)
167        local b = getbytecode(i)
168        if type(b) == "function" then
169            b()
170            return true
171        else
172            return false
173        end
174    end
175
176    local function init(start)
177        local i = start
178        local t = os.clock()
179        while true do
180         -- local b = callbytecode(i)
181            local e, b = pcall(callbytecode,i)
182            if not e then
183                print(string.format("fatal error : unable to load bytecode register %%i, maybe wipe the cache first\n",i))
184                os.exit()
185            end
186            if b then
187                setbytecode(i,nil) ;
188                i = i + 1
189            else
190                break
191            end
192        end
193        return i - start, os.clock() - t
194    end
195
196    -- the stored tables and modules
197
198    storage.noftables , storage.toftables  = init(0)
199    storage.nofmodules, storage.tofmodules = init(%s)
200
201    if modules then
202        local loaded = package.loaded
203        for module, _ in next, modules do
204            loaded[module] = true
205        end
206    end
207
208    texconfig.init = function() end
209
210end
211
212CONTEXTLMTXMODE = 0
213
214-- we provide a qualified path
215
216callback.register('find_format_file',function(name)
217    texconfig.formatname = name
218    return name
219end)
220
221-- done, from now on input and callbacks are internal
222]]
223
224local function makestub()
225    name = name or (environment.jobname .. ".lui")
226    report("creating stub file %a using directives:",name)
227    report()
228    firsttable = firsttable or lua.firstbytecode
229    local t = {
230        "-- this file is generated, don't change it\n",
231        "-- configuration (can be overloaded later)\n"
232    }
233    for v, permitted in table.sortedhash(variablenames) do
234        local d = "luatex." .. gsub(lower(v),"[^%a]","")
235        local dv = directives.value(d)
236        local tv = texconfig[v]
237        if dv then
238            if not tv then
239                report("  %s = %s (%s)",d,dv,"configured")
240                tv = dv
241            elseif not permitted then
242                report("  %s = %s (%s)",d,tv,"frozen")
243            elseif tonumber(dv) >= tonumber(tv) then
244                report("  %s = %s (%s)",d,dv,"overloaded")
245                tv = dv
246            else
247                report("  %s = %s (%s)",d,tv,"preset kept")
248            end
249        elseif tv then
250            report("  %s = %s (%s)",d,tv,permitted and "preset" or "frozen")
251        else
252            report("  %s = <unset>",d)
253        end
254        if tv then
255            t[#t+1] = format("texconfig.%s=%s",v,tv)
256        end
257    end
258    t[#t+1] = ""
259    t[#t+1] = format(stub,firsttable)
260    io.savedata(name,concat(t,"\n"))
261    logs.newline()
262end
263
264lua.registerinitexfinalizer(makestub,"create stub file")
265