@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,515 @@
|
|
|
1
|
+
import './icon-schema.mjs';
|
|
2
|
+
import {
|
|
3
|
+
BitError,
|
|
4
|
+
BitWriter,
|
|
5
|
+
asNode,
|
|
6
|
+
bad,
|
|
7
|
+
bitsFor,
|
|
8
|
+
fieldsOf,
|
|
9
|
+
fixed,
|
|
10
|
+
node,
|
|
11
|
+
num,
|
|
12
|
+
ref,
|
|
13
|
+
uint,
|
|
14
|
+
whole,
|
|
15
|
+
} from './icon-schema.mjs';
|
|
16
|
+
|
|
17
|
+
function extend(base, fields) {
|
|
18
|
+
if (base.kind !== "struct") bad("extend() takes a struct.");
|
|
19
|
+
if (!base.open) bad("Only an open struct can be extended: declare it struct({...}, { open: true }) from its first version.");
|
|
20
|
+
const group = fieldsOf(fields, "extend");
|
|
21
|
+
if (!group.length) bad("extend() needs at least one field.");
|
|
22
|
+
const names = new Set([...base.fields, ...base.ext.flat()].map((f) => f.name));
|
|
23
|
+
for (const f of group) {
|
|
24
|
+
if (names.has(f.name)) bad(`extend(): the struct already has a field ${f.name}.`);
|
|
25
|
+
if (f.type.kind !== "optional" && f.type.kind !== "default") bad(`extend(): new field ${f.name} must be optional() or withDefault() (old data doesn't carry it).`);
|
|
26
|
+
}
|
|
27
|
+
return node({ kind: "struct", fields: base.fields, open: true, ext: Object.freeze([...base.ext, Object.freeze(group)]) });
|
|
28
|
+
}
|
|
29
|
+
var tuple = (items) => node({ kind: "tuple", items: Object.freeze(items.map(asNode)) });
|
|
30
|
+
function union(tag, variants, { capacity = 0 } = {}) {
|
|
31
|
+
const list = fieldsOf(variants, "union");
|
|
32
|
+
if (!list.length) bad("A union needs variants.");
|
|
33
|
+
for (const v of list) {
|
|
34
|
+
if (v.type.kind !== "struct") bad(`Union variant ${v.name} must be a struct.`);
|
|
35
|
+
const st = v.type;
|
|
36
|
+
if ([...st.fields, ...st.ext.flat()].some((f) => f.name === tag)) bad(`Union variant ${v.name} has a field called ${tag}, its tag.`);
|
|
37
|
+
}
|
|
38
|
+
if (capacity && capacity < list.length) bad(`Union capacity ${capacity} is below its ${list.length} variants.`);
|
|
39
|
+
return node({ kind: "union", tag, variants: Object.freeze(list), capacity: whole(capacity, 0, 2 ** 16, "union capacity") });
|
|
40
|
+
}
|
|
41
|
+
function alt(of) {
|
|
42
|
+
if (of.length < 2) bad("alt() takes two or more types.");
|
|
43
|
+
const cats = of.map((t) => categoryOf(asNode(t)));
|
|
44
|
+
const seen = /* @__PURE__ */ new Set();
|
|
45
|
+
for (const c of cats) {
|
|
46
|
+
if (c === "any") bad("alt() branches must have a definite category (not dyn, optional or a recursion).");
|
|
47
|
+
if (seen.has(c)) bad(`alt() has two ${c} branches: nothing tells them apart.`);
|
|
48
|
+
seen.add(c);
|
|
49
|
+
}
|
|
50
|
+
return node({ kind: "alt", of: Object.freeze(of.map(asNode)) });
|
|
51
|
+
}
|
|
52
|
+
var planes = (bits) => node({ kind: "planes", bits: whole(bits, 1, 32, "planes bits") });
|
|
53
|
+
var named = (name, of, { version = 1, doc = "" } = {}) => node({ kind: "named", name: name || bad("named() needs a name."), version: whole(version, 0, 2 ** 32, "version"), doc, of: asNode(of) });
|
|
54
|
+
function recursive(build2) {
|
|
55
|
+
const placeholder = Object.freeze({ kind: "self", depth: -1 });
|
|
56
|
+
const body = asNode(build2(placeholder));
|
|
57
|
+
const fix = (n, depth) => {
|
|
58
|
+
if (n === placeholder) return node({ kind: "self", depth });
|
|
59
|
+
return mapChildren(n, (c) => fix(c, depth + (n.kind === "rec" ? 1 : 0)));
|
|
60
|
+
};
|
|
61
|
+
return node({ kind: "rec", of: fix(body, 0) });
|
|
62
|
+
}
|
|
63
|
+
function mapChildren(n, f) {
|
|
64
|
+
const fl = (fs) => {
|
|
65
|
+
let changed = false;
|
|
66
|
+
const out = fs.map((x) => {
|
|
67
|
+
const t = f(x.type);
|
|
68
|
+
if (t !== x.type) changed = true;
|
|
69
|
+
return t === x.type ? x : Object.freeze({ name: x.name, type: t });
|
|
70
|
+
});
|
|
71
|
+
return changed ? Object.freeze(out) : fs;
|
|
72
|
+
};
|
|
73
|
+
switch (n.kind) {
|
|
74
|
+
case "array":
|
|
75
|
+
case "optional":
|
|
76
|
+
case "nullable":
|
|
77
|
+
case "default":
|
|
78
|
+
case "delta":
|
|
79
|
+
case "runs":
|
|
80
|
+
case "named":
|
|
81
|
+
case "rec":
|
|
82
|
+
case "lz": {
|
|
83
|
+
const of = f(n.of);
|
|
84
|
+
return of === n.of ? n : node({ ...n, of });
|
|
85
|
+
}
|
|
86
|
+
case "struct": {
|
|
87
|
+
const fields = fl(n.fields);
|
|
88
|
+
const ext = n.ext.map(fl);
|
|
89
|
+
return fields === n.fields && ext.every((g, i) => g === n.ext[i]) ? n : node({ ...n, fields, ext: Object.freeze(ext) });
|
|
90
|
+
}
|
|
91
|
+
case "union": {
|
|
92
|
+
const variants = fl(n.variants);
|
|
93
|
+
return variants === n.variants ? n : node({ ...n, variants });
|
|
94
|
+
}
|
|
95
|
+
case "tuple": {
|
|
96
|
+
const items = n.items.map(f);
|
|
97
|
+
return items.every((x, i) => x === n.items[i]) ? n : node({ ...n, items: Object.freeze(items) });
|
|
98
|
+
}
|
|
99
|
+
case "alt": {
|
|
100
|
+
const of = n.of.map(f);
|
|
101
|
+
return of.every((x, i) => x === n.of[i]) ? n : node({ ...n, of: Object.freeze(of) });
|
|
102
|
+
}
|
|
103
|
+
case "map": {
|
|
104
|
+
const key = f(n.key);
|
|
105
|
+
const value = f(n.value);
|
|
106
|
+
return key === n.key && value === n.value ? n : node({ ...n, key, value });
|
|
107
|
+
}
|
|
108
|
+
case "grow": {
|
|
109
|
+
const base = f(n.base);
|
|
110
|
+
const of = f(n.of);
|
|
111
|
+
return base === n.base && of === n.of ? n : node({ ...n, base, of });
|
|
112
|
+
}
|
|
113
|
+
default:
|
|
114
|
+
return n;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function categoryOf(n) {
|
|
118
|
+
switch (n.kind) {
|
|
119
|
+
case "uint":
|
|
120
|
+
case "int":
|
|
121
|
+
case "fixed":
|
|
122
|
+
case "float":
|
|
123
|
+
case "varuint":
|
|
124
|
+
case "varint":
|
|
125
|
+
case "num":
|
|
126
|
+
return "number";
|
|
127
|
+
case "bool":
|
|
128
|
+
return "boolean";
|
|
129
|
+
case "string":
|
|
130
|
+
case "ref":
|
|
131
|
+
case "hex":
|
|
132
|
+
return "string";
|
|
133
|
+
case "biguint":
|
|
134
|
+
return "bigint";
|
|
135
|
+
case "bytes":
|
|
136
|
+
return "bytes";
|
|
137
|
+
case "array":
|
|
138
|
+
case "delta":
|
|
139
|
+
case "runs":
|
|
140
|
+
case "planes":
|
|
141
|
+
case "tuple":
|
|
142
|
+
case "lz":
|
|
143
|
+
case "grow":
|
|
144
|
+
return "array";
|
|
145
|
+
case "struct":
|
|
146
|
+
case "union":
|
|
147
|
+
case "map":
|
|
148
|
+
return "object";
|
|
149
|
+
case "nullable":
|
|
150
|
+
return "any";
|
|
151
|
+
case "enum": {
|
|
152
|
+
const c = /* @__PURE__ */ new Set([...n.values.map((v) => typeof v), ...n.other ? ["string"] : []]);
|
|
153
|
+
return c.size === 1 ? [...c][0] : "any";
|
|
154
|
+
}
|
|
155
|
+
case "const":
|
|
156
|
+
return n.value === null ? "null" : Array.isArray(n.value) ? "array" : typeof n.value;
|
|
157
|
+
case "named":
|
|
158
|
+
case "default":
|
|
159
|
+
return categoryOf(n.of);
|
|
160
|
+
default:
|
|
161
|
+
return "any";
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// packages/codec/src/schemas/common.ts
|
|
166
|
+
var coord = fixed(-8192, 8192, 1e-3);
|
|
167
|
+
var local = fixed(-8192, 8192, 1e-3, { k: 10 });
|
|
168
|
+
var localDelta = fixed(-8192, 8192, 1e-3, { k: 9, delta: true });
|
|
169
|
+
var size = fixed(0, 1024, 1e-3, { k: 9 });
|
|
170
|
+
var sizeDelta = fixed(0, 1024, 1e-3, { k: 9, delta: true });
|
|
171
|
+
var yaw = fixed(-Math.PI, Math.PI, Math.PI / 32768);
|
|
172
|
+
var unit = fixed(0, 1, 1 / 1024);
|
|
173
|
+
var signed = fixed(-1, 1, 1e-3);
|
|
174
|
+
var vec3 = tuple([coord, coord, coord]);
|
|
175
|
+
var lvec3 = tuple([local, local, local]);
|
|
176
|
+
var dvec3 = tuple([localDelta, localDelta, localDelta]);
|
|
177
|
+
var dhalf3 = tuple([sizeDelta, sizeDelta, sizeDelta]);
|
|
178
|
+
var half3 = tuple([size, size, size]);
|
|
179
|
+
var dir3 = tuple([signed, signed, signed]);
|
|
180
|
+
var seedText = ref("str", { packHex: true });
|
|
181
|
+
var seedAny = alt([num(), seedText]);
|
|
182
|
+
var oklch2 = tuple([fixed(0, 1, 1e-3, { off: "exact" }), fixed(0, 0.5, 1e-3, { off: "exact" }), fixed(0, 360, 0.01, { off: "exact" })]);
|
|
183
|
+
var id = ref("ids");
|
|
184
|
+
var count8 = uint(8);
|
|
185
|
+
|
|
186
|
+
// packages/codec/src/codec-primitives.ts
|
|
187
|
+
var CodecError = class _CodecError extends Error {
|
|
188
|
+
/** Field names and indices from the root ("parts", 3, "shape(box)", "h", 1). */
|
|
189
|
+
path;
|
|
190
|
+
/** The reason, without the path. */
|
|
191
|
+
why;
|
|
192
|
+
/** Decoding: the bit it happened at. */
|
|
193
|
+
bit;
|
|
194
|
+
constructor(why, path = [], bit = null) {
|
|
195
|
+
super(why);
|
|
196
|
+
this.name = "CodecError";
|
|
197
|
+
this.why = why;
|
|
198
|
+
this.path = path;
|
|
199
|
+
this.bit = bit;
|
|
200
|
+
this.message = _CodecError.format(why, path, bit);
|
|
201
|
+
}
|
|
202
|
+
static format(why, path, bit) {
|
|
203
|
+
const p = pathText(path);
|
|
204
|
+
return `${p ? `${p}: ` : ""}${why}${bit !== null ? ` (at bit ${bit})` : ""}`;
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
function pathText(path) {
|
|
208
|
+
let out = "";
|
|
209
|
+
for (const k of path) out += typeof k === "number" ? `[${k}]` : out ? `.${k}` : k;
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
var fail = (why) => {
|
|
213
|
+
throw new CodecError(why);
|
|
214
|
+
};
|
|
215
|
+
function under(e, label, bit = null) {
|
|
216
|
+
if (e instanceof CodecError) {
|
|
217
|
+
e.path.unshift(label);
|
|
218
|
+
if (e.bit === null && bit !== null) e.bit = bit;
|
|
219
|
+
e.message = CodecError.format(e.why, e.path, e.bit);
|
|
220
|
+
return e;
|
|
221
|
+
}
|
|
222
|
+
if (e instanceof BitError) return new CodecError(e.message.replace(/ at bit \d+/, ""), [label], e.bit);
|
|
223
|
+
if (e instanceof Error) return new CodecError(e.message, [label]);
|
|
224
|
+
return new CodecError(String(e), [label]);
|
|
225
|
+
}
|
|
226
|
+
var Tables = class {
|
|
227
|
+
m = /* @__PURE__ */ new Map();
|
|
228
|
+
constructor(init = {}) {
|
|
229
|
+
for (const [name, list] of Object.entries(init)) for (const s of list) this.add(name, s);
|
|
230
|
+
}
|
|
231
|
+
table(name) {
|
|
232
|
+
let t = this.m.get(name);
|
|
233
|
+
if (!t) {
|
|
234
|
+
t = { list: [], index: /* @__PURE__ */ new Map() };
|
|
235
|
+
this.m.set(name, t);
|
|
236
|
+
}
|
|
237
|
+
return t;
|
|
238
|
+
}
|
|
239
|
+
add(name, s) {
|
|
240
|
+
const t = this.table(name);
|
|
241
|
+
const have = t.index.get(s);
|
|
242
|
+
if (have !== void 0) return have;
|
|
243
|
+
t.list.push(s);
|
|
244
|
+
t.index.set(s, t.list.length - 1);
|
|
245
|
+
return t.list.length - 1;
|
|
246
|
+
}
|
|
247
|
+
n = /* @__PURE__ */ new Map();
|
|
248
|
+
/** A table of numbers (dyn()'s fractional numbers: a repeated one is its index). */
|
|
249
|
+
numbers(name) {
|
|
250
|
+
let t = this.n.get(name);
|
|
251
|
+
if (!t) {
|
|
252
|
+
t = { list: [], index: /* @__PURE__ */ new Map() };
|
|
253
|
+
this.n.set(name, t);
|
|
254
|
+
}
|
|
255
|
+
return t;
|
|
256
|
+
}
|
|
257
|
+
sh = /* @__PURE__ */ new Map();
|
|
258
|
+
/** A table of object shapes (dyn()'s key lists: an object shaped like one before is its index, then its values). */
|
|
259
|
+
shapes(name) {
|
|
260
|
+
let t = this.sh.get(name);
|
|
261
|
+
if (!t) {
|
|
262
|
+
t = { list: [], index: /* @__PURE__ */ new Map() };
|
|
263
|
+
this.sh.set(name, t);
|
|
264
|
+
}
|
|
265
|
+
return t;
|
|
266
|
+
}
|
|
267
|
+
/** Every table's strings, by name (what a pack stores once when its documents share tables). */
|
|
268
|
+
toJSON() {
|
|
269
|
+
return Object.fromEntries([...this.m.entries()].sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => [k, [...v.list]]));
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var fresh = () => ({ tables: new Tables(), trace: null, strict: true, text: new BitWriter(64), tr: null, last: null });
|
|
273
|
+
var lastOf = (x) => x.last ??= /* @__PURE__ */ new Map();
|
|
274
|
+
function wr(c, o, v, x, label) {
|
|
275
|
+
try {
|
|
276
|
+
c.w(o, v, x);
|
|
277
|
+
} catch (e) {
|
|
278
|
+
throw under(e, label, null);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
function rd(c, i, x, label) {
|
|
282
|
+
const start = i.at;
|
|
283
|
+
try {
|
|
284
|
+
if (x.trace === null) return c.r(i, x);
|
|
285
|
+
x.trace.open(label, c.node, start);
|
|
286
|
+
const v = c.r(i, x);
|
|
287
|
+
x.trace.close(v, i.at);
|
|
288
|
+
return v;
|
|
289
|
+
} catch (e) {
|
|
290
|
+
throw under(e, label, start);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
var meta = (x, label, start, i, value, note = "") => {
|
|
294
|
+
x.trace?.meta(label, start, i.at - start, value, note);
|
|
295
|
+
};
|
|
296
|
+
function same(a, b) {
|
|
297
|
+
if (typeof a === "number" || typeof b === "number") return Object.is(a, b);
|
|
298
|
+
if (a === b) return true;
|
|
299
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
300
|
+
if (a instanceof Uint8Array || b instanceof Uint8Array) {
|
|
301
|
+
if (!(a instanceof Uint8Array && b instanceof Uint8Array) || a.length !== b.length) return false;
|
|
302
|
+
for (let k = 0; k < a.length; k += 1) if (a[k] !== b[k]) return false;
|
|
303
|
+
return true;
|
|
304
|
+
}
|
|
305
|
+
const aa = Array.isArray(a) || ArrayBuffer.isView(a);
|
|
306
|
+
if (aa !== (Array.isArray(b) || ArrayBuffer.isView(b))) return false;
|
|
307
|
+
if (aa) {
|
|
308
|
+
const x = a, y = b;
|
|
309
|
+
if (x.length !== y.length) return false;
|
|
310
|
+
for (let k = 0; k < x.length; k += 1) if (!same(x[k], y[k])) return false;
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
const ka = Object.keys(a).filter((k) => a[k] !== void 0);
|
|
314
|
+
const kb = Object.keys(b).filter((k) => b[k] !== void 0);
|
|
315
|
+
if (ka.length !== kb.length) return false;
|
|
316
|
+
for (const k of ka) if (!same(a[k], b[k])) return false;
|
|
317
|
+
return true;
|
|
318
|
+
}
|
|
319
|
+
function put(o, k, v) {
|
|
320
|
+
if (k === "__proto__") Object.defineProperty(o, k, { value: v, enumerable: true, writable: true, configurable: true });
|
|
321
|
+
else o[k] = v;
|
|
322
|
+
}
|
|
323
|
+
var zig = (v) => v >= 0 ? v * 2 : -v * 2 - 1;
|
|
324
|
+
var unzig = (u2) => u2 % 2 ? -(u2 + 1) / 2 : u2 / 2;
|
|
325
|
+
var describe = (v) => typeof v === "string" ? JSON.stringify(v.length > 40 ? `${v.slice(0, 40)}...` : v) : typeof v === "bigint" ? `${v}n` : Array.isArray(v) ? `an array of ${v.length}` : v instanceof Uint8Array ? `${v.length} bytes` : v === null ? "null" : typeof v === "object" ? "an object" : String(v);
|
|
326
|
+
var GRIDS = /* @__PURE__ */ new WeakMap();
|
|
327
|
+
function gridOf(n) {
|
|
328
|
+
const have = GRIDS.get(n);
|
|
329
|
+
if (have) return have;
|
|
330
|
+
const inv = 1 / n.step;
|
|
331
|
+
const den = Math.abs(inv - Math.round(inv)) < 1e-9 * Math.max(1, inv) ? Math.round(inv) : 0;
|
|
332
|
+
const value = den ? (k) => k / den : (k) => k * n.step;
|
|
333
|
+
const units = den ? (v) => Math.round(v * den) : (v) => Math.round(v / n.step);
|
|
334
|
+
const near = (x) => Math.abs(x - Math.round(x)) <= 1e-9 * Math.max(1, Math.abs(x)) ? Math.round(x) : null;
|
|
335
|
+
const lo = near(n.min / n.step) ?? Math.ceil(n.min / n.step);
|
|
336
|
+
const hi = near(n.max / n.step) ?? Math.floor(n.max / n.step);
|
|
337
|
+
let places = -1;
|
|
338
|
+
if (den) {
|
|
339
|
+
for (let p = 0, ten = 1; p <= 15; p += 1, ten *= 10) if (ten % den === 0) {
|
|
340
|
+
places = p;
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
const g = { lo, hi, bits: bitsFor(hi - lo + 1), den, step: n.step, places, value, units };
|
|
345
|
+
GRIDS.set(n, g);
|
|
346
|
+
return g;
|
|
347
|
+
}
|
|
348
|
+
function intCheck(v, what) {
|
|
349
|
+
if (typeof v !== "number" || !Number.isInteger(v)) fail(`${describe(v)} is not a whole number (${what}).`);
|
|
350
|
+
return v;
|
|
351
|
+
}
|
|
352
|
+
var NUM_KINDS = ["int", "decimal", "float32", "float64"];
|
|
353
|
+
var TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8];
|
|
354
|
+
function placesOf(v) {
|
|
355
|
+
if (!Number.isFinite(v) || Object.is(v, -0) || Math.abs(v) > 2 ** 40) return 0;
|
|
356
|
+
for (let p = 1; p <= 8; p += 1) {
|
|
357
|
+
const m = Math.round(v * TEN[p]);
|
|
358
|
+
if (Math.abs(m) <= 2 ** 51 && m / TEN[p] === v) return p;
|
|
359
|
+
}
|
|
360
|
+
return 0;
|
|
361
|
+
}
|
|
362
|
+
function writeNum(o, v, tagBits) {
|
|
363
|
+
if (Number.isSafeInteger(v) && !Object.is(v, -0) && Math.abs(v) <= 2 ** 51) {
|
|
364
|
+
o.bits(0, tagBits);
|
|
365
|
+
o.golomb(zig(v), 0);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
const p = placesOf(v);
|
|
369
|
+
if (p) {
|
|
370
|
+
o.bits(1, tagBits);
|
|
371
|
+
o.bits(p - 1, 3);
|
|
372
|
+
o.golomb(zig(Math.round(v * TEN[p])), 0);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
if (Number.isNaN(v) || Object.is(Math.fround(v), v)) {
|
|
376
|
+
o.bits(2, tagBits);
|
|
377
|
+
o.f32(v);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
o.bits(3, tagBits);
|
|
381
|
+
o.f64(v);
|
|
382
|
+
}
|
|
383
|
+
function readNum(i, tag) {
|
|
384
|
+
if (tag === 0) return unzig(i.golomb(0));
|
|
385
|
+
if (tag === 1) {
|
|
386
|
+
const p = i.bits(3) + 1;
|
|
387
|
+
const m = unzig(i.golomb(0));
|
|
388
|
+
const v = m / TEN[p];
|
|
389
|
+
if (Number.isInteger(v) || placesOf(v) !== p) throw new CodecError(`A decimal with ${p} places that isn't its shortest spelling (not canonical).`);
|
|
390
|
+
return v;
|
|
391
|
+
}
|
|
392
|
+
if (tag === 2) return i.f32();
|
|
393
|
+
return i.f64();
|
|
394
|
+
}
|
|
395
|
+
var COMPILED = /* @__PURE__ */ new WeakMap();
|
|
396
|
+
function compiled(root, compile2) {
|
|
397
|
+
const have = COMPILED.get(root);
|
|
398
|
+
if (have) return have;
|
|
399
|
+
const c = compile2(root, []);
|
|
400
|
+
COMPILED.set(root, c);
|
|
401
|
+
return c;
|
|
402
|
+
}
|
|
403
|
+
var enc = new TextEncoder();
|
|
404
|
+
function utf8(s) {
|
|
405
|
+
const n = s.length;
|
|
406
|
+
if (n < 64) {
|
|
407
|
+
let ascii = true;
|
|
408
|
+
for (let k = 0; k < n; k += 1) if (s.charCodeAt(k) > 127) {
|
|
409
|
+
ascii = false;
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
if (ascii) {
|
|
413
|
+
const b = new Uint8Array(n);
|
|
414
|
+
for (let k = 0; k < n; k += 1) b[k] = s.charCodeAt(k);
|
|
415
|
+
return b;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
return enc.encode(s);
|
|
419
|
+
}
|
|
420
|
+
function fromUtf8(tr, len2) {
|
|
421
|
+
const b = tr.bytes(len2);
|
|
422
|
+
if (len2 < 64) {
|
|
423
|
+
let ascii = true;
|
|
424
|
+
for (let k = 0; k < len2; k += 1) if (b[k] > 127) {
|
|
425
|
+
ascii = false;
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
if (ascii) return String.fromCharCode.apply(null, b);
|
|
429
|
+
}
|
|
430
|
+
return dec.decode(b);
|
|
431
|
+
}
|
|
432
|
+
var LONE = /[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/;
|
|
433
|
+
var wellFormed = (s) => !LONE.test(s);
|
|
434
|
+
var SCRATCH = new BitWriter(64);
|
|
435
|
+
var dec = new TextDecoder("utf-8", { fatal: true });
|
|
436
|
+
|
|
437
|
+
// packages/codec/src/codec.ts
|
|
438
|
+
|
|
439
|
+
function compileUint(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
440
|
+
const max = 2 ** n.bits;
|
|
441
|
+
const what = `uint(${n.bits})`;
|
|
442
|
+
return {
|
|
443
|
+
node: n,
|
|
444
|
+
w(o, v) {
|
|
445
|
+
const k = intCheck(v, what);
|
|
446
|
+
if (k < 0 || k >= max) fail(`${k} is outside ${what}'s 0..${max - 1}.`);
|
|
447
|
+
o.wide(k, n.bits);
|
|
448
|
+
},
|
|
449
|
+
r: (i) => i.bits(n.bits)
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function compileInt(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
454
|
+
const half = 2 ** (n.bits - 1);
|
|
455
|
+
const what = `int(${n.bits})`;
|
|
456
|
+
return {
|
|
457
|
+
node: n,
|
|
458
|
+
w(o, v) {
|
|
459
|
+
const k = intCheck(v, what);
|
|
460
|
+
if (k < -half || k >= half) fail(`${k} is outside ${what}'s ${-half}..${half - 1}.`);
|
|
461
|
+
o.wide(zig(k), n.bits);
|
|
462
|
+
},
|
|
463
|
+
r: (i) => unzig(i.bits(n.bits))
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
function compileBool(n, recs, compile, dynCodec, structCodec, holds, valueCategory) {
|
|
468
|
+
return {
|
|
469
|
+
node: n,
|
|
470
|
+
w(o, v) {
|
|
471
|
+
if (typeof v !== "boolean") fail(`${describe(v)} is not a boolean.`);
|
|
472
|
+
o.bool(v);
|
|
473
|
+
},
|
|
474
|
+
r: (i) => i.bool()
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
export {
|
|
479
|
+
CodecError,
|
|
480
|
+
NUM_KINDS,
|
|
481
|
+
SCRATCH,
|
|
482
|
+
Tables,
|
|
483
|
+
alt,
|
|
484
|
+
categoryOf,
|
|
485
|
+
compileBool,
|
|
486
|
+
compileInt,
|
|
487
|
+
compileUint,
|
|
488
|
+
compiled,
|
|
489
|
+
describe,
|
|
490
|
+
extend,
|
|
491
|
+
fail,
|
|
492
|
+
fresh,
|
|
493
|
+
fromUtf8,
|
|
494
|
+
gridOf,
|
|
495
|
+
intCheck,
|
|
496
|
+
lastOf,
|
|
497
|
+
meta,
|
|
498
|
+
named,
|
|
499
|
+
planes,
|
|
500
|
+
put,
|
|
501
|
+
rd,
|
|
502
|
+
readNum,
|
|
503
|
+
recursive,
|
|
504
|
+
same,
|
|
505
|
+
seedText,
|
|
506
|
+
tuple,
|
|
507
|
+
under,
|
|
508
|
+
union,
|
|
509
|
+
unzig,
|
|
510
|
+
utf8,
|
|
511
|
+
wellFormed,
|
|
512
|
+
wr,
|
|
513
|
+
writeNum,
|
|
514
|
+
zig,
|
|
515
|
+
};
|