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,53 @@
1
+ import {
2
+ I_EDITABLE,
3
+ I_TRANSIENT_COLLECTION,
4
+ dispatchEditableTransient,
5
+ dispatchTransientAssoc,
6
+ dispatchTransientConj,
7
+ dispatchTransientDissoc,
8
+ dispatchTransientPersistent,
9
+ } from "./transient-internals.mjs";
10
+
11
+ export const IEditable = I_EDITABLE;
12
+ export const ITransientCollection = I_TRANSIENT_COLLECTION;
13
+
14
+ export function transient(collection) {
15
+ return dispatchEditableTransient(collection);
16
+ }
17
+
18
+ export function conjBang(collection, ...values) {
19
+ let result = collection;
20
+ for (const value of values) {
21
+ result = dispatchTransientConj(result, value);
22
+ }
23
+ return result;
24
+ }
25
+
26
+ export function assocBang(collection, key, value, ...keyValues) {
27
+ if (arguments.length < 3 || keyValues.length % 2 !== 0) {
28
+ throw new TypeError(
29
+ "assocBang requires a transient collection followed by one or more key/value pairs",
30
+ );
31
+ }
32
+ let result = dispatchTransientAssoc(collection, key, value);
33
+ for (let index = 0; index < keyValues.length; index += 2) {
34
+ result = dispatchTransientAssoc(
35
+ result,
36
+ keyValues[index],
37
+ keyValues[index + 1],
38
+ );
39
+ }
40
+ return result;
41
+ }
42
+
43
+ export function dissocBang(collection, ...keys) {
44
+ let result = collection;
45
+ for (const key of keys) {
46
+ result = dispatchTransientDissoc(result, key);
47
+ }
48
+ return result;
49
+ }
50
+
51
+ export function persistentBang(collection) {
52
+ return dispatchTransientPersistent(collection);
53
+ }
@@ -0,0 +1,3 @@
1
+ export function isTruthy(value) {
2
+ return value !== false && value != null;
3
+ }
@@ -0,0 +1,262 @@
1
+ const MAX_FIELDS = 1024;
2
+ const MAX_PROTOCOLS = 1024;
3
+ const TYPE_CONSTRUCTOR_TOKEN = Symbol("eliscript.type.constructor-token");
4
+ const TYPE_VALUE_STATE = new WeakMap();
5
+ const typeStates = new WeakMap();
6
+
7
+ function fail(message) {
8
+ throw new TypeError(message);
9
+ }
10
+
11
+ function denseArray(value, label, maximum) {
12
+ if (!Array.isArray(value) || value.length > maximum) {
13
+ fail(`${label} must be a dense array of at most ${maximum} values`);
14
+ }
15
+ const descriptors = Object.getOwnPropertyDescriptors(value);
16
+ if (Reflect.ownKeys(descriptors).length !== value.length + 1) {
17
+ fail(`${label} must be a dense array with no extra properties`);
18
+ }
19
+ for (let index = 0; index < value.length; index += 1) {
20
+ const descriptor = descriptors[String(index)];
21
+ if (!descriptor?.enumerable || !("value" in descriptor)) {
22
+ fail(`${label} must contain enumerable data values`);
23
+ }
24
+ }
25
+ return value;
26
+ }
27
+
28
+ function normalizeTypeName(name) {
29
+ if (typeof name !== "string" || name.length === 0) {
30
+ fail("type name must be a non-empty string");
31
+ }
32
+ return name;
33
+ }
34
+
35
+ function normalizeFieldNames(fields) {
36
+ denseArray(fields, "type fields", MAX_FIELDS);
37
+ const seen = new Set();
38
+ const normalized = fields.map((field) => {
39
+ if (typeof field !== "string" || field.length === 0 || field.includes("/")) {
40
+ fail("type field names must be non-empty unqualified strings");
41
+ }
42
+ if (seen.has(field)) fail(`type declares duplicate field ${field}`);
43
+ seen.add(field);
44
+ return field;
45
+ });
46
+ return Object.freeze(normalized);
47
+ }
48
+
49
+ function protocolShape(protocol) {
50
+ if (protocol === null || typeof protocol !== "object") {
51
+ fail("expected an Eliscript protocol");
52
+ }
53
+ const descriptors = Object.getOwnPropertyDescriptors(protocol);
54
+ const keys = Reflect.ownKeys(descriptors);
55
+ const nameDescriptor = descriptors.name;
56
+ const operationsDescriptor = descriptors.operations;
57
+ if (keys.length !== 2 || !nameDescriptor?.enumerable ||
58
+ !("value" in nameDescriptor) ||
59
+ typeof nameDescriptor.value !== "string" ||
60
+ nameDescriptor.value.length === 0 ||
61
+ !operationsDescriptor?.enumerable || !("value" in operationsDescriptor) ||
62
+ !Object.isFrozen(protocol)) {
63
+ fail("expected an Eliscript protocol");
64
+ }
65
+ const operations = operationsDescriptor.value;
66
+ if (operations === null || typeof operations !== "object" ||
67
+ Array.isArray(operations) || !Object.isFrozen(operations)) {
68
+ fail("expected an Eliscript protocol");
69
+ }
70
+ const operationDescriptors = Object.getOwnPropertyDescriptors(operations);
71
+ const operationNames = Reflect.ownKeys(operationDescriptors);
72
+ if (operationNames.length === 0 || operationNames.length > MAX_FIELDS ||
73
+ operationNames.some((operation) => typeof operation !== "string")) {
74
+ fail("expected an Eliscript protocol");
75
+ }
76
+ const methods = operationNames.map((operation) => {
77
+ const descriptor = operationDescriptors[operation];
78
+ const method = descriptor?.value;
79
+ const methodDescriptors = typeof method === "function"
80
+ ? Object.getOwnPropertyDescriptors(method)
81
+ : {};
82
+ if (!descriptor?.enumerable || !("value" in descriptor) ||
83
+ methodDescriptors.protocol?.value !== protocol ||
84
+ methodDescriptors.operation?.value !== operation ||
85
+ typeof methodDescriptors.slot?.value !== "symbol" ||
86
+ !Object.isFrozen(method)) {
87
+ fail("expected an Eliscript protocol");
88
+ }
89
+ return Object.freeze({ operation, slot: methodDescriptors.slot.value });
90
+ });
91
+ return Object.freeze({
92
+ name: nameDescriptor.value,
93
+ operations: Object.freeze(methods),
94
+ });
95
+ }
96
+
97
+ function normalizeMethods(protocol, implementations) {
98
+ const definition = protocolShape(protocol);
99
+ if (implementations === null || typeof implementations !== "object" ||
100
+ Array.isArray(implementations)) {
101
+ fail(`protocol ${definition.name} implementations must be a plain object`);
102
+ }
103
+ const prototype = Object.getPrototypeOf(implementations);
104
+ if (prototype !== Object.prototype && prototype !== null) {
105
+ fail(`protocol ${definition.name} implementations must be a plain object`);
106
+ }
107
+ const descriptors = Object.getOwnPropertyDescriptors(implementations);
108
+ const keys = Reflect.ownKeys(descriptors);
109
+ if (keys.length !== definition.operations.length) {
110
+ fail(`protocol ${definition.name} implementation must define every operation`);
111
+ }
112
+ const operationNames = definition.operations.map(({ operation }) => operation);
113
+ if (keys.some((key) => typeof key !== "string" ||
114
+ !operationNames.includes(key))) {
115
+ fail(`protocol ${definition.name} implementation contains an unknown operation`);
116
+ }
117
+ const normalized = [];
118
+ for (const { operation, slot } of definition.operations) {
119
+ const descriptor = descriptors[operation];
120
+ if (!descriptor?.enumerable || !("value" in descriptor) ||
121
+ typeof descriptor.value !== "function") {
122
+ fail(`protocol ${definition.name}/${operation} implementation must be a function`);
123
+ }
124
+ normalized.push(Object.freeze({
125
+ operation,
126
+ slot,
127
+ implementation: descriptor.value,
128
+ }));
129
+ }
130
+ return Object.freeze({ protocol, name: definition.name, methods: normalized });
131
+ }
132
+
133
+ function normalizeProtocolPlans(plans, allowEmpty) {
134
+ denseArray(plans, "protocol implementations", MAX_PROTOCOLS);
135
+ if (!allowEmpty && plans.length === 0) {
136
+ fail("reify requires at least one protocol implementation");
137
+ }
138
+ const seen = new Set();
139
+ return Object.freeze(plans.map((plan, index) => {
140
+ denseArray(plan, `protocol implementation ${index}`, 2);
141
+ if (plan.length !== 2) {
142
+ fail(`protocol implementation ${index} must contain a protocol and methods`);
143
+ }
144
+ const [protocol, methods] = plan;
145
+ if (seen.has(protocol)) fail("protocol may be implemented only once");
146
+ seen.add(protocol);
147
+ return normalizeMethods(protocol, methods);
148
+ }));
149
+ }
150
+
151
+ function installProtocolMethods(target, plans) {
152
+ for (const plan of plans) {
153
+ for (const { slot, implementation } of plan.methods) {
154
+ if (Reflect.has(target, slot)) {
155
+ fail(`protocol slot conflict while implementing ${plan.name}`);
156
+ }
157
+ Object.defineProperty(target, slot, {
158
+ configurable: false,
159
+ enumerable: false,
160
+ writable: false,
161
+ value: function (...argumentsList) {
162
+ return implementation(this, ...argumentsList);
163
+ },
164
+ });
165
+ }
166
+ }
167
+ return target;
168
+ }
169
+
170
+ function defineFieldAccessors(TypeValue, fieldNames) {
171
+ for (let index = 0; index < fieldNames.length; index += 1) {
172
+ const fieldName = fieldNames[index];
173
+ if (fieldName in TypeValue.prototype) {
174
+ fail(`type field ${fieldName} conflicts with a type member`);
175
+ }
176
+ Object.defineProperty(TypeValue.prototype, fieldName, {
177
+ configurable: false,
178
+ enumerable: true,
179
+ get() {
180
+ return TYPE_VALUE_STATE.get(this).values[index];
181
+ },
182
+ });
183
+ }
184
+ }
185
+
186
+ export function defineType(name, fields, protocolPlans = []) {
187
+ const typeName = normalizeTypeName(name);
188
+ const fieldNames = normalizeFieldNames(fields);
189
+ const plans = normalizeProtocolPlans(protocolPlans, true);
190
+
191
+ class TypeValue {
192
+ constructor(token, values) {
193
+ if (token !== TYPE_CONSTRUCTOR_TOKEN) {
194
+ fail(`${typeName} values must be created with its positional constructor`);
195
+ }
196
+ TYPE_VALUE_STATE.set(this, Object.freeze({
197
+ type: TypeValue,
198
+ values: Object.freeze(values),
199
+ }));
200
+ Object.freeze(this);
201
+ }
202
+
203
+ static create(...values) {
204
+ if (values.length !== fieldNames.length) {
205
+ fail(
206
+ `->${typeName} expects ${fieldNames.length} values, received ${values.length}`,
207
+ );
208
+ }
209
+ return new TypeValue(TYPE_CONSTRUCTOR_TOKEN, values);
210
+ }
211
+
212
+ static isInstance(value) {
213
+ return TYPE_VALUE_STATE.get(value)?.type === TypeValue;
214
+ }
215
+
216
+ get [Symbol.toStringTag]() {
217
+ return `EliscriptType:${typeName}`;
218
+ }
219
+ }
220
+
221
+ Object.defineProperty(TypeValue, "name", { value: typeName });
222
+ Object.defineProperties(TypeValue, {
223
+ typeName: { enumerable: true, value: typeName },
224
+ typeFields: { enumerable: true, value: fieldNames },
225
+ });
226
+ defineFieldAccessors(TypeValue, fieldNames);
227
+ installProtocolMethods(TypeValue.prototype, plans);
228
+ Object.freeze(TypeValue.prototype);
229
+ Object.freeze(TypeValue);
230
+ typeStates.set(TypeValue, Object.freeze({ name: typeName, fields: fieldNames }));
231
+ return TypeValue;
232
+ }
233
+
234
+ export function reifyProtocols(protocolPlans) {
235
+ const plans = normalizeProtocolPlans(protocolPlans, false);
236
+ return Object.freeze(installProtocolMethods(Object.create(null), plans));
237
+ }
238
+
239
+ export function isType(value) {
240
+ return typeof value === "function" && typeStates.has(value);
241
+ }
242
+
243
+ export function isTypeValue(value) {
244
+ return value !== null && typeof value === "object" &&
245
+ TYPE_VALUE_STATE.has(value);
246
+ }
247
+
248
+ export function typeOf(value) {
249
+ const state = TYPE_VALUE_STATE.get(value);
250
+ if (state === undefined) fail("expected an Eliscript type value");
251
+ return state.type;
252
+ }
253
+
254
+ export function typeName(value) {
255
+ const type = isType(value) ? value : typeOf(value);
256
+ return typeStates.get(type).name;
257
+ }
258
+
259
+ export function typeFieldNames(value) {
260
+ const type = isType(value) ? value : typeOf(value);
261
+ return typeStates.get(type).fields;
262
+ }
@@ -0,0 +1,188 @@
1
+ import {
2
+ defineProtocol,
3
+ protocolMethod,
4
+ protocolSlot,
5
+ } from "./protocol.mjs";
6
+
7
+ export const I_EQUIV = defineProtocol("IEquiv", ["equal"]);
8
+ export const I_HASH = defineProtocol("IHash", ["hash"]);
9
+ export const VALUE_EQUAL = protocolSlot(I_EQUIV, "equal");
10
+ export const VALUE_HASH = protocolSlot(I_HASH, "hash");
11
+ export const dispatchValueEqual = protocolMethod(I_EQUIV, "equal");
12
+ export const dispatchValueHash = protocolMethod(I_HASH, "hash");
13
+
14
+ const BOOLEAN_TAG = 0x4b1d_9f73;
15
+ const BIGINT_TAG = 0x7845_9ad1;
16
+ const GLOBAL_SYMBOL_TAG = 0x9e65_8cab;
17
+ const HOST_IDENTITY_TAG = 0x2d6f_5a8d;
18
+ const NUMBER_TAG = 0x1656_67b1;
19
+ const STRING_TAG = 0x6d2b_79f5;
20
+
21
+ const FALSE_HASH = 0x193a_7c41;
22
+ const TRUE_HASH = 0x71f4_9d0b;
23
+ const NULL_HASH = 0x4210_8421;
24
+ const UNDEFINED_HASH = 0x7f4a_7c15;
25
+ const NAN_HASH = 0x58d0_2a6f;
26
+
27
+ const floatBytes = new DataView(new ArrayBuffer(8));
28
+
29
+ let protocolHashes = new WeakMap();
30
+ let hostObjectHashes = new WeakMap();
31
+ let hostSymbolHashes = new Map();
32
+ let nextHostIdentity = 1;
33
+
34
+ const metrics = {
35
+ hashValueCalls: 0,
36
+ protocolHashComputations: 0,
37
+ protocolHashCacheHits: 0,
38
+ hostIdentityAssignments: 0,
39
+ };
40
+
41
+ export function avalancheHash(value) {
42
+ let hash = value >>> 0;
43
+ hash ^= hash >>> 16;
44
+ hash = Math.imul(hash, 0x85eb_ca6b);
45
+ hash ^= hash >>> 13;
46
+ hash = Math.imul(hash, 0xc2b2_ae35);
47
+ hash ^= hash >>> 16;
48
+ return hash >>> 0;
49
+ }
50
+
51
+ export function mixHash(hash, value) {
52
+ let mixed = (hash ^ value) >>> 0;
53
+ mixed = Math.imul(mixed, 0x5bd1_e995);
54
+ mixed ^= mixed >>> 15;
55
+ return mixed >>> 0;
56
+ }
57
+
58
+ export function finishHash(hash, count) {
59
+ return avalancheHash(mixHash(hash, count >>> 0));
60
+ }
61
+
62
+ export function hashString(value, tag = STRING_TAG) {
63
+ let hash = mixHash(tag, value.length);
64
+ for (let index = 0; index < value.length; index += 1) {
65
+ hash = mixHash(hash, value.charCodeAt(index));
66
+ }
67
+ return finishHash(hash, value.length);
68
+ }
69
+
70
+ export function hashNumber(value) {
71
+ if (Number.isNaN(value)) {
72
+ return NAN_HASH;
73
+ }
74
+ const normalized = value === 0 ? 0 : value;
75
+ floatBytes.setFloat64(0, normalized, true);
76
+ const low = floatBytes.getUint32(0, true);
77
+ const high = floatBytes.getUint32(4, true);
78
+ return finishHash(mixHash(mixHash(NUMBER_TAG, low), high), 2);
79
+ }
80
+
81
+ export function hashBigInt(value) {
82
+ let magnitude = value < 0n ? -value : value;
83
+ let hash = mixHash(BIGINT_TAG, value < 0n ? 1 : 0);
84
+ let limbs = 0;
85
+ do {
86
+ hash = mixHash(hash, Number(magnitude & 0xffff_ffffn));
87
+ magnitude >>= 32n;
88
+ limbs += 1;
89
+ } while (magnitude !== 0n);
90
+ return finishHash(hash, limbs);
91
+ }
92
+
93
+ export function hashBoolean(value) {
94
+ return mixHash(BOOLEAN_TAG, value ? TRUE_HASH : FALSE_HASH);
95
+ }
96
+
97
+ export function hashNull() {
98
+ return NULL_HASH;
99
+ }
100
+
101
+ export function hashUndefined() {
102
+ return UNDEFINED_HASH;
103
+ }
104
+
105
+ export function hashGlobalSymbol(key) {
106
+ return hashString(key, GLOBAL_SYMBOL_TAG);
107
+ }
108
+
109
+ export function orderedCollectionHash(iterable, hashValue, tag) {
110
+ let hash = tag >>> 0;
111
+ let count = 0;
112
+ for (const value of iterable) {
113
+ hash = mixHash(hash, hashValue(value));
114
+ count += 1;
115
+ }
116
+ return finishHash(hash, count);
117
+ }
118
+
119
+ function rotateLeft(value, distance) {
120
+ const shift = distance & 31;
121
+ return ((value << shift) | (value >>> (32 - shift))) >>> 0;
122
+ }
123
+
124
+ export function unorderedCollectionHash(iterable, hashElement, tag) {
125
+ let sum = 0;
126
+ let xor = 0;
127
+ let product = 1;
128
+ let count = 0;
129
+ for (const element of iterable) {
130
+ const hash = hashElement(element) >>> 0;
131
+ sum = (sum + hash) >>> 0;
132
+ xor = (xor ^ rotateLeft(hash, hash & 31)) >>> 0;
133
+ product = Math.imul(product, hash | 1) >>> 0;
134
+ count += 1;
135
+ }
136
+ return finishHash(
137
+ mixHash(mixHash(mixHash(tag, sum), xor), product),
138
+ count,
139
+ );
140
+ }
141
+
142
+ export function cachedProtocolHash(value, compute) {
143
+ const cached = protocolHashes.get(value);
144
+ if (cached !== undefined) {
145
+ metrics.protocolHashCacheHits += 1;
146
+ return cached;
147
+ }
148
+ metrics.protocolHashComputations += 1;
149
+ const hash = compute() >>> 0;
150
+ protocolHashes.set(value, hash);
151
+ return hash;
152
+ }
153
+
154
+ export function recordHashValueCall() {
155
+ metrics.hashValueCalls += 1;
156
+ }
157
+
158
+ export function hostIdentityHash(value) {
159
+ const hashes = typeof value === "symbol" ? hostSymbolHashes : hostObjectHashes;
160
+ const cached = hashes.get(value);
161
+ if (cached !== undefined) {
162
+ return cached;
163
+ }
164
+ const hash = finishHash(HOST_IDENTITY_TAG, nextHostIdentity);
165
+ nextHostIdentity += 1;
166
+ metrics.hostIdentityAssignments += 1;
167
+ hashes.set(value, hash);
168
+ return hash;
169
+ }
170
+
171
+ export function resetValueMetrics() {
172
+ metrics.hashValueCalls = 0;
173
+ metrics.protocolHashComputations = 0;
174
+ metrics.protocolHashCacheHits = 0;
175
+ metrics.hostIdentityAssignments = 0;
176
+ }
177
+
178
+ export function clearValueCaches() {
179
+ protocolHashes = new WeakMap();
180
+ hostObjectHashes = new WeakMap();
181
+ hostSymbolHashes = new Map();
182
+ nextHostIdentity = 1;
183
+ resetValueMetrics();
184
+ }
185
+
186
+ export function readValueMetrics() {
187
+ return Object.freeze({ ...metrics });
188
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ I_EQUIV,
3
+ I_HASH,
4
+ cachedProtocolHash,
5
+ dispatchValueEqual,
6
+ dispatchValueHash,
7
+ hashBigInt,
8
+ hashBoolean,
9
+ hashGlobalSymbol,
10
+ hashNull,
11
+ hashNumber,
12
+ hashString,
13
+ hashUndefined,
14
+ hostIdentityHash,
15
+ recordHashValueCall,
16
+ } from "./value-internals.mjs";
17
+ import {
18
+ extendProtocolType,
19
+ implementsProtocolOperation,
20
+ } from "./protocol.mjs";
21
+
22
+ export const IEquiv = I_EQUIV;
23
+ export const IHash = I_HASH;
24
+
25
+ export function extendValueType(constructor, implementations) {
26
+ if (implementations === null || typeof implementations !== "object" ||
27
+ typeof implementations.equal !== "function" ||
28
+ typeof implementations.hash !== "function") {
29
+ throw new TypeError(
30
+ "value type extension requires equal and hash functions",
31
+ );
32
+ }
33
+ extendProtocolType(IEquiv, constructor, { equal: implementations.equal });
34
+ extendProtocolType(IHash, constructor, { hash: implementations.hash });
35
+ return constructor;
36
+ }
37
+
38
+ export function equalValues(left, right) {
39
+ if (left === right) {
40
+ return true;
41
+ }
42
+ if (typeof left === "number" && typeof right === "number") {
43
+ return Number.isNaN(left) && Number.isNaN(right);
44
+ }
45
+ if (left === null || right === null ||
46
+ (typeof left !== "object" && typeof left !== "function") ||
47
+ (typeof right !== "object" && typeof right !== "function")) {
48
+ return false;
49
+ }
50
+
51
+ if (!implementsProtocolOperation(IEquiv, "equal", left) ||
52
+ !implementsProtocolOperation(IEquiv, "equal", right)) {
53
+ return false;
54
+ }
55
+ return dispatchValueEqual(left, right, equalValues) === true;
56
+ }
57
+
58
+ export function hashValue(value) {
59
+ recordHashValueCall();
60
+ if (value === null) {
61
+ return hashNull();
62
+ }
63
+ switch (typeof value) {
64
+ case "undefined":
65
+ return hashUndefined();
66
+ case "boolean":
67
+ return hashBoolean(value);
68
+ case "number":
69
+ return hashNumber(value);
70
+ case "bigint":
71
+ return hashBigInt(value);
72
+ case "string":
73
+ return hashString(value);
74
+ case "symbol": {
75
+ const globalKey = Symbol.keyFor(value);
76
+ return globalKey === undefined
77
+ ? hostIdentityHash(value)
78
+ : hashGlobalSymbol(globalKey);
79
+ }
80
+ case "function":
81
+ case "object": {
82
+ if (implementsProtocolOperation(IHash, "hash", value)) {
83
+ return cachedProtocolHash(
84
+ value,
85
+ () => dispatchValueHash(value, hashValue),
86
+ );
87
+ }
88
+ return hostIdentityHash(value);
89
+ }
90
+ default:
91
+ throw new TypeError(`cannot hash JavaScript value of type ${typeof value}`);
92
+ }
93
+ }
@@ -0,0 +1,102 @@
1
+ export const BRANCH_BITS = 5;
2
+ export const BRANCH_WIDTH = 1 << BRANCH_BITS;
3
+ export const BRANCH_MASK = BRANCH_WIDTH - 1;
4
+
5
+ export const VECTOR_STATE = Symbol("eliscript.vector.state");
6
+ export const VECTOR_CONSTRUCTOR_TOKEN = Symbol("eliscript.vector.constructor");
7
+ export const TRANSIENT_VECTOR_STATE = Symbol("eliscript.vector.transient-state");
8
+ export const TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN = Symbol(
9
+ "eliscript.vector.transient-constructor",
10
+ );
11
+
12
+ const metrics = {
13
+ nodeAllocations: 0,
14
+ nodeVisits: 0,
15
+ tailAllocations: 0,
16
+ rootGrowths: 0,
17
+ };
18
+
19
+ const transientMetrics = {
20
+ nodeClones: 0,
21
+ nodeMutations: 0,
22
+ tailCopies: 0,
23
+ tailMutations: 0,
24
+ persistentCalls: 0,
25
+ invalidCalls: 0,
26
+ };
27
+
28
+ export class VectorNode {
29
+ constructor(slots = [], owner = null) {
30
+ metrics.nodeAllocations += 1;
31
+ this.owner = owner;
32
+ this.slots = owner === null ? Object.freeze(slots) : slots;
33
+ if (owner === null) {
34
+ Object.freeze(this);
35
+ }
36
+ }
37
+ }
38
+
39
+ export function editableVectorNode(node, owner) {
40
+ if (node.owner === owner) {
41
+ return node;
42
+ }
43
+ transientMetrics.nodeClones += 1;
44
+ return new VectorNode(node.slots.slice(), owner);
45
+ }
46
+
47
+ export function recordTransientVectorNodeMutation() {
48
+ transientMetrics.nodeMutations += 1;
49
+ }
50
+
51
+ export function recordTransientVectorTailCopy() {
52
+ transientMetrics.tailCopies += 1;
53
+ }
54
+
55
+ export function recordTransientVectorTailMutation() {
56
+ transientMetrics.tailMutations += 1;
57
+ }
58
+
59
+ export function recordTransientVectorPersistent() {
60
+ transientMetrics.persistentCalls += 1;
61
+ }
62
+
63
+ export function recordInvalidTransientVectorCall() {
64
+ transientMetrics.invalidCalls += 1;
65
+ }
66
+
67
+ export function allocateTail(values) {
68
+ metrics.tailAllocations += 1;
69
+ return Object.freeze(values);
70
+ }
71
+
72
+ export function visitNode(node) {
73
+ metrics.nodeVisits += 1;
74
+ return node;
75
+ }
76
+
77
+ export function recordRootGrowth() {
78
+ metrics.rootGrowths += 1;
79
+ }
80
+
81
+ export function resetVectorMetrics() {
82
+ for (const key of Object.keys(metrics)) {
83
+ metrics[key] = 0;
84
+ }
85
+ }
86
+
87
+ export function readVectorMetrics() {
88
+ return Object.freeze({ ...metrics });
89
+ }
90
+
91
+ export function resetTransientVectorMetrics() {
92
+ for (const key of Object.keys(transientMetrics)) {
93
+ transientMetrics[key] = 0;
94
+ }
95
+ }
96
+
97
+ export function readTransientVectorMetrics() {
98
+ return Object.freeze({ ...transientMetrics });
99
+ }
100
+
101
+ export const EMPTY_ROOT = new VectorNode();
102
+ export const EMPTY_TAIL = allocateTail([]);