Debian Codegraph @

texmf-dist/doc/luametatex/base/luametatex-lua.tex

raw
1 % language=us runpath=texruns:manuals/luametatex
2
3 \environment luametatex-style
4
5 \startdocument[title=\LUA]
6
7 \startsection[title={Introduction}]
8
9 In this chapter aspects of the \LUA\ interfaces will be discusses. The \type
10 {lua} module described here is rather low level and probably not of much interest
11 to the average user as its functions are meant to be used in higher level
12 interfaces.
13
14 \stopsection
15
16 \startsection[title={Initialization}]
17
18 \startsubsection[title={A bare bone engine}]
19
20 When the \LUAMETATEX\ program launches it will not do much useful. You can
21 compare it to computer hardware without (high level) operating system with a
22 \TEX\ kernel being the bios. It can interpret \TEX\ code but for typesetting you
23 need a reasonable setup. You also need to load fonts, and for output you need a
24 backend, and both can be implemented in \LUA. If you don't like that and want to
25 get up and running immediately, you will be more happy with \LUATEX, \PDFTEX\ or
26 \XETEX, combined with your favorite macro package.
27
28 If you just want to play around you can install the \CONTEXT\ distribution which
29 (including manuals and some fonts) is tiny compared to a full \TEXLIVE\
30 installation and can be run alongside it without problems. If there are issues
31 you can go to the usual \CONTEXT\ support platforms and seek help where you can
32 find the people who made \LUATEX\ and \LUAMETATEX.
33
34 If you use the engine as \TEX\ interpreter you need to set up a few characters.
35 Of course one can wonder why this is the case, but let's consider this to be
36 educational of nature as it right from the start forces one to wonder what
37 category codes are.
38
39 \starttyping
40 \catcode`\{=1 \catcode`\}=2 \catcode`\#=6
41 \stoptyping
42
43 After that you can start defining macros. Contrary to \LUATEX\ the \LUAMETATEX\
44 engine initializes all the primitives but it will quit when the minimal set of
45 callback is not initialized, like a logger. The lack of font loader and backend
46 makes that it is not usable for loading an arbitrary macro package that doesn't
47 set up these components. There is simply no argument for starting in in original
48 \TEX\ mode without \ETEX\ extensions and such.
49
50 \stopsubsection
51
52 \startsubsection[title={\LUAMETATEX\ as a \LUA\ interpreter}]
53
54 Although \LUAMETATEX\ is primarily meant as a \TEX\ engine, it can also serve as
55 a stand alone \LUA\ interpreter and there are two ways to make \LUAMETATEX\
56 behave like one. The first method uses the command line option \type {--luaonly}
57 followed by a filename. The second is more automatic: if the only non|-|option
58 argument (file) on the command line has the extension \type {lmt} or \type {lua}.
59 The \type {luc} extension has been dropped because bytecode compiled files are
60 not portable and one can always load them indirectly. The \type {lmt} suffix is
61 more \CONTEXT\ specific and makes it possible to have files for \LUATEX\ and
62 \LUAMETATEX\ alongside.
63
64 In interpreter mode, the program will set \LUA's \type {arg[0]} to the found
65 script name, pushing preceding options in negative values and the rest of the
66 command line in the positive values, just like the \LUA\ interpreter does. The
67 program will exit immediately after executing the specified \LUA\ script and is
68 thereby effectively just a somewhat bulky stand alone \LUA\ interpreter with a
69 bunch of extra preloaded libraries. But we still wanted and managed to keep the
70 binary small, somewhere around 3MB, which is okay for a script engine.
71
72 When no argument is given, \LUAMETATEX\ will look for a \LUA\ file with the same
73 name as the binary and run that one when present. This makes it possible to use
74 the engine as a stub. For instance, in \CONTEXT\ a symlink from \type {mtxrun} to
75 type {luametatex} will run the \type {mtxrun.lmt} or \type {mtxrun.lua} script
76 when present in the same path as the binary itself. As mentioned before first
77 checking for (\CONTEXT) \type {lmt} files permits different files for different
78 engines in the same path.
79
80 \stopsubsection
81
82 \startsubsection[title={Other commandline processing}]
83
84 When the \LUAMETATEX\ executable starts, it looks for the \type {--lua} command
85 line option. If there is no such option, the command line is interpreted in a
86 similar fashion as the other \TEX\ engines. All options are accepted but only
87 some are understood by \LUAMETATEX\ itself:
88
89 \starttabulate[|l|p|]
90 \FL
91 \BC commandline argument \BC explanation \NC \NR
92 \TL
93 \NC \type{--credits} \NC display credits and exit \NC \NR
94 \NC \type{--fmt=FORMAT} \NC load the format file \type {FORMAT} \NC\NR
95 \NC \type{--help} \NC display help and exit \NC\NR
96 \NC \type{--ini} \NC be \type {iniluatex}, for dumping formats \NC\NR
97 \NC \type{--jobname=STRING} \NC set the job name to \type {STRING} \NC \NR
98 \NC \type{--lua=FILE} \NC load and execute a \LUA\ initialization script \NC\NR
99 \NC \type{--version} \NC display version and exit \NC \NR
100 \NC \type{--permitloadlib} \NC permits loading of external libraries \NC \NR
101 \LL
102 \stoptabulate
103
104 There are less options than with \LUATEX, because one has to deal with them in
105 \LUA\ anyway. So for instance there are no options to enter a safer mode or
106 control executing programs because this can easily be achieved with a startup
107 \LUA\ script, which can interpret whatever options got passed.
108
109 Next the initialization script is loaded and executed. From within the script,
110 the entire command line is available in the \LUA\ table \type {arg}, beginning
111 with \type {arg[0]}, containing the name of the executable. As consequence
112 warnings about unrecognized options are suppressed. Command line processing
113 happens very early on. So early, in fact, that none of \TEX's initializations
114 have taken place yet. The \LUA\ libraries that don't deal with \TEX\ are
115 initialized rather soon so you have these available.
116
117 \LUAMETATEX\ allows some of the command line options to be overridden by reading
118 values from the \type {texconfig} table at the end of script execution (see the
119 description of the \type {texconfig} table later on in this document for more
120 details on which ones exactly). The value to use for \type {\jobname} is decided
121 as follows:
122
123 \startitemize
124 \startitem
125 If \type {--jobname} is given on the command line, its argument will be the
126 value for \type {\jobname}, without any changes. The argument will not be
127 used for actual input so it need not exist. The \type {--jobname} switch only
128 controls the \type {\jobname} setting.
129 \stopitem
130 \startitem
131 Otherwise, \type {\jobname} will be the name of the first file that is read
132 from the file system, with any path components and the last extension (the
133 part following the last \type {.}) stripped off.
134 \stopitem
135 \startitem
136 There is an exception to the previous point: if the command line goes into
137 interactive mode (by starting with a command) and there are no files input
138 via \type {\everyjob} either, then the \type {\jobname} is set to \type
139 {texput} as a last resort.
140 \stopitem
141 \stopitemize
142
143 So let's summarize this. The handling of what is called job name is a bit complex.
144 There can be explicit names set on the command line but when not set they can be
145 taken from the \type {texconfig} table.
146
147 \starttabulate[|l|T|T|T|]
148 \NC startup filename \NC --lua \NC a \LUA\ file \NC \NC \NR
149 \NC startup jobname \NC --jobname \NC a \TEX\ tex \NC texconfig.jobname \NC \NR
150 \NC startup dumpname \NC --fmt \NC a format file \NC texconfig.formatname \NC \NR
151 \stoptabulate
152
153 These names are initialized according to \type {--luaonly} or the first filename
154 seen in the list of options. Special treatment of \type {&} and \type {*} as well
155 as interactive startup is gone but we still enter \TEX\ via an forced \type {\input}
156 into the input buffer. \footnote {This might change at some point into an explicit
157 loading triggered via \LUA.}
158
159 When we are in \TEX\ mode at some point the engine needs a filename, for instance
160 for opening a log file. At that moment the set jobname becomes the internal one
161 and when it has not been set which internalized to jobname but when not set
162 becomes \type {texput}. When you see a \type {texput.log} file someplace on your
163 system it normally indicates a bad run.
164
165 % fileio_state .jobname : a tex string (set when a (log) file is opened)
166 % engine_state .startup_jobname : handles by option parser
167 % environment_state.input_name : temporary interceptor
168
169 The command line option \type{--permitloadlib} has to be given when you load
170 external libraries via \LUA. Although you could manage this via \LUA\ itself in a
171 startup script, the reason for having this as option is the wish for security (at
172 some point that became a demand for \LUATEX), so this might give an extra feeling
173 of protection.
174
175 \stopsubsection
176
177 \stopsection
178
179 \startsection[title={\LUA\ behaviour}]
180
181 \startsubsection[title={The \LUA\ version}]
182
183 We currently use \LUA\ version \luaversion\ and will follow developments of the
184 language but normally with some delay. Therefore the user needs to keep an eye on
185 (subtle) differences in successive versions of the language. Here are a few
186 examples.
187
188 \LUA s \type {tostring} function (and \type {string.format}) may return values in
189 scientific notation, thereby confusing the \TEX\ end of things when it is used as
190 the right|-|hand side of an assignment to a \type {\dimen} or \type {\count}. The
191 output of these serializers also depend on the \LUA\ version, so in \LUA\ 5.3 you
192 can get different output than from 5.2. It is best not to depend the automatic
193 cast from string to number and vise versa as this can change in future versions.
194
195 When \LUA\ introduced bitwise operators, instead of providing functions in the
196 \type {bit32} library, we wanted to use these. The solution in \CONTEXT\ was to
197 implement a macro subsystem (kind of like what \CCODE\ does) and replace the
198 function calls by native bitwise operations. However, because \LUAJITTEX\ didn't
199 evolve we dropped that and when we split the code base between \MKIV\ and \MKXL\
200 we went native bitwise. The \type {bit32} library is still there but implemented
201 in \LUA\ instead.
202
203 \stopsubsection
204
205 \startsubsection[title={Locales}]
206
207 In stock \LUA, many things depend on the current locale. In \LUAMETATEX, we can't
208 do that, because it makes documents non|-|portable. While \LUAMETATEX\ is running
209 if forces the following locale settings:
210
211 \starttyping
212 LC_CTYPE=C
213 LC_COLLATE=C
214 LC_NUMERIC=C
215 \stoptyping
216
217 There is no way to change that as it would interfere badly with the often
218 language specific conversions needed at the \TEX\ end.
219
220 \stopsubsection
221
222 \stopsection
223
224 \startsection[title={\LUA\ modules}]
225
226 Of course the regular \LUA\ modules are present. In addition we provide the \type
227 {lpeg} library by Roberto Ierusalimschy, This library is not \UNICODE|-|aware,
228 but interprets strings on a byte|-|per|-|byte basis. This mainly means that \type
229 {lpeg.S} cannot be used with \UTF8 characters that need more than one byte, and
230 thus \type {lpeg.S} will look for one of those two bytes when matching, not the
231 combination of the two. The same is true for \type {lpeg.R}, although the latter
232 will display an error message if used with multi-byte characters. Therefore \type
233 {lpeg.R('aä')} results in the message \type {bad argument #1 to 'R' (range must
234 have two characters)}, since to \type {lpeg}, \type {ä} is two 'characters'
235 (bytes), so \type {aä} totals three. In practice this is no real issue and with
236 some care you can deal with \UNICODE\ just fine.
237
238 There are some more libraries present. For instance we embed \type {luasocket}
239 but contrary to \LUATEX\ don't embed the related \LUA\ code but some patched and
240 extended variant. The \type {luafilesystem} module has been replaced by a more
241 efficient one that also deals with the \MSWINDOWS\ file and environment
242 properties better (\UNICODE\ support in \MSWINDOWS\ dates from before \UTF8
243 became dominant so we need to deal with wide \UNICODE16). We don't have a
244 \UNICODE\ library because we always did conversions in \LUA, but there are some
245 helpers in the \type {string} library, which makes sense because \LUA\ itself is
246 now also becoming \UNICODE\ aware.
247
248 There are more extensive math libraries and there are libraries that deal with
249 encryption and compression. There are also some optional libraries that we do
250 interface but that are loaded on demand. The interfaces are as minimal as can be
251 because we so much in \LUA, which also means that one can tune behavior to usage
252 better.
253
254 \stopsection
255
256 \startsection[title={Files}]
257
258 \startsubsection[title={File syntax}]
259
260 \LUAMETATEX\ will accept a braced argument as a file name:
261
262 \starttyping
263 \input {plain}
264 \openin 0 {plain}
265 \stoptyping
266
267 This allows for embedded spaces, without the need for double quotes. Macro
268 expansion takes place inside the argument. Keep in mind that as side effect of
269 delegating \IO\ to \LUA\ the \tex {openin} primitive is not provided by the
270 engine and has to be implemented by the macro package. This also means that the
271 limit on the number of open files is not enforced by the engine.
272
273 \stopsubsection
274
275 \startsubsection[title={Writing to file}]
276
277 Writing to a file in \TEX\ has two forms: delayed and immediate. Delayed writing
278 means that the to be written text is anchored in the node list and flushed by the
279 backend. As all \IO\ is delegated to \LUA, this also means that it has to deal
280 with distinction. In \LUATEX\ the number of open files was already bumped to 127,
281 but in \LUAMETATEX\ it depends on the macro package. The special meaning of
282 channel 18 was already dropped in \LUATEX\ because we have \type {os.execute}.
283
284 \stopsubsection
285
286 \stopsection
287
288 \startsection[title={Testing}]
289
290 For development reasons you can influence the used startup date and time. By
291 setting the \type {start_time} variable in the \type {texconfig} table; as with
292 other variables we use the internal name there. When Universal Time is needed,
293 set the entry \type {use_utc_time} in the \type {texconfig} table.
294
295 In \CONTEXT\ we provide the command line argument \type {--nodates} that does
296 a bit more than disabling dates; it avoids time dependent information in the
297 output file for instance.
298
299 \stopsection
300
301 \startsection[title={Helpers}]
302
303 \startsubsection[title=Basics]
304
305 The \type {lua} library is relatively small and only provides a few functions.
306 There are many more helpers but these are organized in specific modules for file
307 i/o, handling strings, and manipulating table.
308
309 The \LUA\ interpreter is stack bases and when you put a lot of values on the stack
310 it can overflow. However, if that is the case you're probably doing something wrong.
311 The next function returns the current top and is mainly there for development reasons.
312
313 \starttyping[option=LUA]
314 function lua.getstacktop ( )
315 return <t:integer>
316 end
317 \stoptyping
318
319 The next example:
320
321 \startbuffer
322 \startluacode
323 context(lua.getstacktop())
324 context(lua.getstacktop(1,2,3))
325 context(lua.getstacktop(1,2,3,4,5,6))
326 \stopluacode
327 \stopbuffer
328
329 \typebuffer
330
331 typesets: \inlinebuffer, so we're fine.
332
333 \startbuffer
334 \startluacode
335 context(lua.getstacktop(unpack(token.getprimitives())))
336 \stopluacode
337 \stopbuffer
338
339 \typebuffer
340
341 But even this one os okay: \inlinebuffer, because some thousand plus entries is not
342 bothering the engine. Of course it makes little sense because now one has to loop
343 over the arguments.
344
345 % \starttyping[option=LUA]
346 % function lua.getdebuginfo ( )
347 % -- return todo
348 % end
349 % \stoptyping
350
351 The engines exit code can be set with:
352
353 \starttyping[option=LUA]
354 function lua.setexitcode ( <t:integer> )
355 -- no return values
356 end
357 \stoptyping
358
359 and queried with:
360
361 \starttyping[option=LUA]
362 function lua.getexitcode ( )
363 return <t:integer>
364 end
365 \stoptyping
366
367 The name of the startup file, in our case \quote {\tttf \cldcontext
368 {file.basename (lua.getstartupfile ())}} with the path part stripped, can be
369 fetched with:
370
371 \starttyping[option=LUA]
372 function lua.getstartupfile ( )
373 return <t:string>
374 end
375 \stoptyping
376
377 The current \LUA\ version, as reported by the next helper, is \cldcontext {lua.getversion ()}.
378
379 \starttyping[option=LUA]
380 function lua.getversion ( )
381 -- return todo
382 end
383 \stoptyping
384
385 We provide high resolution timers so that we can more reliable do performance
386 tests when needed and for that we have time related helpers. The \type
387 {getruntime} function returns the time passed since startup. The \type
388 {getcurrenttime} does what its name says. Just play with them to see how it pays
389 off. The \type {getpreciseticks} returns a number that can be used later, after a
390 similar call, to get a difference. The \type {getpreciseseconds} function gets
391 such a tick (delta) as argument and returns the number of seconds. Ticks can
392 differ per operating system, but one always creates a reference first and then
393 use deltas to this reference.
394
395 \stopsubsection
396
397 \startsubsection[title=Timers]
398
399 \starttyping[option=LUA]
400 function lua.getruntime ( )
401 return <t:number> -- actually an integer
402 end
403 \stoptyping
404
405 \starttyping[option=LUA]
406 function lua.getcurrenttime ( )
407 return <t:number> -- actually an integer
408 end
409 \stoptyping
410
411 \starttyping[option=LUA]
412 function lua.getpreciseticks ( )
413 return <t:number> -- actually an integer
414 end
415 \stoptyping
416
417 \starttyping[option=LUA]
418 function lua.getpreciseseconds ( <t:number> ticks )
419 return <t:number>
420 end
421 \stoptyping
422
423 There is a little bit of duplication in the timers; here is what they report at
424 this stage of the current run:
425
426 \starttabulate[|lT|lT|l|]
427 \FL
428 \BC library \NC function \BC result \NC \NR
429 \TL
430 \NC lua \NC getruntime \NC \cldcontext{lua.getruntime()} \NC \NR
431 \NC \NC getcurrenttime \NC \cldcontext{lua.getcurrenttime()} \NC \NR % 11644473600.0
432 \NC \NC getpreciseticks \NC \cldcontext{lua.getpreciseticks()} \NC \NR
433 \NC \NC getpreciseseconds \NC \cldcontext{lua.getpreciseseconds(lua.getpreciseticks())} \NC \NR
434 \ML
435 \NC os \NC clock \NC \cldcontext{os.clock()} \NC \NR
436 \NC \NC time \NC \cldcontext{os.time()} \NC \NR
437 \NC \NC gettimeofday \NC \cldcontext{os.gettimeofday()} \NC \NR
438 \LL
439 \stoptabulate
440
441 \stopsubsection
442
443 \startsubsection[title=Bytecode registers]
444
445 \LUA\ registers can be used to store \LUA\ code chunks. The accepted values for
446 assignments are functions and \type {nil}. Likewise, the retrieved value is
447 either a function or \type {nil}.
448
449 \starttyping[option=LUA]
450 function lua.setbytecode (
451 <t:integer> register,
452 <t:function> loader,
453 <t:boolean> strip
454 )
455 -- no return values
456 end
457 \stoptyping
458
459 An example of a valid call is \type {lua.setbytecode(5,loadfile("foo.lua"))}. The complement
460 of this helper is:
461
462 \starttyping[option=LUA]
463 function lua.getbytecode ( <t:integer> register )
464 retrurn <t:bytecode>
465 end
466 \stoptyping
467
468 The codes are stored in the virtual table \type {lua.bytecode}. The contents of
469 this array is stored inside the format file as actual \LUA\ bytecode, so it can
470 also be used to preload \LUA\ code. The function must not contain any upvalues.
471 The associated function calls are:
472
473 \starttyping[option=LUA]
474 function lua.callbytecode ( <t:integer> register )
475 -- <t:boolean> -- success
476 end
477 \stoptyping
478
479 Note that the path of the file is stored in the \LUA\ bytecode to be used in
480 stack backtraces and therefore dumped into the format file if the above code is
481 used in \INITEX. If it contains private information, i.e. the user name, this
482 information is then contained in the format file as well. This should be kept in
483 mind when preloading files into a bytecode register in \INITEX.
484
485 \stopsubsection
486
487 \startsubsection[title=Tables]
488
489 You can preallocate tables with these two helpers. The first one preallocates the
490 given amount of hash entries and index entries. The \type {newindex} function
491 create an indexed table with default values:
492
493 \starttyping[option=LUA]
494 function lua.newtable ( <t:integer> hashsize, <t:integer> indexsize )
495 return <t:table>
496 end
497
498 function lua.newindex ( <t:integer> size, <t:whatever> default )
499 return <t:table>
500 end
501 \stoptyping
502
503 \stopsubsection
504
505 \startsubsection[title=Nibbles]
506
507 Nibbles are half bytes so they run from \type {0x0} upto \type {0xF}. When we
508 needed this for math state fields, the helpers made it here.
509
510 \starttyping[option=LUA]
511 function lua.setnibble ( <t:integer> original, <t:integer> position, <t:integer> value )
512 return <t:integer>
513 end
514 \stoptyping
515
516 \starttyping[option=LUA]
517 function lua.getnibble ( <t:integer> original, <t:integer> position )
518 return <t:integer>
519 end
520 \stoptyping
521
522 \starttyping[option=LUA]
523 function lua.addnibble ( <t:integer> original, <t:integer> position, <t:integer> value )
524 return <t:integer>
525 end
526 \stoptyping
527
528 \starttyping[option=LUA]
529 function lua.subnibble ( <t:integer> original, <t:integer> position, <t:integer> value )
530 return <t:integer>
531 end
532 \stoptyping
533
534 Here a a few examples (positions go from right to left and start at one):
535
536 \protected\def\Test#1#2{\NC #2 \NC \cldcontext{"0x\letterpercent#1X",#2} \NC \NR}
537
538 \starttabulate[|lT|r|]
539 \Test{04}{lua.setnibble(0x0000,2,0x1)}
540 \Test{04}{lua.setnibble(0x0000,4,0x7)}
541 \Test{01}{lua.getnibble(0x1234,2)}
542 \Test{01}{lua.getnibble(0x1234,4)}
543 \Test{04}{lua.addnibble(0x0000,2)}
544 \Test{04}{lua.addnibble(0x0030,2)}
545 \Test{04}{lua.subnibble(0x00F0,2)}
546 \Test{04}{lua.subnibble(0x0080,2)}
547 \stoptabulate
548
549 \stopsubsection
550
551 \startsubsection[title=Functions]
552
553 The functions table stores functions by index. The index can be used with the
554 primitives that call functions by index. In order to prevent interferences a
555 macro package should provide some interface to the function call mechanisms, just
556 like it does with registers.
557
558 \starttyping[option=LUA]
559 function lua.getfunctionstable ( )
560 return <t:table>
561 end
562 \stoptyping
563
564 \stopsubsection
565
566 \startsubsection[title=Tracing]
567
568 The engine also includes the serializer from the \type {luac} program, just
569 because it can be interesting to see what \LUA\ does with your code.
570
571 \starttyping[option=LUA]
572 function luac.print ( <t:string> bytecode, <t:boolean> detailed )
573 -- nothing to return
574 end
575 \stoptyping
576
577 \stopsubsection
578
579 \stopsection
580
581 \stopdocument
582