quilt-wasm 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -17
- package/package.json +7 -4
- package/quilt_runtime.js +666 -0
- package/quilt_wasm.d.ts +97 -190
- package/quilt_wasm.js +6 -672
- package/quilt_wasm_raw.d.ts +66 -0
- package/quilt_wasm_raw.js +382 -0
- package/quilt_wasm_raw_bg.wasm +0 -0
- package/quilt_wasm_bg.wasm +0 -0
package/quilt_runtime.js
ADDED
|
@@ -0,0 +1,666 @@
|
|
|
1
|
+
// The quilt-wasm runtime: the builder API expanded `.ts.quilt` programs call,
|
|
2
|
+
// as plain JavaScript (issue #306).
|
|
3
|
+
//
|
|
4
|
+
// Terms used to be wasm-bindgen wrappers around a Rust `Arc<QTerm>`, so every
|
|
5
|
+
// `tb(..)`, `.t(..)`, `.w(..)` and `.b()` crossed into wasm, allocated a
|
|
6
|
+
// wrapper and registered it with a FinalizationRegistry. A page of a few
|
|
7
|
+
// thousand terms cost ~25x what the same builder in plain JS did, and terms a
|
|
8
|
+
// program kept pinned their subtrees in wasm memory until a finalizer ran.
|
|
9
|
+
//
|
|
10
|
+
// Here a term is an ordinary object the garbage collector owns, and `coparse`
|
|
11
|
+
// is the short loop over write / newline / push / pop that Rust's
|
|
12
|
+
// `PrefixWriter` runs. The shared corpus (conformance/runtime/cases.json) pins
|
|
13
|
+
// it against the Rust and Python runtimes, and test/differential.cjs against
|
|
14
|
+
// the Rust `coparse` itself.
|
|
15
|
+
//
|
|
16
|
+
// Wasm is used only at the edges, where reimplementing would mean a second
|
|
17
|
+
// copy of Rust logic that changes with the grammars:
|
|
18
|
+
//
|
|
19
|
+
// * `qlift(term)` — the TypeScript that rebuilds a term comes from the
|
|
20
|
+
// generated tables in `quilt::langs::typescript::ops`;
|
|
21
|
+
// * `qlift(n)` for a number JavaScript would print in exponent form, where
|
|
22
|
+
// Rust's `{}` does not.
|
|
23
|
+
//
|
|
24
|
+
// `qlift(term)` sends the term across once, in the flat encoding below.
|
|
25
|
+
//
|
|
26
|
+
// This file is the source; `assemble.mjs` copies it into the wasm-pack
|
|
27
|
+
// package as `quilt_runtime.js` (CommonJS for `--target nodejs`) and writes
|
|
28
|
+
// the `quilt_wasm.js` entry that binds it to the wasm. Nothing here may run
|
|
29
|
+
// wasm at import time: in the browser the wasm exists only after `init()`.
|
|
30
|
+
|
|
31
|
+
/**************************************************************/
|
|
32
|
+
// Edges into wasm.
|
|
33
|
+
|
|
34
|
+
let wasm = null;
|
|
35
|
+
|
|
36
|
+
/** Called once by the package entry with the wasm-bindgen module. Not API. */
|
|
37
|
+
function __bindWasm(raw) {
|
|
38
|
+
wasm = raw;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function needWasm(what) {
|
|
42
|
+
if (wasm === null) throw new Error(`quilt-wasm: ${what} needs the wasm module, which is not loaded`);
|
|
43
|
+
return wasm;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**************************************************************/
|
|
47
|
+
// Commands.
|
|
48
|
+
|
|
49
|
+
const K_TERM = 0;
|
|
50
|
+
const K_NL = 1;
|
|
51
|
+
const K_POP = 2;
|
|
52
|
+
const K_PUSH = 3;
|
|
53
|
+
const K_WRITE = 4;
|
|
54
|
+
const K_HOLE = 5;
|
|
55
|
+
|
|
56
|
+
const TUPLE = 0;
|
|
57
|
+
const QUOTE = 1;
|
|
58
|
+
const UNQUOTE = 2;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A single string command: `write(s)`, `NL()`, `push(s)` or `POP()`.
|
|
62
|
+
*
|
|
63
|
+
* Inside a term only newline, push and pop are stored as these; a write is
|
|
64
|
+
* stored as the bare string, which is most of what a term holds.
|
|
65
|
+
*/
|
|
66
|
+
class WasmStrCmd {
|
|
67
|
+
constructor(k, s) {
|
|
68
|
+
this.k = k;
|
|
69
|
+
this.s = s;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** A `StrCmd` or a child placeholder (`HOLE()`), for `quote`/`unquote` cmds. */
|
|
74
|
+
class WasmCmdOrHole {
|
|
75
|
+
constructor(k, s) {
|
|
76
|
+
this.k = k;
|
|
77
|
+
this.s = s;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const NL_CMD = new WasmStrCmd(K_NL, "");
|
|
82
|
+
const POP_CMD = new WasmStrCmd(K_POP, "");
|
|
83
|
+
const HOLE_CMD = new WasmCmdOrHole(K_HOLE, "");
|
|
84
|
+
|
|
85
|
+
/**************************************************************/
|
|
86
|
+
// Terms.
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* A quilt term. Immutable once built, so it can be spliced any number of
|
|
90
|
+
* times, kept, and shared between trees.
|
|
91
|
+
*
|
|
92
|
+
* `seq` is the term's commands in order, with each child *in* its hole: a
|
|
93
|
+
* string is a write, a `WasmQTerm` is a child, anything else a `WasmStrCmd`.
|
|
94
|
+
* For a quote or unquote, `term` is the quoted child, and `seq` holds it at
|
|
95
|
+
* every `HOLE` in the cmds it was built with.
|
|
96
|
+
*/
|
|
97
|
+
class WasmQTerm {
|
|
98
|
+
constructor(k, tag, seq, index, lang, term) {
|
|
99
|
+
this.k = K_TERM;
|
|
100
|
+
this.kind = k;
|
|
101
|
+
this.tag = tag;
|
|
102
|
+
this.seq = seq;
|
|
103
|
+
this.index = index;
|
|
104
|
+
this.lang = lang;
|
|
105
|
+
this.term = term;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Serialize the term back to source code. */
|
|
109
|
+
coparse() {
|
|
110
|
+
return coparse(this);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
toString() {
|
|
114
|
+
return coparse(this);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Nothing to free: the garbage collector owns a term. Kept so code written
|
|
119
|
+
* for the wasm-bindgen classes (`t.free()`, `using t = …`) still runs.
|
|
120
|
+
*/
|
|
121
|
+
free() {}
|
|
122
|
+
|
|
123
|
+
[Symbol.dispose]() {}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* A fluent term builder. Each method appends to it and returns it, so
|
|
128
|
+
* `tb("x").w("a").t(child).b()` reads as it does in Rust.
|
|
129
|
+
*/
|
|
130
|
+
class WasmBuilder {
|
|
131
|
+
constructor(tag) {
|
|
132
|
+
this.tag = tag;
|
|
133
|
+
this.seq = [];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Splice a child term. A primitive splices as its text (see `asChild`):
|
|
138
|
+
* `↙name↘` inside a quoted comment writes the name, `↙n↘` the number.
|
|
139
|
+
*/
|
|
140
|
+
c(child) {
|
|
141
|
+
this.seq.push(asChild(child));
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Splice a child term. The same as `c` here; it once *took* the child, which
|
|
147
|
+
* mattered when a term was a wasm handle (#297), and expanded code still
|
|
148
|
+
* spells a child it has just built this way.
|
|
149
|
+
*/
|
|
150
|
+
t(child) {
|
|
151
|
+
this.seq.push(asChild(child));
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** Emit a child term (for a single term, the same as `c`). */
|
|
156
|
+
e(child) {
|
|
157
|
+
this.seq.push(asChild(child));
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Write literal source text. Like Rust's builder, an empty write is dropped. */
|
|
162
|
+
w(s) {
|
|
163
|
+
if (s !== "") this.seq.push(s);
|
|
164
|
+
return this;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Emit a newline (respecting the current prefix). */
|
|
168
|
+
n() {
|
|
169
|
+
this.seq.push(NL_CMD);
|
|
170
|
+
return this;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Push an indentation prefix. */
|
|
174
|
+
p(s) {
|
|
175
|
+
this.seq.push(new WasmStrCmd(K_PUSH, s));
|
|
176
|
+
return this;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Pop an indentation prefix. */
|
|
180
|
+
x() {
|
|
181
|
+
this.seq.push(POP_CMD);
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Build the term. */
|
|
186
|
+
b() {
|
|
187
|
+
return new WasmQTerm(TUPLE, this.tag, this.seq, 0, "", null);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
free() {}
|
|
191
|
+
|
|
192
|
+
[Symbol.dispose]() {}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function asTerm(x) {
|
|
196
|
+
if (x instanceof WasmQTerm) return x;
|
|
197
|
+
throw new TypeError(`expected a WasmQTerm, got ${describe(x)}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* A child a builder splices: a term, or a primitive written as its text — a
|
|
202
|
+
* string as itself, as a `&str` splices in Rust, and a number, boolean or
|
|
203
|
+
* bigint as `String(x)` does, as a template literal's `${x}` would. `↑(x)` is
|
|
204
|
+
* still how a value becomes a TypeScript *literal* (a string with its quotes).
|
|
205
|
+
*/
|
|
206
|
+
function asChild(x) {
|
|
207
|
+
if (x instanceof WasmQTerm) return x;
|
|
208
|
+
switch (typeof x) {
|
|
209
|
+
case "string":
|
|
210
|
+
return x;
|
|
211
|
+
case "number":
|
|
212
|
+
case "boolean":
|
|
213
|
+
case "bigint":
|
|
214
|
+
return String(x);
|
|
215
|
+
default:
|
|
216
|
+
throw new TypeError(`expected a WasmQTerm or a primitive, got ${describe(x)}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function describe(x) {
|
|
221
|
+
if (x === null) return "null";
|
|
222
|
+
if (typeof x === "object") return x.constructor?.name ?? "object";
|
|
223
|
+
return typeof x;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**************************************************************/
|
|
227
|
+
// Constructors.
|
|
228
|
+
|
|
229
|
+
/** Start building a tuple node with the given tag. */
|
|
230
|
+
function tb(tag) {
|
|
231
|
+
return new WasmBuilder(tag);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** A leaf node: a tag whose only content is `code`. */
|
|
235
|
+
function leaf(tag, code) {
|
|
236
|
+
return new WasmQTerm(TUPLE, tag, [code], 0, "", null);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A symbol: a leaf whose tag and code are the same. */
|
|
240
|
+
function sym(s) {
|
|
241
|
+
return new WasmQTerm(TUPLE, s, [s], 0, "", null);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** An identifier term (the `⟨N⟩` operator). */
|
|
245
|
+
function name(s) {
|
|
246
|
+
return new WasmQTerm(TUPLE, "identifier", [s], 0, "", null);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* A sequence of terms, for `←(items)`: each item spliced in order, with a
|
|
251
|
+
* newline between them (respecting the current prefix, so a sequence spliced
|
|
252
|
+
* into an indented block stays indented), or `sep` written between them when
|
|
253
|
+
* given — `←(args, ", ")`. Items may be terms or strings, and any iterable
|
|
254
|
+
* works. The term has no tag of its own, so it splices into whatever holds it
|
|
255
|
+
* as its items would one after another.
|
|
256
|
+
*/
|
|
257
|
+
function qseq(items, sep) {
|
|
258
|
+
const seq = [];
|
|
259
|
+
let first = true;
|
|
260
|
+
for (const item of items) {
|
|
261
|
+
if (!first) seq.push(sep === undefined ? NL_CMD : String(sep));
|
|
262
|
+
first = false;
|
|
263
|
+
seq.push(asChild(item));
|
|
264
|
+
}
|
|
265
|
+
return new WasmQTerm(TUPLE, "", seq, 0, "", null);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** A quoted fragment. */
|
|
269
|
+
function quote(tag, index, lang, term, cmds) {
|
|
270
|
+
return quoted(QUOTE, tag, index, lang, term, cmds);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** An unquoted splice. */
|
|
274
|
+
function unquote(tag, index, lang, term, cmds) {
|
|
275
|
+
return quoted(UNQUOTE, tag, index, lang, term, cmds);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function quoted(kind, tag, index, lang, term, cmds) {
|
|
279
|
+
asTerm(term);
|
|
280
|
+
const seq = [];
|
|
281
|
+
for (const c of cmds) {
|
|
282
|
+
if (!(c instanceof WasmStrCmd || c instanceof WasmCmdOrHole)) {
|
|
283
|
+
throw new TypeError(`expected a cmd(..) or HOLE(), got ${describe(c)}`);
|
|
284
|
+
}
|
|
285
|
+
if (c.k === K_HOLE) seq.push(term);
|
|
286
|
+
else if (c.k === K_WRITE) seq.push(c.s);
|
|
287
|
+
else seq.push(c);
|
|
288
|
+
}
|
|
289
|
+
// wasm-bindgen passed `index` as a u8.
|
|
290
|
+
return new WasmQTerm(kind, tag, seq, (index | 0) & 0xff, lang, term);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Wrap a `StrCmd` as a `CmdOrHole`. */
|
|
294
|
+
function cmd(c) {
|
|
295
|
+
if (c instanceof WasmStrCmd) return c;
|
|
296
|
+
throw new TypeError(`expected a WasmStrCmd, got ${describe(c)}`);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** A `Write` command. */
|
|
300
|
+
function write(s) {
|
|
301
|
+
return new WasmStrCmd(K_WRITE, s);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** A `Push` command. */
|
|
305
|
+
function push(s) {
|
|
306
|
+
return new WasmStrCmd(K_PUSH, s);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* The `NewLine` command. A function, like `POP()` and `HOLE()`, because the
|
|
311
|
+
* wasm-bindgen runtime could not export a constant (#167); generated code
|
|
312
|
+
* calls it, so it stays one.
|
|
313
|
+
*/
|
|
314
|
+
function NL() {
|
|
315
|
+
return NL_CMD;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** The `Pop` command. */
|
|
319
|
+
function POP() {
|
|
320
|
+
return POP_CMD;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** A child placeholder. */
|
|
324
|
+
function HOLE() {
|
|
325
|
+
return HOLE_CMD;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* A `⟨str⟩` quote's value is a plain string, and code written for terms calls
|
|
330
|
+
* `.coparse()` on what a quote returns. So a string's `coparse()` is itself:
|
|
331
|
+
* switching a quote between representations leaves its callers alone. Added
|
|
332
|
+
* once, non-enumerable, and never over a `coparse` someone else defined.
|
|
333
|
+
*/
|
|
334
|
+
if (!Object.prototype.hasOwnProperty.call(String.prototype, "coparse")) {
|
|
335
|
+
Object.defineProperty(String.prototype, "coparse", {
|
|
336
|
+
value: function coparseString() {
|
|
337
|
+
return String(this);
|
|
338
|
+
},
|
|
339
|
+
writable: true,
|
|
340
|
+
configurable: true,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**************************************************************/
|
|
345
|
+
// Coparse.
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Write `t` as Rust's `PrefixWriter` does: a newline writes `\n` and then
|
|
349
|
+
* every pushed prefix, and popping an empty stack does nothing. (Escaping and
|
|
350
|
+
* bracketing, the writer's other state, only matter to `coparse_quilt`.)
|
|
351
|
+
*/
|
|
352
|
+
function coparse(t) {
|
|
353
|
+
out = "";
|
|
354
|
+
prefix = "";
|
|
355
|
+
stack = [];
|
|
356
|
+
writeTerm(t);
|
|
357
|
+
// Rust receives every string as UTF-8, so a lone surrogate came back as
|
|
358
|
+
// U+FFFD; keep that.
|
|
359
|
+
const s = out;
|
|
360
|
+
out = "";
|
|
361
|
+
return typeof s.toWellFormed === "function" ? s.toWellFormed() : s;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// The writer's state. Module-level rather than an object passed down, which
|
|
365
|
+
// is measurably faster; `coparse` calls nothing it could re-enter through.
|
|
366
|
+
let out = "";
|
|
367
|
+
let prefix = "";
|
|
368
|
+
let stack = [];
|
|
369
|
+
|
|
370
|
+
function writeTerm(t) {
|
|
371
|
+
const seq = t.seq;
|
|
372
|
+
for (let i = 0; i < seq.length; i++) {
|
|
373
|
+
const x = seq[i];
|
|
374
|
+
if (typeof x === "string") {
|
|
375
|
+
out += x;
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
switch (x.k) {
|
|
379
|
+
case K_TERM:
|
|
380
|
+
writeTerm(x);
|
|
381
|
+
break;
|
|
382
|
+
case K_NL:
|
|
383
|
+
out += "\n" + prefix;
|
|
384
|
+
break;
|
|
385
|
+
case K_PUSH:
|
|
386
|
+
stack.push(prefix);
|
|
387
|
+
prefix += x.s;
|
|
388
|
+
break;
|
|
389
|
+
case K_POP:
|
|
390
|
+
if (stack.length > 0) prefix = stack.pop();
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**************************************************************/
|
|
397
|
+
// Lifts.
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Lift a JS value to a term that reconstructs it (the homogeneous `↑`,
|
|
401
|
+
* TypeScript into TypeScript): `number`, `string`, `boolean`, and a term,
|
|
402
|
+
* which lifts to the TypeScript that rebuilds it, so `↓(↑(x)) == x` (#166).
|
|
403
|
+
*/
|
|
404
|
+
function qlift(value) {
|
|
405
|
+
if (value instanceof WasmQTerm) return liftTerm(value);
|
|
406
|
+
if (typeof value === "boolean") {
|
|
407
|
+
const s = value ? "true" : "false";
|
|
408
|
+
return leaf(s, s);
|
|
409
|
+
}
|
|
410
|
+
if (typeof value === "number") return leaf("number", fmtNumber(value));
|
|
411
|
+
if (typeof value === "string") return leaf("string", tsStringLit(value));
|
|
412
|
+
throw new Error("qlift: unsupported type (expected number, string, boolean, or QTerm)");
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* `qlift` inside a `⟨str⟩` quote: the TypeScript literal for `value` as a
|
|
417
|
+
* string rather than a term — what `${…}` splices. A safe integer, the common
|
|
418
|
+
* case in generated code, goes straight through `String`; everything else is
|
|
419
|
+
* spelled as `qlift` spells it, so the two representations agree.
|
|
420
|
+
*/
|
|
421
|
+
function qlift_str(value) {
|
|
422
|
+
if (typeof value === "number" && Number.isSafeInteger(value)) return String(value);
|
|
423
|
+
if (typeof value === "string") return tsStringLit(value);
|
|
424
|
+
return coparse(qlift(value));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Lift a JS value to an HTML term (the `↑` operator with an `html` splice
|
|
429
|
+
* target). Strings become entity-escaped `text` leaves; a term passes through
|
|
430
|
+
* unchanged, since an HTML fragment is already escaped.
|
|
431
|
+
*/
|
|
432
|
+
function qlift_html(value) {
|
|
433
|
+
if (value instanceof WasmQTerm) return value;
|
|
434
|
+
if (typeof value === "string") return leaf("text", escapeHtml(value));
|
|
435
|
+
if (typeof value === "boolean") return leaf("text", value ? "true" : "false");
|
|
436
|
+
if (typeof value === "number") return leaf("text", fmtNumber(value));
|
|
437
|
+
throw new Error("qlift_html: unsupported type (expected number, string, boolean, or QTerm)");
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Lift a JS value into a text quote (the `↑` operator with a `text` splice
|
|
442
|
+
* target), in the rules of `quilt::lift::text`: by default JSON's spelling —
|
|
443
|
+
* a string as a `"…"` literal, a number as its digits, a boolean as
|
|
444
|
+
* `true`/`false`, an array as `[a, b]`, and a term as the text it stands for.
|
|
445
|
+
*
|
|
446
|
+
* `dialect` overrides any of `string(s)`, `in_quotes(s)`, `boolean(b)`,
|
|
447
|
+
* `float(f)` (functions returning a string) and `list_open`, `list_sep`,
|
|
448
|
+
* `list_close` (strings), the names the Python runtime's `TextDialect` has;
|
|
449
|
+
* what it leaves out stays JSON's. A number with no fractional part is an
|
|
450
|
+
* integer here, as in `qlift`: JS has one number type, so `1.0` is `1`.
|
|
451
|
+
*/
|
|
452
|
+
function qlift_text(value, dialect) {
|
|
453
|
+
return leaf("text", renderText(value, dialect ?? null, false));
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* `qlift_text` for a hole between two `"` the author wrote: a string renders
|
|
458
|
+
* as its escaped contents, with no quotes of its own, and an array as its
|
|
459
|
+
* elements joined by the separator.
|
|
460
|
+
*/
|
|
461
|
+
function qlift_text_in_quotes(value, dialect) {
|
|
462
|
+
return leaf("text", renderText(value, dialect ?? null, true));
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
function dialectRule(dialect, rule, arg) {
|
|
466
|
+
const f = dialect?.[rule];
|
|
467
|
+
if (typeof f !== "function") return null;
|
|
468
|
+
const out = f.call(dialect, arg);
|
|
469
|
+
if (typeof out !== "string") throw new TypeError(`qlift_text: dialect.${rule} must return a string`);
|
|
470
|
+
return out;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
function dialectStr(dialect, key, fallback) {
|
|
474
|
+
const v = dialect?.[key];
|
|
475
|
+
return typeof v === "string" ? v : fallback;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function renderText(value, dialect, inQuotes) {
|
|
479
|
+
if (value instanceof WasmQTerm) return coparse(value);
|
|
480
|
+
if (typeof value === "boolean") return dialectRule(dialect, "boolean", value) ?? String(value);
|
|
481
|
+
if (typeof value === "number") {
|
|
482
|
+
if (Number.isInteger(value) && Math.abs(value) < 9007199254740992) return String(value);
|
|
483
|
+
const custom = dialectRule(dialect, "float", value);
|
|
484
|
+
if (custom !== null) return custom;
|
|
485
|
+
if (!Number.isFinite(value)) {
|
|
486
|
+
throw new Error(
|
|
487
|
+
`cannot lift ${value} into text: JSON has no spelling for NaN or infinity ` +
|
|
488
|
+
"(give the dialect a `float` rule that has one)",
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
return fmtNumber(value);
|
|
492
|
+
}
|
|
493
|
+
if (typeof value === "string") {
|
|
494
|
+
const custom = dialectRule(dialect, inQuotes ? "in_quotes" : "string", value);
|
|
495
|
+
if (custom !== null) return custom;
|
|
496
|
+
// `quilt::lift::json_escape` is `ts_dquote_escape`'s table.
|
|
497
|
+
const lit = tsStringLit(value);
|
|
498
|
+
return inQuotes ? lit.slice(1, -1) : lit;
|
|
499
|
+
}
|
|
500
|
+
if (Array.isArray(value)) {
|
|
501
|
+
const body = value.map((x) => renderText(x, dialect, inQuotes)).join(dialectStr(dialect, "list_sep", ", "));
|
|
502
|
+
return inQuotes ? body : dialectStr(dialect, "list_open", "[") + body + dialectStr(dialect, "list_close", "]");
|
|
503
|
+
}
|
|
504
|
+
throw new Error("qlift_text: unsupported type (expected number, string, boolean, array, or QTerm)");
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// `quilt::lift::escape_html`.
|
|
508
|
+
const HTML_ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
|
|
509
|
+
const HTML_SPECIAL = /[&<>"']/;
|
|
510
|
+
const HTML_SPECIALS = /[&<>"']/g;
|
|
511
|
+
const htmlEscape = (c) => HTML_ESCAPES[c];
|
|
512
|
+
|
|
513
|
+
function escapeHtml(s) {
|
|
514
|
+
return HTML_SPECIAL.test(s) ? s.replace(HTML_SPECIALS, htmlEscape) : s;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Format a number as the wasm runtime's `fmt_number` does: an integral value
|
|
519
|
+
* below 2^53 as an integer, anything else as Rust's `{}` prints an f64. In
|
|
520
|
+
* [1e-6, 1e21) JavaScript prints the same shortest round-trip digits Rust
|
|
521
|
+
* does; outside it JavaScript switches to exponent form, so ask Rust.
|
|
522
|
+
*/
|
|
523
|
+
function fmtNumber(n) {
|
|
524
|
+
if (Number.isInteger(n) && Math.abs(n) < 9007199254740992) return String(n);
|
|
525
|
+
const a = Math.abs(n);
|
|
526
|
+
if (a >= 1e-6 && a < 1e21) return String(n);
|
|
527
|
+
return needWasm("qlift of this number").fmt_number(n);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// `str_lit` in `quilt::langs::typescript::ops`: a double-quoted literal with
|
|
531
|
+
// `"` and `\` escaped and control characters (Unicode Cc) spelled out.
|
|
532
|
+
const TS_SPECIAL = /["\\\u0000-\u001f\u007f-\u009f]/;
|
|
533
|
+
|
|
534
|
+
function tsStringLit(s) {
|
|
535
|
+
if (!TS_SPECIAL.test(s)) return `"${s}"`;
|
|
536
|
+
let out = '"';
|
|
537
|
+
for (const c of s) {
|
|
538
|
+
const code = c.codePointAt(0);
|
|
539
|
+
if (c === '"' || c === "\\") out += "\\" + c;
|
|
540
|
+
else if (c === "\n") out += "\\n";
|
|
541
|
+
else if (c === "\r") out += "\\r";
|
|
542
|
+
else if (c === "\t") out += "\\t";
|
|
543
|
+
else if (code <= 0x1f || (code >= 0x7f && code <= 0x9f)) out += "\\u" + code.toString(16).padStart(4, "0");
|
|
544
|
+
else out += c;
|
|
545
|
+
}
|
|
546
|
+
return out + '"';
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**************************************************************/
|
|
550
|
+
// The flat encoding a term crosses into wasm in, and comes back in.
|
|
551
|
+
//
|
|
552
|
+
// Pre-order, as u32 ops plus a string table:
|
|
553
|
+
//
|
|
554
|
+
// tuple: 0 tag n cmd*n cmd: 0 s write
|
|
555
|
+
// quote: 1 tag index lang term n cmd*n 1 newline
|
|
556
|
+
// unquote: 2 tag index lang term n cmd*n 2 s push
|
|
557
|
+
// 3 pop
|
|
558
|
+
// 4 [child] hole
|
|
559
|
+
//
|
|
560
|
+
// A tuple's hole is followed by its child; a quote's is not, its child being
|
|
561
|
+
// `term`. Mirrored by `decode`/`encode` in src/lib.rs.
|
|
562
|
+
|
|
563
|
+
const OP_WRITE = 0;
|
|
564
|
+
const OP_NL = 1;
|
|
565
|
+
const OP_PUSH = 2;
|
|
566
|
+
const OP_POP = 3;
|
|
567
|
+
const OP_HOLE = 4;
|
|
568
|
+
|
|
569
|
+
function encode(t) {
|
|
570
|
+
const ops = [];
|
|
571
|
+
const strings = [];
|
|
572
|
+
const str = (s) => strings.push(String(s)) - 1;
|
|
573
|
+
const node = (t) => {
|
|
574
|
+
const quoted = t.kind !== TUPLE;
|
|
575
|
+
ops.push(t.kind, str(t.tag));
|
|
576
|
+
if (quoted) {
|
|
577
|
+
ops.push(t.index, str(t.lang));
|
|
578
|
+
node(t.term);
|
|
579
|
+
}
|
|
580
|
+
ops.push(t.seq.length);
|
|
581
|
+
for (const x of t.seq) {
|
|
582
|
+
if (typeof x === "string") ops.push(OP_WRITE, str(x));
|
|
583
|
+
else if (x.k === K_TERM) {
|
|
584
|
+
ops.push(OP_HOLE);
|
|
585
|
+
if (!quoted) node(x);
|
|
586
|
+
} else if (x.k === K_NL) ops.push(OP_NL);
|
|
587
|
+
else if (x.k === K_POP) ops.push(OP_POP);
|
|
588
|
+
else ops.push(OP_PUSH, str(x.s));
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
node(t);
|
|
592
|
+
return [Uint32Array.from(ops), strings];
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
function decode(ops, strings) {
|
|
596
|
+
let i = 0;
|
|
597
|
+
const node = () => {
|
|
598
|
+
const kind = ops[i++];
|
|
599
|
+
const tag = strings[ops[i++]];
|
|
600
|
+
let index = 0;
|
|
601
|
+
let lang = "";
|
|
602
|
+
let term = null;
|
|
603
|
+
if (kind !== TUPLE) {
|
|
604
|
+
index = ops[i++];
|
|
605
|
+
lang = strings[ops[i++]];
|
|
606
|
+
term = node();
|
|
607
|
+
}
|
|
608
|
+
const n = ops[i++];
|
|
609
|
+
const seq = new Array(n);
|
|
610
|
+
for (let j = 0; j < n; j++) {
|
|
611
|
+
const op = ops[i++];
|
|
612
|
+
if (op === OP_WRITE) seq[j] = strings[ops[i++]];
|
|
613
|
+
else if (op === OP_NL) seq[j] = NL_CMD;
|
|
614
|
+
else if (op === OP_POP) seq[j] = POP_CMD;
|
|
615
|
+
else if (op === OP_PUSH) seq[j] = new WasmStrCmd(K_PUSH, strings[ops[i++]]);
|
|
616
|
+
else seq[j] = kind === TUPLE ? node() : term;
|
|
617
|
+
}
|
|
618
|
+
return new WasmQTerm(kind, tag, seq, index, lang, term);
|
|
619
|
+
};
|
|
620
|
+
return node();
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
function liftTerm(t) {
|
|
624
|
+
const [ops, strings] = encode(t);
|
|
625
|
+
const [outOps, outStrings] = needWasm("qlift of a term").lift(ops, strings);
|
|
626
|
+
return decode(outOps, outStrings);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
/**
|
|
630
|
+
* `term.coparse()` done by the Rust `QTerm` instead: one crossing for the
|
|
631
|
+
* whole tree. For tests and the benchmark, which hold the two to the same
|
|
632
|
+
* output. Not API.
|
|
633
|
+
*/
|
|
634
|
+
function __coparseWasm(t) {
|
|
635
|
+
const [ops, strings] = encode(asTerm(t));
|
|
636
|
+
return needWasm("__coparseWasm").coparse(ops, strings);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/**************************************************************/
|
|
640
|
+
|
|
641
|
+
export {
|
|
642
|
+
HOLE,
|
|
643
|
+
NL,
|
|
644
|
+
POP,
|
|
645
|
+
WasmBuilder,
|
|
646
|
+
WasmCmdOrHole,
|
|
647
|
+
WasmQTerm,
|
|
648
|
+
WasmStrCmd,
|
|
649
|
+
__bindWasm,
|
|
650
|
+
__coparseWasm,
|
|
651
|
+
cmd,
|
|
652
|
+
leaf,
|
|
653
|
+
name,
|
|
654
|
+
push,
|
|
655
|
+
qlift,
|
|
656
|
+
qlift_html,
|
|
657
|
+
qlift_text,
|
|
658
|
+
qlift_str,
|
|
659
|
+
qlift_text_in_quotes,
|
|
660
|
+
qseq,
|
|
661
|
+
quote,
|
|
662
|
+
sym,
|
|
663
|
+
tb,
|
|
664
|
+
unquote,
|
|
665
|
+
write,
|
|
666
|
+
};
|