-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasi-runtime.mjs
More file actions
1674 lines (1641 loc) · 63.4 KB
/
Copy pathwasi-runtime.mjs
File metadata and controls
1674 lines (1641 loc) · 63.4 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { readFile } from "node:fs/promises";
import { WASI } from "node:wasi";
import {
assertLatticeProvider,
assertSemiringProvider,
assertScalarWfstProvider,
normalizeLatticeProviderOptions,
normalizeSemiringProviderOptions,
normalizeScalarWfstProviderOptions,
} from "@vinary-tree/vinary-tree-interop";
const FAILURE = 0xffff_ffff;
const RECORD_SIZE = 32;
const ENTRY_RECORD_HEADER_SIZE = 24;
const encoder = new TextEncoder();
const decoder = new TextDecoder("utf-8", { fatal: true });
const DOMAINS = new Map([["byte", 0], ["unicode", 1], ["u64", 2]]);
const ALGEBRA_OPERATIONS = new Map([
["union", 1], ["intersection", 2], ["difference", 3], ["symmetric-difference", 4],
]);
const VALUE_MERGES = new Map([
["first", 1], ["last", 2], ["lattice-join", 3], ["lattice-meet", 4],
]);
const ALGORITHMS = new Map([
["standard", 0],
["transposition", 1],
["merge-and-split", 2],
["damerau-levenshtein", 3],
]);
const ORDERS = new Map([["traversal", 0], ["distance-then-term", 1]]);
const DUALLITY_KINDS = new Map([
["levenshtein", 0], ["universal-standard", 1], ["universal-transposition", 2],
["universal-merge-and-split", 3], ["generalized-standard", 4],
["generalized-transposition", 5], ["generalized-merge-and-split", 6],
["generalized-phonetic", 7], ["fzf", 8],
]);
const WEIGHT_DOMAINS = new Map([
[1, "tropical-f64"], [2, "log-f64"], [3, "probability-f64"], [4, "arctic-f64"],
[5, "signed-tropical-f64"], [6, "count-f64"], [7, "boolean-f64"],
]);
const WFST_UNIT_DOMAINS = new Map([["byte", 1], ["unicode", 2], ["u64", 3]]);
const WFST_UNIT_DOMAIN_NAMES = new Map(Array.from(WFST_UNIT_DOMAINS, ([name, value]) => [value, name]));
const WEIGHT_DOMAIN_VALUES = new Map(Array.from(WEIGHT_DOMAINS, ([value, name]) => [name, value]));
const HOST_OK = 0;
const HOST_INVALID_ARGUMENT = 2;
const HOST_CLOSED = 6;
const HOST_LIMIT_EXCEEDED = 7;
const HOST_PROVIDER_ERROR = 8;
const HOST_INDEX_MASK = 0xffff;
const HOST_MAX_COMPONENT = 0xfffe;
const HOST_LATTICE_THREAD_BOUND = 1n;
const HOST_LATTICE_STABLE_BYTES = 4n;
const HOST_LATTICE_BATCH = 8n;
const HOST_LATTICE_JOIN = 1;
const HOST_LATTICE_MEET = 2;
const HOST_LATTICE_STABLE = 1;
const HOST_LATTICE_DIAGNOSTIC = 2;
const HOST_SEMIRING_THREAD_BOUND = 1n;
const HOST_SEMIRING_STABLE_BYTES = 4n;
const HOST_SEMIRING_BATCH = 8n;
const HOST_SEMIRING_DIVISION = 1;
const HOST_SEMIRING_STAR = 2;
const HOST_SEMIRING_NUMERIC = 4;
const HOST_SEMIRING_ZERO = 1;
const HOST_SEMIRING_ONE = 2;
const HOST_SEMIRING_PLUS = 1;
const HOST_SEMIRING_TIMES = 2;
const HOST_SEMIRING_EQUAL = 1;
const HOST_SEMIRING_APPROX_EQUAL = 2;
const HOST_SEMIRING_STABLE = 1;
const HOST_SEMIRING_DIAGNOSTIC = 2;
const HOST_SEMIRING_DIVIDE = 1;
const HOST_SEMIRING_LEFT_DIVIDE = 2;
const HOST_SEMIRING_NUMERICAL = 1;
const HOST_SEMIRING_PROBABILITY = 2;
const SEMIRING_PROPERTIES = new Map([
["hashable", 1n],
["idempotent-plus", 2n],
["k-closed", 4n],
["zero-sum-free", 8n],
["commutative-times", 16n],
["totally-ordered", 32n],
["nonnegative", 64n],
]);
const MAXIMUM_PROVIDER_BYTES = 16 * 1024 * 1024;
const MAXIMUM_LATTICE_BATCH = 256;
const MAXIMUM_LAW_SAMPLES = 16;
class GenerationalProviderTable {
#slots = [];
#free = [];
insert(provider, metadata) {
let index;
if (this.#free.length > 0) {
index = this.#free.pop();
} else {
if (this.#slots.length >= HOST_MAX_COMPONENT) {
throw new RangeError("WASI host provider table is full");
}
index = this.#slots.length;
this.#slots.push({ generation: 1, provider: null, metadata: null, active: false });
}
const slot = this.#slots[index];
slot.provider = provider;
slot.metadata = metadata;
slot.active = false;
return ((slot.generation << 16) | (index + 1)) >>> 0;
}
release(handle) {
const selected = this.#lookup(handle);
if (selected === null) return;
const { index, slot } = selected;
slot.provider = null;
slot.metadata = null;
slot.active = false;
slot.generation = slot.generation === HOST_MAX_COMPONENT ? 1 : slot.generation + 1;
this.#free.push(index);
}
resolve(handle, expectedKind) {
const selected = this.#lookup(handle);
if (selected === null || selected.slot.metadata?.kind !== expectedKind) return null;
return selected.slot;
}
invoke(handle, expectedKind, operation) {
const selected = this.#lookup(handle);
if (selected === null || selected.slot.metadata?.kind !== expectedKind) return HOST_CLOSED;
const { slot } = selected;
if (slot.active) return HOST_PROVIDER_ERROR;
slot.active = true;
try {
operation(slot.provider, slot.metadata);
return HOST_OK;
} catch {
return HOST_PROVIDER_ERROR;
} finally {
slot.active = false;
}
}
#lookup(handle) {
const unsigned = handle >>> 0;
const low = unsigned & HOST_INDEX_MASK;
const generation = unsigned >>> 16;
if (low === 0 || low > HOST_MAX_COMPONENT || generation === 0 || generation > HOST_MAX_COMPONENT) {
return null;
}
const index = low - 1;
const slot = this.#slots[index];
if (slot === undefined || slot.metadata === null || slot.generation !== generation) return null;
return { index, slot };
}
}
function requireScalarWfstProvider(provider) {
assertScalarWfstProvider(provider);
return provider;
}
function hostWfstOptions(options) {
const { unitDomain, weightDomain, lazy, acyclic } = normalizeScalarWfstProviderOptions(options);
const unitDomainValue = WFST_UNIT_DOMAINS.get(unitDomain);
const weightDomainValue = WEIGHT_DOMAIN_VALUES.get(weightDomain);
let flags = 2n;
if (lazy) flags |= 4n;
if (acyclic) flags |= 8n;
return {
unitDomain: unitDomainValue,
weightDomain: weightDomainValue,
flags,
unitDomainName: unitDomain,
weightDomainName: weightDomain,
};
}
function latticeProviderFlags(provider) {
let flags = HOST_LATTICE_THREAD_BOUND;
if (typeof provider.stableBytes === "function") flags |= HOST_LATTICE_STABLE_BYTES;
if (typeof provider.joinMany === "function") flags |= HOST_LATTICE_BATCH;
return flags;
}
function hostLatticeOptions(provider, options) {
assertLatticeProvider(provider);
const { domainId } = normalizeLatticeProviderOptions(options);
return { provider, domainId, flags: latticeProviderFlags(provider) };
}
function hostSemiringOptions(provider, options) {
assertSemiringProvider(provider);
const normalized = normalizeSemiringProviderOptions(options);
const propertyBits = normalized.properties.reduce((bits, property) => {
const bit = SEMIRING_PROPERTIES.get(property);
if (bit === undefined) throw new TypeError(`unknown semiring property ${property}`);
return bits | bit;
}, 0n);
let flags = HOST_SEMIRING_THREAD_BOUND;
if (typeof provider.stableBytes === "function") flags |= HOST_SEMIRING_STABLE_BYTES;
if (typeof provider.plusMany === "function") flags |= HOST_SEMIRING_BATCH;
if ((propertyBits & 1n) !== 0n && (flags & HOST_SEMIRING_STABLE_BYTES) === 0n) {
throw new TypeError("hashable semirings must implement stableBytes");
}
let capabilities = 0;
if (typeof provider.divide === "function") capabilities |= HOST_SEMIRING_DIVISION;
if (typeof provider.star === "function") capabilities |= HOST_SEMIRING_STAR;
if (typeof provider.numericalValue === "function") capabilities |= HOST_SEMIRING_NUMERIC;
return { provider, ...normalized, propertyBits, flags, capabilities };
}
function exactU64(value, name) {
if (typeof value !== "bigint" || value < 0n || value > 0xffff_ffff_ffff_ffffn) {
throw new RangeError(`${name} must be an unsigned 64-bit bigint`);
}
return value;
}
function exactNumber(value, name) {
if (typeof value !== "number" || Number.isNaN(value)) {
throw new TypeError(`${name} must be a non-NaN number`);
}
return value;
}
function providerLabel(value, unitDomain) {
if (value === null) return [0n, 0];
if (unitDomain === 1) {
if (!Number.isInteger(value) || value < 0 || value > 255) {
throw new RangeError("byte WFST labels must be integers from 0 through 255 or null");
}
return [BigInt(value), 1];
}
if (unitDomain === 2) {
if (typeof value !== "string" || [...value].length !== 1) {
throw new TypeError("Unicode WFST labels must contain one scalar or be null");
}
return [BigInt(value.codePointAt(0)), 1];
}
return [exactU64(value, "u64 WFST label"), 1];
}
function providerArc(value, unitDomain) {
if (value === null || typeof value !== "object" || Array.isArray(value)) {
throw new TypeError("WFST arc must be an object");
}
const [input, hasInput] = providerLabel(value.input, unitDomain);
const [output, hasOutput] = providerLabel(value.output, unitDomain);
return {
input,
output,
target: exactU64(value.target, "WFST arc target"),
weight: exactNumber(value.weight, "WFST arc weight"),
hasInput,
hasOutput,
};
}
/** Instantiate an isolated WASI runtime with explicit guest-to-host preopens. */
export async function createWasiRuntime({
preopens = { "/workspace": process.cwd() },
wasm = new URL("./generated/wasi/vinary_tree.wasm", import.meta.url),
} = {}) {
const wasi = new WASI({ version: "preview1", args: [], env: {}, preopens });
const source = wasm instanceof WebAssembly.Module ? wasm : await readFile(wasm);
const module = source instanceof WebAssembly.Module ? source : await WebAssembly.compile(source);
const providers = new GenerationalProviderTable();
let ffi;
const memoryView = () => new DataView(ffi.memory.buffer);
const latticeOperand = (handle, domainId) => {
const slot = providers.resolve(handle, "lattice");
if (slot === null) throw new Error("stale or foreign WASI lattice handle");
if (slot.metadata.domainId !== domainId) {
throw new TypeError("lattice operands belong to different semantic domains");
}
return Object.freeze({ domainId, localValue: slot.provider, stableBytes: null });
};
const writeLatticeResult = (provider, domainId, handlePointer, flagsPointer) => {
assertLatticeProvider(provider);
const flags = latticeProviderFlags(provider);
const handle = providers.insert(provider, { kind: "lattice", domainId, flags });
try {
const memory = memoryView();
memory.setUint32(handlePointer, handle, true);
memory.setBigUint64(flagsPointer, flags, true);
} catch (error) {
providers.release(handle);
throw error;
}
};
const latticeHandleArray = (pointer, count, domainId) => {
if (count > MAXIMUM_LATTICE_BATCH) throw new RangeError("lattice batch exceeds 256 values");
const memory = memoryView();
return Array.from(
{ length: count },
(_value, index) => latticeOperand(memory.getUint32(pointer + index * 4, true), domainId),
);
};
const semiringValue = (context, handle) => {
const slot = providers.resolve(handle, "semiring-value");
if (slot === null) throw new Error("stale or foreign WASI semiring weight handle");
if (slot.metadata.context !== context) {
throw new TypeError("semiring weight belongs to a different operation context");
}
return slot.provider;
};
const writeSemiringValue = (context, value, outputPointer) => {
const handle = providers.insert(value, { kind: "semiring-value", context });
try {
memoryView().setUint32(outputPointer, handle, true);
} catch (error) {
providers.release(handle);
throw error;
}
};
const semiringValueArray = (context, pointer, count) => {
if (count > MAXIMUM_LATTICE_BATCH) throw new RangeError("semiring batch exceeds 256 values");
const memory = memoryView();
return Array.from(
{ length: count },
(_value, index) => semiringValue(context, memory.getUint32(pointer + index * 4, true)),
);
};
const semiringOptionalResult = (context, value, handlePointer, definedPointer) => {
if (value === null) {
const memory = memoryView();
memory.setUint32(handlePointer, 0, true);
memory.setUint8(definedPointer, 0);
return;
}
writeSemiringValue(context, value, handlePointer);
memoryView().setUint8(definedPointer, 1);
};
const host = {
host_provider_release(handle) {
providers.release(handle);
},
host_provider_start(handle, outputPointer) {
return providers.invoke(handle, "wfst", (provider) => {
const state = exactU64(provider.startState(), "WFST start state");
memoryView().setBigUint64(outputPointer, state, true);
});
},
host_provider_num_states(handle, countPointer, knownPointer) {
return providers.invoke(handle, "wfst", (provider) => {
const count = provider.stateCount();
const memory = memoryView();
if (count === null) {
memory.setUint32(countPointer, 0, true);
memory.setUint8(knownPointer, 0);
return;
}
const exact = exactU64(count, "WFST state count");
if (exact > 0xffff_ffffn) throw new RangeError("WASI WFST state count exceeds u32");
memory.setUint32(countPointer, Number(exact), true);
memory.setUint8(knownPointer, 1);
});
},
host_provider_state_info(handle, state, validPointer, finalPointer, weightPointer) {
return providers.invoke(handle, "wfst", (provider) => {
const value = provider.stateInfo(state);
if (value === null || typeof value !== "object" || Array.isArray(value)) {
throw new TypeError("WFST stateInfo must return an object");
}
if (typeof value.valid !== "boolean" || typeof value.final !== "boolean") {
throw new TypeError("WFST state valid and final metadata must be boolean");
}
if (!value.valid && value.final) {
throw new TypeError("an invalid WFST state cannot be final");
}
const finalWeight = exactNumber(value.finalWeight, "WFST final weight");
const memory = memoryView();
memory.setUint8(validPointer, Number(value.valid));
memory.setUint8(finalPointer, Number(value.final));
memory.setFloat64(weightPointer, finalWeight, true);
});
},
host_provider_state_arcs(
handle,
state,
start,
arcsPointer,
capacity,
writtenPointer,
totalPointer,
) {
return providers.invoke(handle, "wfst", (provider, { unitDomain }) => {
let values;
let total;
if (provider.stateArcsPage === undefined) {
const all = provider.stateArcs(state);
if (!Array.isArray(all)) throw new TypeError("WFST stateArcs must return an array");
total = all.length;
values = all.slice(start, start + capacity);
} else {
const page = provider.stateArcsPage(state, BigInt(start), capacity);
if (page === null || typeof page !== "object" || Array.isArray(page)) {
throw new TypeError("WFST stateArcsPage must return an object");
}
if (!Array.isArray(page.arcs)) throw new TypeError("WFST arc page must contain an array");
const exactTotal = exactU64(page.total, "WFST arc-page total");
if (exactTotal > 0xffff_ffffn) throw new RangeError("WASI WFST arc count exceeds u32");
total = Number(exactTotal);
values = page.arcs;
}
const arcs = values.map((arc) => providerArc(arc, unitDomain));
if (
start > total
|| arcs.length > capacity
|| start + arcs.length > total
|| (capacity !== 0 && arcs.length === 0 && start < total)
) {
throw new RangeError("WFST provider returned invalid arc paging");
}
const memory = memoryView();
for (const [index, arc] of arcs.entries()) {
const record = arcsPointer + index * 40;
memory.setBigUint64(record, arc.input, true);
memory.setBigUint64(record + 8, arc.output, true);
memory.setBigUint64(record + 16, arc.target, true);
memory.setFloat64(record + 24, arc.weight, true);
memory.setUint8(record + 32, arc.hasInput);
memory.setUint8(record + 33, arc.hasOutput);
for (let reserved = 34; reserved < 40; reserved += 1) memory.setUint8(record + reserved, 0);
}
memory.setUint32(writtenPointer, arcs.length, true);
memory.setUint32(totalPointer, total, true);
});
},
host_lattice_binary(receiver, other, operation, handlePointer, flagsPointer) {
return providers.invoke(receiver, "lattice", (provider, { domainId }) => {
const method = operation === HOST_LATTICE_JOIN
? "join"
: operation === HOST_LATTICE_MEET ? "meet" : null;
if (method === null) throw new TypeError("unknown lattice binary operation");
writeLatticeResult(
provider[method](latticeOperand(other, domainId)),
domainId,
handlePointer,
flagsPointer,
);
});
},
host_lattice_equal(receiver, other, equalPointer) {
return providers.invoke(receiver, "lattice", (provider, { domainId }) => {
const equal = provider.equal(latticeOperand(other, domainId));
if (typeof equal !== "boolean") throw new TypeError("lattice equal must return a boolean");
memoryView().setUint8(equalPointer, Number(equal));
});
},
host_lattice_bytes(handle, operation, outputPointer, capacity, writtenPointer, requiredPointer) {
return providers.invoke(handle, "lattice", (provider) => {
let value;
if (operation === HOST_LATTICE_STABLE) {
value = provider.stableBytes();
if (!(value instanceof Uint8Array)) {
throw new TypeError("lattice stableBytes must return Uint8Array");
}
} else if (operation === HOST_LATTICE_DIAGNOSTIC) {
const diagnostic = provider.diagnostic();
if (typeof diagnostic !== "string") {
throw new TypeError("lattice diagnostic must return a string");
}
value = encoder.encode(diagnostic);
} else {
throw new TypeError("unknown lattice byte operation");
}
if (value.byteLength > MAXIMUM_PROVIDER_BYTES) {
throw new RangeError("lattice provider bytes exceed the defensive limit");
}
const count = Math.min(capacity, value.byteLength);
if (count !== 0) {
new Uint8Array(ffi.memory.buffer).set(value.subarray(0, count), outputPointer);
}
const memory = memoryView();
memory.setUint32(writtenPointer, count, true);
memory.setUint32(requiredPointer, value.byteLength, true);
});
},
host_lattice_many(
receiver,
othersPointer,
count,
operation,
handlePointer,
flagsPointer,
) {
return providers.invoke(receiver, "lattice", (provider, { domainId }) => {
const method = operation === HOST_LATTICE_JOIN
? "joinMany"
: operation === HOST_LATTICE_MEET ? "meetMany" : null;
if (method === null || typeof provider[method] !== "function") {
throw new TypeError("unknown or unavailable lattice batch operation");
}
writeLatticeResult(
provider[method](latticeHandleArray(othersPointer, count, domainId)),
domainId,
handlePointer,
flagsPointer,
);
});
},
host_semiring_construct(context, operation, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const method = operation === HOST_SEMIRING_ZERO
? "zero"
: operation === HOST_SEMIRING_ONE ? "one" : null;
if (method === null) throw new TypeError("unknown semiring constructor operation");
writeSemiringValue(context, provider[method](), outputPointer);
});
},
host_semiring_clone(context, value, outputPointer) {
return providers.invoke(context, "semiring", () => {
writeSemiringValue(context, semiringValue(context, value), outputPointer);
});
},
host_semiring_value_release(context, value) {
const contextSlot = providers.resolve(context, "semiring");
const valueSlot = providers.resolve(value, "semiring-value");
if (contextSlot === null || valueSlot === null) return HOST_CLOSED;
if (valueSlot.metadata.context !== context) return HOST_INVALID_ARGUMENT;
providers.release(value);
return HOST_OK;
},
host_semiring_binary(context, left, right, operation, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const method = operation === HOST_SEMIRING_PLUS
? "plus"
: operation === HOST_SEMIRING_TIMES ? "times" : null;
if (method === null) throw new TypeError("unknown semiring binary operation");
writeSemiringValue(
context,
provider[method](semiringValue(context, left), semiringValue(context, right)),
outputPointer,
);
});
},
host_semiring_compare(context, left, right, operation, epsilon, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const leftValue = semiringValue(context, left);
const rightValue = semiringValue(context, right);
const result = operation === HOST_SEMIRING_EQUAL
? provider.equal(leftValue, rightValue)
: operation === HOST_SEMIRING_APPROX_EQUAL
? provider.approximatelyEqual(leftValue, rightValue, epsilon)
: null;
if (typeof result !== "boolean") {
throw new TypeError("semiring equality must return a boolean");
}
memoryView().setUint8(outputPointer, Number(result));
});
},
host_semiring_order(context, left, right, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const result = provider.naturalOrder(
semiringValue(context, left), semiringValue(context, right),
);
const encoded = new Map([
["better", -1], ["equal", 0], ["worse", 1], ["incomparable", 2],
]).get(result);
if (encoded === undefined) throw new TypeError("invalid semiring natural-order result");
memoryView().setInt32(outputPointer, encoded, true);
});
},
host_semiring_bytes(
context,
value,
operation,
outputPointer,
capacity,
writtenPointer,
requiredPointer,
) {
return providers.invoke(context, "semiring", (provider) => {
let encoded;
if (operation === HOST_SEMIRING_STABLE) {
encoded = provider.stableBytes(semiringValue(context, value));
if (!(encoded instanceof Uint8Array)) {
throw new TypeError("semiring stableBytes must return Uint8Array");
}
} else if (operation === HOST_SEMIRING_DIAGNOSTIC) {
const diagnostic = value === 0
? provider.diagnostic()
: provider.diagnostic(semiringValue(context, value));
if (typeof diagnostic !== "string") {
throw new TypeError("semiring diagnostic must return a string");
}
encoded = encoder.encode(diagnostic);
} else {
throw new TypeError("unknown semiring byte operation");
}
if (encoded.byteLength > MAXIMUM_PROVIDER_BYTES) {
throw new RangeError("semiring provider bytes exceed the defensive limit");
}
const written = Math.min(capacity, encoded.byteLength);
if (written !== 0) {
new Uint8Array(ffi.memory.buffer).set(encoded.subarray(0, written), outputPointer);
}
const memory = memoryView();
memory.setUint32(writtenPointer, written, true);
memory.setUint32(requiredPointer, encoded.byteLength, true);
});
},
host_semiring_many(context, valuesPointer, count, operation, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const method = operation === HOST_SEMIRING_PLUS
? "plusMany"
: operation === HOST_SEMIRING_TIMES ? "timesMany" : null;
if (method === null || typeof provider[method] !== "function") {
throw new TypeError("unknown or unavailable semiring batch operation");
}
writeSemiringValue(
context,
provider[method](semiringValueArray(context, valuesPointer, count)),
outputPointer,
);
});
},
host_semiring_optional(
context,
left,
right,
operation,
handlePointer,
definedPointer,
) {
return providers.invoke(context, "semiring", (provider) => {
const method = operation === HOST_SEMIRING_DIVIDE
? "divide"
: operation === HOST_SEMIRING_LEFT_DIVIDE ? "leftDivide" : null;
if (method === null || typeof provider[method] !== "function") {
throw new TypeError("unknown or unavailable semiring division operation");
}
semiringOptionalResult(
context,
provider[method](semiringValue(context, left), semiringValue(context, right)),
handlePointer,
definedPointer,
);
});
},
host_semiring_star(context, value, handlePointer, definedPointer) {
return providers.invoke(context, "semiring", (provider) => {
if (typeof provider.star !== "function") {
throw new TypeError("semiring star is unavailable");
}
semiringOptionalResult(
context,
provider.star(semiringValue(context, value)),
handlePointer,
definedPointer,
);
});
},
host_semiring_numeric(context, value, operation, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const method = operation === HOST_SEMIRING_NUMERICAL
? "numericalValue"
: operation === HOST_SEMIRING_PROBABILITY ? "toProbability" : null;
if (method === null || typeof provider[method] !== "function") {
throw new TypeError("unknown or unavailable semiring numerical operation");
}
const result = exactNumber(
provider[method](semiringValue(context, value)), `semiring ${method} result`,
);
memoryView().setFloat64(outputPointer, result, true);
});
},
host_semiring_quantize(context, value, epsilon, outputPointer) {
return providers.invoke(context, "semiring", (provider) => {
const result = provider.quantize(semiringValue(context, value), epsilon);
if (typeof result !== "bigint" ||
result < -0x8000_0000_0000_0000n || result > 0x7fff_ffff_ffff_ffffn) {
throw new RangeError("semiring quantize must return a signed 64-bit bigint");
}
memoryView().setBigInt64(outputPointer, result, true);
});
},
host_semiring_closure_bound(context, boundPointer, knownPointer) {
return providers.invoke(context, "semiring", (_provider, metadata) => {
const memory = memoryView();
if (metadata.closureBound === null) {
memory.setUint32(boundPointer, 0, true);
memory.setUint8(knownPointer, 0);
} else {
if (metadata.closureBound > 0xffff_ffffn) {
throw new RangeError("WASI semiring closureBound exceeds u32");
}
memory.setUint32(boundPointer, Number(metadata.closureBound), true);
memory.setUint8(knownPointer, 1);
}
});
},
};
const imports = wasi.getImportObject();
imports.vinary_tree_host = host;
const instance = await WebAssembly.instantiate(module, imports);
wasi.initialize(instance);
ffi = instance.exports;
const runtimeIdentity = Object.freeze({ implementation: "vinary-tree-wasi-preview1-v1", instance });
function view() { return new DataView(ffi.memory.buffer); }
function bytes() { return new Uint8Array(ffi.memory.buffer); }
function failure(value) {
const unsigned = value >>> 0;
if (unsigned !== FAILURE) return unsigned;
const pointer = ffi.vt_error_pointer();
const length = ffi.vt_error_length();
throw new Error(decoder.decode(bytes().slice(pointer, pointer + length)));
}
function withBytes(value, operation) {
const encoded = typeof value === "string" ? encoder.encode(value) : value;
const pointer = ffi.vt_alloc(encoded.length);
try {
bytes().set(encoded, pointer);
return operation(pointer, encoded.length);
} finally {
ffi.vt_dealloc(pointer, encoded.length);
}
}
function withTokens(value, operation) {
if (!(value instanceof BigUint64Array)) {
throw new TypeError("u64 dictionary keys require BigUint64Array");
}
const encoded = new Uint8Array(value.length * 8);
const data = new DataView(encoded.buffer);
for (let index = 0; index < value.length; index += 1) {
data.setBigUint64(index * 8, value[index], true);
}
return withBytes(encoded, (pointer) => operation(pointer, value.length));
}
function select(table, value, kind) {
const selected = table.get(value);
if (selected === undefined) throw new TypeError(`unknown ${kind}: ${value}`);
return selected;
}
function cacheLimit(value, fallback, name) {
const selected = value ?? fallback;
if (!Number.isSafeInteger(selected) || selected < 0 || selected > 0xffff_ffff) {
throw new RangeError(`${name} must be an integer from 0 through 4294967295`);
}
return selected;
}
function requireLattice(value, domainId = undefined) {
if (value?.interfaceId !== "vt.lattice.val.1" || value.runtimeIdentity !== runtimeIdentity) {
throw new TypeError("lattice belongs to a different Vinary Tree runtime");
}
if (domainId !== undefined && value.domainId !== domainId) {
throw new TypeError("lattice belongs to a different semantic domain");
}
return value;
}
function requireSemiringWeight(value, semiring) {
if (value?.interfaceId !== "vt.semiring.val1" || value.runtimeIdentity !== runtimeIdentity ||
value._semiring !== semiring) {
throw new TypeError("semiring weight belongs to a different operation context");
}
return value;
}
function withLatticeHandles(values, maximum, domainId, operation) {
if (!Array.isArray(values) || values.length > maximum) {
throw new TypeError(`lattice values must be an array of at most ${maximum} entries`);
}
const encoded = new Uint8Array(values.length * 4);
const data = new DataView(encoded.buffer);
values.forEach((value, index) => {
data.setUint32(index * 4, requireLattice(value, domainId)._handle, true);
});
return withBytes(encoded, (pointer) => operation(pointer, values.length));
}
function withSemiringHandles(values, maximum, semiring, operation) {
if (!Array.isArray(values) || values.length > maximum) {
throw new TypeError(`semiring values must be an array of at most ${maximum} entries`);
}
const encoded = new Uint8Array(values.length * 4);
const data = new DataView(encoded.buffer);
values.forEach((value, index) => {
data.setUint32(
index * 4, requireSemiringWeight(value, semiring)._handle, true,
);
});
return withBytes(encoded, (pointer) => operation(pointer, values.length));
}
class Dictionary {
#handle;
#persistent;
constructor(handle, unitDomain, persistent) {
this.#handle = failure(handle);
this.#persistent = persistent;
Object.defineProperties(this, {
interfaceId: { value: "vt.dictionary.v1", enumerable: true },
runtimeIdentity: { value: runtimeIdentity, enumerable: true },
unitDomain: { value: unitDomain, enumerable: true },
valueDomain: { value: "optional-u64", enumerable: true },
});
}
get _handle() {
if (this.#handle === 0) throw new Error("dictionary is closed");
return this.#handle;
}
get size() { return failure(ffi.vt_dictionary_len(this._handle)); }
put(term, value = null) {
if (value !== null && (typeof value !== "bigint" || value < 0n || value > 0xffff_ffff_ffff_ffffn)) {
throw new RangeError("dictionary value must be null or an unsigned 64-bit bigint");
}
if (term instanceof BigUint64Array) {
return withTokens(term, (pointer, length) => Boolean(failure(
ffi.vt_dictionary_put_u64(this._handle, pointer, length, Number(value !== null), value ?? 0n),
)));
}
if (typeof term !== "string" && !(term instanceof Uint8Array)) {
throw new TypeError("dictionary key must be a string, Uint8Array, or BigUint64Array");
}
return withBytes(term, (pointer, length) => Boolean(failure(
ffi.vt_dictionary_put_text(this._handle, pointer, length, Number(value !== null), value ?? 0n),
)));
}
putU64(term, value = null) { return this.put(term, value); }
set(term, value = null) { this.put(term, value); return this; }
remove(term) {
if (term instanceof BigUint64Array) {
return withTokens(term, (pointer, length) => Boolean(failure(
ffi.vt_dictionary_remove_u64(this._handle, pointer, length),
)));
}
return withBytes(term, (pointer, length) => Boolean(failure(
ffi.vt_dictionary_remove_text(this._handle, pointer, length),
)));
}
removeU64(term) { return this.remove(term); }
delete(term) { return this.remove(term); }
lookup(term) {
const decode = (termPointer, termLength, operation) => {
const output = ffi.vt_alloc(16);
try {
failure(operation(this._handle, termPointer, termLength, output));
const memory = view();
const found = memory.getUint32(output, true) !== 0;
const present = memory.getUint32(output + 4, true) !== 0;
return { found, value: present ? memory.getBigUint64(output + 8, true) : null };
} finally {
ffi.vt_dealloc(output, 16);
}
};
return term instanceof BigUint64Array
? withTokens(term, (pointer, length) => decode(pointer, length, ffi.vt_dictionary_get_u64))
: withBytes(term, (pointer, length) => decode(pointer, length, ffi.vt_dictionary_get_text));
}
lookupU64(term) { return this.lookup(term); }
get(term) { const result = this.lookup(term); return result.found ? result.value : undefined; }
getU64(term) { return this.get(term); }
has(term) { return this.lookup(term).found; }
hasU64(term) { return this.has(term); }
streamEntries() { return new DictionaryEntryCursor(ffi.vt_dictionary_entries_open(this._handle)); }
algebra(right, operation, valueMerge = "last") {
if (!(right instanceof Dictionary)) {
throw new TypeError("dictionary algebra requires a dictionary from this runtime");
}
return new Dictionary(
ffi.vt_dictionary_algebra(
this._handle,
right._handle,
select(ALGEBRA_OPERATIONS, operation, "dictionary algebra operation"),
select(VALUE_MERGES, valueMerge, "dictionary value-merge policy"),
),
this.unitDomain,
false,
);
}
union(right, valueMerge = "last") { return this.algebra(right, "union", valueMerge); }
intersection(right, valueMerge = "lattice-meet") {
return this.algebra(right, "intersection", valueMerge);
}
difference(right) { return this.algebra(right, "difference"); }
symmetricDifference(right) { return this.algebra(right, "symmetric-difference"); }
snapshotEntries() {
const result = [];
const cursor = this.streamEntries();
try {
for (const [key, value] of cursor) result.push(Object.freeze([key, value]));
} finally {
cursor.close();
}
return Object.freeze(result);
}
entries() { return this.snapshotEntries()[Symbol.iterator](); }
[Symbol.iterator]() { return this.entries(); }
*keys() { for (const [key] of this) yield key; }
*values() { for (const [, value] of this) yield value; }
forEach(callback, thisArg = undefined) {
for (const [key, value] of this) callback.call(thisArg, value, key, this);
}
toMap() {
if (this.unitDomain !== "unicode") {
throw new TypeError("toMap is defined only for value-equal JavaScript string keys");
}
return new Map(this);
}
clear() { failure(ffi.vt_dictionary_clear(this._handle)); }
compact() { return failure(ffi.vt_dictionary_compact(this._handle)); }
checkpoint() {
if (!this.#persistent) throw new Error("in-memory dictionaries do not checkpoint");
failure(ffi.vt_dictionary_checkpoint(this._handle));
}
close() {
if (this.#handle !== 0) {
ffi.vt_handle_close(this.#handle);
this.#handle = 0;
}
}
[Symbol.dispose]() { this.close(); }
}
class DictionaryEntryCursor {
#handle;
#pending = [];
#offset = 0;
constructor(handle) {
this.#handle = failure(handle);
this.size = failure(ffi.vt_entry_cursor_len(this.#handle));
this.identity = null;
}
[Symbol.iterator]() { return this; }
next() {
if (this.#offset >= this.#pending.length) {
this.#pending = this.#fetch(256);
this.#offset = 0;
}
if (this.#pending.length === 0) {
this.close();
return { done: true, value: undefined };
}
return { done: false, value: this.#pending[this.#offset++] };
}
nextBatch(maximum) {
if (!Number.isSafeInteger(maximum) || maximum <= 0) {
throw new RangeError("batch size must be a positive safe integer");
}
const result = [];
while (result.length < maximum) {
while (this.#offset < this.#pending.length && result.length < maximum) {
result.push(this.#pending[this.#offset++]);
}
if (result.length === maximum || this.#handle === 0) break;
this.#pending = this.#fetch(maximum - result.length);
this.#offset = 0;
if (this.#pending.length === 0) {
this.close();
break;
}
}
return result;
}
#fetch(maximum) {
if (this.#handle === 0) return [];
const count = failure(ffi.vt_entry_cursor_next_batch(this.#handle, maximum));
if (count === 0) return [];
let record = failure(ffi.vt_entry_cursor_batch_pointer(this.#handle));
const memory = view();
const output = [];
for (let index = 0; index < count; index += 1) {
const payloadLength = memory.getUint32(record, true);
const domain = memory.getUint32(record + 4, true);
const present = memory.getUint32(record + 8, true) !== 0;
const value = present ? memory.getBigUint64(record + 16, true) : null;
const payload = record + ENTRY_RECORD_HEADER_SIZE;
let key;
if (domain === 0) {
key = bytes().slice(payload, payload + payloadLength);
} else if (domain === 1) {
key = decoder.decode(bytes().slice(payload, payload + payloadLength));
} else {
if (payloadLength % 8 !== 0) throw new Error("invalid u64 dictionary entry payload");
key = new BigUint64Array(payloadLength / 8);
for (let token = 0; token < key.length; token += 1) {
key[token] = memory.getBigUint64(payload + token * 8, true);
}
}
output.push([key, value]);
record = payload + payloadLength;
}
return output;
}
reduceBatches(reducer, initial, batchSize = 256) {
let accumulator = initial;
try {
for (;;) {
const batch = this.nextBatch(batchSize);
if (batch.length === 0) return accumulator;
accumulator = reducer(accumulator, batch);
}
} finally {
this.close();
}
}
return() { this.close(); return { done: true, value: undefined }; }
close() {