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,48 @@
1
+ # Platform Packages
2
+
3
+ [Project README](../README.md) | [Runtime](../runtime/README.md) |
4
+ [Specifications](../specs/README.md)
5
+
6
+ Platform packages expose concrete JavaScript host facilities without adding
7
+ them to the portable language core. They never select a browser or worker
8
+ global implicitly. Applications pass a host object and an explicit grant list,
9
+ then pass the resulting frozen token only to code that needs that authority.
10
+
11
+ ## Browser
12
+
13
+ ```js
14
+ import {
15
+ browserCapabilities,
16
+ browserDocument,
17
+ browserFetch,
18
+ } from "eliscript/platform/browser.mjs";
19
+
20
+ const capabilities = browserCapabilities(globalThis, ["document", "network"]);
21
+ const root = browserDocument(capabilities).querySelector("#app");
22
+ const response = await browserFetch(capabilities, "/articles.json");
23
+ ```
24
+
25
+ Available grants are `clock`, `document`, `network`, `randomness`, and
26
+ `timers`. Omit a grant and its operation fails with `missing-capability`.
27
+
28
+ ## Worker
29
+
30
+ The reference Emacs compute worker appends a request context whose
31
+ `capabilities` field grants only `progress` and `cancellation`:
32
+
33
+ ```js
34
+ import {
35
+ workerCancelled,
36
+ workerProgress,
37
+ } from "eliscript/platform/worker.mjs";
38
+
39
+ export function index(values, context) {
40
+ if (workerCancelled(context.capabilities)) return null;
41
+ workerProgress(context.capabilities, { stage: "indexing" });
42
+ return values.length;
43
+ }
44
+ ```
45
+
46
+ The same package re-exports the canonical worker value-codec and chunk-stream
47
+ APIs. Exact semantics and limitations are specified in
48
+ [0126](../specs/0126-explicit-host-capability-packages.md).
@@ -0,0 +1,204 @@
1
+ import { sourceMappedFailure } from "../runtime/source-mapping.mjs";
2
+
3
+ export const browserCapabilityKinds = Object.freeze([
4
+ "clock",
5
+ "document",
6
+ "network",
7
+ "randomness",
8
+ "timers",
9
+ ]);
10
+
11
+ const capabilityState = new WeakMap();
12
+ const knownCapabilities = new Set(browserCapabilityKinds);
13
+
14
+ export class BrowserCapabilityError extends Error {
15
+ constructor(code, message, capability) {
16
+ super(message);
17
+ this.name = "BrowserCapabilityError";
18
+ this.code = code;
19
+ this.capability = capability;
20
+ }
21
+ }
22
+
23
+ function fail(code, message, capability) {
24
+ throw new BrowserCapabilityError(code, message, capability);
25
+ }
26
+
27
+ function normalizeGrants(grants) {
28
+ if (!Array.isArray(grants) || grants.some(
29
+ (grant) => typeof grant !== "string" || grant.length === 0,
30
+ )) {
31
+ fail("invalid-grants", "browser grants must be an array of non-empty strings");
32
+ }
33
+ const normalized = [...new Set(grants)].sort();
34
+ if (normalized.length !== grants.length) {
35
+ fail("duplicate-grant", "browser grants must not contain duplicates");
36
+ }
37
+ for (const grant of normalized) {
38
+ if (!knownCapabilities.has(grant)) {
39
+ fail("unknown-grant", `unknown browser capability ${grant}`, grant);
40
+ }
41
+ }
42
+ return normalized;
43
+ }
44
+
45
+ function requireFunction(receiver, name, capability) {
46
+ const operation = receiver?.[name];
47
+ if (typeof operation !== "function") {
48
+ fail(
49
+ "unavailable-capability",
50
+ `browser host does not provide ${capability}`,
51
+ capability,
52
+ );
53
+ }
54
+ return (...arguments_) => Reflect.apply(operation, receiver, arguments_);
55
+ }
56
+
57
+ function operationTable(scope, grants) {
58
+ const operations = Object.create(null);
59
+ for (const grant of grants) {
60
+ switch (grant) {
61
+ case "clock":
62
+ operations.clock = requireFunction(scope.performance, "now", grant);
63
+ break;
64
+ case "document":
65
+ {
66
+ const document = scope.document;
67
+ if (document === null || typeof document !== "object") {
68
+ fail(
69
+ "unavailable-capability",
70
+ "browser host does not provide document",
71
+ grant,
72
+ );
73
+ }
74
+ operations.document = () => document;
75
+ }
76
+ break;
77
+ case "network":
78
+ operations.network = requireFunction(scope, "fetch", grant);
79
+ break;
80
+ case "randomness":
81
+ operations.randomness = requireFunction(
82
+ scope.crypto,
83
+ "getRandomValues",
84
+ grant,
85
+ );
86
+ break;
87
+ case "timers":
88
+ operations.setTimeout = requireFunction(scope, "setTimeout", grant);
89
+ operations.clearTimeout = requireFunction(scope, "clearTimeout", grant);
90
+ break;
91
+ }
92
+ }
93
+ return Object.freeze(operations);
94
+ }
95
+
96
+ export function browserCapabilities(scope, grants = []) {
97
+ if (scope === null || (typeof scope !== "object" && typeof scope !== "function")) {
98
+ fail("invalid-host", "browser host must be an object or function");
99
+ }
100
+ const normalized = normalizeGrants(grants);
101
+ const capabilities = Object.freeze({
102
+ format: "eliscript-browser-capabilities",
103
+ version: 1,
104
+ grants: Object.freeze(normalized),
105
+ });
106
+ capabilityState.set(capabilities, operationTable(scope, normalized));
107
+ return capabilities;
108
+ }
109
+
110
+ export function isBrowserCapabilities(value) {
111
+ return value !== null && typeof value === "object" && capabilityState.has(value);
112
+ }
113
+
114
+ function stateFor(capabilities) {
115
+ const state = isBrowserCapabilities(capabilities)
116
+ ? capabilityState.get(capabilities)
117
+ : undefined;
118
+ if (state === undefined) {
119
+ fail("invalid-capabilities", "expected Eliscript browser capabilities");
120
+ }
121
+ return state;
122
+ }
123
+
124
+ export function browserCapabilityDescriptor(capabilities) {
125
+ stateFor(capabilities);
126
+ return Object.freeze({
127
+ format: capabilities.format,
128
+ version: capabilities.version,
129
+ grants: Object.freeze([...capabilities.grants]),
130
+ });
131
+ }
132
+
133
+ export function hasBrowserCapability(capabilities, capability) {
134
+ stateFor(capabilities);
135
+ return typeof capability === "string" && capabilities.grants.includes(capability);
136
+ }
137
+
138
+ function operationFor(capabilities, operation, capability = operation) {
139
+ const operation_ = stateFor(capabilities)[operation];
140
+ if (operation_ === undefined) {
141
+ fail(
142
+ "missing-capability",
143
+ `browser capability ${capability} was not granted`,
144
+ capability,
145
+ );
146
+ }
147
+ return operation_;
148
+ }
149
+
150
+ export function browserDocument(capabilities) {
151
+ return operationFor(capabilities, "document")();
152
+ }
153
+
154
+ export function browserFetch(capabilities, input, init) {
155
+ return operationFor(capabilities, "network")(input, init);
156
+ }
157
+
158
+ export function browserNow(capabilities) {
159
+ return operationFor(capabilities, "clock")();
160
+ }
161
+
162
+ export function browserRandomValues(capabilities, values) {
163
+ return operationFor(capabilities, "randomness")(values);
164
+ }
165
+
166
+ export function browserSetTimeout(capabilities, callback, delay, ...arguments_) {
167
+ return operationFor(capabilities, "setTimeout", "timers")(
168
+ callback,
169
+ delay,
170
+ ...arguments_,
171
+ );
172
+ }
173
+
174
+ export function browserClearTimeout(capabilities, handle) {
175
+ return operationFor(capabilities, "clearTimeout", "timers")(handle);
176
+ }
177
+
178
+ export function browserEventBoundary(handler, report, sourceMaps = []) {
179
+ if (typeof handler !== "function") {
180
+ fail("invalid-event-handler", "browser event handler must be a function");
181
+ }
182
+ if (typeof report !== "function") {
183
+ fail("invalid-event-reporter", "browser event reporter must be a function");
184
+ }
185
+ if (!Array.isArray(sourceMaps)) {
186
+ fail("invalid-source-maps", "browser event source maps must be an array");
187
+ }
188
+ return function boundedBrowserEvent(...arguments_) {
189
+ const reportFailure = (error) => {
190
+ report(sourceMappedFailure(error, sourceMaps), arguments_[0]);
191
+ return undefined;
192
+ };
193
+ try {
194
+ const result = Reflect.apply(handler, this, arguments_);
195
+ return result !== null &&
196
+ (typeof result === "object" || typeof result === "function") &&
197
+ typeof result.then === "function"
198
+ ? Promise.resolve(result).catch(reportFailure)
199
+ : result;
200
+ } catch (error) {
201
+ return reportFailure(error);
202
+ }
203
+ };
204
+ }
@@ -0,0 +1,166 @@
1
+ export {
2
+ WorkerValueCodecError,
3
+ decodeWorkerValue,
4
+ decodeWorkerValues,
5
+ encodeWorkerValue,
6
+ encodeWorkerValues,
7
+ workerValueEncoding,
8
+ workerValueLimits,
9
+ } from "../runtime/worker-value-codec.mjs";
10
+ export {
11
+ WorkerValueStreamDecoder,
12
+ WorkerValueStreamError,
13
+ decodeWorkerValueChunks,
14
+ encodeWorkerValueChunks,
15
+ encodeWorkerValueEvents,
16
+ workerValueFraming,
17
+ workerValueStreamLimits,
18
+ } from "../runtime/worker-value-stream.mjs";
19
+
20
+ export const workerCapabilityKinds = Object.freeze([
21
+ "cancellation",
22
+ "progress",
23
+ ]);
24
+
25
+ const capabilityState = new WeakMap();
26
+ const knownCapabilities = new Set(workerCapabilityKinds);
27
+
28
+ export class WorkerCapabilityError extends Error {
29
+ constructor(code, message, capability) {
30
+ super(message);
31
+ this.name = "WorkerCapabilityError";
32
+ this.code = code;
33
+ this.capability = capability;
34
+ }
35
+ }
36
+
37
+ function fail(code, message, capability) {
38
+ throw new WorkerCapabilityError(code, message, capability);
39
+ }
40
+
41
+ function normalizeGrants(grants) {
42
+ if (!Array.isArray(grants) || grants.some(
43
+ (grant) => typeof grant !== "string" || grant.length === 0,
44
+ )) {
45
+ fail("invalid-grants", "worker grants must be an array of non-empty strings");
46
+ }
47
+ const normalized = [...new Set(grants)].sort();
48
+ if (normalized.length !== grants.length) {
49
+ fail("duplicate-grant", "worker grants must not contain duplicates");
50
+ }
51
+ for (const grant of normalized) {
52
+ if (!knownCapabilities.has(grant)) {
53
+ fail("unknown-grant", `unknown worker capability ${grant}`, grant);
54
+ }
55
+ }
56
+ return normalized;
57
+ }
58
+
59
+ function operationTable(context, grants) {
60
+ const operations = Object.create(null);
61
+ for (const grant of grants) {
62
+ if (grant === "progress") {
63
+ if (typeof context.progress !== "function") {
64
+ fail(
65
+ "unavailable-capability",
66
+ "worker context does not provide progress",
67
+ grant,
68
+ );
69
+ }
70
+ operations.progress = (value) => Reflect.apply(
71
+ context.progress,
72
+ context,
73
+ [value],
74
+ );
75
+ } else if (grant === "cancellation") {
76
+ const signal = context.signal;
77
+ if (signal === null || typeof signal !== "object" ||
78
+ typeof signal.aborted !== "boolean" ||
79
+ typeof signal.addEventListener !== "function") {
80
+ fail(
81
+ "unavailable-capability",
82
+ "worker context does not provide an AbortSignal",
83
+ grant,
84
+ );
85
+ }
86
+ operations.signal = () => signal;
87
+ }
88
+ }
89
+ return Object.freeze(operations);
90
+ }
91
+
92
+ export function workerCapabilities(context, grants = []) {
93
+ if (context === null || typeof context !== "object") {
94
+ fail("invalid-host", "worker context must be an object");
95
+ }
96
+ const normalized = normalizeGrants(grants);
97
+ const capabilities = Object.freeze({
98
+ format: "eliscript-worker-capabilities",
99
+ version: 1,
100
+ grants: Object.freeze(normalized),
101
+ });
102
+ capabilityState.set(capabilities, operationTable(context, normalized));
103
+ return capabilities;
104
+ }
105
+
106
+ export function isWorkerCapabilities(value) {
107
+ return value !== null && typeof value === "object" && capabilityState.has(value);
108
+ }
109
+
110
+ function stateFor(capabilities) {
111
+ const state = isWorkerCapabilities(capabilities)
112
+ ? capabilityState.get(capabilities)
113
+ : undefined;
114
+ if (state === undefined) {
115
+ fail("invalid-capabilities", "expected Eliscript worker capabilities");
116
+ }
117
+ return state;
118
+ }
119
+
120
+ export function workerCapabilityDescriptor(capabilities) {
121
+ stateFor(capabilities);
122
+ return Object.freeze({
123
+ format: capabilities.format,
124
+ version: capabilities.version,
125
+ grants: Object.freeze([...capabilities.grants]),
126
+ });
127
+ }
128
+
129
+ export function hasWorkerCapability(capabilities, capability) {
130
+ stateFor(capabilities);
131
+ return typeof capability === "string" && capabilities.grants.includes(capability);
132
+ }
133
+
134
+ function operationFor(capabilities, operation, capability) {
135
+ const operation_ = stateFor(capabilities)[operation];
136
+ if (operation_ === undefined) {
137
+ fail(
138
+ "missing-capability",
139
+ `worker capability ${capability} was not granted`,
140
+ capability,
141
+ );
142
+ }
143
+ return operation_;
144
+ }
145
+
146
+ export function workerProgress(capabilities, value) {
147
+ return operationFor(capabilities, "progress", "progress")(value);
148
+ }
149
+
150
+ export function workerSignal(capabilities) {
151
+ return operationFor(capabilities, "signal", "cancellation")();
152
+ }
153
+
154
+ export function workerCancelled(capabilities) {
155
+ return workerSignal(capabilities).aborted;
156
+ }
157
+
158
+ export function throwIfWorkerCancelled(capabilities) {
159
+ if (workerCancelled(capabilities)) {
160
+ fail(
161
+ "worker-cancelled",
162
+ "worker operation has been cancelled",
163
+ "cancellation",
164
+ );
165
+ }
166
+ }