@weft-js/plugin-surface-keel 0.1.0 → 0.1.3
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 +312 -3
- package/package.json +12 -3
- package/src/index.d.ts +5 -1
- package/src/index.mjs +20 -5
- package/src/runtime.mjs +6 -2
- package/vendor/README.md +7 -4
- package/vendor/keel-render/core-primitives.mjs +478 -0
- package/vendor/keel-render/effects-world.mjs +439 -0
- package/vendor/keel-render/indexed-render.mjs +516 -0
- package/vendor/keel-render/pixel-raster.mjs +512 -0
- package/vendor/keel-render/render-passes.mjs +530 -0
- package/vendor/keel-render/renderer-api.mjs +121 -0
- package/vendor/keel-render.mjs +1 -2268
- package/vendor/keel-theme/codec-runtime.mjs +549 -0
- package/vendor/keel-theme/collection-codecs.mjs +360 -0
- package/vendor/keel-theme/color-font.mjs +471 -0
- package/vendor/keel-theme/glyph-theme.mjs +529 -0
- package/vendor/keel-theme/icon-schema.mjs +540 -0
- package/vendor/keel-theme/scalar-codecs.mjs +535 -0
- package/vendor/keel-theme/schema-codec.mjs +515 -0
- package/vendor/keel-theme/schema-theme.mjs +533 -0
- package/vendor/keel-theme.mjs +1 -3571
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import './scalar-codecs.mjs';
|
|
2
|
+
import {
|
|
3
|
+
SchemaError,
|
|
4
|
+
bitsFor,
|
|
5
|
+
} from './icon-schema.mjs';
|
|
6
|
+
import {
|
|
7
|
+
CodecError,
|
|
8
|
+
SCRATCH,
|
|
9
|
+
categoryOf,
|
|
10
|
+
describe,
|
|
11
|
+
fail,
|
|
12
|
+
gridOf,
|
|
13
|
+
meta,
|
|
14
|
+
put,
|
|
15
|
+
rd,
|
|
16
|
+
same,
|
|
17
|
+
under,
|
|
18
|
+
unzig,
|
|
19
|
+
wr,
|
|
20
|
+
zig,
|
|
21
|
+
} from './schema-codec.mjs';
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function compileUnion(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
25
|
+
const variants = n.variants.map((f) => {
|
|
26
|
+
const s = f.type;
|
|
27
|
+
return { name: f.name, c: structCodec(s.fields, s.open, s.ext, recs, s, n.tag, compile) };
|
|
28
|
+
});
|
|
29
|
+
const index = new Map(variants.map((v, k) => [v.name, k]));
|
|
30
|
+
const width = bitsFor(n.capacity || variants.length);
|
|
31
|
+
return {
|
|
32
|
+
node: n,
|
|
33
|
+
w(o, v, x) {
|
|
34
|
+
if (typeof v !== "object" || v === null) fail(`${describe(v)} is not an object (a union by "${n.tag}").`);
|
|
35
|
+
const tag = v[n.tag];
|
|
36
|
+
const k = typeof tag === "string" ? index.get(tag) : void 0;
|
|
37
|
+
if (k === void 0) fail(`${n.tag} ${describe(tag)} is not one of ${variants.map((x2) => x2.name).join(", ")}.`);
|
|
38
|
+
o.bits(k, width);
|
|
39
|
+
wr(variants[k].c, o, v, x, `${n.tag}(${String(tag)})`);
|
|
40
|
+
},
|
|
41
|
+
r(i, x) {
|
|
42
|
+
const s = i.at;
|
|
43
|
+
const k = i.bits(width);
|
|
44
|
+
const vt = variants[k];
|
|
45
|
+
if (!vt) fail(`Union tag ${k} is not a variant this reader knows (it knows ${variants.length}: data from a newer schema?).`);
|
|
46
|
+
if (x.trace) meta(x, n.tag, s, i, vt.name, `variant ${k}: ${vt.name}`);
|
|
47
|
+
const body = rd(vt.c, i, x, `${n.tag}(${vt.name})`);
|
|
48
|
+
const out = { [n.tag]: vt.name };
|
|
49
|
+
for (const key in body) put(out, key, body[key]);
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function compileAlt(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
56
|
+
const branches = n.of.map((b) => ({ cat: categoryOf(b), c: compile(b, recs) }));
|
|
57
|
+
const width = bitsFor(branches.length);
|
|
58
|
+
return {
|
|
59
|
+
node: n,
|
|
60
|
+
w(o, v, x) {
|
|
61
|
+
const cat = valueCategory(v);
|
|
62
|
+
const k = branches.findIndex((b) => b.cat === cat);
|
|
63
|
+
if (k < 0) fail(`${describe(v)} (${cat}) matches none of ${branches.map((b) => b.cat).join(", ")}.`);
|
|
64
|
+
o.bits(k, width);
|
|
65
|
+
wr(branches[k].c, o, v, x, `(${cat})`);
|
|
66
|
+
},
|
|
67
|
+
r(i, x) {
|
|
68
|
+
const s = i.at;
|
|
69
|
+
const k = i.bits(width);
|
|
70
|
+
const b = branches[k];
|
|
71
|
+
if (!b) fail(`Alt branch ${k} doesn't exist.`);
|
|
72
|
+
if (x.trace) meta(x, "branch", s, i, b.cat, `branch ${k}: ${b.cat}`);
|
|
73
|
+
return rd(b.c, i, x, `(${b.cat})`);
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function compileMap(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
79
|
+
const kc = compile(n.key, recs);
|
|
80
|
+
const vc = compile(n.value, recs);
|
|
81
|
+
return {
|
|
82
|
+
node: n,
|
|
83
|
+
w(o, v, x) {
|
|
84
|
+
if (typeof v !== "object" || v === null || Array.isArray(v)) fail(`${describe(v)} is not a record.`);
|
|
85
|
+
const rec = v;
|
|
86
|
+
const keys = Object.keys(rec).filter((k) => rec[k] !== void 0);
|
|
87
|
+
if (n.order === "sorted") keys.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
88
|
+
o.golomb(keys.length, 0);
|
|
89
|
+
for (const k of keys) {
|
|
90
|
+
wr(kc, o, k, x, `${k}(key)`);
|
|
91
|
+
wr(vc, o, rec[k], x, k);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
r(i, x) {
|
|
95
|
+
const s = i.at;
|
|
96
|
+
const count = i.golomb(0);
|
|
97
|
+
if (x.trace) meta(x, "count", s, i, count, `${count} entries`);
|
|
98
|
+
const out = {};
|
|
99
|
+
let last = null;
|
|
100
|
+
for (let k = 0; k < count; k += 1) {
|
|
101
|
+
const key = rd(kc, i, x, `#${k}(key)`);
|
|
102
|
+
if (Object.hasOwn(out, key)) fail(`The key ${JSON.stringify(key)} is in the map twice.`);
|
|
103
|
+
if (n.order === "sorted" && last !== null && !(last < key)) fail(`Map keys out of order (${JSON.stringify(last)} before ${JSON.stringify(key)}): not canonical.`);
|
|
104
|
+
last = key;
|
|
105
|
+
put(out, key, rd(vc, i, x, key));
|
|
106
|
+
}
|
|
107
|
+
return out;
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function compileDelta(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
113
|
+
const first = compile(n.of, recs);
|
|
114
|
+
const of = n.of;
|
|
115
|
+
const g = of.kind === "fixed" ? gridOf(of) : null;
|
|
116
|
+
const toUnits = g ? (v) => g.units(v) : (v) => v;
|
|
117
|
+
const fromUnits = g ? (k) => {
|
|
118
|
+
const v = g.value(k);
|
|
119
|
+
return v === 0 ? 0 : v;
|
|
120
|
+
} : (k) => k;
|
|
121
|
+
const lo = g ? g.lo : of.kind === "uint" || of.kind === "varuint" ? 0 : -Infinity;
|
|
122
|
+
const hi = g ? g.hi : of.kind === "uint" ? 2 ** of.bits - 1 : of.kind === "int" ? 2 ** (of.bits - 1) - 1 : Infinity;
|
|
123
|
+
const lo2 = of.kind === "int" ? -(2 ** (of.bits - 1)) : lo;
|
|
124
|
+
return {
|
|
125
|
+
node: n,
|
|
126
|
+
w(o, v, x) {
|
|
127
|
+
if (!Array.isArray(v) && !ArrayBuffer.isView(v)) fail(`${describe(v)} is not an array.`);
|
|
128
|
+
const a = v;
|
|
129
|
+
o.golomb(a.length, 0);
|
|
130
|
+
if (!a.length) return;
|
|
131
|
+
wr(first, o, a[0], x, 0);
|
|
132
|
+
let prev = toUnits(a[0]);
|
|
133
|
+
for (let k = 1; k < a.length; k += 1) {
|
|
134
|
+
const e = a[k];
|
|
135
|
+
SCRATCH.reset();
|
|
136
|
+
try {
|
|
137
|
+
first.w(SCRATCH, e, x);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
throw under(err, k, null);
|
|
140
|
+
}
|
|
141
|
+
const u2 = toUnits(e);
|
|
142
|
+
o.golomb(zig(u2 - prev), n.k);
|
|
143
|
+
prev = u2;
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
r(i, x) {
|
|
147
|
+
const s = i.at;
|
|
148
|
+
const len2 = i.golomb(0);
|
|
149
|
+
if (x.trace) meta(x, "length", s, i, len2, `${len2} items`);
|
|
150
|
+
const out = new Array(len2);
|
|
151
|
+
if (!len2) return out;
|
|
152
|
+
out[0] = rd(first, i, x, 0);
|
|
153
|
+
let prev = toUnits(out[0]);
|
|
154
|
+
for (let k = 1; k < len2; k += 1) {
|
|
155
|
+
const s1 = i.at;
|
|
156
|
+
const d = unzig(i.golomb(n.k));
|
|
157
|
+
prev += d;
|
|
158
|
+
if (prev < lo2 || prev > hi) throw under(new CodecError(`A delta walks out of range (${prev}).`), k, s1);
|
|
159
|
+
out[k] = fromUnits(prev);
|
|
160
|
+
if (x.trace) meta(x, `[${k}]`, s1, i, out[k], `${d >= 0 ? "+" : ""}${d}`);
|
|
161
|
+
}
|
|
162
|
+
return out;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function compileRuns(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
168
|
+
const of = compile(n.of, recs);
|
|
169
|
+
return {
|
|
170
|
+
node: n,
|
|
171
|
+
w(o, v, x) {
|
|
172
|
+
if (!Array.isArray(v) && !ArrayBuffer.isView(v)) fail(`${describe(v)} is not an array.`);
|
|
173
|
+
const a = v;
|
|
174
|
+
o.golomb(a.length, 0);
|
|
175
|
+
let k = 0;
|
|
176
|
+
while (k < a.length) {
|
|
177
|
+
let j = k + 1;
|
|
178
|
+
while (j < a.length && same(a[j], a[k])) j += 1;
|
|
179
|
+
wr(of, o, a[k], x, k);
|
|
180
|
+
o.golomb(j - k - 1, 0);
|
|
181
|
+
k = j;
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
r(i, x) {
|
|
185
|
+
const s = i.at;
|
|
186
|
+
const len2 = i.golomb(0);
|
|
187
|
+
if (x.trace) meta(x, "length", s, i, len2, `${len2} items`);
|
|
188
|
+
const out = new Array(len2);
|
|
189
|
+
let k = 0;
|
|
190
|
+
let prev = void 0;
|
|
191
|
+
while (k < len2) {
|
|
192
|
+
const v = rd(of, i, x, k);
|
|
193
|
+
if (k > 0 && same(v, prev)) fail(`Two runs of the same value at ${k}: not canonical.`);
|
|
194
|
+
const s1 = i.at;
|
|
195
|
+
const run = i.golomb(0) + 1;
|
|
196
|
+
if (x.trace) meta(x, "run", s1, i, run, `x${run}`);
|
|
197
|
+
if (k + run > len2) fail(`A run of ${run} overruns the ${len2} items.`);
|
|
198
|
+
for (let j = 0; j < run; j += 1) out[k + j] = j === 0 ? v : structuredClone(v);
|
|
199
|
+
k += run;
|
|
200
|
+
prev = v;
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function lzWriter(of, MIN, WINDOW, CHAIN) {
|
|
208
|
+
return function w(o, v, x) {
|
|
209
|
+
if (!Array.isArray(v) && !ArrayBuffer.isView(v)) fail(`${describe(v)} is not an array.`);
|
|
210
|
+
const a = v;
|
|
211
|
+
const L = a.length;
|
|
212
|
+
o.golomb(L, 0);
|
|
213
|
+
const heads = /* @__PURE__ */ new Map();
|
|
214
|
+
const keyAt = (p) => {
|
|
215
|
+
let key = "";
|
|
216
|
+
for (let q = 0; q < MIN; q += 1) key += `${typeof a[p + q] === "string" ? "s" : ""}${String(a[p + q])}\0`;
|
|
217
|
+
return key;
|
|
218
|
+
};
|
|
219
|
+
const insert = (p) => {
|
|
220
|
+
if (p + MIN > L) return;
|
|
221
|
+
const key = keyAt(p);
|
|
222
|
+
let c = heads.get(key);
|
|
223
|
+
if (!c) {
|
|
224
|
+
c = [];
|
|
225
|
+
heads.set(key, c);
|
|
226
|
+
}
|
|
227
|
+
c.push(p);
|
|
228
|
+
if (c.length > CHAIN * 2) c.splice(0, c.length - CHAIN);
|
|
229
|
+
};
|
|
230
|
+
let i = 0;
|
|
231
|
+
while (i < L) {
|
|
232
|
+
let bestLen = 0, bestDist = 0;
|
|
233
|
+
if (i + MIN <= L) {
|
|
234
|
+
const c = heads.get(keyAt(i));
|
|
235
|
+
if (c) for (let q = c.length - 1, tries = 0; q >= 0 && tries < CHAIN; q -= 1, tries += 1) {
|
|
236
|
+
const j = c[q];
|
|
237
|
+
if (i - j > WINDOW) break;
|
|
238
|
+
let len2 = 0;
|
|
239
|
+
while (i + len2 < L && a[j + len2] === a[i + len2]) len2 += 1;
|
|
240
|
+
if (len2 > bestLen) {
|
|
241
|
+
bestLen = len2;
|
|
242
|
+
bestDist = i - j;
|
|
243
|
+
if (i + len2 === L) break;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
if (bestLen >= MIN) {
|
|
248
|
+
o.bool(true);
|
|
249
|
+
o.golomb(bestDist - 1, 0);
|
|
250
|
+
o.golomb(bestLen - MIN, 0);
|
|
251
|
+
for (let q = 0; q < bestLen; q += 1) insert(i + q);
|
|
252
|
+
i += bestLen;
|
|
253
|
+
} else {
|
|
254
|
+
o.bool(false);
|
|
255
|
+
wr(of, o, a[i], x, i);
|
|
256
|
+
insert(i);
|
|
257
|
+
i += 1;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function compileLz(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
264
|
+
const of = compile(n.of, recs);
|
|
265
|
+
const MIN = n.min;
|
|
266
|
+
const WINDOW = 1 << 16;
|
|
267
|
+
const CHAIN = 48;
|
|
268
|
+
return {
|
|
269
|
+
node: n,
|
|
270
|
+
w: lzWriter(of, MIN, WINDOW, CHAIN),
|
|
271
|
+
r(i, x) {
|
|
272
|
+
const s = i.at;
|
|
273
|
+
const L = i.golomb(0);
|
|
274
|
+
if (x.trace) meta(x, "length", s, i, L, `${L} items`);
|
|
275
|
+
const out = new Array(L);
|
|
276
|
+
let k = 0;
|
|
277
|
+
while (k < L) {
|
|
278
|
+
const s1 = i.at;
|
|
279
|
+
if (i.bool()) {
|
|
280
|
+
const dist = i.golomb(0) + 1;
|
|
281
|
+
const len2 = i.golomb(0) + MIN;
|
|
282
|
+
if (x.trace) meta(x, "copy", s1, i, len2, `${len2} items from ${dist} back`);
|
|
283
|
+
if (dist > k) fail(`A copy from ${dist} back at item ${k} reaches before the start.`);
|
|
284
|
+
if (k + len2 > L) fail(`A copy of ${len2} at item ${k} overruns the ${L} items.`);
|
|
285
|
+
for (let q = 0; q < len2; q += 1) out[k + q] = out[k + q - dist];
|
|
286
|
+
k += len2;
|
|
287
|
+
} else {
|
|
288
|
+
if (x.trace) meta(x, "literal", s1, i, null, "a literal item");
|
|
289
|
+
out[k] = rd(of, i, x, k);
|
|
290
|
+
k += 1;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function compilePlanes(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
299
|
+
const lim = 2 ** n.bits;
|
|
300
|
+
return {
|
|
301
|
+
node: n,
|
|
302
|
+
w(o, v) {
|
|
303
|
+
if (!Array.isArray(v) && !ArrayBuffer.isView(v)) fail(`${describe(v)} is not an array.`);
|
|
304
|
+
const a = v;
|
|
305
|
+
for (let k = 0; k < a.length; k += 1) {
|
|
306
|
+
const e = a[k];
|
|
307
|
+
if (typeof e !== "number" || !Number.isInteger(e) || e < 0 || e >= lim) throw under(new CodecError(`${describe(e)} is outside planes(${n.bits})'s 0..${lim - 1}.`), k, null);
|
|
308
|
+
}
|
|
309
|
+
o.golomb(a.length, 0);
|
|
310
|
+
for (let b = n.bits - 1; b >= 0; b -= 1) {
|
|
311
|
+
const m = 2 ** b;
|
|
312
|
+
for (let k = 0; k < a.length; k += 1) o.bits(Math.floor(a[k] / m) % 2, 1);
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
r(i, x) {
|
|
316
|
+
const s = i.at;
|
|
317
|
+
const len2 = i.golomb(0);
|
|
318
|
+
if (x.trace) meta(x, "length", s, i, len2, `${len2} items`);
|
|
319
|
+
const out = new Array(len2).fill(0);
|
|
320
|
+
for (let b = n.bits - 1; b >= 0; b -= 1) {
|
|
321
|
+
const s1 = i.at;
|
|
322
|
+
const m = 2 ** b;
|
|
323
|
+
for (let k = 0; k < len2; k += 1) if (i.bool()) out[k] = out[k] + m;
|
|
324
|
+
if (x.trace) meta(x, `plane ${b}`, s1, i, null, `bit ${b} of every value`);
|
|
325
|
+
}
|
|
326
|
+
return out;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function compileNamed(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
332
|
+
const of = compile(n.of, recs);
|
|
333
|
+
return { node: n, w: (o, v, x) => of.w(o, v, x), r: (i, x) => of.r(i, x) };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function compileRec(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
337
|
+
const fwd = { c: null };
|
|
338
|
+
const self = { node: n, w: (o, v, x) => fwd.c.w(o, v, x), r: (i, x) => fwd.c.r(i, x) };
|
|
339
|
+
fwd.c = compile(n.of, [...recs, self]);
|
|
340
|
+
return self;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function compileSelf(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
344
|
+
const target = recs[recs.length - 1 - n.depth];
|
|
345
|
+
if (!target) throw new SchemaError(`A self node (depth ${n.depth}) outside its recursive().`);
|
|
346
|
+
return { node: n, w: (o, v, x) => target.w(o, v, x), r: (i, x) => target.r(i, x) };
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export {
|
|
350
|
+
compileAlt,
|
|
351
|
+
compileDelta,
|
|
352
|
+
compileLz,
|
|
353
|
+
compileMap,
|
|
354
|
+
compileNamed,
|
|
355
|
+
compilePlanes,
|
|
356
|
+
compileRec,
|
|
357
|
+
compileRuns,
|
|
358
|
+
compileSelf,
|
|
359
|
+
compileUnion,
|
|
360
|
+
};
|