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.
Files changed (195) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +135 -0
  3. package/bin/eliscript +16 -0
  4. package/bin/eliscript-bootstrap +15 -0
  5. package/bin/eliscript-build +15 -0
  6. package/bin/eliscript-check +15 -0
  7. package/bin/eliscript-eval +15 -0
  8. package/bin/eliscript-format +15 -0
  9. package/bin/eliscript-org +10 -0
  10. package/bin/eliscript-portable +17 -0
  11. package/bin/eliscript-seed +10 -0
  12. package/bin/eliscript-seed-build +10 -0
  13. package/bin/eliscript-watch +15 -0
  14. package/bootstrap/README.md +100 -0
  15. package/bootstrap/compiler/analyzer.eli +1426 -0
  16. package/bootstrap/compiler/compiler.eli +472 -0
  17. package/bootstrap/compiler/emitter.eli +1745 -0
  18. package/bootstrap/compiler/expander.eli +2522 -0
  19. package/bootstrap/compiler/formatter.eli +359 -0
  20. package/bootstrap/compiler/ir.eli +297 -0
  21. package/bootstrap/compiler/lower.eli +661 -0
  22. package/bootstrap/compiler/project.eli +1120 -0
  23. package/bootstrap/compiler/reader.eli +321 -0
  24. package/bootstrap/compiler/source-map.eli +165 -0
  25. package/bootstrap/compiler/symbol.eli +92 -0
  26. package/bootstrap/compiler/syntax.eli +54 -0
  27. package/bootstrap/compiler/transient-analysis.eli +825 -0
  28. package/bootstrap/host/build.mjs +28 -0
  29. package/bootstrap/host/bun.mjs +282 -0
  30. package/bootstrap/host/check-cli.mjs +163 -0
  31. package/bootstrap/host/eliscript-cli.mjs +36 -0
  32. package/bootstrap/host/evaluation-cli.mjs +401 -0
  33. package/bootstrap/host/evaluation.mjs +560 -0
  34. package/bootstrap/host/format-cli.mjs +139 -0
  35. package/bootstrap/host/project-cli.mjs +295 -0
  36. package/bootstrap/host/project.mjs +1080 -0
  37. package/bootstrap/host/watch-cli.mjs +129 -0
  38. package/bootstrap/host/watch.mjs +267 -0
  39. package/browser/host.mjs +357 -0
  40. package/browser/worker.mjs +60 -0
  41. package/compiler/README.md +118 -0
  42. package/compiler/eliscript-analyzer.el +716 -0
  43. package/compiler/eliscript-cli.el +108 -0
  44. package/compiler/eliscript-diagnostic.el +158 -0
  45. package/compiler/eliscript-emitter.el +1351 -0
  46. package/compiler/eliscript-evaluation.el +259 -0
  47. package/compiler/eliscript-expander.el +1630 -0
  48. package/compiler/eliscript-form.el +79 -0
  49. package/compiler/eliscript-ir-emitter.el +1460 -0
  50. package/compiler/eliscript-ir.el +356 -0
  51. package/compiler/eliscript-lower.el +463 -0
  52. package/compiler/eliscript-macro-eval.el +811 -0
  53. package/compiler/eliscript-parameters.el +324 -0
  54. package/compiler/eliscript-portable.el +492 -0
  55. package/compiler/eliscript-project-cli.el +156 -0
  56. package/compiler/eliscript-project.el +1676 -0
  57. package/compiler/eliscript-reader.el +421 -0
  58. package/compiler/eliscript-source-map.el +192 -0
  59. package/compiler/eliscript-symbol.el +101 -0
  60. package/compiler/eliscript-transient-analysis.el +817 -0
  61. package/compiler/eliscript.el +239 -0
  62. package/dist/bootstrap/analyzer.mjs +968 -0
  63. package/dist/bootstrap/analyzer.mjs.map +1 -0
  64. package/dist/bootstrap/compiler.mjs +228 -0
  65. package/dist/bootstrap/compiler.mjs.map +1 -0
  66. package/dist/bootstrap/emitter.mjs +1173 -0
  67. package/dist/bootstrap/emitter.mjs.map +1 -0
  68. package/dist/bootstrap/expander.mjs +1469 -0
  69. package/dist/bootstrap/expander.mjs.map +1 -0
  70. package/dist/bootstrap/formatter.mjs +304 -0
  71. package/dist/bootstrap/formatter.mjs.map +1 -0
  72. package/dist/bootstrap/ir.mjs +193 -0
  73. package/dist/bootstrap/ir.mjs.map +1 -0
  74. package/dist/bootstrap/lower.mjs +344 -0
  75. package/dist/bootstrap/lower.mjs.map +1 -0
  76. package/dist/bootstrap/project.mjs +585 -0
  77. package/dist/bootstrap/project.mjs.map +1 -0
  78. package/dist/bootstrap/reader.mjs +259 -0
  79. package/dist/bootstrap/reader.mjs.map +1 -0
  80. package/dist/bootstrap/source-map.mjs +154 -0
  81. package/dist/bootstrap/source-map.mjs.map +1 -0
  82. package/dist/bootstrap/symbol.mjs +74 -0
  83. package/dist/bootstrap/symbol.mjs.map +1 -0
  84. package/dist/bootstrap/syntax.mjs +38 -0
  85. package/dist/bootstrap/syntax.mjs.map +1 -0
  86. package/dist/bootstrap/transient-analysis.mjs +660 -0
  87. package/dist/bootstrap/transient-analysis.mjs.map +1 -0
  88. package/dist/browser/compiler.js +6491 -0
  89. package/editor/README.md +83 -0
  90. package/editor/eliscript-mode.el +878 -0
  91. package/editor/eliscript-repl.el +501 -0
  92. package/package.json +163 -0
  93. package/platform/README.md +48 -0
  94. package/platform/browser.mjs +204 -0
  95. package/platform/worker.mjs +166 -0
  96. package/runtime/README.md +296 -0
  97. package/runtime/core/collection-internals.mjs +512 -0
  98. package/runtime/core/collection.mjs +645 -0
  99. package/runtime/core/data-text.mjs +532 -0
  100. package/runtime/core/data.mjs +268 -0
  101. package/runtime/core/identifier.mjs +314 -0
  102. package/runtime/core/lazy-sequence.mjs +244 -0
  103. package/runtime/core/list-internals.mjs +18 -0
  104. package/runtime/core/list.mjs +273 -0
  105. package/runtime/core/map-internals.mjs +793 -0
  106. package/runtime/core/map.mjs +394 -0
  107. package/runtime/core/metadata-internals.mjs +14 -0
  108. package/runtime/core/metadata.mjs +44 -0
  109. package/runtime/core/object-impl.mjs +65 -0
  110. package/runtime/core/object-impl.mjs.map +1 -0
  111. package/runtime/core/object.mjs +13 -0
  112. package/runtime/core/order.mjs +234 -0
  113. package/runtime/core/persistent-kind.mjs +43 -0
  114. package/runtime/core/protocol-error.mjs +36 -0
  115. package/runtime/core/protocol-impl.mjs +326 -0
  116. package/runtime/core/protocol-impl.mjs.map +1 -0
  117. package/runtime/core/protocol.mjs +14 -0
  118. package/runtime/core/queue-internals.mjs +27 -0
  119. package/runtime/core/queue.mjs +238 -0
  120. package/runtime/core/record.mjs +346 -0
  121. package/runtime/core/regex.mjs +184 -0
  122. package/runtime/core/sequence.mjs +632 -0
  123. package/runtime/core/set-algebra.mjs +287 -0
  124. package/runtime/core/set-internals.mjs +33 -0
  125. package/runtime/core/set.mjs +378 -0
  126. package/runtime/core/sorted-internals.mjs +215 -0
  127. package/runtime/core/sorted.mjs +598 -0
  128. package/runtime/core/text-impl.mjs +95 -0
  129. package/runtime/core/text-impl.mjs.map +1 -0
  130. package/runtime/core/text.mjs +15 -0
  131. package/runtime/core/transducer.mjs +668 -0
  132. package/runtime/core/transient-internals.mjs +38 -0
  133. package/runtime/core/transient.mjs +53 -0
  134. package/runtime/core/truth.mjs +3 -0
  135. package/runtime/core/type.mjs +262 -0
  136. package/runtime/core/value-internals.mjs +188 -0
  137. package/runtime/core/value.mjs +93 -0
  138. package/runtime/core/vector-internals.mjs +102 -0
  139. package/runtime/core/vector.mjs +951 -0
  140. package/runtime/literals.mjs +34 -0
  141. package/runtime/source-mapping.mjs +220 -0
  142. package/runtime/testing/list.mjs +52 -0
  143. package/runtime/testing/map.mjs +145 -0
  144. package/runtime/testing/queue.mjs +49 -0
  145. package/runtime/testing/set.mjs +61 -0
  146. package/runtime/testing/sorted.mjs +105 -0
  147. package/runtime/testing/value.mjs +17 -0
  148. package/runtime/testing/vector.mjs +102 -0
  149. package/runtime/worker-value-codec.mjs +565 -0
  150. package/runtime/worker-value-stream.mjs +786 -0
  151. package/runtime/worker.mjs +975 -0
  152. package/stdlib/README.md +774 -0
  153. package/stdlib/bit.eli +36 -0
  154. package/stdlib/core/collection.eli +134 -0
  155. package/stdlib/core/data-text.eli +16 -0
  156. package/stdlib/core/data.eli +306 -0
  157. package/stdlib/core/identifier.eli +32 -0
  158. package/stdlib/core/lazy-sequence.eli +63 -0
  159. package/stdlib/core/metadata.eli +20 -0
  160. package/stdlib/core/object.eli +89 -0
  161. package/stdlib/core/order.eli +133 -0
  162. package/stdlib/core/protocol.eli +387 -0
  163. package/stdlib/core/record.eli +16 -0
  164. package/stdlib/core/regex.eli +17 -0
  165. package/stdlib/core/seq.eli +701 -0
  166. package/stdlib/core/set.eli +312 -0
  167. package/stdlib/core/sorted.eli +53 -0
  168. package/stdlib/core/text.eli +103 -0
  169. package/stdlib/core/transducer.eli +59 -0
  170. package/stdlib/core/transient.eli +24 -0
  171. package/stdlib/core/type.eli +19 -0
  172. package/stdlib/core/walk.eli +195 -0
  173. package/stdlib/core/zipper.eli +508 -0
  174. package/stdlib/data-text.eli +1092 -0
  175. package/stdlib/data.eli +42 -0
  176. package/stdlib/deferred.eli +70 -0
  177. package/stdlib/function.eli +120 -0
  178. package/stdlib/hierarchy.eli +245 -0
  179. package/stdlib/identifier.eli +147 -0
  180. package/stdlib/interop/js.eli +616 -0
  181. package/stdlib/json.eli +870 -0
  182. package/stdlib/metadata.eli +56 -0
  183. package/stdlib/multimethod.eli +464 -0
  184. package/stdlib/numeric.eli +193 -0
  185. package/stdlib/object.eli +96 -0
  186. package/stdlib/persistent-list.eli +145 -0
  187. package/stdlib/persistent-map.eli +724 -0
  188. package/stdlib/persistent-queue.eli +32 -0
  189. package/stdlib/persistent-set.eli +236 -0
  190. package/stdlib/persistent-vector.eli +342 -0
  191. package/stdlib/result.eli +141 -0
  192. package/stdlib/sequence.eli +106 -0
  193. package/stdlib/state/atom.eli +265 -0
  194. package/stdlib/text.eli +88 -0
  195. package/stdlib/value.eli +325 -0
@@ -0,0 +1,34 @@
1
+ import { persistentList } from "./core/list.mjs";
2
+ import { persistentHashMap } from "./core/map.mjs";
3
+ import { persistentQueue } from "./core/queue.mjs";
4
+ import { persistentHashSet } from "./core/set.mjs";
5
+ import { persistentVector } from "./core/vector.mjs";
6
+
7
+ export { eliscriptSymbol as symbol, keyword } from "./core/identifier.mjs";
8
+
9
+ export function list(...values) {
10
+ return persistentList(...values);
11
+ }
12
+
13
+ export function vector(...values) {
14
+ return persistentVector(...values);
15
+ }
16
+
17
+ export function hashMap(...keyValues) {
18
+ if (keyValues.length % 2 !== 0) {
19
+ throw new TypeError("hash-map expects complete key/value pairs");
20
+ }
21
+ const entries = [];
22
+ for (let index = 0; index < keyValues.length; index += 2) {
23
+ entries.push([keyValues[index], keyValues[index + 1]]);
24
+ }
25
+ return persistentHashMap(...entries);
26
+ }
27
+
28
+ export function hashSet(...values) {
29
+ return persistentHashSet(...values);
30
+ }
31
+
32
+ export function queue(...values) {
33
+ return persistentQueue(...values);
34
+ }
@@ -0,0 +1,220 @@
1
+ const base64Vlq = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2
+
3
+ export class SourceMappingError extends Error {
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.name = "SourceMappingError";
7
+ this.code = code;
8
+ }
9
+ }
10
+
11
+ function fail(code, message) {
12
+ throw new SourceMappingError(code, message);
13
+ }
14
+
15
+ function isPlainObject(value) {
16
+ return value !== null && typeof value === "object" && !Array.isArray(value);
17
+ }
18
+
19
+ function decodeVlq(segment) {
20
+ const values = [];
21
+ let index = 0;
22
+ while (index < segment.length) {
23
+ let value = 0;
24
+ let shift = 0;
25
+ let continuation;
26
+ do {
27
+ if (index >= segment.length) {
28
+ fail("invalid-vlq", "unterminated Base64 VLQ value");
29
+ }
30
+ const digit = base64Vlq.indexOf(segment[index]);
31
+ if (digit === -1) fail("invalid-vlq", "invalid Base64 VLQ digit");
32
+ index += 1;
33
+ value += (digit & 31) * (2 ** shift);
34
+ continuation = (digit & 32) !== 0;
35
+ shift += 5;
36
+ if (shift > 50) fail("invalid-vlq", "Base64 VLQ value exceeds safe bounds");
37
+ } while (continuation);
38
+ const negative = (value & 1) === 1;
39
+ const magnitude = Math.floor(value / 2);
40
+ values.push(negative ? -magnitude : magnitude);
41
+ }
42
+ return values;
43
+ }
44
+
45
+ export function decodeSourceMappings(mappings) {
46
+ if (typeof mappings !== "string") {
47
+ fail("invalid-mappings", "source mappings must be a string");
48
+ }
49
+ let source = 0;
50
+ let originalLine = 0;
51
+ let originalColumn = 0;
52
+ return Object.freeze(mappings.split(";").map((encodedLine) => {
53
+ let generatedColumn = 0;
54
+ const line = [];
55
+ for (const encoded of encodedLine.split(",")) {
56
+ if (encoded.length === 0) continue;
57
+ const values = decodeVlq(encoded);
58
+ if (![1, 4, 5].includes(values.length)) {
59
+ fail("invalid-segment", "source map segments must contain 1, 4, or 5 fields");
60
+ }
61
+ generatedColumn += values[0];
62
+ if (generatedColumn < 0) {
63
+ fail("invalid-segment", "generated source map columns must not be negative");
64
+ }
65
+ const decoded = { generatedColumn };
66
+ if (values.length >= 4) {
67
+ source += values[1];
68
+ originalLine += values[2];
69
+ originalColumn += values[3];
70
+ if (source < 0 || originalLine < 0 || originalColumn < 0) {
71
+ fail("invalid-segment", "decoded source map positions must not be negative");
72
+ }
73
+ decoded.source = source;
74
+ decoded.originalLine = originalLine;
75
+ decoded.originalColumn = originalColumn;
76
+ }
77
+ line.push(Object.freeze(decoded));
78
+ }
79
+ return Object.freeze(line);
80
+ }));
81
+ }
82
+
83
+ export function sourceMapDescriptor(value) {
84
+ if (!isPlainObject(value) || typeof value.generatedFile !== "string" ||
85
+ value.generatedFile.length === 0 || !Array.isArray(value.sources) ||
86
+ value.sources.length === 0 || value.sources.some(
87
+ (source) => typeof source !== "string" || source.length === 0,
88
+ )) {
89
+ fail(
90
+ "invalid-source-map",
91
+ "source map descriptor requires generatedFile sources and mappings",
92
+ );
93
+ }
94
+ const lines = value.lines === undefined
95
+ ? decodeSourceMappings(value.mappings)
96
+ : value.lines;
97
+ if (!Array.isArray(lines) || lines.some((line) => !Array.isArray(line))) {
98
+ fail("invalid-source-map", "source map descriptor lines must be arrays");
99
+ }
100
+ for (const line of lines) {
101
+ for (const segment of line) {
102
+ if (!isPlainObject(segment) ||
103
+ !Number.isInteger(segment.generatedColumn) ||
104
+ segment.generatedColumn < 0 ||
105
+ (segment.source !== undefined &&
106
+ (!Number.isInteger(segment.source) || segment.source < 0 ||
107
+ !Number.isInteger(segment.originalLine) ||
108
+ segment.originalLine < 0 ||
109
+ !Number.isInteger(segment.originalColumn) ||
110
+ segment.originalColumn < 0))) {
111
+ fail("invalid-source-map", "source map descriptor contains an invalid segment");
112
+ }
113
+ }
114
+ }
115
+ return Object.freeze({
116
+ generatedFile: value.generatedFile,
117
+ sources: Object.freeze([...value.sources]),
118
+ lines: Object.freeze(lines.map((line) => Object.freeze(
119
+ line.map((segment) => Object.freeze({ ...segment })),
120
+ ))),
121
+ });
122
+ }
123
+
124
+ export function parseJavaScriptStack(stack, options = {}) {
125
+ if (typeof stack !== "string") return Object.freeze([]);
126
+ const normalizeFile = typeof options.normalizeFile === "function"
127
+ ? options.normalizeFile
128
+ : (file) => file;
129
+ const frames = [];
130
+ for (const line of stack.split("\n")) {
131
+ const v8 = /^\s*at (?:(.*?) \()?(.+):(\d+):(\d+)\)?$/u.exec(line);
132
+ const browser = v8 ? null : /^(.*?)@(.+):(\d+):(\d+)$/u.exec(line.trim());
133
+ const match = v8 ?? browser;
134
+ if (!match) continue;
135
+ frames.push(Object.freeze({
136
+ function: match[1] || undefined,
137
+ file: normalizeFile(match[2]),
138
+ line: Number(match[3]),
139
+ column: Number(match[4]),
140
+ }));
141
+ }
142
+ return Object.freeze(frames);
143
+ }
144
+
145
+ function cleanIdentity(value) {
146
+ try {
147
+ const url = new URL(value);
148
+ url.search = "";
149
+ url.hash = "";
150
+ return url.href;
151
+ } catch {
152
+ return value.replace(/[?#].*$/u, "");
153
+ }
154
+ }
155
+
156
+ function sourceIdentities(value) {
157
+ const clean = cleanIdentity(value);
158
+ const identities = [clean];
159
+ try {
160
+ const url = new URL(clean);
161
+ if (url.protocol === "file:") identities.push(decodeURIComponent(url.pathname));
162
+ } catch {
163
+ // Plain filesystem paths and runtime pseudo-URLs keep their clean identity.
164
+ }
165
+ return identities;
166
+ }
167
+
168
+ export function mapSourceFrames(frames, sourceMaps) {
169
+ if (!Array.isArray(frames) || !Array.isArray(sourceMaps)) {
170
+ fail("invalid-frames", "frames and source maps must be arrays");
171
+ }
172
+ const mapsByFile = new Map();
173
+ for (const sourceMap of sourceMaps) {
174
+ for (const identity of sourceIdentities(sourceMap.generatedFile)) {
175
+ mapsByFile.set(identity, sourceMap);
176
+ }
177
+ }
178
+ return Object.freeze(frames.map((frame) => {
179
+ const sourceMap = sourceIdentities(frame.file)
180
+ .map((identity) => mapsByFile.get(identity))
181
+ .find(Boolean);
182
+ const mapping = sourceMap?.lines[frame.line - 1];
183
+ if (!mapping) return frame;
184
+ const generatedColumn = Math.max(0, frame.column - 1);
185
+ let segment;
186
+ for (const candidate of mapping) {
187
+ if (candidate.generatedColumn > generatedColumn) break;
188
+ segment = candidate;
189
+ }
190
+ if (segment?.source === undefined ||
191
+ sourceMap.sources[segment.source] === undefined) return frame;
192
+ return Object.freeze({
193
+ ...frame,
194
+ generated: Object.freeze({
195
+ file: frame.file,
196
+ line: frame.line,
197
+ column: frame.column,
198
+ }),
199
+ file: sourceMap.sources[segment.source],
200
+ line: segment.originalLine + 1,
201
+ column: segment.originalColumn + 1,
202
+ });
203
+ }));
204
+ }
205
+
206
+ export function sourceMappedFailure(error, sourceMaps, options = {}) {
207
+ const stack = typeof error?.stack === "string" ? error.stack : undefined;
208
+ const frames = stack
209
+ ? mapSourceFrames(parseJavaScriptStack(stack, options), sourceMaps)
210
+ : Object.freeze([]);
211
+ const location = frames.find((frame) => frame.file.endsWith(".eli")) ??
212
+ frames[0];
213
+ return Object.freeze({
214
+ name: typeof error?.name === "string" ? error.name : "Error",
215
+ message: typeof error?.message === "string" ? error.message : String(error),
216
+ ...(stack ? { stack } : {}),
217
+ frames,
218
+ ...(location ? { location } : {}),
219
+ });
220
+ }
@@ -0,0 +1,52 @@
1
+ import {
2
+ LIST_STATE,
3
+ readListMetrics,
4
+ resetListMetrics,
5
+ } from "../core/list-internals.mjs";
6
+
7
+ function stateOf(list) {
8
+ const state = list?.[LIST_STATE];
9
+ if (state === undefined) {
10
+ throw new TypeError("expected an Eliscript persistent list");
11
+ }
12
+ return state;
13
+ }
14
+
15
+ function collectNodes(list) {
16
+ const nodes = new Set();
17
+ let node = list;
18
+ while (!stateOf(node).empty) {
19
+ nodes.add(node);
20
+ node = stateOf(node).rest;
21
+ }
22
+ return nodes;
23
+ }
24
+
25
+ export function resetPersistentListMetrics() {
26
+ resetListMetrics();
27
+ }
28
+
29
+ export function persistentListMetrics() {
30
+ return readListMetrics();
31
+ }
32
+
33
+ export function inspectPersistentList(list) {
34
+ const state = stateOf(list);
35
+ return Object.freeze({
36
+ count: state.count,
37
+ empty: state.empty,
38
+ nodeCount: collectNodes(list).size,
39
+ });
40
+ }
41
+
42
+ export function sharedPersistentListNodes(left, right) {
43
+ const leftNodes = collectNodes(left);
44
+ const rightNodes = collectNodes(right);
45
+ let shared = 0;
46
+ for (const node of leftNodes) {
47
+ if (rightNodes.has(node)) {
48
+ shared += 1;
49
+ }
50
+ }
51
+ return shared;
52
+ }
@@ -0,0 +1,145 @@
1
+ import {
2
+ MAP_STATE,
3
+ TRANSIENT_MAP_STATE,
4
+ ArrayNode,
5
+ BitmapIndexedNode,
6
+ HashCollisionNode,
7
+ MapEntry,
8
+ readMapMetrics,
9
+ readTransientMapMetrics,
10
+ resetMapMetrics,
11
+ resetTransientMapMetrics as resetTransientMapMetricsInternal,
12
+ } from "../core/map-internals.mjs";
13
+
14
+ function stateOf(map) {
15
+ const state = map?.[MAP_STATE];
16
+ if (state === undefined) {
17
+ throw new TypeError("expected an Eliscript persistent hash map");
18
+ }
19
+ return state;
20
+ }
21
+
22
+ function transientStateOf(map) {
23
+ const state = map?.[TRANSIENT_MAP_STATE];
24
+ if (state === undefined) {
25
+ throw new TypeError("expected an Eliscript transient hash map");
26
+ }
27
+ return state;
28
+ }
29
+
30
+ function inspectItem(item, depth, result, nodes) {
31
+ if (item instanceof MapEntry) {
32
+ result.entries += 1;
33
+ result.maxDepth = Math.max(result.maxDepth, depth);
34
+ return;
35
+ }
36
+ if (nodes.has(item)) {
37
+ return;
38
+ }
39
+ nodes.add(item);
40
+ result.maxDepth = Math.max(result.maxDepth, depth);
41
+ if (item instanceof BitmapIndexedNode) {
42
+ result.bitmapNodes += 1;
43
+ for (const child of item.items) {
44
+ inspectItem(child, depth + 1, result, nodes);
45
+ }
46
+ return;
47
+ }
48
+ if (item instanceof ArrayNode) {
49
+ result.arrayNodes += 1;
50
+ for (const child of item.children) {
51
+ if (child !== undefined) {
52
+ inspectItem(child, depth + 1, result, nodes);
53
+ }
54
+ }
55
+ return;
56
+ }
57
+ if (item instanceof HashCollisionNode) {
58
+ result.collisionNodes += 1;
59
+ for (const entry of item.entries) {
60
+ inspectItem(entry, depth + 1, result, nodes);
61
+ }
62
+ }
63
+ }
64
+
65
+ function collectNodes(item, nodes) {
66
+ if (item instanceof MapEntry || nodes.has(item)) {
67
+ return;
68
+ }
69
+ nodes.add(item);
70
+ const children = item instanceof BitmapIndexedNode
71
+ ? item.items
72
+ : item instanceof ArrayNode
73
+ ? item.children
74
+ : item.entries;
75
+ for (const child of children) {
76
+ if (child !== undefined) {
77
+ collectNodes(child, nodes);
78
+ }
79
+ }
80
+ }
81
+
82
+ export function resetPersistentMapMetrics() {
83
+ resetMapMetrics();
84
+ }
85
+
86
+ export function persistentMapMetrics() {
87
+ return readMapMetrics();
88
+ }
89
+
90
+ export function resetTransientMapMetrics() {
91
+ resetTransientMapMetricsInternal();
92
+ }
93
+
94
+ export function transientMapMetrics() {
95
+ return readTransientMapMetrics();
96
+ }
97
+
98
+ export function inspectTransientMap(map) {
99
+ const state = transientStateOf(map);
100
+ const nodes = new Set();
101
+ collectNodes(state.root, nodes);
102
+ let ownedNodeCount = 0;
103
+ for (const node of nodes) {
104
+ if (node.owner === state.owner) {
105
+ ownedNodeCount += 1;
106
+ }
107
+ }
108
+ return Object.freeze({
109
+ active: state.active,
110
+ count: state.count,
111
+ nodeCount: nodes.size,
112
+ ownedNodeCount,
113
+ sharesSourceRoot: state.root === state.source?.[MAP_STATE].root,
114
+ });
115
+ }
116
+
117
+ export function inspectPersistentMap(map) {
118
+ const result = {
119
+ count: stateOf(map).count,
120
+ bitmapNodes: 0,
121
+ arrayNodes: 0,
122
+ collisionNodes: 0,
123
+ entries: 0,
124
+ nodeCount: 0,
125
+ maxDepth: 0,
126
+ };
127
+ const nodes = new Set();
128
+ inspectItem(stateOf(map).root, 1, result, nodes);
129
+ result.nodeCount = nodes.size;
130
+ return Object.freeze(result);
131
+ }
132
+
133
+ export function sharedPersistentMapNodes(left, right) {
134
+ const leftNodes = new Set();
135
+ const rightNodes = new Set();
136
+ collectNodes(stateOf(left).root, leftNodes);
137
+ collectNodes(stateOf(right).root, rightNodes);
138
+ let shared = 0;
139
+ for (const node of leftNodes) {
140
+ if (rightNodes.has(node)) {
141
+ shared += 1;
142
+ }
143
+ }
144
+ return shared;
145
+ }
@@ -0,0 +1,49 @@
1
+ import {
2
+ QUEUE_STATE,
3
+ readQueueMetrics,
4
+ resetQueueMetrics,
5
+ } from "../core/queue-internals.mjs";
6
+ import { sharedPersistentVectorNodes } from "./vector.mjs";
7
+
8
+ function stateOf(queue) {
9
+ const state = queue?.[QUEUE_STATE];
10
+ if (state === undefined) {
11
+ throw new TypeError("expected an Eliscript persistent queue");
12
+ }
13
+ return state;
14
+ }
15
+
16
+ export function resetPersistentQueueMetrics() {
17
+ resetQueueMetrics();
18
+ }
19
+
20
+ export function persistentQueueMetrics() {
21
+ return readQueueMetrics();
22
+ }
23
+
24
+ export function inspectPersistentQueue(queue) {
25
+ const state = stateOf(queue);
26
+ return Object.freeze({
27
+ count: state.count,
28
+ frontCount: state.front.count,
29
+ rearCount: state.rear.count,
30
+ });
31
+ }
32
+
33
+ export function sharedPersistentQueueParts(left, right) {
34
+ const leftState = stateOf(left);
35
+ const rightState = stateOf(right);
36
+ return Object.freeze({
37
+ frontIdentity: leftState.front === rightState.front,
38
+ rearIdentity: leftState.rear === rightState.rear,
39
+ rearPromotedToFront: leftState.rear === rightState.front,
40
+ sharedFrontNodes: sharedPersistentVectorNodes(
41
+ leftState.front,
42
+ rightState.front,
43
+ ),
44
+ sharedRearNodes: sharedPersistentVectorNodes(
45
+ leftState.rear,
46
+ rightState.rear,
47
+ ),
48
+ });
49
+ }
@@ -0,0 +1,61 @@
1
+ import {
2
+ SET_STATE,
3
+ TRANSIENT_SET_STATE,
4
+ readTransientSetMetrics,
5
+ resetTransientSetMetrics as resetTransientSetMetricsInternal,
6
+ } from "../core/set-internals.mjs";
7
+ import {
8
+ inspectPersistentMap,
9
+ inspectTransientMap,
10
+ persistentMapMetrics,
11
+ resetPersistentMapMetrics,
12
+ resetTransientMapMetrics,
13
+ sharedPersistentMapNodes,
14
+ transientMapMetrics,
15
+ } from "./map.mjs";
16
+
17
+ function mapOf(set) {
18
+ const state = set?.[SET_STATE];
19
+ if (state === undefined) {
20
+ throw new TypeError("expected an Eliscript persistent hash set");
21
+ }
22
+ return state.map;
23
+ }
24
+
25
+ export function resetPersistentSetMetrics() {
26
+ resetPersistentMapMetrics();
27
+ }
28
+
29
+ export function persistentSetMetrics() {
30
+ return persistentMapMetrics();
31
+ }
32
+
33
+ export function resetTransientSetMetrics() {
34
+ resetTransientSetMetricsInternal();
35
+ resetTransientMapMetrics();
36
+ }
37
+
38
+ export function transientSetMetrics() {
39
+ const set = readTransientSetMetrics();
40
+ return Object.freeze({
41
+ ...transientMapMetrics(),
42
+ setPersistentCalls: set.persistentCalls,
43
+ setInvalidCalls: set.invalidCalls,
44
+ });
45
+ }
46
+
47
+ export function inspectTransientSet(set) {
48
+ const state = set?.[TRANSIENT_SET_STATE];
49
+ if (state === undefined) {
50
+ throw new TypeError("expected an Eliscript transient hash set");
51
+ }
52
+ return inspectTransientMap(state.map);
53
+ }
54
+
55
+ export function inspectPersistentSet(set) {
56
+ return inspectPersistentMap(mapOf(set));
57
+ }
58
+
59
+ export function sharedPersistentSetNodes(left, right) {
60
+ return sharedPersistentMapNodes(mapOf(left), mapOf(right));
61
+ }
@@ -0,0 +1,105 @@
1
+ import {
2
+ SORTED_MAP_STATE,
3
+ SORTED_SET_STATE,
4
+ SortedNode,
5
+ readSortedMetrics,
6
+ resetSortedMetrics as resetMetrics,
7
+ } from "../core/sorted-internals.mjs";
8
+
9
+ function mapState(value) {
10
+ if (value?.[SORTED_MAP_STATE] !== undefined) {
11
+ return value[SORTED_MAP_STATE];
12
+ }
13
+ if (value?.[SORTED_SET_STATE] !== undefined) {
14
+ return value[SORTED_SET_STATE].map[SORTED_MAP_STATE];
15
+ }
16
+ throw new TypeError("expected an Eliscript persistent sorted collection");
17
+ }
18
+
19
+ function inspectNode(node, comparison, minimum, maximum, nodes, result) {
20
+ if (node === null) return { height: 0, size: 0 };
21
+ if (!(node instanceof SortedNode) || nodes.has(node)) {
22
+ throw new TypeError("persistent sorted collection contains an invalid tree");
23
+ }
24
+ nodes.add(node);
25
+ if (minimum !== null && comparison(node.key, minimum) <= 0) {
26
+ throw new TypeError("persistent sorted collection violates lower ordering");
27
+ }
28
+ if (maximum !== null && comparison(node.key, maximum) >= 0) {
29
+ throw new TypeError("persistent sorted collection violates upper ordering");
30
+ }
31
+ const left = inspectNode(
32
+ node.left,
33
+ comparison,
34
+ minimum,
35
+ node.key,
36
+ nodes,
37
+ result,
38
+ );
39
+ const right = inspectNode(
40
+ node.right,
41
+ comparison,
42
+ node.key,
43
+ maximum,
44
+ nodes,
45
+ result,
46
+ );
47
+ const height = Math.max(left.height, right.height) + 1;
48
+ const size = left.size + right.size + 1;
49
+ if (node.height !== height || node.size !== size ||
50
+ Math.abs(left.height - right.height) > 1) {
51
+ throw new TypeError("persistent sorted collection violates AVL balance");
52
+ }
53
+ result.maxBalance = Math.max(
54
+ result.maxBalance,
55
+ Math.abs(left.height - right.height),
56
+ );
57
+ return { height, size };
58
+ }
59
+
60
+ function collectNodes(node, result) {
61
+ if (node === null || result.has(node)) return;
62
+ result.add(node);
63
+ collectNodes(node.left, result);
64
+ collectNodes(node.right, result);
65
+ }
66
+
67
+ export function inspectPersistentSortedCollection(value) {
68
+ const state = mapState(value);
69
+ const nodes = new Set();
70
+ const result = { maxBalance: 0 };
71
+ const tree = inspectNode(
72
+ state.root,
73
+ state.comparison,
74
+ null,
75
+ null,
76
+ nodes,
77
+ result,
78
+ );
79
+ return Object.freeze({
80
+ count: tree.size,
81
+ height: tree.height,
82
+ nodes: nodes.size,
83
+ maxBalance: result.maxBalance,
84
+ });
85
+ }
86
+
87
+ export function sharedPersistentSortedNodes(left, right) {
88
+ const leftNodes = new Set();
89
+ const rightNodes = new Set();
90
+ collectNodes(mapState(left).root, leftNodes);
91
+ collectNodes(mapState(right).root, rightNodes);
92
+ let shared = 0;
93
+ for (const node of leftNodes) {
94
+ if (rightNodes.has(node)) shared += 1;
95
+ }
96
+ return shared;
97
+ }
98
+
99
+ export function resetSortedMetrics() {
100
+ resetMetrics();
101
+ }
102
+
103
+ export function sortedMetrics() {
104
+ return readSortedMetrics();
105
+ }
@@ -0,0 +1,17 @@
1
+ import {
2
+ clearValueCaches,
3
+ readValueMetrics,
4
+ resetValueMetrics,
5
+ } from "../core/value-internals.mjs";
6
+
7
+ export function resetValueHashMetrics() {
8
+ resetValueMetrics();
9
+ }
10
+
11
+ export function clearValueHashCaches() {
12
+ clearValueCaches();
13
+ }
14
+
15
+ export function valueHashMetrics() {
16
+ return readValueMetrics();
17
+ }