summaryrefslogtreecommitdiff
path: root/tex/context/base/mkiv/luat-cnf.lua
blob: f61ec43d2f1b04d12ffc2095fc48415ea11566f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
if not modules then modules = { } end modules ['luat-cnf'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

local type, next, tostring, tonumber =  type, next, tostring, tonumber
local format, concat, find, lower, gsub = string.format, table.concat, string.find, string.lower, string.gsub

local report = logs.reporter("system")

local allocate = utilities.storage.allocate

texconfig.kpse_init    = false
texconfig.shell_escape = 't'

luatex       = luatex or { }
local luatex = luatex

texconfig.error_line      =      250
texconfig.expand_depth    =    10000
texconfig.half_error_line =      125
texconfig.max_print_line  =   100000
texconfig.max_strings     =   500000
texconfig.hash_extra      =   250000
texconfig.function_size   =    32768
texconfig.properties_size =    10000

if CONTEXTLMTXMODE > 0 then

texconfig.max_in_open     =     2000
texconfig.nest_size       =    10000
texconfig.param_size      =   100000
texconfig.save_size       =   500000
texconfig.stack_size      =   100000
texconfig.buffer_size     = 10000000
texconfig.token_size      = 10000000
texconfig.node_size       = 50000000
texconfig.max_pool        = 10000000

else

texconfig.max_in_open     =     1000
texconfig.nest_size       =     1000
texconfig.param_size      =    25000
texconfig.save_size       =   100000
texconfig.stack_size      =    10000
texconfig.buf_size        = 10000000
texconfig.fix_mem_init    =  1000000

end

local variablenames = CONTEXTLMTXMODE > 0 and {
    error_line      = false,
    half_error_line = false,
    max_print_line  = false,
    max_in_open     = false,
    expand_depth    = true,
    hash_extra      = true,
    nest_size       = true,
    max_strings     = true,
    max_pool        = true,
    param_size      = true,
    save_size       = true,
    stack_size      = true,
    function_size   = true,
    properties_size = true,
    token_size      = true,
    node_size       = true,
    buffer_size     = true,
} or {
    error_line      = false,
    half_error_line = false,
    max_print_line  = false,
    max_in_open     = false,
    expand_depth    = true,
    hash_extra      = true,
    nest_size       = true,
    max_strings     = true,
    param_size      = true,
    save_size       = true,
    stack_size      = true,
    function_size   = true,
    properties_size = true,
    fix_mem_init    = true,
}

local stub = [[

-- checking

storage = storage or { }
luatex  = luatex  or { }

-- we provide our own file handling

texconfig.kpse_init    = false
texconfig.shell_escape = 't'
---------.start_time   = tonumber(os.getenv("SOURCE_DATE_EPOCH")) -- not used in context

-- as soon as possible

luatex.starttime = os.gettimeofday()

-- this will happen after the format is loaded

function texconfig.init()

    -- development

    local builtin, globals = { }, { }

    libraries = { -- we set it here as we want libraries also 'indexed'
        basiclua = {
            -- always
            "string", "table", "coroutine", "debug", "file", "io", "lpeg", "math", "os", "package",
            -- bonus
            "bit32", "utf8",
        },
        basictex = {
            -- always
            "callback", "font", "lua", "node", "status", "tex", "texconfig", "texio", "token",
            -- not in luametatex
            "img", "pdf", "lang",
            -- in luametatex
            "language",
        },
        extralua = {
            -- not in luametatex
            "unicode", "utf", "gzip",  "zip", "zlib",
            -- in luametatex
            "xzip", "xmath", "xcomplex", "xdecimal", "basexx",
            -- maybe some day in luametatex
            "lz4", "lzo",
            -- always (mime can go)
            "lfs","socket", "mime", "md5", "sha2", "fio", "sio",
        },
        extratex = {
            -- not in luametatex
            "kpse",
            -- always
            "pdfe", "mplib",
            -- in luametatex
            "pdfdecode", "pngdecode",
        },
        obsolete = {
            "epdf",
            "fontloader", -- can be filled by luat-log
            "kpse",
        },
        functions = {
            "assert", "pcall", "xpcall", "error", "collectgarbage",
            "dofile", "load","loadfile", "require", "module",
            "getmetatable", "setmetatable",
            "ipairs", "pairs", "rawequal", "rawget", "rawset", "next",
            "tonumber", "tostring",
            "type", "unpack", "select", "print",
        },
        builtin = builtin, -- to be filled
        globals = globals, -- to be filled
    }

    for k, v in next, _G do
        globals[k] = tostring(v)
    end

    local function collect(t,fnc)
        local lib = { }
        for k, v in next, t do
            if fnc then
                lib[v] = _G[v]
            else
                local keys = { }
                local gv = _G[v]
                local tv = type(gv)
                if tv == "table" then
                    for k, v in next, gv do
                        keys[k] = tostring(v) -- true -- by tostring we cannot call overloads functions (security)
                    end
                end
                lib[v] = keys
                builtin[v] = keys
            end
        end
        return lib
    end

    libraries.basiclua  = collect(libraries.basiclua)
    libraries.basictex  = collect(libraries.basictex)
    libraries.extralua  = collect(libraries.extralua)
    libraries.extratex  = collect(libraries.extratex)
    libraries.functions = collect(libraries.functions,true)
    libraries.obsolete  = collect(libraries.obsolete)

    -- shortcut and helper

    local setbytecode  = lua.setbytecode
    local getbytecode  = lua.getbytecode
    local callbytecode = lua.callbytecode or function(i)
        local b = getbytecode(i)
        if type(b) == "function" then
            b()
            return true
        else
            return false
        end
    end

    local function init(start)
        local i = start
        local t = os.clock()
        while true do
         -- local b = callbytecode(i)
            local e, b = pcall(callbytecode,i)
            if not e then
                print(string.format("\nfatal error : unable to load bytecode register %%i, maybe wipe the cache first\n",i))
                os.exit()
            end
            if b then
                setbytecode(i,nil) ;
                i = i + 1
            else
                break
            end
        end
        return i - start, os.clock() - t
    end

    -- the stored tables and modules

    storage.noftables , storage.toftables  = init(0)
    storage.nofmodules, storage.tofmodules = init(%s)

    if modules then
        local loaded = package.loaded
        for module, _ in next, modules do
            loaded[module] = true
        end
    end

    texconfig.init = function() end

end

CONTEXTLMTXMODE = %s

-- we provide a qualified path

callback.register('find_format_file',function(name)
    texconfig.formatname = name
    return name
end)

-- done, from now on input and callbacks are internal
]]

local function makestub()
    name = name or (environment.jobname .. ".lui")
    report("creating stub file %a using directives:",name)
    report()
    firsttable = firsttable or lua.firstbytecode
    local t = {
        "-- this file is generated, don't change it\n",
        "-- configuration (can be overloaded later)\n"
    }
    for v, permitted in table.sortedhash(variablenames) do
        local d = "luatex." .. gsub(lower(v),"[^%a]","")
        local dv = directives.value(d)
        local tv = texconfig[v]
        if dv then
            if not tv then
                report("  %s = %s (%s)",d,dv,"configured")
                tv = dv
            elseif not permitted then
                report("  %s = %s (%s)",d,tv,"frozen")
            elseif tonumber(dv) >= tonumber(tv) then
                report("  %s = %s (%s)",d,dv,"overloaded")
                tv = dv
            else
                report("  %s = %s (%s)",d,tv,"preset kept")
            end
        elseif tv then
            report("  %s = %s (%s)",d,tv,permitted and "preset" or "frozen")
        else
            report("  %s = <unset>",d)
        end
        if tv then
            t[#t+1] = format("texconfig.%s=%s",v,tv)
        end
    end
    t[#t+1] = ""
    t[#t+1] = format(stub,firsttable,tostring(CONTEXTLMTXMODE or 0))
    io.savedata(name,concat(t,"\n"))
    logs.newline()
end

lua.registerfinalizer(makestub,"create stub file")