Debian Codegraph @

texmf-dist/tex/luatex/lua-uni-algos/lua-uni-data-preload.lua

raw
1 -- lua-uni-data-preload.lua
2 -- Copyright 2020--2025 Marcel Krüger
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3 or later is part of all distributions of LaTeX
10 -- version 2005/12/01 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Marcel Krüger
15
16 -- This is mainly an internal module used in lua-uni-data to efficiently store
17 -- the data entries.
18 --
19 --
20 local lookup_table = require'lua-uni-stage-tables'
21 local serialize = lookup_table.serialize
22 local deserialize = lookup_table.deserialize
23
24 -- Serialize a Lua value into a string.
25 -- Not designed to be human readable but only to be reloaded into Lua with load().
26 local function serialize_lua(value)
27 local t = type(value)
28 local fmt
29 if t == 'number' then
30 fmt = math.type(value) == 'integer' and '%#X' or '%A'
31 elseif t == 'string' then
32 fmt = '%q'
33 elseif t == 'boolean' or t == 'nil' then
34 fmt = '%s'
35 elseif t == 'table' then
36 local k, v
37 local entries, length = {}, 0
38 while true do
39 local last_key = k or 0
40 k, v = next(value, k)
41 if math.type(k) == 'integer' and k > last_key and k - last_key < 5 then
42 for i = last_key+1, k-1 do
43 length = length + 1
44 entries[length] = 'nil'
45 end
46 length = length + 1
47 entries[length] = serialize_lua(v)
48 else
49 break
50 end
51 end
52 while k ~= nil do
53 length = length + 1
54 entries[length] = string.format('[%s] = %s', serialize_lua(k), serialize_lua(v))
55 k, v = next(value, k)
56 end
57 fmt, value = '{%s}', table.concat(entries, ', ', 1, length)
58 elseif t == 'function' or t == 'thread' or t == 'userdata' then
59 error"Unsupported type in deserialize"
60 end
61 return string.format(fmt, value)
62 end
63
64 return {
65 generate_bytecode = function()
66 local data = require'lua-uni-data'
67
68 local tables = {}
69 for k, v in next, data.tables do
70 tables[#tables + 1] = string.format("[%q] = deserialize %q,", k, serialize(v))
71 end
72 return assert(load(string.format("\z
73 package.preload['lua-uni-data'] = function() \z
74 local deserialize = require'lua-uni-stage-tables'.deserialize \z
75 return { \z
76 tables = { %s }, \z
77 misc = %s, \z
78 }\z
79 end\z
80 ", table.concat(tables), serialize_lua(data.misc, 'misc')), 'preloaded_unicode_data', 't'))
81 end,
82 }