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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
|
% language=uk
\startcomponent about-metafun
\environment about-environment
\startchapter[title={\LUA\ in \METAPOST}]
% Hans Hagen, PRAGMA ADE, April 2014
\startsection[title=Introduction]
Already for some years I have been wondering how it would be if we could escape
to \LUA\ inside \METAPOST, or in practice, in \MPLIB\ in \LUATEX. The idea is
simple: embed \LUA\ code in a \METAPOST\ file that gets run as soon as it's seen.
In case you wonder why \LUA\ code makes sense, imagine generating graphics using
external data. The capabilities of \LUA\ to deal with that is more flexible and
advanced than in \METAPOST. Of course we could generate a \METAPOST\ definition
of a graphic from data but it often makes more sense to do the reverse. I finally
found time and reason to look into this and in the following sections I will
describe how it's done.
\stopsection
\startsection[title=The basics]
The approach is comparable to \LUATEX's \type {\directlua}. That primitive can be
used to execute \LUA\ code and in combination with \type {tex.print} we can pipe
strings back into the \TEX\ input stream. A complication is that we have to be
able to operate under different so called catcode regimes: the meaning of
characters can differ per regime. We also have to deal with line endings in
special ways as they relate to paragraphs and such. In \METAPOST\ we don't have
that complication so getting back input into the \METAPOST\ input, we can do so
with simple strings. For that a mechanism similar to \type {scantokens} can be
used. That way we can return anything (including nothing) as long as \METAPOST\
can interpret it and as long as it fulfils the expectations.
\starttyping
numeric n ; n := scantokens("123.456") ;
\stoptyping
A script is run as follows:
\starttyping
numeric n ; n := runscript("return '123.456'") ;
\stoptyping
This primitive doesn't have the word \type {lua} in its name so in principle any
wrapper around the library can use it as a hook. In the case of \LUATEX\ the
script language is of course \LUA. At the \METAPOST\ end we only expect a string.
How that string is constructed is completely up to the \LUA\ script. In fact, the
user is completely free to implement the runner any way she or he wants, like:
\starttyping
local function scriptrunner(code)
local f = loadstring(code)
if f then
return tostring(f())
else
return ""
end
end
\stoptyping
This is hooked into an instance as follows:
\starttyping
local m = mplib.new {
...
run_script = scriptrunner,
...
}
\stoptyping
Now, beware, this is not the \CONTEXT\ way. We provide print functions and other
helpers, which we will explain in the next section.
\stopsection
\startsection[title=Helpers]
After I got this feature up and running I played a bit with possible interfaces
at the \CONTEXT\ (read: \METAFUN) end and ended up with a bit more advanced runner
where no return value is used. The runner is wrapped in the \type {lua} macro.
\startbuffer
numeric n ; n := lua("mp.print(12.34567)") ;
draw textext(n) xsized 4cm withcolor maincolor ;
\stopbuffer
\typebuffer
This renders as:
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
In case you wonder how efficient calling \LUA\ is, don't worry: it's fast enough,
especially if you consider suboptimal \LUA\ code and the fact that we switch
between machineries.
\startbuffer
draw image (
lua("statistics.starttiming()") ;
for i=1 upto 10000 : draw
lua("mp.pair(math.random(-200,200),math.random(-50,50))") ;
endfor ;
setbounds currentpicture to fullsquare xyscaled (400,100) ;
lua("statistics.stoptiming()") ;
draw textext(lua("mp.print(statistics.elapsedtime())"))
ysized 50 ;
) withcolor maincolor withpen pencircle scaled 1 ;
\stopbuffer
\typebuffer
Here the line:
\starttyping
draw lua("mp.pair(math.random(-200,200),math.random(-50,50))") ;
\stoptyping
effectively becomes (for instance):
\starttyping
draw scantokens "(25,40)" ;
\stoptyping
which in turn becomes:
\starttyping
draw scantokens (25,40) ;
\stoptyping
The same happens with this:
\starttyping
draw textext(lua("mp.print(statistics.elapsedtime())")) ...
\stoptyping
This becomes for instance:
\starttyping
draw textext(scantokens "1.23") ...
\stoptyping
and therefore:
\starttyping
draw textext(1.23) ...
\stoptyping
We can use \type {mp.print} here because the \type {textext} macro can deal with
numbers. The following also works:
\starttyping
draw textext(lua("mp.quoted(statistics.elapsedtime())")) ...
\stoptyping
Now we get (in \METAPOST\ speak):
\starttyping
draw textext(scantokens (ditto & "1.23" & ditto) ...
\stoptyping
Here \type {ditto} represents the double quotes that mark a string. Of course,
because we pass the strings directly to \type {scantokens}, there are no outer
quotes at all, but this is how it can be simulated. In the end we have:
\starttyping
draw textext("1.23") ...
\stoptyping
What print variant you use, \type {mp.print} or \type {mp.quoted}, depends on
what the expected code is: an assignment to a numeric can best be a number or an
expression resulting in a number.
This graphic becomes:
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
The runtime on my current machine is some 0.25 seconds without and 0.12 seconds
with caching. But to be honest, speed is not really a concern here as the amount
of complex \METAPOST\ graphics can be neglected compared to extensive node list
manipulation. Generating the graphic with \LUAJITTEX\ takes 15\% less time.
\footnote {Processing a small 8 page document like this takes about one second,
which includes loading a bunch of fonts.}
\startbuffer
numeric n ; n := lua("mp.print(1) mp.print('+') mp.print(2)") ;
draw textext(n) xsized 1cm withcolor maincolor ;
\stopbuffer
The three print command accumulate their arguments:
\typebuffer
As expected we get:
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
\startbuffer
numeric n ; n := lua("mp.print(1,'+',2)") ;
draw textext(n) xsized 1cm withcolor maincolor ;
\stopbuffer
Equally valid is:
\typebuffer
This gives the same result:
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
Of course all kind of action can happen between the prints. It is also legal to
have nothing returned as could be seen in the 10.000 dot example: there the timer
related code returns nothing, so effectively we have \type {scantokens("")}.
Another helper is \type {mp.quoted}, as in:
\startbuffer
draw
textext(lua("mp.quoted('@0.3f'," & decimal n & ")"))
withcolor maincolor ;
\stopbuffer
\typebuffer
This typesets \processMPbuffer. Note the \type {@}. When no percent character is
found in the format specifier, we assume that an \type {@} is used instead.
\startbuffer
\startluacode
table.save("demo-data.lua",
{
{ 1, 2 }, { 2, 4 }, { 3, 3 }, { 4, 2 },
{ 5, 2 }, { 6, 3 }, { 7, 4 }, { 8, 1 },
}
)
\stopluacode
\stopbuffer
But, the real benefit of embedded \LUA\ is when we deal with data that is stored
at the \LUA\ end. First we define a small dataset:
\typebuffer
\getbuffer
There are several ways to deal with this table. I will show clumsy as well as
better looking ways.
\startbuffer
lua("MP = { } MP.data = table.load('demo-data.lua')") ;
numeric n ;
lua("mp.print('n := ',\#MP.data)") ;
for i=1 upto n :
drawdot
lua("mp.pair(MP.data[" & decimal i & "])") scaled cm
withpen pencircle scaled 2mm
withcolor maincolor ;
endfor ;
\stopbuffer
\typebuffer
Here we load a \LUA\ table and assign the size to a \METAPOST\ numeric. Next we
loop over the table entries and draw the coordinates.
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
We will stepwise improve this code. In the previous examples we omitted wrapper
code but here we show it:
\startbuffer
\startluacode
MP.data = table.load('demo-data.lua')
function MP.n()
mp.print(#MP.data)
end
function MP.dot(i)
mp.pair(MP.data[i])
end
\stopluacode
\startMPcode
numeric n ; n := lua("MP.n()") ;
for i=1 upto n :
drawdot
lua("MP.dot(" & decimal i & ")") scaled cm
withpen pencircle scaled 2mm
withcolor maincolor ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
So, we create a few helpers in the \type {MP} table. This table is predefined so
normally you don't need to define it. You may however decide to wipe it clean.
\startlinecorrection[blank]
\getbuffer
\stoplinecorrection
You can decide to hide the data:
\startbuffer
\startluacode
local data = { }
function MP.load(name)
data = table.load(name)
end
function MP.n()
mp.print(#data)
end
function MP.dot(i)
mp.pair(data[i])
end
\stopluacode
\stopbuffer
\typebuffer \getbuffer
It is possible to use less \LUA, for instance in:
\startbuffer
\startluacode
local data = { }
function MP.loaded(name)
data = table.load(name)
mp.print(#data)
end
function MP.dot(i)
mp.pair(data[i])
end
\stopluacode
\startMPcode
for i=1 upto lua("MP.loaded('demo-data.lua')") :
drawdot
lua("MP.dot(",i,")") scaled cm
withpen pencircle scaled 4mm
withcolor maincolor ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
Here we also omit the \type {decimal} because the \type {lua} macro is clever
enough to recognize it as a number.
\startlinecorrection[blank]
\getbuffer
\stoplinecorrection
By using some \METAPOST\ magic we can even go a step further in readability:
\startbuffer
\startMPcode{doublefun}
lua.MP.load("demo-data.lua") ;
for i=1 upto lua.MP.n() :
drawdot
lua.MP.dot(i) scaled cm
withpen pencircle scaled 4mm
withcolor maincolor ;
endfor ;
for i=1 upto MP.n() :
drawdot
MP.dot(i) scaled cm
withpen pencircle scaled 2mm
withcolor white ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
Here we demonstrate that it also works well in \type {double} mode, which makes
much sense when processing data from other sources. Note how we omit the
type {lua} prefix: the \type {MP} macro will deal with that.
\startlinecorrection[blank]
\getbuffer
\stoplinecorrection
So in the end we can simplify the code that we started with to:
\starttyping
\startMPcode{doublefun}
for i=1 upto MP.loaded("demo-data.lua") :
drawdot
MP.dot(i) scaled cm
withpen pencircle scaled 2mm
withcolor maincolor ;
endfor ;
\stopMPcode
\stoptyping
\stopsection
\startsection[title=Access to variables]
The question with such mechanisms is always: how far should we go. Although
\METAPOST\ is a macro language, it has properties of procedural languages. It also
has more introspective features at the user end. For instance, one can loop over
the resulting picture and manipulate it. This means that we don't need full
access to \METAPOST\ internals. However, it makes sense to provide access to
basic variables: \type {numeric}, \type {string}, and \type {boolean}.
\startbuffer
draw textext(lua("mp.quoted('@0.15f',mp.get.numeric('pi')-math.pi)"))
ysized 1cm
withcolor maincolor ;
\stopbuffer
\typebuffer
In double mode you will get zero printed but in scaled mode we definitely get a
different results:
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
\startbuffer
boolean b ; b := true ;
draw textext(lua("mp.quoted(mp.get.boolean('b') and 'yes' or 'no')"))
ysized 1cm
withcolor maincolor ;
\stopbuffer
In the next example we use \type {mp.quoted} to make sure that indeed we pass a
string. The \type {textext} macro can deal with numbers, but an unquoted \type
{yes} or \type {no} is asking for problems.
\typebuffer
Especially when more text is involved it makes sense to predefine a helper in
the \type {MP} namespace, if only because \METAPOST\ (currently) doesn't like
newlines in the middle of a string, so a \type {lua} call has to be on one line.
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
Here is an example where \LUA\ does something that would be close to impossible,
especially if more complex text is involved.
% \enabletrackers[metapost.lua]
\startbuffer
string s ; s := "ΤΕΧ" ; % "τεχ"
draw textext(lua("mp.quoted(characters.lower(mp.get.string('s')))"))
ysized 1cm
withcolor maincolor ;
\stopbuffer
\typebuffer
As you can see here, the whole repertoire of helper functions can be used in
a \METAFUN\ definition.
\startlinecorrection[blank]
\processMPbuffer
\stoplinecorrection
\stopsection
\startsection[title=The library]
In \CONTEXT\ we have a dedicated runner, but for the record we mention the
low level constructor:
\starttyping
local m = mplib.new {
...
script_runner = function(s) return loadstring(s)() end,
script_error = function(s) print(s) end,
...,
}
\stoptyping
An instance (in this case \type {m}) has a few extra methods. Instead you can use
the helpers in the library.
\starttabulate[|l|l|]
\HL
\NC \type {m:get_numeric(name)} \NC returns a numeric (double) \NC \NR
\NC \type {m:get_boolean(name)} \NC returns a boolean (\type {true} or \type {false}) \NC \NR
\NC \type {m:get_string (name)} \NC returns a string \NC \NR
\HL
\NC \type {mplib.get_numeric(m,name)} \NC returns a numeric (double) \NC \NR
\NC \type {mplib.get_boolean(m,name)} \NC returns a boolean (\type {true} or \type {false}) \NC \NR
\NC \type {mplib.get_string (m,name)} \NC returns a string \NC \NR
\HL
\stoptabulate
In \CONTEXT\ the instances are hidden and wrapped in high level macros, so there
you cannot use these commands.
\stopsection
\startsection[title=\CONTEXT\ helpers]
The \type {mp} namespace provides the following helpers:
\starttabulate[|l|l|]
\HL
\NC \type {print(...)} \NC returns one or more values \NC \NR
\NC \type {pair(x,y)}
\type {pair(t)} \NC returns a proper pair \NC \NR
\NC \type {triplet(x,y,z)}
\type {triplet(t)} \NC returns an \RGB\ color \NC \NR
\NC \type {quadruple(w,x,y,z)}
\type {quadruple(t)} \NC returns an \CMYK\ color \NC \NR
\NC \type {format(fmt,...)} \NC returns a formatted string \NC \NR
\NC \type {quoted(fmt,...)}
\type {quoted(s)} \NC returns a (formatted) quoted string \NC \NR
\NC \type {path(t[,connect][,close])} \NC returns a connected (closed) path \NC \NR
\HL
\stoptabulate
The \type {mp.get} namespace provides the following helpers:
\starttabulate[|l|l|]
\NC \type {numeric(name)} \NC gets a numeric from \METAPOST \NC \NR
\NC \type {boolean(name)} \NC gets a boolean from \METAPOST \NC \NR
\NC \type {string(name)} \NC gets a string from \METAPOST \NC \NR
\HL
\stoptabulate
\stopsection
\startsection[title=Paths]
% {\em This section will move to the metafun manual.} \blank
In the meantime we got several questions on the \CONTEXT\ mailing list about turning
coordinates into paths. Now imagine that we have this dataset:
\startbuffer[dataset]
10 20 20 20 -- sample 1
30 40 40 60
50 10
10 10 20 30 % sample 2
30 50 40 50
50 20
10 20 20 10 # sample 3
30 40 40 20
50 10
\stopbuffer
\typebuffer[dataset]
In this case I have put the data in a buffer, so that it can be shown
here, as well as used in a demo. Look how we can add comments. The
following code converts this into a table with three subtables.
\startbuffer
\startluacode
MP.myset = mp.dataset(buffers.getcontent("dataset"))
\stopluacode
\stopbuffer
\typebuffer \getbuffer
We use the \type {MP} (user) namespace to store the table. Next we turn
these subtables into paths:
\startbuffer
\startMPcode
for i=1 upto lua("mp.print(mp.n(MP.myset))") :
draw
lua("mp.path(MP.myset[" & decimal i & "])")
xysized (HSize,10ExHeight)
withpen pencircle scaled .25ExHeight
withcolor basiccolors[i]/2 ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
This gives:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
Instead we can fill the path, in which case we will also need to close it. The
\type {true} argument deals with that:
\startbuffer
\startMPcode
for i=1 upto lua("mp.print(mp.n(MP.myset))") :
path p ; p :=
lua("mp.path(MP.myset[" & decimal i & "],true)")
xysized (HSize,10ExHeight) ;
fill p
withcolor basiccolors[i]/2
withtransparency (1,.5) ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
We get:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
\startbuffer
\startMPcode
for i=1 upto lua("mp.print(mp.n(MP.myset))") :
path p ; p :=
lua("mp.path(MP.myset[" & decimal i & "])")
xysized (HSize,10ExHeight) ;
p :=
(xpart llcorner boundingbox p,0) --
p --
(xpart lrcorner boundingbox p,0) --
cycle ;
fill p
withcolor basiccolors[i]/2
withtransparency (1,.25) ;
endfor ;
\stopMPcode
\stopbuffer
The following makes more sense:
\typebuffer
So this gives:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
This (area) fill is so common, that we have a helper for it:
\startbuffer
\startMPcode
for i=1 upto lua("mp.size(MP.myset)") :
fill area
lua("mp.path(MP.myset[" & decimal i & "])")
xysized (HSize,5ExHeight)
withcolor basiccolors[i]/2
withtransparency (2,.25) ;
endfor ;
\stopMPcode
\stopbuffer
\typebuffer
So this gives:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
This snippet of \METAPOST\ code still looks kind of horrible, so how can we make
it look better? Here is an attempt. First we define a bit more \LUA:
\startbuffer
\startluacode
local data = mp.dataset(buffers.getcontent("dataset"))
MP.dataset = {
Line = function(n) mp.path(data[n]) end,
Size = function() mp.size(data) end,
}
\stopluacode
\stopbuffer
\typebuffer \getbuffer
\startbuffer
\startMPcode
for i=1 upto lua.MP.dataset.Size() :
path p ; p :=
lua.MP.dataset.Line(i)
xysized (HSize,20ExHeight) ;
draw
p
withpen pencircle scaled .25ExHeight
withcolor basiccolors[i]/2 ;
drawpoints
p
withpen pencircle scaled ExHeight
withcolor .5white ;
endfor ;
\stopMPcode
\stopbuffer
We can now make the \METAPOST\ look more natural. Of course, this is possible
because in \METAFUN\ the \type {lua} macro does some extra work.
\typebuffer
As expected, we get the desired result:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
Once we start making things look nicer and more convenient, we quickly end up
with helpers like those in the next example. First we save some demo data in
files:
\startbuffer
\startluacode
io.savedata("foo.tmp","10 20 20 20 30 40 40 60 50 10")
io.savedata("bar.tmp","10 10 20 30 30 50 40 50 50 20")
\stopluacode
\stopbuffer
\typebuffer \getbuffer
We load the data in datasets:
\startbuffer
\startMPcode
lua.mp.datasets.load("foo","foo.tmp") ;
lua.mp.datasets.load("bar","bar.tmp") ;
fill area
lua.mp.datasets.foo.Line()
xysized (HSize/2-EmWidth,10ExHeight)
withpen pencircle scaled .25ExHeight
withcolor green/2 ;
fill area
lua.mp.datasets.bar.Line()
xysized (HSize/2-EmWidth,10ExHeight)
shifted (HSize/2+EmWidth,0)
withpen pencircle scaled .25ExHeight
withcolor red/2 ;
\stopMPcode
\stopbuffer
\typebuffer
Because the datasets are stored by name, we can use them without worrying about
them being forgotten:
\startlinecorrection[blank] \getbuffer \stoplinecorrection
If no tag is given, the filename (without suffix) is used as a tag, so the
following is valid:
\starttyping
\startMPcode
lua.mp.datasets.load("foo.tmp") ;
lua.mp.datasets.load("bar.tmp") ;
\stopMPcode
\stoptyping
The following methods are defined for a dataset:
\starttabulate[|l|pl|]
\HL
\NC \type {method} \NC usage \NC \NR
\HL
\NC \type {Size} \NC the number of subsets in a dataset \NC \NR
\NC \type {Line} \NC the joined pairs in a dataset making a non|-|closed path \NC \NR
\NC \type {Data} \NC the table containing the data (in subsets, so there is always at least one subset) \NC \NR
\HL
\stoptabulate
{\em Due to limitations in \METAPOST\ suffix handling the methods start with an
uppercase character.}
\stopsection
\startsection[title=Remark]
The features described here are currently still experimental but the interface
will not change. There might be a few more accessors and for sure more \LUA\
helpers will be provided. As usual I need some time to play with it before I make
up my mind. It is also possible to optimize the \METAPOST||\LUA\ script call a
bit, but I might do that later.
When we played with this interface we ran into problems with loop variables
and macro arguments. These are internally kind of anonymous. Take this:
\starttyping
for i=1 upto 100 : draw(i,i) endfor ;
\stoptyping
The \type {i} is not really a variable with name \type {i} but becomes an object
(capsule) when the condition is scanned, and a reference to that object when the
body is scanned. The body of the for loop gets expanded for each step, but at that
time there is no longer a variable \type {i}. The same is true for variables in:
\starttyping
def foo(expr x, y, delta) = draw (x+delta,y+delta) enddef ;
\stoptyping
We are still trying to get this right with the \LUA\ interface. Interesting is
that when we were exploring this, we ran into quite some cases where we could
make \METAPOST\ abort due some memory or stack overflow. Some are just bugs in
the new code (due to the new number model) while others come with the design of
the system: border cases that never seem to happen in interactive use while the
library use assumes no interaction in case of errors.
In \CONTEXT\ there are more features and helpers than shown here but these are
discussed in the \METAFUN\ manual.
\stopsection
\stopchapter
\stopcomponent
% \startMPcode{doublefun}
% numeric n ; n := 123.456 ;
% lua("print('>>>>>>>>>>>> number',mp.get.number('n'))") ;
% lua("print('>>>>>>>>>>>> number',mp.get.boolean('n'))") ;
% lua("print('>>>>>>>>>>>> number',mp.get.string('n'))") ;
% boolean b ; b := true ;
% lua("print('>>>>>>>>>>>> boolean',mp.get.number('b'))") ;
% lua("print('>>>>>>>>>>>> boolean',mp.get.boolean('b'))") ;
% lua("print('>>>>>>>>>>>> boolean',mp.get.string('b'))") ;
% string s ; s := "TEST" ;
% lua("print('>>>>>>>>>>>> string',mp.get.number('s'))") ;
% lua("print('>>>>>>>>>>>> string',mp.get.boolean('s'))") ;
% lua("print('>>>>>>>>>>>> string',mp.get.string('s'))") ;
% \stopMPcode
|