summaryrefslogtreecommitdiff
path: root/doc/context/presentations/context/2020/context-2020-ecmascript.tex
blob: 20877d633760762c9a372d2cc759c70f1f161281 (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
% language=us

\usemodule[present-boring,abbreviations-logos]

\startdocument
  [title={ECMASCRIPT},
   banner={just because it can be done},
   location={context\enspace {\bf 2020}\enspace meeting}]

\starttitle[title=Why oh why]

\startitemize

\startitem
    We use a \type {mupdf} based \PDF\ viewer: \type {SumatraPDF}.
\stopitem
\startitem
    And occasionally we use the tools that come with \type {mupdf}.
\stopitem
\startitem
    So when checking if that viewer supports \JAVASCRIPT\ in widgets I noticed
    the stand alone interpreter. \footnote {The official name is \ECMASCRIPT\
    which is the standardized core language.}
\stopitem
\startitem
    Which made me wonder how easy it would be to interface to it.
\stopitem

\blank

\startitem
    It uses the lightweight library subsystem: like \FFI\ the library interface
    is setup dynamically.
\stopitem
\startitem
    Support is {\em not} integrated in \LUAMETATEX, so there is no overhead and
    there are no dependencies.
\stopitem
\startitem
    We assume that the library is on the system, and when not, then there is also
    also no support.
\stopitem
\startitem
    We stick to the absolute minimum of interfacing needed and delegate
    everything else to \LUA.
\stopitem
\startitem
    We assume a stable \API, and if not, well \unknown\ sorry.
\stopitem

\stopitemize

\stoptitle

\starttitle[title=The components]

\startitemize

\startitem
    The optional, delayed loading, interface, adds only a few KB to \LUAMETATEX.
\stopitem

\startitem
    The \LUA\ library interface that is part of the \CONTEXT\ distribution which means
    that it's officially supported.
\stopitem

\startitem
    There is a \TEX\ module that loads the lot and provides the user interface.
\stopitem

\startitem
    And of course, somewhere on the system, there should be the \type {mujs}
    library. \footnote {Taco compiled the library for his system during the talk
    and confirmed that it also works out of the box on \OSX.}
\stopitem

\startitem
    A module like this should conform to the \CONTEXT\ \LMTX\ standards (a
    minimalistic not bloated \API, interfacing in \LUA\ and \TEX, etc.).
\stopitem

\stopitemize

\blank[2*line]

In \CONTEXT\ libraries go into the platform tree, like:

\starttyping
/tex/texmf-win64/bin/lib/luametatex/mujs/libmujs.dll
/tex/texmf-linux-64/bin/lib/luametatex/mujs/libmujs.so
/tex/texmf-osx-64/bin/lib/luametatex/mujs/libmujs.so
\stoptyping

\stoptitle

\starttitle[title=An example]

\startbuffer
\usemodule[ecmascript]

\ecmacode {
    console("");
    console("When you see this, the loading has succeeded!");
    console("");
}

\ecmacode {texprint("Just a {\\bf short} sentence.")}

\startecmacode
    texprint("And this is \\inframed{\\bs a bit longer} sentence.")
\stopecmacode
\stopbuffer

\typebuffer

\getbuffer

\stoptitle

\starttitle[title=Catcodes]

As with the \LUA\ interface, catcode regimes are supported:

\startbuffer
\ecmacode {texprint(catcodes.vrb,"Just a {\\bf short} sentence.")}
\stopbuffer

\typebuffer

\getbuffer

Possible values are:

\starttabulate
\NC \type {tex} \NC regular \TEX\ catcode regime       \NC \NR
\NC \type {ctx} \NC standard \CONTEXT\ catcode regime  \NC \NR
\NC \type {vrb} \NC verbatim catcode regime            \NC \NR
\NC \type {prt} \NC protected \CONTEXT\ catcode regime \NC \NR
\stoptabulate

\stoptitle

\starttitle[title=Print whatever you want]

\startbuffer
\startecmacode
  console("We're doing some MetaPost!");
  texsprint(
      "\\startMPcode "
    + 'fill fullsquare xyscaled (6cm,1cm) withcolor "darkgray";'
    + 'fill fullsquare xyscaled (4cm,1cm) withcolor "middlegray";'
    + 'fill fullsquare xyscaled (2cm,1cm) withcolor "lightgray";'
    + "\\stopMPcode "
  );
\stopecmacode
\stopbuffer

\typebuffer

\startlinecorrection
\getbuffer
\stoplinecorrection

Of course the code doesn't look pretty but it can serve as a step|-|up to the real
deal: coding in \CONTEXT\ speak (or \LUA).

\stoptitle

\starttitle[title=Files]

Because the interpreter is pretty bare, interfacing to the file system has to be
provided but we can just use what we already have (controlled by \LUA).

\startbuffer
\startecmacode
  var f = File("\jobname","r");
  var l = f.read("*a");
  f.close();
  texprint(
      "This file has "
    + l.length // or: l.length.toString()
    + " bytes!"
  )
\stopecmacode
\stopbuffer

\typebuffer

\getbuffer

We support the usual arguments, like \type {*a}, \type {*l}, a number indicating
the bytes to read etc. There is no support for writing files (let's use the
security excuse).

\page

A file with some script:

\startluacode
io.savedata("context-2020-ecmascript.js",[[
function filesize(name) {
    var f = File(name,"r");
    if (f != undefined) {
        var l = f.seek("end");
        f.close();
        return l;
    } else {
        return 0;
    }
}
]])
\stopluacode

\typefile{context-2020-ecmascript.js}

Loading that file:

\startbuffer
\ecmafile{context-2020-ecmascript.js}
\stopbuffer

\typebuffer \getbuffer

Using that function:

\startbuffer
\ecmacode{texsprint("This file has " + filesize("\jobname.tex") + " bytes!")}
\stopbuffer

\typebuffer \getbuffer

\stoptitle

\starttitle[title=Ecmascript from \LUA]

\startbuffer
\startluacode
  optional.loaded.mujs.execute [[
    var MyMax = 10; // an example of persistence
  ]]

  optional.loaded.mujs.execute [[
    texsprint("\\startpacked");
    for (var i = 1; i <= MyMax; i++) {
      texprint(
        "Here is some rather dumb math test: "
      + Math.sqrt(i/MyMax)
      + "!\\par"
      );
    }
    texsprint("\\stoppacked");
  ]]
\stopluacode
\stopbuffer

\typebuffer \page The result: \getbuffer

\stoptitle

\starttitle[title=So what good is it]

\startitemize
\startitem Not that much value is added compared to what we already have. \stopitem
\startitem But at least we can say that we can do \ECMASCRIPT\ (aka \JAVASCRIPT). \stopitem
\startitem And it might convince (new) users to use the \LUA\ interfaces instead. \stopitem
\startitem So we pay a low price and have no overhead anyway. \stopitem
\stopitemize

\stoptitle

\stopdocument