summaryrefslogtreecommitdiff
path: root/macros/luatex/generic/minim/minim-alloc.lua
blob: 3b14f5cc80c1266f35dcacbf8bedfa9bc1aa74b8 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346

local M = {}

-- 1 messages

function M.msg(...)
    texio.write_nl(string.format(...))
end

function M.amsg(...)
    texio.write(string.format(...))
end

function M.log(...)
    texio.write_nl('log', string.format(...))
end

function M.alog(...)
    texio.write('log', string.format(...))
end

function M.term(...)
    texio.write_nl('term', string.format(...))
end

function M.err (...)
    tex.error(string.format(...))
end

-- 1 pdf helpers

local function insert_formatted(t, ...)
    table.insert(t, string.format(...))
end

local function hexify(c)
    return string.format('#%02x', c:byte())
end
function M.pdf_name(n)
    return '/'..n
        :gsub('[][(){}<>#%%/\\]', hexify)
        :gsub('%G', hexify)
end

-- re-encode to utf-16
local function pdf_hex_string(text)
    local str = { [1] = '<feff' }
    for _, i in utf8.codes(text) do
        if i <= 0xffff then
            insert_formatted(str, '%04x', i)
        else
            i = i - 0x10000
            local m = math.floor(i/0x400) + 0xd800
            local n = ( i % 0x400 ) + 0xdc00
            insert_formatted(str, '%04x%04x', m, n)
        end
    end
    table.insert(str, '>')
    return table.concat(str,'')
end
local function octify(c)
    return string.format('\\%03o', c:byte())
end
function M.pdf_string(text)
    -- do note that \ddd is decimal in lua, octal in pdf
    if text:match('[^%g\009\010\13 ]') then
        return pdf_hex_string(text)
    else
        return string.format('(%s)', text
            :gsub('[()\\]','\\%0')
            :gsub('%c', octify))
    end
end

-- try and produce a date of the format (D:YYYYMMDD)
function M.pdf_date(text)
    local y, m, d = string.match(text, '^([12][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)$')
    if not y then d, m, y  = string.match(text, '^([0-9][0-9]?)-([0-9][0-9]?)-([12][0-9][0-9][0-9])$') end
    if y then
        return string.format('(D:%4d%02d%02d)', y, m, d)
    else
        return string.format('(D:%s)', text)
    end
end

-- 1 scanning helpers

local function make_scanner(fn)
    return function(this, name, multiple)
        this.scanners[name] = multiple and function()
                this.res[name] = this.res[name] or { }
                table.insert(this.res[name], fn())
            end or function()
                this.scanners[name] = nil
                this.res[name] = fn()
            end
        return this
    end
end

function M.options_scanner()
    return { scanners = { }, res = { },
        int = make_scanner(token.scan_int),
        glue = make_scanner(token.scan_glue),
        dimen = make_scanner(token.scan_dimen),
        string = make_scanner(token.scan_string),
        argument = make_scanner(token.scan_argument),
        word = make_scanner(token.scan_word),
        list = make_scanner(token.scan_list),
        keyword = function(this, name, opposite)
            this.scanners[name] = function()
                this.res[name] = true
                if opposite then this.res[opposite] = false end
            end
            if opposite then
                this.scanners[opposite] = function()
                    this.res[name] = false
                    this.res[opposite] = true
                end
            end
            return this
        end,
        table = function(this, name)
            this.scanners[name] = function()
                this.res[name] = this.res[name] or { }
                this.res[name][token.scan_string()] = token.scan_string()
            end
            return this
        end,
        subtable = function(this, name)
            this.scanners[name] = function()
                this.res[name] = this.res[name] or { }
                local sub = token.scan_string();
                this.res[name][sub] = this.res[name][sub] or { }
                this.res[name][sub][token.scan_string()] = token.scan_string()
            end
            return this
        end,
        scan = function(this, defaults)
            this.res = defaults or this.res
            repeat
                local matched = false
                for name, scanner in pairs(this.scanners) do
                    if token.scan_keyword(name) then
                        matched = true
                        scanner()
                    end
                end
            until not matched
            return this.res
        end
    }
end

--1 saving modules and tables

local tables = package.loaded['minim-saved-tables']
    or { ['minim:modules'] = { } }
local modules = tables ['minim:modules']

function M.remember (name)
    if modules[name] == nil then
        modules[name] = false -- will be a number if a bytecode register is reserved
        modules[#modules+1] = name
    end
end

function M.saved_table (identifier, table)
    if tables[identifier] == nil then
        tables[identifier] = table or { }
    end
    return tables[identifier]
end

-- saved tables may only contain values that can be converted to and from 
-- strings with tostring() or other tables meeting the same requirement.
function M.table_to_text (tbl)
    local r = { }
    for i,t in pairs(tbl) do
        local l
        if type(i) == 'string' then
            l = string.format('[%q] = ', i)
        else
            l = string.format('[%s] = ', i)
        end
        if type(t) == 'table' then
            l = l .. M.table_to_text (t)
        elseif type(t) == 'string' then
            l = l .. string.format ('%q', t)
        else
            l = l .. tostring(t)
        end
        r[#r+1] = l
    end
    return '{ ' .. table.concat (r,', ') .. ' }'
end

local cb = require('minim-callbacks')
M.remember('minim-callbacks')
M.remember('minim-alloc')

-- 1 allocation functions

-- like \unset
M.unset = -0x7FFFFFFF

-- works for both \chardef and the likes of \countdef
function M.registernumber(csname)
    if token.is_defined(csname) then
        return token.create(csname).index
    else
        return -- would return 0 otherwise
    end
end

-- we need remember lua-made allocations in the format file, since otherwise, 
-- a different register will be reserved when the lua code is seen again in the 
-- actual run.
local allocations = M.saved_table('minim lua-side allocations')

local function make_alloc_new(fname, globcount)
    allocations[fname] = allocations[fname] or { }
    M['new_'..fname] = function(id)
        local nr
        if id and allocations[fname][id] then
            nr = allocations[fname][id]
        else
            nr = tex.count[globcount] + 1
            tex.setcount('global', globcount, nr)
            if id then allocations[fname][id] = nr end
            M.log('\\%s%d : %s', fname, nr, id or '<unnamed>')
        end
        return nr
    end
end

make_alloc_new ('attribute'    , 'e@alloc@attribute@count'   )
make_alloc_new ('whatsit'      , 'e@alloc@whatsit@count'     )
make_alloc_new ('luabytecode'  , 'e@alloc@bytecode@count'    )
make_alloc_new ('function'     , 'e@alloc@luafunction@count' )
make_alloc_new ('luachunkname' , 'e@alloc@luachunk@count'    )
make_alloc_new ('catcodetable' , 'e@alloc@ccodetable@count'  )
make_alloc_new ('userrule'     , 'e@alloc@rule@count'        )

-- We need different allocation functions for the older registers, because 
-- etex’s global allocation macros are off-by-one w.r.t. all other.
--
local function make_alloc_old(fname, globcount)
    allocations[fname] = allocations[fname] or { }
    M['new_'..fname] = function (id)
        local nr
        if id and allocations[fname][id] then
            nr = allocations[fname][id]
        else
            nr = tex.count[globcount]
            tex.setcount('global', globcount, nr + 1)
            if id then allocations[fname][id] = nr end
            M.log('\\%s%d : %s', fname, nr, id or '<unnamed>')
        end
        return nr
    end
end

-- existing allocation counters
local globoffset = tex.count['alloc:globoffset']
make_alloc_old ('count',  globoffset + 0 )
make_alloc_old ('dimen',  globoffset + 1 )
make_alloc_old ('skip',   globoffset + 2 )
make_alloc_old ('muskip', globoffset + 3 )
make_alloc_old ('box',    globoffset + 4 )
make_alloc_old ('toks',   globoffset + 5 )
make_alloc_old ('marks',  globoffset + 6 )

function M.luadef (csname, fn, ...)
    local nr = M.new_function(csname)
    lua.get_functions_table()[nr] = fn
    token.set_lua(csname, nr, ...)
end

-- the current file
M.luadef('minim:currentfile', function()
    tex.sprint((status.filename:gsub('^.*/', '')))
end)

-- make pdf_string() available as \pdfstring{...}
M.luadef('pdfstring', function() tex.sprint(M.pdf_string(token.scan_string())) end)

-- uselanguage hook callback
cb.new_callback('uselanguage', 'simple')
M.luadef('minim:uselanguagecallback', function()
    local langname = token.scan_string()
    cb.call_callback('uselanguage', langname)
end)

-- copy of \Ucharcat from xetex
M.luadef('Ucharcat', function()
    local chr, cat = token.scan_int(), token.scan_int()
    token.put_next(token.new(chr, cat))
end)

--1 dumping information to the format file

-- reserve a bytecode register
local saved_tables_bytecode = M.new_luabytecode('saved_tables_bytecode')

-- we cannot use set_lua because lua functions are not included in the format file
token.set_macro('minim:restoremodules', '\\luabytecodecall'..saved_tables_bytecode)

local function dump_saved_tables()
    M.msg('pre_dump: save modules and tables to format file')
    -- save modules
    for _, name in ipairs (modules) do
        if not modules[name] then
            M.msg('saving module '..name)
            -- reserve (if necessary) a bytecode register
            modules[name] = M.new_luabytecode ('module '..name)
            -- store the file into the format file
            lua.bytecode[modules[name]] = loadfile(kpse.find_file(name,'lua'))
        end
    end
    -- save tables (and restore modules)
    local saved_tables = [[

        -- include all saved tables in this bytecode register
        local t = ]]..M.table_to_text(tables)..[[

        -- and make them available via require()
        package.loaded['minim-saved-tables'] = t

        -- restore all remembered modules from their saved bytecode
        local s = t['minim:modules']
        for _, name in ipairs (s) do
            texio.write_nl('log', 'minim: restoring module '..name)
            package.loaded[name] = lua.bytecode[ s[name] ] ()
        end

        ]]
    lua.bytecode[saved_tables_bytecode] = load(saved_tables)
end

cb.register ('pre_dump', dump_saved_tables)

--

return M