mk-initialization.tex /size: 8005 b    last modification: 2023-12-21 09:43
1% language=us
2
3\startcomponent mk-initialization
4
5\environment mk-environment
6
7\chapter{Initialization revised}
8
9Initializing \LUATEX\ in such a way that it does what you want it
10to do your way can be tricky. This has to do with the fact that
11if we want to overload certain features (using callbacks) we need
12to do that before the orginals start doing their work. For
13instance, if we want to install our own file handling, we must
14make sure that the built||in file searching does not get
15initialized. This is particularly important when the built in
16search engine is based on the \KPSE\ library. In that case the
17first serious file access will result in loading the \type {ls-R}
18filename databases, which will take an amount of time more or less
19linear with the size of the \TEX\ trees. Among the reasons why we
20want to replace \KPSE\ are the facts that we want to access \ZIP\
21files, do more specific file searches, use \HTTP, \FTP\ and whatever
22comes around, integrate \CONTEXT\ specific methods, etc.
23
24Although modern operating systems will cache files in memory,
25creating the internal data structures (hashes) from the rather
26dumb files take some time. On the machine where I was developing
27the first experimental \LUATEX\ code, we're talking about 0.3
28seconds for \PDFTEX. One would expect a \LUA\ based alternative to
29be slower, but it is not. This may be due to the different
30implementation, but for sure the more efficient file cache plays a
31role as well. So, by completely disabling \KPSE, we can have more
32advanced \IO\ related features (like reading from \ZIP\ files) at
33about the same speed (or even faster). In due time we will also
34support progname (and format) specific caches, which speeds up
35loading. In case one wonders why we bother about a mere few
36hundreds of milliseconds: imagine frequent runs from an editor or
37sub||runs during a job. In such situation every speed up matters.
38
39So, back to initialization: how do we initialize \LUATEX. The
40method described here is developed for \CONTEXT\ but is not
41limited to this macro package; when one tells \TEXEXEC\ to
42generate formats using the \type {--luatex} directive, it will
43generate the \CONTEXT\ formats as well as \MPTOPDF\ using this
44engine.
45
46For practical reasons, the Lua based \IO\ handler is \KPSE\
47compliant. This means that the normal \type {texmf.cnf} and \type
48{ls-R} files can be used. However, their content is converted in a
49more \LUA\ friendly way. Although this can be done at runtime, it
50makes more sense to to this in advance using \LUATOOLS.  The files
51involved are:
52
53\starttabulate[|l|l|l|l|]
54\NC \bold{input}     \NC \bold{raw input} \NC \bold{runtime input}     \NC \bold{runtime fallback}  \NC \NR
55\NC                  \NC \type{ls-R}      \NC \type{files.luc}         \NC \type{files.lua}         \NC \NR
56\NC \type{texmf.lua} \NC \type{temxf.cnf} \NC \type{configuration.luc} \NC \type{configuration.lua} \NC \NR
57\stoptabulate
58
59In due time \LUATOOLS\ will generate the directory listing itself
60(for this some extra libraries need to be linked in). The
61configuration file(s) eventually will move to a \LUA\ table
62format, and when a \type {texmf.lua} file is present, that one
63will be used.
64
65\starttyping
66luatools --generate
67\stoptyping
68
69This command will generate the relevant databases. Optionally you can
70provide \type {--minimize} which will generate a leaner database, which
71in turn will bring down loading time to (on my machine) about 0.1 sec
72instead of 0.2 seconds. The \type {--sort} option will give nicer
73intermediate (\type {.lua}) files that are more handy for debugging.
74
75When done, you can use \LUATOOLS\ roughly in the same manner as
76\KPSEWHICH, for instance to locate files:
77
78\starttyping
79luatools texnansi-lmr10.tfm
80luatools --all tufte.tex
81\stoptyping
82
83You can also inspect its internal state, for instance with:
84
85\starttyping
86luatools --variables  --pattern=TEXMF
87luatools --expansions --pattern=context
88\stoptyping
89
90This will show you the (expanded) variables from the configuration
91files. Normally you don't need to go that deep into the belly.
92
93The \LUATOOLS\ script can also generate a format and run \LUATEX.
94For \CONTEXT\ this is normally done with the \TEXEXEC\ wrapper,
95for instance:
96
97\starttyping
98texexec --make --all --luatex
99\stoptyping
100
101When dealing with this process we need to keep several things in
102mind:
103
104\startitemize[packed]
105\item \LUATEX\ needs a \LUA\ startup file in both ini and runtime mode
106\item these files may be the same but may also be different
107\item here we use the same files but a compiled one in runtime mode
108\item we cannot yet use a file location mechanism
109\stopitemize
110
111A \type {.luc} file is a precompiled \LUA\ chunk. In order to
112guard consistency between \LUA\ code and tex code, \CONTEXT\ will
113preload all \LUA\ code and store them in the bytecode table
114provided by \LUATEX. How this is done, is another story. Contrary
115to these tables, the initialization code can not be put into the
116format, if only because at that stage we still need to set up
117memory and other parameters.
118
119In our case, especially because we want to overload the \IO\
120handler, we want to store the startup file in the same path as the
121format file. This means that scripts that deal with format
122generation also need to take care of (relocating) the startup
123file. Normally we will use \TEXEXEC\ but we can also use \LUATOOLS.
124
125Say that we want to make a plain format. We can call \LUATOOLS\
126as follows:
127
128\starttyping
129luatools --ini plain
130\stoptyping
131
132This will give us (in the current path):
133
134\starttyping
135120,808 plain.fmt
136  2,650 plain.log
137 80,767 plain.lua
138 64,807 plain.luc
139\stoptyping
140
141From now on, only the \type {plain.fmt} and \type {plain.luc} file
142are important. Processing a file
143
144\starttyping
145test \end
146\stoptyping
147
148can be done with:
149
150\starttyping
151luatools --fmt=./plain.fmt test
152\stoptyping
153
154This returns:
155
156\starttyping
157This is luaTeX, Version 3.141592-0.1-alpha-20061018 (Web2C 7.5.5)
158(./test.tex [1] )
159Output written on test.dvi (1 page, 260 bytes).
160Transcript written on test.log.
161\stoptyping
162
163which looks rather familiar. Keep in mind that at this stage we
164still run good old Plain \TEX. In due time we will provide a few
165files that will making work with \LUA\ more convenient in Plain
166\TEX, but at this moment you can already use for instance \type
167{\directlua}.
168
169In case you wonder how this is related to \CONTEXT, well only to
170the extend that it uses a couple of rather generic \CONTEXT\
171related \LUA\ files.
172
173\CONTEXT\ users can best use \TEXEXEC\ which will relocate the
174format related files to the regular engine path. In \LUATOOLS\
175terms we have two choices:
176
177\starttyping
178luatools --ini cont-en
179luatools --ini --compile cont-en
180\stoptyping
181
182The difference is that in the first case \type {context.lua} is
183used as startup file. This \LUA\ file creates the \type
184{cont-en.luc} runtime file. In the second call \LUATOOLS\ will
185create a \type {cont-en.lua} file and compile that one. An even
186more specific call would be:
187
188\starttyping
189luatools --ini --compile --luafile=blabla.lua          cont-en
190luatools --ini --compile --lualibs=bla-1.lua,bla-2.lua cont-en
191\stoptyping
192
193This call does not make much sense for \CONTEXT. Keep in mind
194that \LUATOOLS\ does not set up user specific configurations, for
195instance the \type {--all} switch in \TEXEXEC\ will set up all
196patterns.
197
198I know that it sounds a bit messy, but till we have a more clear
199picture of where \LUATEX\ is heading this is the way to proceed.
200The average \CONTEXT\ user won't notice those details, because
201\TEXEXEC\ will take care of things.
202
203Currently we follow the \TDS\ and \WEBC\ conventions, but in the
204future we may follow different or additional approaches. This may
205as well be driven by more complex \IO\ models. For the moment
206extensions still fit in. For instance, in order to support access
207to remote resources and related caching, we have added to the
208configuration file the variable:
209
210\starttyping
211TEXMFCACHE = $TMP;$TEMP;$TMPDIR;$HOME;$TEXMFVAR;$VARTEXMF;.
212\stoptyping
213
214\stopcomponent
215