context-2020-ecmascript.tex /size: 6109 b    last modification: 2021-10-28 13:50
1% language=us
2
3\usemodule[present-boring,abbreviations-logos]
4
5\startdocument
6  [title={ECMASCRIPT},
7   banner={just because it can be done},
8   location={context\enspace {\bf 2020}\enspace meeting}]
9
10\starttitle[title=Why oh why]
11
12\startitemize
13
14\startitem
15    We use a \type {mupdf} based \PDF\ viewer: \type {SumatraPDF}.
16\stopitem
17\startitem
18    And occasionally we use the tools that come with \type {mupdf}.
19\stopitem
20\startitem
21    So when checking if that viewer supports \JAVASCRIPT\ in widgets I noticed
22    the stand alone interpreter. \footnote {The official name is \ECMASCRIPT\
23    which is the standardized core language.}
24\stopitem
25\startitem
26    Which made me wonder how easy it would be to interface to it.
27\stopitem
28
29\blank
30
31\startitem
32    It uses the lightweight library subsystem: like \FFI\ the library interface
33    is setup dynamically.
34\stopitem
35\startitem
36    Support is {\em not} integrated in \LUAMETATEX, so there is no overhead and
37    there are no dependencies.
38\stopitem
39\startitem
40    We assume that the library is on the system, and when not, then there is also
41    also no support.
42\stopitem
43\startitem
44    We stick to the absolute minimum of interfacing needed and delegate
45    everything else to \LUA.
46\stopitem
47\startitem
48    We assume a stable \API, and if not, well \unknown\ sorry.
49\stopitem
50
51\stopitemize
52
53\stoptitle
54
55\starttitle[title=The components]
56
57\startitemize
58
59\startitem
60    The optional, delayed loading, interface, adds only a few KB to \LUAMETATEX.
61\stopitem
62
63\startitem
64    The \LUA\ library interface that is part of the \CONTEXT\ distribution which means
65    that it's officially supported.
66\stopitem
67
68\startitem
69    There is a \TEX\ module that loads the lot and provides the user interface.
70\stopitem
71
72\startitem
73    And of course, somewhere on the system, there should be the \type {mujs}
74    library. \footnote {Taco compiled the library for his system during the talk
75    and confirmed that it also works out of the box on \OSX.}
76\stopitem
77
78\startitem
79    A module like this should conform to the \CONTEXT\ \LMTX\ standards (a
80    minimalistic not bloated \API, interfacing in \LUA\ and \TEX, etc.).
81\stopitem
82
83\stopitemize
84
85\blank[2*line]
86
87In \CONTEXT\ libraries go into the platform tree, like:
88
89\starttyping
90/tex/texmf-win64/bin/lib/luametatex/mujs/libmujs.dll
91/tex/texmf-linux-64/bin/lib/luametatex/mujs/libmujs.so
92/tex/texmf-osx-64/bin/lib/luametatex/mujs/libmujs.so
93\stoptyping
94
95\stoptitle
96
97\starttitle[title=An example]
98
99\startbuffer
100\usemodule[ecmascript]
101
102\ecmacode {
103    console("");
104    console("When you see this, the loading has succeeded!");
105    console("");
106}
107
108\ecmacode {texprint("Just a {\\bf short} sentence.")}
109
110\startecmacode
111    texprint("And this is \\inframed{\\bs a bit longer} sentence.")
112\stopecmacode
113\stopbuffer
114
115\typebuffer
116
117\getbuffer
118
119\stoptitle
120
121\starttitle[title=Catcodes]
122
123As with the \LUA\ interface, catcode regimes are supported:
124
125\startbuffer
126\ecmacode {texprint(catcodes.vrb,"Just a {\\bf short} sentence.")}
127\stopbuffer
128
129\typebuffer
130
131\getbuffer
132
133Possible values are:
134
135\starttabulate
136\NC \type {tex} \NC regular \TEX\ catcode regime       \NC \NR
137\NC \type {ctx} \NC standard \CONTEXT\ catcode regime  \NC \NR
138\NC \type {vrb} \NC verbatim catcode regime            \NC \NR
139\NC \type {prt} \NC protected \CONTEXT\ catcode regime \NC \NR
140\stoptabulate
141
142\stoptitle
143
144\starttitle[title=Print whatever you want]
145
146\startbuffer
147\startecmacode
148  console("We're doing some MetaPost!");
149  texsprint(
150      "\\startMPcode "
151    + 'fill fullsquare xyscaled (6cm,1cm) withcolor "darkgray";'
152    + 'fill fullsquare xyscaled (4cm,1cm) withcolor "middlegray";'
153    + 'fill fullsquare xyscaled (2cm,1cm) withcolor "lightgray";'
154    + "\\stopMPcode "
155  );
156\stopecmacode
157\stopbuffer
158
159\typebuffer
160
161\startlinecorrection
162\getbuffer
163\stoplinecorrection
164
165Of course the code doesn't look pretty but it can serve as a step|-|up to the real
166deal: coding in \CONTEXT\ speak (or \LUA).
167
168\stoptitle
169
170\starttitle[title=Files]
171
172Because the interpreter is pretty bare, interfacing to the file system has to be
173provided but we can just use what we already have (controlled by \LUA).
174
175\startbuffer
176\startecmacode
177  var f = File("\jobname","r");
178  var l = f.read("*a");
179  f.close();
180  texprint(
181      "This file has "
182    + l.length // or: l.length.toString()
183    + " bytes!"
184  )
185\stopecmacode
186\stopbuffer
187
188\typebuffer
189
190\getbuffer
191
192We support the usual arguments, like \type {*a}, \type {*l}, a number indicating
193the bytes to read etc. There is no support for writing files (let's use the
194security excuse).
195
196\page
197
198A file with some script:
199
200\startluacode
201io.savedata("context-2020-ecmascript.js",[[
202function filesize(name) {
203    var f = File(name,"r");
204    if (f != undefined) {
205        var l = f.seek("end");
206        f.close();
207        return l;
208    } else {
209        return 0;
210    }
211}
212]])
213\stopluacode
214
215\typefile{context-2020-ecmascript.js}
216
217Loading that file:
218
219\startbuffer
220\ecmafile{context-2020-ecmascript.js}
221\stopbuffer
222
223\typebuffer \getbuffer
224
225Using that function:
226
227\startbuffer
228\ecmacode{texsprint("This file has " + filesize("\jobname.tex") + " bytes!")}
229\stopbuffer
230
231\typebuffer \getbuffer
232
233\stoptitle
234
235\starttitle[title=Ecmascript from \LUA]
236
237\startbuffer
238\startluacode
239  optional.loaded.mujs.execute [[
240    var MyMax = 10; // an example of persistence
241  ]]
242
243  optional.loaded.mujs.execute [[
244    texsprint("\\startpacked");
245    for (var i = 1; i <= MyMax; i++) {
246      texprint(
247        "Here is some rather dumb math test: "
248      + Math.sqrt(i/MyMax)
249      + "!\\par"
250      );
251    }
252    texsprint("\\stoppacked");
253  ]]
254\stopluacode
255\stopbuffer
256
257\typebuffer \page The result: \getbuffer
258
259\stoptitle
260
261\starttitle[title=So what good is it]
262
263\startitemize
264\startitem Not that much value is added compared to what we already have. \stopitem
265\startitem But at least we can say that we can do \ECMASCRIPT\ (aka \JAVASCRIPT). \stopitem
266\startitem And it might convince (new) users to use the \LUA\ interfaces instead. \stopitem
267\startitem So we pay a low price and have no overhead anyway. \stopitem
268\stopitemize
269
270\stoptitle
271
272\stopdocument
273