eliscript 0.0.1
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/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VECTOR_STATE,
|
|
3
|
+
TRANSIENT_VECTOR_STATE,
|
|
4
|
+
VectorNode,
|
|
5
|
+
readTransientVectorMetrics,
|
|
6
|
+
readVectorMetrics,
|
|
7
|
+
resetTransientVectorMetrics as resetTransientVectorMetricsInternal,
|
|
8
|
+
resetVectorMetrics,
|
|
9
|
+
} from "../core/vector-internals.mjs";
|
|
10
|
+
|
|
11
|
+
function stateOf(vector) {
|
|
12
|
+
const state = vector?.[VECTOR_STATE];
|
|
13
|
+
if (state === undefined) {
|
|
14
|
+
throw new TypeError("expected an Eliscript persistent vector");
|
|
15
|
+
}
|
|
16
|
+
return state;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function transientStateOf(vector) {
|
|
20
|
+
const state = vector?.[TRANSIENT_VECTOR_STATE];
|
|
21
|
+
if (state === undefined) {
|
|
22
|
+
throw new TypeError("expected an Eliscript transient vector");
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function collectNodes(node, nodes) {
|
|
28
|
+
if (nodes.has(node)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
nodes.add(node);
|
|
32
|
+
for (const child of node.slots) {
|
|
33
|
+
if (child instanceof VectorNode) {
|
|
34
|
+
collectNodes(child, nodes);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function resetPersistentVectorMetrics() {
|
|
40
|
+
resetVectorMetrics();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function persistentVectorMetrics() {
|
|
44
|
+
return readVectorMetrics();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function resetTransientVectorMetrics() {
|
|
48
|
+
resetTransientVectorMetricsInternal();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function transientVectorMetrics() {
|
|
52
|
+
return readTransientVectorMetrics();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function inspectTransientVector(vector) {
|
|
56
|
+
const state = transientStateOf(vector);
|
|
57
|
+
const nodes = new Set();
|
|
58
|
+
collectNodes(state.root, nodes);
|
|
59
|
+
let ownedNodeCount = 0;
|
|
60
|
+
for (const node of nodes) {
|
|
61
|
+
if (node.owner === state.owner) {
|
|
62
|
+
ownedNodeCount += 1;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return Object.freeze({
|
|
66
|
+
active: state.active,
|
|
67
|
+
count: state.count,
|
|
68
|
+
shift: state.shift,
|
|
69
|
+
tailOwned: state.tailOwned,
|
|
70
|
+
nodeCount: nodes.size,
|
|
71
|
+
ownedNodeCount,
|
|
72
|
+
sharesSourceRoot: state.root === state.source?.[VECTOR_STATE].root,
|
|
73
|
+
sharesSourceTail: state.tail === state.source?.[VECTOR_STATE].tail,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function inspectPersistentVector(vector) {
|
|
78
|
+
const state = stateOf(vector);
|
|
79
|
+
const nodes = new Set();
|
|
80
|
+
collectNodes(state.root, nodes);
|
|
81
|
+
return Object.freeze({
|
|
82
|
+
count: state.count,
|
|
83
|
+
shift: state.shift,
|
|
84
|
+
depth: state.shift / 5 + 1,
|
|
85
|
+
tailLength: state.tail.length,
|
|
86
|
+
nodeCount: nodes.size,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function sharedPersistentVectorNodes(left, right) {
|
|
91
|
+
const leftNodes = new Set();
|
|
92
|
+
const rightNodes = new Set();
|
|
93
|
+
collectNodes(stateOf(left).root, leftNodes);
|
|
94
|
+
collectNodes(stateOf(right).root, rightNodes);
|
|
95
|
+
let shared = 0;
|
|
96
|
+
for (const node of leftNodes) {
|
|
97
|
+
if (rightNodes.has(node)) {
|
|
98
|
+
shared += 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return shared;
|
|
102
|
+
}
|
|
@@ -0,0 +1,565 @@
|
|
|
1
|
+
import {
|
|
2
|
+
eliscriptSymbol,
|
|
3
|
+
isEliscriptSymbol,
|
|
4
|
+
isKeyword,
|
|
5
|
+
keyword,
|
|
6
|
+
} from "./core/identifier.mjs";
|
|
7
|
+
import {
|
|
8
|
+
isPersistentList,
|
|
9
|
+
persistentList,
|
|
10
|
+
} from "./core/list.mjs";
|
|
11
|
+
import {
|
|
12
|
+
isPersistentHashMap,
|
|
13
|
+
persistentHashMap,
|
|
14
|
+
} from "./core/map.mjs";
|
|
15
|
+
import { meta, withMeta } from "./core/metadata.mjs";
|
|
16
|
+
import {
|
|
17
|
+
isPersistentHashSet,
|
|
18
|
+
persistentHashSet,
|
|
19
|
+
} from "./core/set.mjs";
|
|
20
|
+
import {
|
|
21
|
+
isPersistentVector,
|
|
22
|
+
persistentVector,
|
|
23
|
+
} from "./core/vector.mjs";
|
|
24
|
+
|
|
25
|
+
export const workerValueEncoding = "eliscript-value-v1";
|
|
26
|
+
|
|
27
|
+
export const workerValueLimits = Object.freeze({
|
|
28
|
+
maxDepth: 64,
|
|
29
|
+
maxNodes: 100_000,
|
|
30
|
+
maxCollectionLength: 100_000,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const SPECIAL_NUMBERS = new Map([
|
|
34
|
+
["nan", Number.NaN],
|
|
35
|
+
["positive-infinity", Number.POSITIVE_INFINITY],
|
|
36
|
+
["negative-infinity", Number.NEGATIVE_INFINITY],
|
|
37
|
+
["negative-zero", -0],
|
|
38
|
+
]);
|
|
39
|
+
const wireEncoder = new TextEncoder();
|
|
40
|
+
|
|
41
|
+
export class WorkerValueCodecError extends TypeError {
|
|
42
|
+
constructor(code, message, path = "$") {
|
|
43
|
+
super(`${message} at ${path}`);
|
|
44
|
+
this.name = "WorkerValueCodecError";
|
|
45
|
+
this.code = code;
|
|
46
|
+
this.path = path;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function normalizedLimits(options = {}) {
|
|
51
|
+
const limits = { ...workerValueLimits, ...options };
|
|
52
|
+
for (const name of Object.keys(workerValueLimits)) {
|
|
53
|
+
if (!Number.isInteger(limits[name]) || limits[name] < 1) {
|
|
54
|
+
throw new TypeError(`${name} must be a positive integer`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return limits;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function createState(mode, options) {
|
|
61
|
+
return {
|
|
62
|
+
active: new WeakSet(),
|
|
63
|
+
limits: normalizedLimits(options),
|
|
64
|
+
mode,
|
|
65
|
+
nodes: 0,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function visit(state, depth, path) {
|
|
70
|
+
state.nodes += 1;
|
|
71
|
+
if (state.nodes > state.limits.maxNodes) {
|
|
72
|
+
throw new WorkerValueCodecError(
|
|
73
|
+
`value-${state.mode}-limit`,
|
|
74
|
+
`value exceeds ${state.limits.maxNodes} nodes`,
|
|
75
|
+
path,
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
if (depth > state.limits.maxDepth) {
|
|
79
|
+
throw new WorkerValueCodecError(
|
|
80
|
+
`value-${state.mode}-limit`,
|
|
81
|
+
`value exceeds depth ${state.limits.maxDepth}`,
|
|
82
|
+
path,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function collectionLength(state, length, path) {
|
|
88
|
+
if (length > state.limits.maxCollectionLength) {
|
|
89
|
+
throw new WorkerValueCodecError(
|
|
90
|
+
`value-${state.mode}-limit`,
|
|
91
|
+
`collection exceeds ${state.limits.maxCollectionLength} values`,
|
|
92
|
+
path,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function withActive(state, value, path, operation) {
|
|
98
|
+
if (state.active.has(value)) {
|
|
99
|
+
throw new WorkerValueCodecError(
|
|
100
|
+
"value-encoding-cycle",
|
|
101
|
+
"cyclic values are not transportable",
|
|
102
|
+
path,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
state.active.add(value);
|
|
106
|
+
try {
|
|
107
|
+
return operation();
|
|
108
|
+
} finally {
|
|
109
|
+
state.active.delete(value);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function encodedMetadata(value, state, depth, path) {
|
|
114
|
+
const metadata = meta(value);
|
|
115
|
+
return metadata === null
|
|
116
|
+
? null
|
|
117
|
+
: encodeValue(metadata, state, depth + 1, `${path}.metadata`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function encodeSequence(tag, value, state, depth, path) {
|
|
121
|
+
return withActive(state, value, path, () => {
|
|
122
|
+
collectionLength(state, value.count, path);
|
|
123
|
+
const values = [];
|
|
124
|
+
let index = 0;
|
|
125
|
+
for (const item of value) {
|
|
126
|
+
values.push(encodeValue(item, state, depth + 1, `${path}[${index}]`));
|
|
127
|
+
index += 1;
|
|
128
|
+
}
|
|
129
|
+
return [tag, values, encodedMetadata(value, state, depth, path)];
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function wireKey(value) {
|
|
134
|
+
return wireEncoder.encode(JSON.stringify(value));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function compareWireKeys(left, right) {
|
|
138
|
+
const length = Math.min(left.length, right.length);
|
|
139
|
+
for (let index = 0; index < length; index += 1) {
|
|
140
|
+
if (left[index] !== right[index]) return left[index] - right[index];
|
|
141
|
+
}
|
|
142
|
+
return left.length - right.length;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function compareWireValues(left, right) {
|
|
146
|
+
return compareWireKeys(wireKey(left), wireKey(right));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function compareText(left, right) {
|
|
150
|
+
return compareWireKeys(wireEncoder.encode(left), wireEncoder.encode(right));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function encodeMap(value, state, depth, path) {
|
|
154
|
+
return withActive(state, value, path, () => {
|
|
155
|
+
collectionLength(state, value.count, path);
|
|
156
|
+
const entries = [];
|
|
157
|
+
let index = 0;
|
|
158
|
+
for (const [key, item] of value) {
|
|
159
|
+
entries.push([
|
|
160
|
+
encodeValue(key, state, depth + 1, `${path}.keys[${index}]`),
|
|
161
|
+
encodeValue(item, state, depth + 1, `${path}.values[${index}]`),
|
|
162
|
+
]);
|
|
163
|
+
index += 1;
|
|
164
|
+
}
|
|
165
|
+
entries.sort(compareWireValues);
|
|
166
|
+
return ["map", entries, encodedMetadata(value, state, depth, path)];
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function encodeSet(value, state, depth, path) {
|
|
171
|
+
return withActive(state, value, path, () => {
|
|
172
|
+
collectionLength(state, value.count, path);
|
|
173
|
+
const values = [];
|
|
174
|
+
let index = 0;
|
|
175
|
+
for (const item of value) {
|
|
176
|
+
values.push(encodeValue(item, state, depth + 1, `${path}[${index}]`));
|
|
177
|
+
index += 1;
|
|
178
|
+
}
|
|
179
|
+
values.sort(compareWireValues);
|
|
180
|
+
return ["set", values, encodedMetadata(value, state, depth, path)];
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function encodeArray(value, state, depth, path) {
|
|
185
|
+
return withActive(state, value, path, () => {
|
|
186
|
+
collectionLength(state, value.length, path);
|
|
187
|
+
const values = [];
|
|
188
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
189
|
+
values.push(encodeValue(
|
|
190
|
+
value[index],
|
|
191
|
+
state,
|
|
192
|
+
depth + 1,
|
|
193
|
+
`${path}[${index}]`,
|
|
194
|
+
));
|
|
195
|
+
}
|
|
196
|
+
return [
|
|
197
|
+
"array",
|
|
198
|
+
values,
|
|
199
|
+
];
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function encodeObject(value, state, depth, path) {
|
|
204
|
+
return withActive(state, value, path, () => {
|
|
205
|
+
const prototype = Object.getPrototypeOf(value);
|
|
206
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
207
|
+
throw new WorkerValueCodecError(
|
|
208
|
+
"value-encoding-unsupported",
|
|
209
|
+
`unsupported object ${Object.prototype.toString.call(value)}`,
|
|
210
|
+
path,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
const symbolKeys = Object.getOwnPropertySymbols(value).filter(
|
|
214
|
+
(key) => Object.prototype.propertyIsEnumerable.call(value, key),
|
|
215
|
+
);
|
|
216
|
+
if (symbolKeys.length > 0) {
|
|
217
|
+
throw new WorkerValueCodecError(
|
|
218
|
+
"value-encoding-unsupported",
|
|
219
|
+
"host objects with enumerable Symbol keys are not transportable",
|
|
220
|
+
path,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
224
|
+
const keys = Object.keys(descriptors)
|
|
225
|
+
.filter((key) => descriptors[key].enumerable)
|
|
226
|
+
.sort(compareText);
|
|
227
|
+
collectionLength(state, keys.length, path);
|
|
228
|
+
const entries = keys.map((key) => {
|
|
229
|
+
const descriptor = descriptors[key];
|
|
230
|
+
if (!("value" in descriptor)) {
|
|
231
|
+
throw new WorkerValueCodecError(
|
|
232
|
+
"value-encoding-unsupported",
|
|
233
|
+
"host object accessors are not transportable",
|
|
234
|
+
`${path}.${key}`,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
return [
|
|
238
|
+
key,
|
|
239
|
+
encodeValue(descriptor.value, state, depth + 1, `${path}.${key}`),
|
|
240
|
+
];
|
|
241
|
+
});
|
|
242
|
+
return ["object", entries];
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function encodeValue(value, state, depth, path) {
|
|
247
|
+
visit(state, depth, path);
|
|
248
|
+
if (value === null || typeof value === "string" ||
|
|
249
|
+
typeof value === "boolean") {
|
|
250
|
+
return value;
|
|
251
|
+
}
|
|
252
|
+
if (value === undefined) return ["undefined"];
|
|
253
|
+
if (typeof value === "number") {
|
|
254
|
+
if (Number.isNaN(value)) return ["number", "nan"];
|
|
255
|
+
if (value === Number.POSITIVE_INFINITY) {
|
|
256
|
+
return ["number", "positive-infinity"];
|
|
257
|
+
}
|
|
258
|
+
if (value === Number.NEGATIVE_INFINITY) {
|
|
259
|
+
return ["number", "negative-infinity"];
|
|
260
|
+
}
|
|
261
|
+
if (Object.is(value, -0)) return ["number", "negative-zero"];
|
|
262
|
+
return value;
|
|
263
|
+
}
|
|
264
|
+
if (isKeyword(value)) {
|
|
265
|
+
return ["keyword", value.namespace, value.name];
|
|
266
|
+
}
|
|
267
|
+
if (isEliscriptSymbol(value)) {
|
|
268
|
+
return withActive(state, value, path, () => [
|
|
269
|
+
"symbol",
|
|
270
|
+
value.namespace,
|
|
271
|
+
value.name,
|
|
272
|
+
encodedMetadata(value, state, depth, path),
|
|
273
|
+
]);
|
|
274
|
+
}
|
|
275
|
+
if (isPersistentList(value)) {
|
|
276
|
+
return encodeSequence("list", value, state, depth, path);
|
|
277
|
+
}
|
|
278
|
+
if (isPersistentVector(value)) {
|
|
279
|
+
return encodeSequence("vector", value, state, depth, path);
|
|
280
|
+
}
|
|
281
|
+
if (isPersistentHashMap(value)) {
|
|
282
|
+
return encodeMap(value, state, depth, path);
|
|
283
|
+
}
|
|
284
|
+
if (isPersistentHashSet(value)) {
|
|
285
|
+
return encodeSet(value, state, depth, path);
|
|
286
|
+
}
|
|
287
|
+
if (Array.isArray(value)) return encodeArray(value, state, depth, path);
|
|
288
|
+
if (typeof value === "object") {
|
|
289
|
+
return encodeObject(value, state, depth, path);
|
|
290
|
+
}
|
|
291
|
+
throw new WorkerValueCodecError(
|
|
292
|
+
"value-encoding-unsupported",
|
|
293
|
+
`unsupported ${typeof value} value`,
|
|
294
|
+
path,
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function requireArray(value, message, path) {
|
|
299
|
+
if (!Array.isArray(value)) {
|
|
300
|
+
throw new WorkerValueCodecError("value-decoding-invalid", message, path);
|
|
301
|
+
}
|
|
302
|
+
return value;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function requireTuple(node, length, tag, path) {
|
|
306
|
+
if (node.length !== length) {
|
|
307
|
+
throw new WorkerValueCodecError(
|
|
308
|
+
"value-decoding-invalid",
|
|
309
|
+
`${tag} node must contain ${length} fields`,
|
|
310
|
+
path,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function requireName(value, label, path, allowNull = false) {
|
|
316
|
+
if (allowNull && value === null) return null;
|
|
317
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
318
|
+
throw new WorkerValueCodecError(
|
|
319
|
+
"value-decoding-invalid",
|
|
320
|
+
`${label} must be a non-empty string${allowNull ? " or null" : ""}`,
|
|
321
|
+
path,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
324
|
+
return value;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function decodedMetadata(node, state, depth, path) {
|
|
328
|
+
if (node === null) return null;
|
|
329
|
+
const metadata = decodeValue(node, state, depth + 1, path);
|
|
330
|
+
if (!isPersistentHashMap(metadata)) {
|
|
331
|
+
throw new WorkerValueCodecError(
|
|
332
|
+
"value-decoding-invalid",
|
|
333
|
+
"metadata must decode to a persistent Map",
|
|
334
|
+
path,
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
return metadata;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
function decodeSequence(node, state, depth, path, constructor) {
|
|
341
|
+
requireTuple(node, 3, node[0], path);
|
|
342
|
+
const values = requireArray(node[1], `${node[0]} values must be an array`, path);
|
|
343
|
+
collectionLength(state, values.length, path);
|
|
344
|
+
const decoded = values.map((value, index) =>
|
|
345
|
+
decodeValue(value, state, depth + 1, `${path}[${index}]`));
|
|
346
|
+
const result = constructor(...decoded);
|
|
347
|
+
const metadata = decodedMetadata(
|
|
348
|
+
node[2],
|
|
349
|
+
state,
|
|
350
|
+
depth,
|
|
351
|
+
`${path}.metadata`,
|
|
352
|
+
);
|
|
353
|
+
return metadata === null ? result : withMeta(result, metadata);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
function decodeMap(node, state, depth, path) {
|
|
357
|
+
requireTuple(node, 3, "map", path);
|
|
358
|
+
const entries = requireArray(node[1], "map entries must be an array", path);
|
|
359
|
+
collectionLength(state, entries.length, path);
|
|
360
|
+
let result = persistentHashMap();
|
|
361
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
362
|
+
const entry = requireArray(
|
|
363
|
+
entries[index],
|
|
364
|
+
"map entry must be a two-field array",
|
|
365
|
+
`${path}[${index}]`,
|
|
366
|
+
);
|
|
367
|
+
requireTuple(entry, 2, "map entry", `${path}[${index}]`);
|
|
368
|
+
const key = decodeValue(entry[0], state, depth + 1, `${path}.keys[${index}]`);
|
|
369
|
+
if (result.has(key)) {
|
|
370
|
+
throw new WorkerValueCodecError(
|
|
371
|
+
"value-decoding-invalid",
|
|
372
|
+
"map contains an equivalent duplicate key",
|
|
373
|
+
`${path}.keys[${index}]`,
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
const value = decodeValue(
|
|
377
|
+
entry[1],
|
|
378
|
+
state,
|
|
379
|
+
depth + 1,
|
|
380
|
+
`${path}.values[${index}]`,
|
|
381
|
+
);
|
|
382
|
+
result = result.assoc(key, value);
|
|
383
|
+
}
|
|
384
|
+
const metadata = decodedMetadata(
|
|
385
|
+
node[2],
|
|
386
|
+
state,
|
|
387
|
+
depth,
|
|
388
|
+
`${path}.metadata`,
|
|
389
|
+
);
|
|
390
|
+
return metadata === null ? result : withMeta(result, metadata);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function decodeSet(node, state, depth, path) {
|
|
394
|
+
requireTuple(node, 3, "set", path);
|
|
395
|
+
const values = requireArray(node[1], "set values must be an array", path);
|
|
396
|
+
collectionLength(state, values.length, path);
|
|
397
|
+
let result = persistentHashSet();
|
|
398
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
399
|
+
const value = decodeValue(values[index], state, depth + 1, `${path}[${index}]`);
|
|
400
|
+
if (result.has(value)) {
|
|
401
|
+
throw new WorkerValueCodecError(
|
|
402
|
+
"value-decoding-invalid",
|
|
403
|
+
"set contains an equivalent duplicate value",
|
|
404
|
+
`${path}[${index}]`,
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
result = result.conj(value);
|
|
408
|
+
}
|
|
409
|
+
const metadata = decodedMetadata(
|
|
410
|
+
node[2],
|
|
411
|
+
state,
|
|
412
|
+
depth,
|
|
413
|
+
`${path}.metadata`,
|
|
414
|
+
);
|
|
415
|
+
return metadata === null ? result : withMeta(result, metadata);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
function decodeArray(node, state, depth, path) {
|
|
419
|
+
requireTuple(node, 2, "array", path);
|
|
420
|
+
const values = requireArray(node[1], "array values must be an array", path);
|
|
421
|
+
collectionLength(state, values.length, path);
|
|
422
|
+
return values.map((value, index) =>
|
|
423
|
+
decodeValue(value, state, depth + 1, `${path}[${index}]`));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function decodeObject(node, state, depth, path) {
|
|
427
|
+
requireTuple(node, 2, "object", path);
|
|
428
|
+
const entries = requireArray(node[1], "object entries must be an array", path);
|
|
429
|
+
collectionLength(state, entries.length, path);
|
|
430
|
+
const result = {};
|
|
431
|
+
const keys = new Set();
|
|
432
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
433
|
+
const entry = requireArray(
|
|
434
|
+
entries[index],
|
|
435
|
+
"object entry must be a two-field array",
|
|
436
|
+
`${path}[${index}]`,
|
|
437
|
+
);
|
|
438
|
+
requireTuple(entry, 2, "object entry", `${path}[${index}]`);
|
|
439
|
+
const key = requireName(entry[0], "object key", `${path}.keys[${index}]`);
|
|
440
|
+
if (keys.has(key)) {
|
|
441
|
+
throw new WorkerValueCodecError(
|
|
442
|
+
"value-decoding-invalid",
|
|
443
|
+
`object contains duplicate key ${key}`,
|
|
444
|
+
`${path}.keys[${index}]`,
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
keys.add(key);
|
|
448
|
+
Object.defineProperty(result, key, {
|
|
449
|
+
configurable: true,
|
|
450
|
+
enumerable: true,
|
|
451
|
+
value: decodeValue(
|
|
452
|
+
entry[1],
|
|
453
|
+
state,
|
|
454
|
+
depth + 1,
|
|
455
|
+
`${path}.${key}`,
|
|
456
|
+
),
|
|
457
|
+
writable: true,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
return result;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
function decodeValue(node, state, depth, path) {
|
|
464
|
+
visit(state, depth, path);
|
|
465
|
+
if (node === null || typeof node === "string" ||
|
|
466
|
+
typeof node === "boolean") {
|
|
467
|
+
return node;
|
|
468
|
+
}
|
|
469
|
+
if (typeof node === "number") return node;
|
|
470
|
+
requireArray(node, "encoded value must be a scalar or tagged array", path);
|
|
471
|
+
if (node.length === 0 || typeof node[0] !== "string") {
|
|
472
|
+
throw new WorkerValueCodecError(
|
|
473
|
+
"value-decoding-invalid",
|
|
474
|
+
"encoded value tag must be a non-empty string",
|
|
475
|
+
path,
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
switch (node[0]) {
|
|
479
|
+
case "undefined":
|
|
480
|
+
requireTuple(node, 1, "undefined", path);
|
|
481
|
+
return undefined;
|
|
482
|
+
case "number": {
|
|
483
|
+
requireTuple(node, 2, "number", path);
|
|
484
|
+
if (!SPECIAL_NUMBERS.has(node[1])) {
|
|
485
|
+
throw new WorkerValueCodecError(
|
|
486
|
+
"value-decoding-invalid",
|
|
487
|
+
`unknown special number ${String(node[1])}`,
|
|
488
|
+
path,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
return SPECIAL_NUMBERS.get(node[1]);
|
|
492
|
+
}
|
|
493
|
+
case "keyword": {
|
|
494
|
+
requireTuple(node, 3, "keyword", path);
|
|
495
|
+
const namespace = requireName(node[1], "keyword namespace", path, true);
|
|
496
|
+
const name = requireName(node[2], "keyword name", path);
|
|
497
|
+
return namespace === null ? keyword(name) : keyword(namespace, name);
|
|
498
|
+
}
|
|
499
|
+
case "symbol": {
|
|
500
|
+
requireTuple(node, 4, "symbol", path);
|
|
501
|
+
const namespace = requireName(node[1], "symbol namespace", path, true);
|
|
502
|
+
const name = requireName(node[2], "symbol name", path);
|
|
503
|
+
const result = namespace === null
|
|
504
|
+
? eliscriptSymbol(name)
|
|
505
|
+
: eliscriptSymbol(namespace, name);
|
|
506
|
+
const metadata = decodedMetadata(
|
|
507
|
+
node[3],
|
|
508
|
+
state,
|
|
509
|
+
depth,
|
|
510
|
+
`${path}.metadata`,
|
|
511
|
+
);
|
|
512
|
+
return metadata === null ? result : withMeta(result, metadata);
|
|
513
|
+
}
|
|
514
|
+
case "list":
|
|
515
|
+
return decodeSequence(node, state, depth, path, persistentList);
|
|
516
|
+
case "vector":
|
|
517
|
+
return decodeSequence(node, state, depth, path, persistentVector);
|
|
518
|
+
case "map":
|
|
519
|
+
return decodeMap(node, state, depth, path);
|
|
520
|
+
case "set":
|
|
521
|
+
return decodeSet(node, state, depth, path);
|
|
522
|
+
case "array":
|
|
523
|
+
return decodeArray(node, state, depth, path);
|
|
524
|
+
case "object":
|
|
525
|
+
return decodeObject(node, state, depth, path);
|
|
526
|
+
default:
|
|
527
|
+
throw new WorkerValueCodecError(
|
|
528
|
+
"value-decoding-invalid",
|
|
529
|
+
`unknown value tag ${node[0]}`,
|
|
530
|
+
path,
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export function encodeWorkerValue(value, options) {
|
|
536
|
+
return encodeValue(value, createState("encoding", options), 0, "$");
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export function encodeWorkerValues(values, options) {
|
|
540
|
+
if (!Array.isArray(values)) {
|
|
541
|
+
throw new TypeError("values must be an array");
|
|
542
|
+
}
|
|
543
|
+
const state = createState("encoding", options);
|
|
544
|
+
collectionLength(state, values.length, "$arguments");
|
|
545
|
+
return values.map((value, index) =>
|
|
546
|
+
encodeValue(value, state, 0, `$arguments[${index}]`));
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
export function decodeWorkerValue(value, options) {
|
|
550
|
+
return decodeValue(value, createState("decoding", options), 0, "$");
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export function decodeWorkerValues(values, options) {
|
|
554
|
+
if (!Array.isArray(values)) {
|
|
555
|
+
throw new WorkerValueCodecError(
|
|
556
|
+
"value-decoding-invalid",
|
|
557
|
+
"encoded values must be an array",
|
|
558
|
+
"$arguments",
|
|
559
|
+
);
|
|
560
|
+
}
|
|
561
|
+
const state = createState("decoding", options);
|
|
562
|
+
collectionLength(state, values.length, "$arguments");
|
|
563
|
+
return values.map((value, index) =>
|
|
564
|
+
decodeValue(value, state, 0, `$arguments[${index}]`));
|
|
565
|
+
}
|