summaryrefslogtreecommitdiff
path: root/doc/context/presentations/context/2021/context-2021-luametafun.tex
blob: 333c68682168a53c31bafdc2d681d1e2d0a11f7b (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
% language=us

\usemodule[present-boring,abbreviations-logos]

\setuptolerance[verytolerant,stretch]

\startdocument
  [title={LUAMETAFUN},
   banner={the new interfaces},
   location={context\enspace {\bf 2021}\enspace meeting}]

\starttitle[title=Three subsystems]

\startitemize

\startitem
    The core of the engine in still \TEX. It all starts there.
\stopitem

\startitem
    The \TEX\ internals are opened up via \LUA. We can call out to \LUA\ from
    \TEX\ and to some extend from \LUA\ to \TEX. Quite often we push back
    something via the input channels.
\stopitem

\startitem
    The \METAPOST\ library is accessed from \LUA. So from within \TEX\ there is
    always \LUA\ in between. Results go back via \LUA. The library can also call
    out to \LUA\ and from there to \TEX.
\stopitem

\startitem
    This means that all three major components of \LUAMETATEX\ can talk to each
    other and use each others capabilities.
\stopitem

\startitem
    With \LUA\ in the center, we also have access to other mechanism, for instance
    fonts, graphics and libraries.
\stopitem

\startitem
    In \CONTEXT\ the \LUA\ language also permits using \XML, \JSON, \CSV, \SQL\
    databases and other input that can be dealt with programmatically.
\stopitem

\startitem
    All has been reasonably optimized for efficiency and performance.
\stopitem

\stopitemize

\stoptitle

\starttitle[title=The (\LUATEX) library]

\startitemize

\startitem
    Turning \METAPOST\ into a library has been a subproject of the \LUATEX\ project. The
    semi|-|official team (Taco, Jacko, Hans, Luigi) got John Hobbies blessing.
\stopitem

\startitem
    This was a rather massive (and impressive) operation by Taco because multiple
    number models were to be supported and the internals had to be made such that
    different backends were possible. All with remaining perfect (DEK) compatibility.
\stopitem

\startitem
    The \METAPOST\ library serves both the stand alone program and \LUATEX.
\stopitem

\startitem
    That means the \POSTSCRIPT\ backend is built in plus some basic (\TYPEONE)
    font handling. We support \PDF\ output via the \METAPOST\ \LUA\ backend (in
    \MKII\ that is done by parsing the \POSTSCRIPT\ and specials).
\stopitem

\startitem
    In addition there is \PNG\ and \SVG\ output. It helps that \METAPOST\ output is
    rather simple.
\stopitem

\startitem
    The \LUATEX\ engine uses the \LUA\ backend which represents the result in \LUA\ tables
    resembling the \METAPOST\ internal representation.
\stopitem

\startitem
    The library supports scaled and double (internal) but also binary and decimal
    number models that use (linked in) libraries.
\stopitem

% this would force a page
%
% \startitem
%     The library is quite stable and Taco transferred maintenance to Luigi.
% \stopitem

\stopitemize

\stoptitle

\starttitle[title=The (\LUAMETATEX) library]

\startitemize

\startitem
    We don't need the \POSTSCRIPT\ backend (which only does \TYPEONE\ anyway).
\stopitem

\startitem
    We also have no use for \SVG\ and \PNG\ output.
\stopitem

\startitem
    The binary number model has no advantages over the decimal one but brings
    quite some dependency with it (library code).
\stopitem

\startitem
    The \TYPEONE\ font support is not used in \CONTEXT\ because we handle text
    differently.
\stopitem

\startitem
    All this means that we can do with a smaller (simplified) \METAPOST\ library.
\stopitem

\startitem
    The codebase has been overhauled. We still have \type {.w} files (\CWEB) but
    use a \LUA\ script to convert that to \CCODE\ which means that we have better
    control over how it comes out.
\stopitem

\startitem
    As with \LUATEX\ the file \IO, message handling etc.\ now largely goes via \LUA;
    it is more integrated.
\stopitem

\startitem
    The same is true for scanning interfaces and return values (injectors). That also
    made for more symbolic coding.
\stopitem

\startitem
    Memory management (allocation) is under engine control (as with \TEX\ and \LUA);
    we use a common high performance allocator library.
\stopitem

\stopitemize

\stoptitle

\starttitle[title=The (\LUAMETATEX) library]

\startitemize

\startitem
    Some already present mechanism have been extended, for instance clips have
    pre- and postscripts.
\stopitem

\startitem
    A grouping wrapper has been added (handy for some graphic trickery supported in the
    backend.)
\stopitem

\startitem
    The \type {runscript} primitive supports symbolic references to functions (of course to
    be provided at the \LUA\ end).
\stopitem

\startitem
    The \type {runscript} return values can be more native, in addition to the already
    present (default) \type {scantokens} support.
\stopitem

\startitem
    Internals are extended with booleans and strings.
\stopitem

\startitem
    Output (paths, clips etc) can be stacked in a different order.
\stopitem

\startitem
    There are additional statistics available.
\stopitem

\startitem
    In some places performance could be improved.
\stopitem

\startitem
    In the meantime it can be considered a major upgrade and (for various
    reasons) backporting to \LUATEX\ makes no sense. And yes, all errors are
    mine.
\stopitem

\stopitemize

\starttitle[title=The \LUA fication]

    {\em See Taco's presentation where he gives some examples.}

\stoptitle

\starttitle[title=Callbacks]

We need to hook in some functions: \blank[2*big]

\starttabulate[|T|T|T|p|]
    \HL
    \NC   \NC find_file    \NC (name,mode,kind)           \NC locate a file (usually within the \TDS\ setup) \NC \NR
    \NC f \NC open_file    \NC (name,mode,kind)           \NC open given file \NC \NR
    \NC   \NC close_file   \NC (handle)                   \NC close opened file \NC \NR
    \NC s \NC read_file    \NC (handle,size)              \NC read from file \NC \NR
    \NC   \NC write_file   \NC (handle,str)               \NC write to file \NC \NR
    \HL
    \NC s \NC run_script   \NC (code,size,index)          \NC run the given string as \LUA\ script \NC \NR
    \NC s \NC make_text    \NC (str,size,mode)            \NC process btex/etex \NC \NR
    \HL
    \NC   \NC run_internal \NC (action,index,kind,name)   \NC act on internal definition \NC \NR
    \HL
    \NC n \NC run_overload \NC (property,name,mode)       \NC process overload check \NC \NR
    \HL
    \NC   \NC run_logger   \NC (target,str,size)          \NC process log message \NC \NR
    \NC   \NC run_error    \NC (message,help,interaction) \NC handle error (message) \NC \NR
    \NC   \NC run_warning  \NC (message)                  \NC handle warning \NC \NR
    \HL
\stoptabulate

% check_overload shipout_backend

\stoptitle

\starttitle[title=Two calling methods]

The runner can be called as:

\starttyping
runscript("mp.MyFunction()")
\stoptyping

which implies at the \LUA\ end:

\starttyping
function mp.MyFunction()
    ...
end
\stoptyping

Here the callback function is responsible for loading the string and executing it.

Alternatively one can say:

\starttyping
runscript <number>
\stoptyping

The number can be intercepted at the \LUA\ end to do some associated action.

\stoptitle

\starttitle[title=Variables]

We can do:

\starttyping
lua.mp.MyFunction("foo",123,true)
\stoptyping

which in the end is equivalent to:

\starttyping
runscript("mp.MyFunction('foo',123,true)")
\stoptyping

Alternatively one can pick up values by scanning: like \type {scannext}, \type
{scanexpression}, \type {scantoken}, \type {scansymbol}, \type {scannumeric},
\type {scaninteger}, \type {scanboolean}, \type {scanstring}, \type {scanpair},
\type {scancolor}, \type {scancmykcolor}, \type {scantransform}, \type
{scanpath}, \type {scanpen}, etc.

\stoptitle

\starttitle[title=Return values]

The runner can return:

\startitemize

\startitem
    a string that gets fed into the \type {scantokens} primitive
\stopitem

\startitem
    a numeric or boolean that gets injected as native \METAPOST\ object
\stopitem

\startitem
    a table that gets concatenated and fed into the \type {scantokens} primitive
\stopitem

\startitem
    \type {true} and a second argument that gets converted into a native \METAPOST\ object
\stopitem

\startitem
    in the last case the number of table elements determines the object
\stopitem

\stopitemize

Instead of returning a value one can inject: \type {injectnumeric}, \type
{injectinteger}, \type {injectboolean}, \type {injectstring}, \type {injectpair},
\type {injectcolor}, \type {injectcmykcolor}, \type {injecttransform}, \type
{injectpath}, \type {injectwhatever}, etc.\ and these accept one or more values
and|/|or tables.

These mechanisms might evolve a bit over time. Lots of examples can be found in
the \type {mlib-*.lmt} files.

\stoptitle

\starttitle[title=Parameters]

\startitemize
    \startitem
        The new interfaces permit us to program quite robust parameter driven
        interfaces that (sort of) match the way we do things at the \TEX\ end.
    \stopitem
    \startitem
        The distribution has several examples of usage and more will be added.
    \stopitem
    \startitem
        Macros that use the new mechanisms can be recognized by the \type {lmt_}
        prefix.
    \stopitem
\stopitemize

\starttyping
lmt_mytrick [
    somestring  = "test",
    somenumeric = 123,
    someboolean = true,
    somecolor   = (1, 0, 1),
    somepath    = fullsquare scaled 10cm,
    somelist    = { (0, 0), (1, 3), (8, 9) },
    sometable   = [
        somenumeric = 321,
   ],
] ;
\stoptyping

{\em Show the pattern of defining these at the \LUA\ end and in \METAPOST\ files.}

\stoptitle

\stopdocument