eliscript 0.1.3 → 0.1.4
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/COPYING.RUNTIME +93 -0
- package/README.md +15 -3
- package/bin/eliscript-test +29 -0
- package/bootstrap/backends/README.md +5 -4
- package/bootstrap/backends/binary.eli +89 -22
- package/bootstrap/backends/capabilities.eli +9 -4
- package/bootstrap/backends/managed-layout.eli +93 -81
- package/bootstrap/backends/managed.eli +651 -125
- package/bootstrap/backends/native.eli +1791 -324
- package/bootstrap/backends/numeric.eli +71 -7
- package/bootstrap/backends/sha256.eli +165 -68
- package/bootstrap/backends/wasm.eli +461 -52
- package/bootstrap/compiler/analyzer.eli +692 -2
- package/bootstrap/compiler/compiler.eli +16 -3
- package/bootstrap/compiler/emitter.eli +80 -35
- package/bootstrap/compiler/project.eli +175 -1
- package/bootstrap/compiler/transient-analysis.eli +2 -2
- package/bootstrap/host/build.eli +1 -0
- package/bootstrap/host/build.mjs +1 -1
- package/bootstrap/host/build.mjs.map +1 -1
- package/bootstrap/host/bun.mjs +7 -9
- package/bootstrap/host/bun.mjs.map +1 -1
- package/bootstrap/host/check-cli-ground.eli +47 -5
- package/bootstrap/host/check-cli-ground.mjs +60 -19
- package/bootstrap/host/check-cli-ground.mjs.map +1 -1
- package/bootstrap/host/check-cli.mjs +41 -1
- package/bootstrap/host/check-cli.mjs.map +1 -1
- package/bootstrap/host/compiler-session.eli +136 -22
- package/bootstrap/host/eliscript-cli.mjs +1 -1
- package/bootstrap/host/eliscript-cli.mjs.map +1 -1
- package/bootstrap/host/evaluation-cli.eli +33 -1
- package/bootstrap/host/evaluation-cli.mjs +65 -28
- package/bootstrap/host/evaluation-cli.mjs.map +1 -1
- package/bootstrap/host/evaluation.eli +651 -42
- package/bootstrap/host/evaluation.mjs +884 -72
- package/bootstrap/host/evaluation.mjs.map +1 -1
- package/bootstrap/host/project-cli-ground.eli +6 -1
- package/bootstrap/host/project-cli-ground.mjs +33 -32
- package/bootstrap/host/project-cli-ground.mjs.map +1 -1
- package/bootstrap/host/project-cli.mjs +1 -1
- package/bootstrap/host/project-cli.mjs.map +1 -1
- package/bootstrap/host/project-ground.eli +266 -45
- package/bootstrap/host/project-ground.mjs +429 -159
- package/bootstrap/host/project-ground.mjs.map +1 -1
- package/bootstrap/host/project.mjs +30 -40
- package/bootstrap/host/project.mjs.map +1 -1
- package/bootstrap/host/test-cli.eli +379 -0
- package/bootstrap/host/test-cli.mjs +494 -0
- package/bootstrap/host/test-cli.mjs.map +1 -0
- package/browser/README.md +7 -0
- package/browser/host.mjs +21 -29
- package/browser/host.mjs.map +1 -1
- package/browser/worker.mjs +7 -9
- package/browser/worker.mjs.map +1 -1
- package/compiler/eliscript-analyzer.el +694 -0
- package/compiler/eliscript-cli.el +66 -22
- package/compiler/eliscript-emitter.el +67 -43
- package/compiler/eliscript-ir-emitter.el +71 -36
- package/compiler/eliscript-project.el +360 -80
- package/compiler/eliscript-transient-analysis.el +5 -5
- package/compiler/eliscript.el +6 -0
- package/dist/backends/binary.mjs +148 -26
- package/dist/backends/binary.mjs.map +1 -1
- package/dist/backends/capabilities.mjs +11 -4
- package/dist/backends/capabilities.mjs.map +1 -1
- package/dist/backends/elf.mjs +4 -4
- package/dist/backends/elf.mjs.map +1 -1
- package/dist/backends/managed-layout.mjs +53 -56
- package/dist/backends/managed-layout.mjs.map +1 -1
- package/dist/backends/managed.mjs +1116 -375
- package/dist/backends/managed.mjs.map +1 -1
- package/dist/backends/memory.mjs +24 -26
- package/dist/backends/memory.mjs.map +1 -1
- package/dist/backends/native.mjs +2529 -434
- package/dist/backends/native.mjs.map +1 -1
- package/dist/backends/numeric.mjs +140 -33
- package/dist/backends/numeric.mjs.map +1 -1
- package/dist/backends/optimize.mjs +2 -2
- package/dist/backends/optimize.mjs.map +1 -1
- package/dist/backends/regexp.mjs +33 -35
- package/dist/backends/regexp.mjs.map +1 -1
- package/dist/backends/sha256.mjs +159 -59
- package/dist/backends/sha256.mjs.map +1 -1
- package/dist/backends/wasm.mjs +609 -73
- package/dist/backends/wasm.mjs.map +1 -1
- package/dist/bootstrap/analyzer.mjs +1018 -2
- package/dist/bootstrap/analyzer.mjs.map +1 -1
- package/dist/bootstrap/compiler.mjs +30 -23
- package/dist/bootstrap/compiler.mjs.map +1 -1
- package/dist/bootstrap/emitter.mjs +206 -161
- package/dist/bootstrap/emitter.mjs.macro-origins.json +16 -16
- package/dist/bootstrap/emitter.mjs.map +1 -1
- package/dist/bootstrap/expander.mjs +27 -31
- package/dist/bootstrap/expander.mjs.map +1 -1
- package/dist/bootstrap/ir.mjs +22 -24
- package/dist/bootstrap/ir.mjs.map +1 -1
- package/dist/bootstrap/project.mjs +246 -1
- package/dist/bootstrap/project.mjs.map +1 -1
- package/dist/bootstrap/reader.mjs +2 -2
- package/dist/bootstrap/reader.mjs.map +1 -1
- package/dist/bootstrap/source-map.mjs +34 -48
- package/dist/bootstrap/source-map.mjs.map +1 -1
- package/dist/bootstrap/transient-analysis.mjs +2 -2
- package/dist/bootstrap/transient-analysis.mjs.map +1 -1
- package/dist/browser/compiler.js +106 -106
- package/dist/browser/host.mjs +21 -29
- package/dist/browser/loader.js +1 -1
- package/dist/browser/worker.mjs +95 -95
- package/dist/compiler-artifact.json +1 -1
- package/dist/host/compiler-session.mjs +405 -228
- package/dist/host/compiler-session.mjs.map +1 -1
- package/dist/host/compiler-tools.mjs +44 -46
- package/dist/host/compiler-tools.mjs.map +1 -1
- package/dist/literal-sources/runtime/core/array-map.eli +45 -0
- package/dist/literal-sources/runtime/core/collection-internals.eli +22 -2
- package/dist/literal-sources/runtime/core/collection.eli +282 -23
- package/dist/literal-sources/runtime/core/data-text.eli +196 -13
- package/dist/literal-sources/runtime/core/data.eli +1 -1
- package/dist/literal-sources/runtime/core/identifier.eli +1 -1
- package/dist/literal-sources/runtime/core/lazy-sequence.eli +822 -44
- package/dist/literal-sources/runtime/core/list.eli +1 -1
- package/dist/literal-sources/runtime/core/map.eli +1 -1
- package/dist/literal-sources/runtime/core/metadata-internals.eli +2 -2
- package/dist/literal-sources/runtime/core/metadata.eli +2 -2
- package/dist/literal-sources/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
- package/dist/literal-sources/runtime/core/object.eli +1 -1
- package/dist/literal-sources/runtime/core/order.eli +3 -3
- package/dist/literal-sources/runtime/core/persistent-kind.eli +1 -1
- package/dist/literal-sources/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
- package/dist/literal-sources/runtime/core/protocol.eli +5 -3
- package/dist/literal-sources/runtime/core/queue.eli +1 -1
- package/dist/literal-sources/runtime/core/record.eli +3 -3
- package/dist/literal-sources/runtime/core/regex.eli +2 -2
- package/dist/literal-sources/runtime/core/sequence.eli +1 -1
- package/dist/literal-sources/runtime/core/set-algebra.eli +1 -1
- package/dist/literal-sources/runtime/core/set.eli +1 -1
- package/dist/literal-sources/runtime/core/sorted.eli +4 -4
- package/dist/literal-sources/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
- package/dist/literal-sources/runtime/core/text.eli +1 -1
- package/dist/literal-sources/runtime/core/transducer.eli +24 -2
- package/dist/literal-sources/runtime/core/transient.eli +7 -5
- package/dist/literal-sources/runtime/core/type.eli +1 -1
- package/dist/literal-sources/runtime/core/value-internals.eli +3 -3
- package/dist/literal-sources/runtime/core/value.eli +2 -2
- package/dist/literal-sources/runtime/core/vector.eli +5 -2
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-literals.eli +406 -81
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-queue-state.eli +1 -1
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-sorted-state.eli +1 -1
- package/dist/literal-sources/runtime/managed/managed-transient-state.eli +1206 -0
- package/{stdlib → dist/literal-sources/runtime/managed}/managed-value-state.eli +30 -6
- package/dist/literal-sources/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
- package/dist/literal-sources/runtime/wire/http-rpc.eli +1 -1
- package/dist/literal-sources/runtime/wire/markdown.eli +1 -1
- package/dist/literal-sources/runtime/wire/schema.eli +1 -1
- package/dist/literal-sources/stdlib/bit.eli +89 -1
- package/dist/literal-sources/stdlib/data.eli +522 -0
- package/dist/literal-sources/stdlib/interop/object.eli +39 -0
- package/dist/literal-sources/stdlib/persistent-list.eli +60 -21
- package/dist/literal-sources/stdlib/persistent-map.eli +335 -324
- package/dist/literal-sources/stdlib/persistent-set.eli +8 -6
- package/dist/literal-sources/stdlib/persistent-vector.eli +18 -18
- package/dist/literal-sources/stdlib/protocol.eli +63 -0
- package/{stdlib/core → dist/literal-sources/stdlib}/set.eli +8 -8
- package/dist/literal-sources/stdlib/string.eli +362 -0
- package/dist/runtime/lisp/async-runtime.mjs +55 -59
- package/dist/runtime/lisp/async-runtime.mjs.map +1 -1
- package/dist/runtime/lisp/builtins.mjs +232 -280
- package/dist/runtime/lisp/builtins.mjs.map +1 -1
- package/dist/runtime/lisp/cli.mjs +159 -167
- package/dist/runtime/lisp/cli.mjs.map +1 -1
- package/dist/runtime/lisp/compiler.mjs +359 -419
- package/dist/runtime/lisp/compiler.mjs.map +1 -1
- package/dist/runtime/lisp/core.mjs +24 -24
- package/dist/runtime/lisp/core.mjs.map +1 -1
- package/dist/runtime/lisp/evaluator.mjs +315 -385
- package/dist/runtime/lisp/evaluator.mjs.map +1 -1
- package/dist/runtime/lisp/extensions.mjs +85 -103
- package/dist/runtime/lisp/extensions.mjs.map +1 -1
- package/dist/runtime/lisp/host-io.mjs +106 -122
- package/dist/runtime/lisp/host-io.mjs.map +1 -1
- package/dist/runtime/lisp/io-worker.mjs +117 -135
- package/dist/runtime/lisp/io-worker.mjs.map +1 -1
- package/dist/runtime/lisp/keymap.mjs +5 -5
- package/dist/runtime/lisp/keymap.mjs.map +1 -1
- package/dist/runtime/lisp/macros-forms.mjs +49 -51
- package/dist/runtime/lisp/macros-forms.mjs.map +1 -1
- package/dist/runtime/lisp/reader-format.mjs +31 -31
- package/dist/runtime/lisp/reader-format.mjs.map +1 -1
- package/dist/runtime/lisp/regexp-buffer.mjs +9 -9
- package/dist/runtime/lisp/regexp-buffer.mjs.map +1 -1
- package/dist/runtime/math.mjs +32 -38
- package/dist/runtime/math.mjs.map +1 -1
- package/dist/runtime/number-radix.mjs +5 -7
- package/dist/runtime/number-radix.mjs.map +1 -1
- package/dist/runtime/primitives.mjs +81 -46
- package/dist/runtime/primitives.mjs.macro-origins.json +294 -44
- package/dist/runtime/primitives.mjs.map +1 -1
- package/dist/runtime/unicode-case.mjs +6 -6
- package/dist/runtime/unicode-case.mjs.map +1 -1
- package/dist/runtime.eli +3068 -1882
- package/dist/stage3-release.json +1 -1
- package/dist/stdlib/async.mjs +1838 -0
- package/dist/stdlib/bit.mjs +116 -0
- package/dist/stdlib/collection.mjs +193 -0
- package/dist/stdlib/core.mjs +28 -0
- package/dist/stdlib/data-text.mjs +54 -0
- package/dist/stdlib/data.mjs +601 -0
- package/dist/stdlib/devtools.mjs +565 -0
- package/dist/stdlib/diff.mjs +180 -0
- package/dist/stdlib/fn.mjs +164 -0
- package/dist/stdlib/hierarchy.mjs +189 -0
- package/dist/stdlib/identifier.mjs +33 -0
- package/dist/stdlib/interop/js.mjs +706 -0
- package/dist/stdlib/interop/object.mjs +40 -0
- package/dist/stdlib/json.mjs +843 -0
- package/dist/stdlib/lazy-sequence.mjs +117 -0
- package/dist/stdlib/math.mjs +386 -0
- package/dist/stdlib/meta.mjs +21 -0
- package/dist/stdlib/multimethod.mjs +345 -0
- package/dist/stdlib/numeric.mjs +327 -0
- package/dist/stdlib/order.mjs +194 -0
- package/dist/stdlib/persistent-list.mjs +171 -0
- package/dist/stdlib/persistent-map.mjs +781 -0
- package/dist/stdlib/persistent-queue.mjs +44 -0
- package/dist/stdlib/persistent-set.mjs +225 -0
- package/dist/stdlib/persistent-vector.mjs +316 -0
- package/dist/stdlib/portable/data-text.mjs +1094 -0
- package/dist/stdlib/portable/data.mjs +62 -0
- package/dist/stdlib/portable/identifier.mjs +179 -0
- package/dist/stdlib/portable/metadata.mjs +63 -0
- package/dist/stdlib/portable/object.mjs +140 -0
- package/dist/stdlib/portable/sequence.mjs +142 -0
- package/dist/stdlib/portable/text.mjs +111 -0
- package/dist/stdlib/pprint.mjs +807 -0
- package/dist/stdlib/protocol.mjs +61 -0
- package/dist/stdlib/record.mjs +25 -0
- package/dist/stdlib/regex.mjs +115 -0
- package/dist/stdlib/result.mjs +198 -0
- package/dist/stdlib/schema.mjs +2828 -0
- package/dist/stdlib/seq.mjs +913 -0
- package/dist/stdlib/set.mjs +317 -0
- package/dist/stdlib/sorted.mjs +56 -0
- package/dist/stdlib/state.mjs +451 -0
- package/dist/stdlib/string.mjs +494 -0
- package/dist/stdlib/tap.mjs +187 -0
- package/dist/stdlib/test.mjs +669 -0
- package/dist/stdlib/time.mjs +522 -0
- package/dist/stdlib/transducer.mjs +85 -0
- package/dist/stdlib/transient.mjs +32 -0
- package/dist/stdlib/type.mjs +28 -0
- package/dist/stdlib/uuid.mjs +377 -0
- package/dist/stdlib/value.mjs +334 -0
- package/dist/stdlib/walk.mjs +243 -0
- package/dist/stdlib/wire/gepo.mjs +28 -0
- package/dist/stdlib/wire/mdp.mjs +13 -0
- package/dist/stdlib/wire/mds.mjs +10 -0
- package/dist/stdlib/zipper.mjs +1025 -0
- package/editor/README.md +15 -0
- package/editor/eliscript-data.el +221 -8
- package/editor/eliscript-mode.el +207 -48
- package/editor/eliscript-runtime.el +6 -3
- package/package.json +49 -15
- package/platform/README.md +6 -0
- package/platform/browser.mjs +10 -12
- package/platform/browser.mjs.map +1 -1
- package/runtime/README.md +12 -5
- package/runtime/core/array-map.eli +538 -0
- package/runtime/core/array-map.mjs +651 -0
- package/runtime/core/array-map.mjs.map +1 -0
- package/runtime/core/collection-internals.eli +85 -33
- package/runtime/core/collection-internals.mjs +245 -193
- package/runtime/core/collection-internals.mjs.map +1 -1
- package/runtime/core/collection.eli +105 -3
- package/runtime/core/collection.mjs +190 -53
- package/runtime/core/collection.mjs.map +1 -1
- package/runtime/core/data-text.eli +224 -16
- package/runtime/core/data-text.mjs +345 -83
- package/runtime/core/data-text.mjs.map +1 -1
- package/runtime/core/data.eli +50 -33
- package/runtime/core/data.mjs +72 -36
- package/runtime/core/data.mjs.map +1 -1
- package/runtime/core/identifier.mjs +15 -19
- package/runtime/core/identifier.mjs.map +1 -1
- package/runtime/core/lazy-sequence.eli +885 -15
- package/runtime/core/lazy-sequence.mjs +1265 -38
- package/runtime/core/lazy-sequence.mjs.map +1 -1
- package/runtime/core/map-internals.eli +209 -1
- package/runtime/core/map-internals.mjs +359 -71
- package/runtime/core/map-internals.mjs.map +1 -1
- package/runtime/core/map.eli +75 -7
- package/runtime/core/map.mjs +122 -47
- package/runtime/core/map.mjs.map +1 -1
- package/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
- package/runtime/core/object-impl.mjs +5 -5
- package/runtime/core/object-impl.mjs.map +1 -1
- package/runtime/core/order.eli +15 -3
- package/runtime/core/order.mjs +23 -8
- package/runtime/core/order.mjs.map +1 -1
- package/runtime/core/persistent-kind.mjs +9 -13
- package/runtime/core/persistent-kind.mjs.map +1 -1
- package/runtime/core/protocol-error.mjs +9 -11
- package/runtime/core/protocol-error.mjs.map +1 -1
- package/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
- package/runtime/core/protocol-impl.mjs +10 -10
- package/runtime/core/protocol-impl.mjs.map +1 -1
- package/runtime/core/queue.mjs +14 -16
- package/runtime/core/queue.mjs.map +1 -1
- package/runtime/core/record.eli +2 -2
- package/runtime/core/record.mjs +13 -17
- package/runtime/core/record.mjs.map +1 -1
- package/runtime/core/regex.mjs +18 -22
- package/runtime/core/regex.mjs.map +1 -1
- package/runtime/core/sequence.eli +33 -31
- package/runtime/core/sequence.mjs +60 -93
- package/runtime/core/sequence.mjs.map +1 -1
- package/runtime/core/set.eli +53 -27
- package/runtime/core/set.mjs +113 -82
- package/runtime/core/set.mjs.map +1 -1
- package/runtime/core/sorted.mjs +91 -108
- package/runtime/core/sorted.mjs.map +1 -1
- package/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
- package/runtime/core/text-impl.mjs +1 -1
- package/runtime/core/text-impl.mjs.map +1 -1
- package/runtime/core/transducer.eli +24 -0
- package/runtime/core/transducer.mjs +27 -1
- package/runtime/core/transducer.mjs.map +1 -1
- package/runtime/core/transient-internals.eli +12 -4
- package/runtime/core/transient-internals.mjs +19 -6
- package/runtime/core/transient-internals.mjs.map +1 -1
- package/runtime/core/transient.eli +31 -0
- package/runtime/core/transient.mjs +33 -2
- package/runtime/core/transient.mjs.map +1 -1
- package/runtime/core/value-internals.mjs +29 -35
- package/runtime/core/value-internals.mjs.map +1 -1
- package/runtime/core/vector.eli +73 -20
- package/runtime/core/vector.mjs +159 -156
- package/runtime/core/vector.mjs.map +1 -1
- package/runtime/literals.eli +16 -9
- package/runtime/literals.mjs +25 -9
- package/runtime/literals.mjs.map +1 -1
- package/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
- package/{stdlib → runtime/managed}/managed-literals.eli +406 -81
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-queue-state.eli +1 -1
- package/runtime/managed/managed-runtime-array-map.eli +45 -0
- package/{stdlib → runtime/managed}/managed-runtime-collection-internals.eli +22 -2
- package/{stdlib → runtime/managed}/managed-runtime-collection.eli +282 -23
- package/{stdlib → runtime/managed}/managed-runtime-data-text.eli +196 -13
- package/{stdlib → runtime/managed}/managed-runtime-data.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-identifier.eli +1 -1
- package/runtime/managed/managed-runtime-lazy-sequence.eli +935 -0
- package/{stdlib → runtime/managed}/managed-runtime-list.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-map.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-metadata-internals.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-metadata.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-object.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-order.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-persistent-kind.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-protocol.eli +5 -3
- package/{stdlib → runtime/managed}/managed-runtime-queue.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-record.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-regex.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-sequence.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-set-algebra.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-set.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-sorted.eli +4 -4
- package/{stdlib → runtime/managed}/managed-runtime-text.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-transducer.eli +24 -2
- package/{stdlib → runtime/managed}/managed-runtime-transient.eli +7 -5
- package/{stdlib → runtime/managed}/managed-runtime-type.eli +1 -1
- package/{stdlib → runtime/managed}/managed-runtime-value-internals.eli +3 -3
- package/{stdlib → runtime/managed}/managed-runtime-value.eli +2 -2
- package/{stdlib → runtime/managed}/managed-runtime-vector.eli +5 -2
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-sorted-state.eli +1 -1
- package/runtime/managed/managed-transient-state.eli +1206 -0
- package/{dist/literal-sources/stdlib → runtime/managed}/managed-value-state.eli +30 -6
- package/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-http-rpc.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-markdown.eli +1 -1
- package/{stdlib → runtime/managed}/managed-wire-schema.eli +1 -1
- package/runtime/math.mjs +32 -38
- package/runtime/math.mjs.map +1 -1
- package/runtime/number-radix.mjs +5 -7
- package/runtime/number-radix.mjs.map +1 -1
- package/runtime/primitives.eli +19 -6
- package/runtime/primitives.mjs +81 -46
- package/runtime/primitives.mjs.map +1 -1
- package/runtime/source-mapping.mjs +17 -23
- package/runtime/source-mapping.mjs.map +1 -1
- package/runtime/testing/map.eli +70 -41
- package/runtime/testing/map.mjs +57 -34
- package/runtime/testing/map.mjs.map +1 -1
- package/runtime/unicode-case.mjs +6 -6
- package/runtime/unicode-case.mjs.map +1 -1
- package/runtime/wire/http-rpc.mjs +12 -18
- package/runtime/wire/http-rpc.mjs.map +1 -1
- package/runtime/wire/markdown.mjs +42 -42
- package/runtime/wire/markdown.mjs.map +1 -1
- package/runtime/wire/schema.mjs +13 -13
- package/runtime/wire/schema.mjs.map +1 -1
- package/runtime/worker-value-codec.mjs +5 -7
- package/runtime/worker-value-codec.mjs.map +1 -1
- package/runtime/worker-value-stream.mjs +128 -135
- package/runtime/worker-value-stream.mjs.map +1 -1
- package/runtime/worker.mjs +75 -89
- package/runtime/worker.mjs.map +1 -1
- package/stdlib/README.md +25 -18
- package/stdlib/async.eli +1245 -0
- package/stdlib/bit.eli +89 -1
- package/stdlib/bit.mjs +89 -2
- package/stdlib/bit.mjs.map +1 -1
- package/stdlib/{core/collection.eli → collection.eli} +39 -4
- package/stdlib/core.eli +956 -0
- package/stdlib/data-text.eli +62 -1091
- package/stdlib/data.eli +517 -37
- package/stdlib/devtools.eli +399 -0
- package/stdlib/diff.eli +134 -0
- package/stdlib/{function.eli → fn.eli} +1 -1
- package/stdlib/identifier.eli +21 -136
- package/stdlib/interop/object.eli +39 -0
- package/stdlib/{core/lazy-sequence.eli → lazy-sequence.eli} +28 -2
- package/stdlib/math.eli +267 -0
- package/stdlib/{core/metadata.eli → meta.eli} +2 -2
- package/stdlib/{core/order.eli → order.eli} +6 -6
- package/stdlib/persistent-list.eli +60 -21
- package/stdlib/persistent-map.eli +335 -324
- package/stdlib/persistent-set.eli +8 -6
- package/stdlib/persistent-vector.eli +18 -18
- package/stdlib/portable/data-text.eli +1092 -0
- package/stdlib/portable/data.eli +42 -0
- package/stdlib/portable/identifier.eli +147 -0
- package/stdlib/{metadata.eli → portable/metadata.eli} +5 -5
- package/stdlib/{object.eli → portable/object.eli} +1 -1
- package/stdlib/{sequence.eli → portable/sequence.eli} +1 -1
- package/stdlib/{text.eli → portable/text.eli} +1 -1
- package/stdlib/pprint.eli +588 -0
- package/stdlib/protocol.eli +63 -0
- package/stdlib/{core/record.eli → record.eli} +2 -2
- package/stdlib/regex.eli +70 -0
- package/stdlib/result.eli +53 -44
- package/stdlib/schema.eli +1886 -0
- package/stdlib/{core/seq.eli → seq.eli} +97 -90
- package/{dist/literal-sources/stdlib/core → stdlib}/set.eli +8 -8
- package/stdlib/{core/sorted.eli → sorted.eli} +2 -2
- package/stdlib/{state/atom.eli → state.eli} +137 -7
- package/stdlib/string.eli +362 -0
- package/stdlib/tap.eli +135 -0
- package/stdlib/test.eli +566 -0
- package/stdlib/time.eli +419 -0
- package/stdlib/{core/transducer.eli → transducer.eli} +4 -2
- package/stdlib/{core/transient.eli → transient.eli} +11 -5
- package/stdlib/{core/type.eli → type.eli} +2 -2
- package/stdlib/uuid.eli +288 -0
- package/stdlib/{core/walk.eli → walk.eli} +6 -6
- package/stdlib/{gepo.eli → wire/gepo.eli} +2 -2
- package/stdlib/{mdp.eli → wire/mdp.eli} +2 -2
- package/stdlib/{mds.eli → wire/mds.eli} +2 -2
- package/stdlib/{core/zipper.eli → zipper.eli} +179 -138
- package/tests/helpers/wasm-managed-math-node.mjs +495 -582
- package/tools/acceptance/check-ground.mjs +106 -132
- package/tools/acceptance/check.mjs +16 -16
- package/tools/acceptance/retained-performance-snapshot.mjs +265 -149
- package/tools/acceptance/retained-soak.mjs +5 -7
- package/tools/acceptance/retained-transient.mjs +5 -7
- package/tools/backends/build.mjs +373 -95
- package/tools/backends/compiler-bootstrap.mjs +271 -317
- package/tools/backends/compiler-browser.mjs +7 -9
- package/tools/backends/compiler-build.mjs +77 -97
- package/tools/backends/compiler-cli-ground.mjs +47 -47
- package/tools/backends/compiler-data-transport.mjs +27 -31
- package/tools/backends/compiler-host-iteration.mjs +13 -21
- package/tools/backends/compiler-host-record.mjs +28 -34
- package/tools/backends/compiler-metadata.mjs +145 -53
- package/tools/backends/compiler-package-ground.mjs +39 -47
- package/tools/backends/compiler-package-loader.mjs +164 -206
- package/tools/backends/compiler-session.mjs +432 -324
- package/tools/backends/compiler-worker.mjs +79 -89
- package/tools/backends/dist/bootstrap-js-stages.mjs +17 -19
- package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
- package/tools/backends/dist/release-cli.mjs +151 -22
- package/tools/backends/dist/release-cli.mjs.map +1 -1
- package/tools/backends/dist/run-artifact.mjs +54 -76
- package/tools/backends/dist/run-artifact.mjs.map +1 -1
- package/tools/backends/managed-foreign-binding.mjs +370 -114
- package/tools/backends/managed-host-ground.mjs +14 -17
- package/tools/backends/managed-host-lifecycle.mjs +42 -53
- package/tools/backends/managed-host-object-realm.mjs +121 -111
- package/tools/backends/managed-host-proxy.mjs +5 -7
- package/tools/backends/managed-host-runtime.mjs +6295 -4350
- package/tools/backends/managed-host.mjs +234 -188
- package/tools/backends/managed-layout.mjs +53 -56
- package/tools/backends/managed-module-graph-ground.mjs +488 -568
- package/tools/backends/managed-module-graph.mjs +62 -76
- package/tools/backends/managed-runtime-host-ground.mjs +352 -400
- package/tools/backends/managed-runtime.mjs +227 -266
- package/tools/backends/managed-source-view-ground.mjs +165 -161
- package/tools/backends/managed-source-view.mjs +415 -154
- package/tools/backends/native-managed-runtime.mjs +96 -138
- package/tools/backends/native-memory.mjs +1122 -742
- package/tools/backends/native-node-ffi.mjs +2439 -0
- package/tools/backends/native-node-memory.mjs +793 -383
- package/tools/backends/native-node-runner.mjs +1458 -579
- package/tools/backends/native-node-watchdog.mjs +12 -14
- package/tools/backends/native-node-worker.mjs +46 -50
- package/tools/backends/stage3-targets.mjs +2 -2
- package/tools/bootstrap-runtime.el +4 -4
- package/tools/collections/transient-builder-benchmark.mjs +17 -17
- package/tools/project/check-cli.eli +27 -3
- package/tools/project/project-cli.eli +1 -1
- package/tools/testing/maintained-suite-source.mjs +5 -7
- package/tools/testing/maintained-suite-source.mjs.map +1 -1
- package/dist/literal-sources/stdlib/core/data.eli +0 -306
- package/dist/literal-sources/stdlib/managed-transient-state.eli +0 -353
- package/stdlib/core/data-text.eli +0 -16
- package/stdlib/core/data.eli +0 -306
- package/stdlib/core/identifier.eli +0 -32
- package/stdlib/core/regex.eli +0 -17
- package/stdlib/deferred.eli +0 -70
- package/stdlib/managed-runtime-lazy-sequence.eli +0 -157
- package/stdlib/managed-transient-state.eli +0 -353
- /package/{stdlib → runtime/managed}/managed-protocol-error.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-runtime-literals.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-runtime-truth.eli +0 -0
- /package/{stdlib → runtime/managed}/managed-testing-value.eli +0 -0
package/stdlib/async.eli
ADDED
|
@@ -0,0 +1,1245 @@
|
|
|
1
|
+
(module eliscript.async
|
|
2
|
+
(import "../runtime/core/collection.mjs" :as Collection)
|
|
3
|
+
(import "../runtime/core/identifier.mjs" keyword)
|
|
4
|
+
(import "../runtime/core/map.mjs" EMPTY_MAP)
|
|
5
|
+
(import "../runtime/core/vector.mjs" persistentVector)
|
|
6
|
+
|
|
7
|
+
(defconst KW_DEFAULT (keyword "default"))
|
|
8
|
+
(defconst MISSING (js-object))
|
|
9
|
+
|
|
10
|
+
(defvar fallback-async-bindings EMPTY_MAP)
|
|
11
|
+
|
|
12
|
+
(defconst async-local-storage
|
|
13
|
+
(try
|
|
14
|
+
(let ((proc (js* "typeof process !== 'undefined' ? process : null")))
|
|
15
|
+
(if (and proc (= (value-type (get proc :getBuiltinModule)) "function"))
|
|
16
|
+
(let* ((mod (js-call proc :getBuiltinModule "node:async_hooks"))
|
|
17
|
+
(ALS (if mod (get mod :AsyncLocalStorage) nil)))
|
|
18
|
+
(if ALS (new ALS) nil))
|
|
19
|
+
nil))
|
|
20
|
+
(catch _err nil)))
|
|
21
|
+
|
|
22
|
+
(defun truthy? (value)
|
|
23
|
+
(not (or (nil? value) (undefined? value) (eq value false))))
|
|
24
|
+
|
|
25
|
+
(defun read-opt (options k1 &optional k2 fallback)
|
|
26
|
+
(cond
|
|
27
|
+
((nullish? options) fallback)
|
|
28
|
+
((Collection/isMap options)
|
|
29
|
+
(let ((v1 (Collection/get options (keyword k1) MISSING)))
|
|
30
|
+
(if (not (eq v1 MISSING))
|
|
31
|
+
v1
|
|
32
|
+
(let ((s1 (Collection/get options k1 MISSING)))
|
|
33
|
+
(if (not (eq s1 MISSING))
|
|
34
|
+
s1
|
|
35
|
+
(if (not (nullish? k2))
|
|
36
|
+
(let ((v2 (Collection/get options (keyword k2) MISSING)))
|
|
37
|
+
(if (not (eq v2 MISSING)) v2 (Collection/get options k2 fallback)))
|
|
38
|
+
fallback))))))
|
|
39
|
+
((= (value-type options) "object")
|
|
40
|
+
(cond
|
|
41
|
+
((Object/hasOwn options k1) (aref options k1))
|
|
42
|
+
((and (not (nullish? k2)) (Object/hasOwn options k2)) (aref options k2))
|
|
43
|
+
(t fallback)))
|
|
44
|
+
(t fallback)))
|
|
45
|
+
|
|
46
|
+
(defun has-opt? (options key-name)
|
|
47
|
+
(cond
|
|
48
|
+
((nullish? options) false)
|
|
49
|
+
((Collection/isMap options)
|
|
50
|
+
(or (Collection/contains options (keyword key-name))
|
|
51
|
+
(Collection/contains options key-name)))
|
|
52
|
+
((= (value-type options) "object")
|
|
53
|
+
(Object/hasOwn options key-name))
|
|
54
|
+
(t false)))
|
|
55
|
+
|
|
56
|
+
;; ============================================================================
|
|
57
|
+
;; Async Dynamic Bindings
|
|
58
|
+
;; ============================================================================
|
|
59
|
+
|
|
60
|
+
(defun current-async-bindings ()
|
|
61
|
+
(if (not (nil? async-local-storage))
|
|
62
|
+
(let ((store (js-call async-local-storage :getStore)))
|
|
63
|
+
(if (undefined? store) fallback-async-bindings store))
|
|
64
|
+
fallback-async-bindings))
|
|
65
|
+
|
|
66
|
+
(defun merge-bindings (base extra)
|
|
67
|
+
(cond
|
|
68
|
+
((nullish? extra) base)
|
|
69
|
+
((Collection/isMap extra)
|
|
70
|
+
(let ((entries (js-array))
|
|
71
|
+
(next base)
|
|
72
|
+
(i 0))
|
|
73
|
+
(Collection/reduceKV extra
|
|
74
|
+
(lambda (_acc k v) (js-call entries :push (js-array k v)) nil)
|
|
75
|
+
nil)
|
|
76
|
+
(while (< i (js-length entries))
|
|
77
|
+
(let ((pair (aref entries i)))
|
|
78
|
+
(setq next (Collection/assoc next (aref pair 0) (aref pair 1))))
|
|
79
|
+
(setq i (1+ i)))
|
|
80
|
+
next))
|
|
81
|
+
((= (value-type extra) "object")
|
|
82
|
+
(let ((ks (Object/keys extra))
|
|
83
|
+
(next base)
|
|
84
|
+
(i 0))
|
|
85
|
+
(while (< i (js-length ks))
|
|
86
|
+
(let ((k (aref ks i)))
|
|
87
|
+
(setq next (Collection/assoc next (keyword k) (aref extra k))))
|
|
88
|
+
(setq i (1+ i)))
|
|
89
|
+
next))
|
|
90
|
+
(t
|
|
91
|
+
(throw (new globalThis/TypeError "with-async-bindings expects a map or object")))))
|
|
92
|
+
|
|
93
|
+
(defun run-with-bindings (captured thunk &rest args)
|
|
94
|
+
(if (not (nil? async-local-storage))
|
|
95
|
+
(js-call async-local-storage :run captured (lambda () (apply thunk args)))
|
|
96
|
+
(let ((prev fallback-async-bindings))
|
|
97
|
+
(setq fallback-async-bindings captured)
|
|
98
|
+
(try
|
|
99
|
+
(let ((res (apply thunk args)))
|
|
100
|
+
(if (and res (= (value-type (get res :then)) "function"))
|
|
101
|
+
(js-call res :finally (lambda () (setq fallback-async-bindings prev)))
|
|
102
|
+
(progn
|
|
103
|
+
(setq fallback-async-bindings prev)
|
|
104
|
+
res)))
|
|
105
|
+
(catch err
|
|
106
|
+
(setq fallback-async-bindings prev)
|
|
107
|
+
(throw err))))))
|
|
108
|
+
|
|
109
|
+
(defun with-async-bindings (bindings thunk)
|
|
110
|
+
(unless (= (value-type thunk) "function")
|
|
111
|
+
(throw (new globalThis/TypeError "with-async-bindings expects a thunk function")))
|
|
112
|
+
(let ((merged (merge-bindings (current-async-bindings) bindings)))
|
|
113
|
+
(run-with-bindings merged thunk)))
|
|
114
|
+
|
|
115
|
+
;; ============================================================================
|
|
116
|
+
;; Buffers: Fixed, Dropping, Sliding
|
|
117
|
+
;; ============================================================================
|
|
118
|
+
|
|
119
|
+
(defconst ChannelBuffer
|
|
120
|
+
(js* "(class ChannelBuffer { constructor(kind, capacity) { this.kind = kind; this.capacity = capacity; this.items = []; Object.freeze(this); } })"))
|
|
121
|
+
|
|
122
|
+
(defun buffer? (value)
|
|
123
|
+
(js* "value instanceof ChannelBuffer"))
|
|
124
|
+
|
|
125
|
+
(defun buffer (n)
|
|
126
|
+
(unless (and (Number/isSafeInteger n) (>= n 0))
|
|
127
|
+
(throw (new globalThis/RangeError "buffer capacity must be a non-negative safe integer")))
|
|
128
|
+
(new ChannelBuffer "fixed" n))
|
|
129
|
+
|
|
130
|
+
(defun dropping-buffer (n)
|
|
131
|
+
(unless (and (Number/isSafeInteger n) (>= n 1))
|
|
132
|
+
(throw (new globalThis/RangeError "dropping-buffer capacity must be >= 1")))
|
|
133
|
+
(new ChannelBuffer "dropping" n))
|
|
134
|
+
|
|
135
|
+
(defun sliding-buffer (n)
|
|
136
|
+
(unless (and (Number/isSafeInteger n) (>= n 1))
|
|
137
|
+
(throw (new globalThis/RangeError "sliding-buffer capacity must be >= 1")))
|
|
138
|
+
(new ChannelBuffer "sliding" n))
|
|
139
|
+
|
|
140
|
+
(defun buffer-count (buf)
|
|
141
|
+
(js-length (get buf :items)))
|
|
142
|
+
|
|
143
|
+
(defun buffer-full? (buf)
|
|
144
|
+
(let ((kind (get buf :kind)))
|
|
145
|
+
(cond
|
|
146
|
+
((= kind "fixed") (>= (js-length (get buf :items)) (get buf :capacity)))
|
|
147
|
+
((or (= kind "dropping") (= kind "sliding")) false)
|
|
148
|
+
(t t))))
|
|
149
|
+
|
|
150
|
+
(defun buffer-add! (buf val)
|
|
151
|
+
(let ((kind (get buf :kind))
|
|
152
|
+
(items (get buf :items))
|
|
153
|
+
(cap (get buf :capacity)))
|
|
154
|
+
(cond
|
|
155
|
+
((= kind "dropping")
|
|
156
|
+
(when (< (js-length items) cap)
|
|
157
|
+
(js-call items :push val)))
|
|
158
|
+
((= kind "sliding")
|
|
159
|
+
(when (>= (js-length items) cap)
|
|
160
|
+
(js-call items :shift))
|
|
161
|
+
(js-call items :push val))
|
|
162
|
+
(t
|
|
163
|
+
(js-call items :push val)))))
|
|
164
|
+
|
|
165
|
+
(defun buffer-remove! (buf)
|
|
166
|
+
(js-call (get buf :items) :shift))
|
|
167
|
+
|
|
168
|
+
;; ============================================================================
|
|
169
|
+
;; Channels (CSP + Transducers + Non-blocking + Alt Handlers)
|
|
170
|
+
;; ============================================================================
|
|
171
|
+
|
|
172
|
+
(defconst AsyncChannel
|
|
173
|
+
(js* "(class AsyncChannel { constructor(buf, xform, exHandler) { this.buf = buf; this.closed = false; this.takes = []; this.puts = []; this.xform = xform || null; this.exHandler = exHandler || null; this.xformCompleted = false; } })"))
|
|
174
|
+
|
|
175
|
+
(defun chan? (value)
|
|
176
|
+
(js* "value instanceof AsyncChannel"))
|
|
177
|
+
|
|
178
|
+
(defun require-chan (value op)
|
|
179
|
+
(unless (chan? value)
|
|
180
|
+
(throw (new globalThis/TypeError (str op " expects a channel"))))
|
|
181
|
+
value)
|
|
182
|
+
|
|
183
|
+
(defun default-ex-handler (err)
|
|
184
|
+
nil)
|
|
185
|
+
|
|
186
|
+
(defun make-channel-rf (ch)
|
|
187
|
+
(lambda (&rest args)
|
|
188
|
+
(let ((argc (js-length args)))
|
|
189
|
+
(cond
|
|
190
|
+
((= argc 0) (get ch :buf))
|
|
191
|
+
((= argc 1) (aref args 0))
|
|
192
|
+
(t
|
|
193
|
+
(buffer-add! (get ch :buf) (aref args 1))
|
|
194
|
+
(aref args 0))))))
|
|
195
|
+
|
|
196
|
+
(defun chan (&optional buf-or-n xform ex-handler)
|
|
197
|
+
(let ((buf nil))
|
|
198
|
+
(cond
|
|
199
|
+
((nullish? buf-or-n) (setq buf nil))
|
|
200
|
+
((Number/isSafeInteger buf-or-n)
|
|
201
|
+
(when (< buf-or-n 0)
|
|
202
|
+
(throw (new globalThis/RangeError "channel buffer size must be >= 0")))
|
|
203
|
+
(when (> buf-or-n 0)
|
|
204
|
+
(setq buf (buffer buf-or-n))))
|
|
205
|
+
((buffer? buf-or-n) (setq buf buf-or-n))
|
|
206
|
+
(t (throw (new globalThis/TypeError "chan buffer must be nil, integer, or Buffer"))))
|
|
207
|
+
(when (and (not (nullish? xform)) (not (= (value-type xform) "function")))
|
|
208
|
+
(throw (new globalThis/TypeError "chan xform must be a transducer function")))
|
|
209
|
+
(when (and (not (nullish? ex-handler)) (not (= (value-type ex-handler) "function")))
|
|
210
|
+
(throw (new globalThis/TypeError "chan ex-handler must be a function")))
|
|
211
|
+
(when (and (not (nullish? xform)) (nil? buf))
|
|
212
|
+
;; Transducers require a buffer to support 0-to-many expansions.
|
|
213
|
+
(setq buf (buffer 1)))
|
|
214
|
+
(let ((ch (new AsyncChannel buf nil
|
|
215
|
+
(if (nullish? ex-handler) default-ex-handler ex-handler))))
|
|
216
|
+
(when (not (nullish? xform))
|
|
217
|
+
(let ((step (funcall xform (make-channel-rf ch))))
|
|
218
|
+
(put ch :xform step)))
|
|
219
|
+
ch)))
|
|
220
|
+
|
|
221
|
+
(defun closed? (ch)
|
|
222
|
+
(get (require-chan ch "closed?") :closed))
|
|
223
|
+
|
|
224
|
+
(defun handler-active? (h)
|
|
225
|
+
(get (get h :lock) :active))
|
|
226
|
+
|
|
227
|
+
(defun commit-handler (h)
|
|
228
|
+
(let ((lock (get h :lock)))
|
|
229
|
+
(if (get lock :active)
|
|
230
|
+
(progn
|
|
231
|
+
(put lock :active false)
|
|
232
|
+
t)
|
|
233
|
+
false)))
|
|
234
|
+
|
|
235
|
+
(defun pop-active! (queue)
|
|
236
|
+
(let ((found nil))
|
|
237
|
+
(while (and (nil? found) (> (js-length queue) 0))
|
|
238
|
+
(let ((h (js-call queue :shift)))
|
|
239
|
+
(when (handler-active? h)
|
|
240
|
+
(setq found h))))
|
|
241
|
+
found))
|
|
242
|
+
|
|
243
|
+
(defun commit-next-active! (queue)
|
|
244
|
+
(let ((found nil))
|
|
245
|
+
(while (and (nil? found) (> (js-length queue) 0))
|
|
246
|
+
(let ((h (js-call queue :shift)))
|
|
247
|
+
(when (commit-handler h)
|
|
248
|
+
(setq found h))))
|
|
249
|
+
found))
|
|
250
|
+
|
|
251
|
+
(defun invoke-waiter (waiter arg)
|
|
252
|
+
(let ((bindings (get waiter :bindings))
|
|
253
|
+
(cb (get waiter :fn)))
|
|
254
|
+
(if (not (nil? bindings))
|
|
255
|
+
(run-with-bindings bindings cb arg)
|
|
256
|
+
(funcall cb arg))))
|
|
257
|
+
|
|
258
|
+
(defun drain-buffer-to-takers! (ch)
|
|
259
|
+
(let ((buf (get ch :buf)))
|
|
260
|
+
(when (not (nil? buf))
|
|
261
|
+
(let ((keep-going t))
|
|
262
|
+
(while (and keep-going (> (buffer-count buf) 0))
|
|
263
|
+
(let ((taker (commit-next-active! (get ch :takes))))
|
|
264
|
+
(if (nil? taker)
|
|
265
|
+
(setq keep-going false)
|
|
266
|
+
(invoke-waiter taker (buffer-remove! buf)))))))))
|
|
267
|
+
|
|
268
|
+
(defun run-xform-step (ch val)
|
|
269
|
+
(let ((xf (get ch :xform))
|
|
270
|
+
(exh (get ch :exHandler))
|
|
271
|
+
(should-close false))
|
|
272
|
+
(try
|
|
273
|
+
(let ((res (funcall xf (get ch :buf) val)))
|
|
274
|
+
(when (Collection/isReduced res)
|
|
275
|
+
(setq should-close t)))
|
|
276
|
+
(catch err
|
|
277
|
+
(let ((recovered (funcall exh err)))
|
|
278
|
+
(unless (nullish? recovered)
|
|
279
|
+
(buffer-add! (get ch :buf) recovered)))))
|
|
280
|
+
(drain-buffer-to-takers! ch)
|
|
281
|
+
should-close))
|
|
282
|
+
|
|
283
|
+
(defun drain-waiters-on-close! (ch)
|
|
284
|
+
(let ((takes (get ch :takes))
|
|
285
|
+
(puts (get ch :puts)))
|
|
286
|
+
(while (> (js-length takes) 0)
|
|
287
|
+
(let ((taker (commit-next-active! takes)))
|
|
288
|
+
(unless (nil? taker)
|
|
289
|
+
(invoke-waiter taker nil))))
|
|
290
|
+
(while (> (js-length puts) 0)
|
|
291
|
+
(let ((putter (commit-next-active! puts)))
|
|
292
|
+
(unless (nil? putter)
|
|
293
|
+
(invoke-waiter putter false))))))
|
|
294
|
+
|
|
295
|
+
(defun close! (ch)
|
|
296
|
+
(require-chan ch "close!")
|
|
297
|
+
(unless (get ch :closed)
|
|
298
|
+
(put ch :closed t)
|
|
299
|
+
(let ((xf (get ch :xform))
|
|
300
|
+
(buf (get ch :buf)))
|
|
301
|
+
(when (and (not (nil? xf)) (not (get ch :xformCompleted)))
|
|
302
|
+
(put ch :xformCompleted t)
|
|
303
|
+
(try
|
|
304
|
+
(funcall xf buf)
|
|
305
|
+
(catch err
|
|
306
|
+
(let ((recovered (funcall (get ch :exHandler) err)))
|
|
307
|
+
(unless (nullish? recovered)
|
|
308
|
+
(buffer-add! buf recovered))))))
|
|
309
|
+
(drain-buffer-to-takers! ch))
|
|
310
|
+
(drain-waiters-on-close! ch))
|
|
311
|
+
nil)
|
|
312
|
+
|
|
313
|
+
(defun pump-buffer! (ch)
|
|
314
|
+
(let ((buf (get ch :buf)))
|
|
315
|
+
(when (and (not (nil? buf)) (not (get ch :closed)))
|
|
316
|
+
(let ((puts (get ch :puts)))
|
|
317
|
+
(while (and (not (get ch :closed))
|
|
318
|
+
(not (buffer-full? buf))
|
|
319
|
+
(> (js-length puts) 0))
|
|
320
|
+
(let ((putter (commit-next-active! puts)))
|
|
321
|
+
(unless (nil? putter)
|
|
322
|
+
(let ((val (get putter :val)))
|
|
323
|
+
(if (nil? (get ch :xform))
|
|
324
|
+
(progn
|
|
325
|
+
(buffer-add! buf val)
|
|
326
|
+
(invoke-waiter putter t)
|
|
327
|
+
(drain-buffer-to-takers! ch))
|
|
328
|
+
(let ((stop (run-xform-step ch val)))
|
|
329
|
+
(invoke-waiter putter t)
|
|
330
|
+
(when stop (close! ch))))))))))))
|
|
331
|
+
|
|
332
|
+
(defun try-put-with-lock! (ch val putter)
|
|
333
|
+
(require-chan ch "put!")
|
|
334
|
+
(when (nullish? val)
|
|
335
|
+
(throw (new globalThis/TypeError "cannot put nil or undefined onto a channel")))
|
|
336
|
+
(cond
|
|
337
|
+
((get ch :closed)
|
|
338
|
+
(if (commit-handler putter)
|
|
339
|
+
(js-object :done t :accepted false)
|
|
340
|
+
(js-object :done t :accepted false)))
|
|
341
|
+
((and (nil? (get ch :buf)) (nil? (get ch :xform)))
|
|
342
|
+
;; Unbuffered direct rendezvous
|
|
343
|
+
(let ((taker (pop-active! (get ch :takes))))
|
|
344
|
+
(if (nil? taker)
|
|
345
|
+
(js-object :done false)
|
|
346
|
+
(if (commit-handler putter)
|
|
347
|
+
(if (commit-handler taker)
|
|
348
|
+
(progn
|
|
349
|
+
(invoke-waiter taker val)
|
|
350
|
+
(js-object :done t :accepted t))
|
|
351
|
+
;; Rare concurrent alts taker consumed right between pop & commit; retry take queue
|
|
352
|
+
(progn
|
|
353
|
+
;; Put back lock is already spent only for taker, putter still active — re-enable lock for retry
|
|
354
|
+
(put (get putter :lock) :active t)
|
|
355
|
+
(try-put-with-lock! ch val putter)))
|
|
356
|
+
(progn
|
|
357
|
+
;; Our putter lock was already taken concurrently
|
|
358
|
+
(js-call (get ch :takes) :unshift taker)
|
|
359
|
+
(js-object :done t :accepted false))))))
|
|
360
|
+
(t
|
|
361
|
+
(let ((buf (get ch :buf)))
|
|
362
|
+
(if (buffer-full? buf)
|
|
363
|
+
(js-object :done false)
|
|
364
|
+
(if (commit-handler putter)
|
|
365
|
+
(progn
|
|
366
|
+
(if (nil? (get ch :xform))
|
|
367
|
+
(if (and (= (buffer-count buf) 0) (> (js-length (get ch :takes)) 0))
|
|
368
|
+
(let ((taker (commit-next-active! (get ch :takes))))
|
|
369
|
+
(if (not (nil? taker))
|
|
370
|
+
(invoke-waiter taker val)
|
|
371
|
+
(buffer-add! buf val)))
|
|
372
|
+
(progn
|
|
373
|
+
(buffer-add! buf val)
|
|
374
|
+
(drain-buffer-to-takers! ch)))
|
|
375
|
+
(when (run-xform-step ch val)
|
|
376
|
+
(close! ch)))
|
|
377
|
+
(js-object :done t :accepted t))
|
|
378
|
+
(js-object :done t :accepted false)))))))
|
|
379
|
+
|
|
380
|
+
(defun try-take-with-lock! (ch taker)
|
|
381
|
+
(require-chan ch "take!")
|
|
382
|
+
(let ((buf (get ch :buf)))
|
|
383
|
+
(cond
|
|
384
|
+
((and (not (nil? buf)) (> (buffer-count buf) 0))
|
|
385
|
+
(if (commit-handler taker)
|
|
386
|
+
(let ((val (buffer-remove! buf)))
|
|
387
|
+
(pump-buffer! ch)
|
|
388
|
+
(js-object :done t :value val))
|
|
389
|
+
(js-object :done t :value nil)))
|
|
390
|
+
((get ch :closed)
|
|
391
|
+
(if (commit-handler taker)
|
|
392
|
+
(js-object :done t :value nil)
|
|
393
|
+
(js-object :done t :value nil)))
|
|
394
|
+
(t
|
|
395
|
+
;; Try taking directly from a waiting putter
|
|
396
|
+
(let ((found false)
|
|
397
|
+
(res-val nil))
|
|
398
|
+
(while (and (not found) (> (js-length (get ch :puts)) 0))
|
|
399
|
+
(let ((putter (pop-active! (get ch :puts))))
|
|
400
|
+
(when (and (not (nil? putter)) (commit-handler taker))
|
|
401
|
+
(if (commit-handler putter)
|
|
402
|
+
(progn
|
|
403
|
+
(setq found t res-val (get putter :val))
|
|
404
|
+
(invoke-waiter putter t))
|
|
405
|
+
(put (get taker :lock) :active t)))))
|
|
406
|
+
(if found
|
|
407
|
+
(js-object :done t :value res-val)
|
|
408
|
+
(js-object :done false)))))))
|
|
409
|
+
|
|
410
|
+
(defun make-waiter (lock fn &optional val)
|
|
411
|
+
(js-object :lock lock :fn fn :val val :bindings (current-async-bindings)))
|
|
412
|
+
|
|
413
|
+
(defun offer! (ch val)
|
|
414
|
+
(require-chan ch "offer!")
|
|
415
|
+
(when (nullish? val)
|
|
416
|
+
(throw (new globalThis/TypeError "cannot offer nil or undefined onto a channel")))
|
|
417
|
+
(let* ((lock (js-object :active t))
|
|
418
|
+
(waiter (make-waiter lock (lambda (_v) nil) val))
|
|
419
|
+
(res (try-put-with-lock! ch val waiter)))
|
|
420
|
+
(if (get res :done) (get res :accepted) false)))
|
|
421
|
+
|
|
422
|
+
(defun poll! (ch)
|
|
423
|
+
(require-chan ch "poll!")
|
|
424
|
+
(let* ((lock (js-object :active t))
|
|
425
|
+
(waiter (make-waiter lock (lambda (_v) nil)))
|
|
426
|
+
(res (try-take-with-lock! ch waiter)))
|
|
427
|
+
(if (get res :done) (get res :value) nil)))
|
|
428
|
+
|
|
429
|
+
(defun put! (ch val &optional on-complete)
|
|
430
|
+
(require-chan ch "put!")
|
|
431
|
+
(when (nullish? val)
|
|
432
|
+
(throw (new globalThis/TypeError "cannot put nil or undefined onto a channel")))
|
|
433
|
+
(new globalThis/Promise
|
|
434
|
+
(lambda (resolve)
|
|
435
|
+
(let* ((lock (js-object :active t))
|
|
436
|
+
(done-fn (lambda (accepted)
|
|
437
|
+
(unless (nullish? on-complete)
|
|
438
|
+
(funcall on-complete accepted))
|
|
439
|
+
(funcall resolve accepted)))
|
|
440
|
+
(waiter (make-waiter lock done-fn val))
|
|
441
|
+
(res (try-put-with-lock! ch val waiter)))
|
|
442
|
+
(if (get res :done)
|
|
443
|
+
(funcall done-fn (get res :accepted))
|
|
444
|
+
(js-call (get ch :puts) :push waiter))))))
|
|
445
|
+
|
|
446
|
+
(defun take! (ch &optional on-complete)
|
|
447
|
+
(require-chan ch "take!")
|
|
448
|
+
(new globalThis/Promise
|
|
449
|
+
(lambda (resolve)
|
|
450
|
+
(let* ((lock (js-object :active t))
|
|
451
|
+
(done-fn (lambda (val)
|
|
452
|
+
(unless (nullish? on-complete)
|
|
453
|
+
(funcall on-complete val))
|
|
454
|
+
(funcall resolve val)))
|
|
455
|
+
(waiter (make-waiter lock done-fn))
|
|
456
|
+
(res (try-take-with-lock! ch waiter)))
|
|
457
|
+
(if (get res :done)
|
|
458
|
+
(funcall done-fn (get res :value))
|
|
459
|
+
(js-call (get ch :takes) :push waiter))))))
|
|
460
|
+
|
|
461
|
+
(defun timeout (ms)
|
|
462
|
+
(let ((dur (Math/max 0 (if (nullish? ms) 0 ms)))
|
|
463
|
+
(ch (chan)))
|
|
464
|
+
(globalThis/setTimeout (lambda () (close! ch)) dur)
|
|
465
|
+
ch))
|
|
466
|
+
|
|
467
|
+
(defun to-chan! (coll)
|
|
468
|
+
(let ((items (js-array)))
|
|
469
|
+
(cond
|
|
470
|
+
((nullish? coll) nil)
|
|
471
|
+
((Array/isArray coll)
|
|
472
|
+
(let ((i 0))
|
|
473
|
+
(while (< i (js-length coll))
|
|
474
|
+
(js-call items :push (aref coll i))
|
|
475
|
+
(setq i (1+ i)))))
|
|
476
|
+
((Collection/isSeqable coll)
|
|
477
|
+
(Collection/reduce coll (lambda (_a v) (js-call items :push v) nil) nil))
|
|
478
|
+
(t
|
|
479
|
+
(throw (new globalThis/TypeError "to-chan! expects a seqable collection or array"))))
|
|
480
|
+
(let ((ch (chan (Math/max 1 (js-length items))))
|
|
481
|
+
(i 0))
|
|
482
|
+
(while (< i (js-length items))
|
|
483
|
+
(offer! ch (aref items i))
|
|
484
|
+
(setq i (1+ i)))
|
|
485
|
+
(close! ch)
|
|
486
|
+
ch)))
|
|
487
|
+
|
|
488
|
+
(defasync onto-chan! (ch coll &optional close?)
|
|
489
|
+
(let ((should-close (if (nullish? close?) t (truthy? close?)))
|
|
490
|
+
(items (js-array)))
|
|
491
|
+
(cond
|
|
492
|
+
((nullish? coll) nil)
|
|
493
|
+
((Array/isArray coll) (setq items coll))
|
|
494
|
+
((Collection/isSeqable coll)
|
|
495
|
+
(Collection/reduce coll (lambda (_a v) (js-call items :push v) nil) nil))
|
|
496
|
+
(t
|
|
497
|
+
(throw (new globalThis/TypeError "onto-chan! expects a seqable collection or array"))))
|
|
498
|
+
(let ((i 0)
|
|
499
|
+
(open t))
|
|
500
|
+
(while (and open (< i (js-length items)))
|
|
501
|
+
(setq open (await (put! ch (aref items i))))
|
|
502
|
+
(setq i (1+ i))))
|
|
503
|
+
(when should-close
|
|
504
|
+
(close! ch))
|
|
505
|
+
ch))
|
|
506
|
+
|
|
507
|
+
;; ============================================================================
|
|
508
|
+
;; Multiplexing: alts!
|
|
509
|
+
;; ============================================================================
|
|
510
|
+
|
|
511
|
+
(defun normalize-alts-ports (ports)
|
|
512
|
+
(let ((raw (cond
|
|
513
|
+
((Array/isArray ports) ports)
|
|
514
|
+
((Collection/isSeqable ports)
|
|
515
|
+
(let ((arr (js-array)))
|
|
516
|
+
(Collection/reduce ports (lambda (_a p) (js-call arr :push p) nil) nil)
|
|
517
|
+
arr))
|
|
518
|
+
(t (throw (new globalThis/TypeError "alts! expects an array or vector of ports"))))))
|
|
519
|
+
(when (= (js-length raw) 0)
|
|
520
|
+
(throw (new globalThis/RangeError "alts! requires at least one port")))
|
|
521
|
+
(js-call raw :map
|
|
522
|
+
(lambda (entry)
|
|
523
|
+
(cond
|
|
524
|
+
((chan? entry) (js-object :kind "take" :ch entry :port entry))
|
|
525
|
+
((or (Array/isArray entry) (Collection/isVector entry))
|
|
526
|
+
(let* ((arr (if (Array/isArray entry)
|
|
527
|
+
entry
|
|
528
|
+
(let ((tmp (js-array)))
|
|
529
|
+
(Collection/reduce entry (lambda (_a v) (js-call tmp :push v) nil) nil)
|
|
530
|
+
tmp)))
|
|
531
|
+
(_check (unless (= (js-length arr) 2)
|
|
532
|
+
(throw (new globalThis/TypeError "alts! put port must be [ch val]"))))
|
|
533
|
+
(ch (require-chan (aref arr 0) "alts!"))
|
|
534
|
+
(val (aref arr 1)))
|
|
535
|
+
(when (nullish? val)
|
|
536
|
+
(throw (new globalThis/TypeError "alts! cannot put nil or undefined onto channel")))
|
|
537
|
+
(js-object :kind "put" :ch ch :val val :port entry)))
|
|
538
|
+
(t
|
|
539
|
+
(throw (new globalThis/TypeError "alts! port must be channel or [channel value]"))))))))
|
|
540
|
+
|
|
541
|
+
(defun shuffle-indices (n)
|
|
542
|
+
(let ((indices (js-array))
|
|
543
|
+
(i 0))
|
|
544
|
+
(while (< i n)
|
|
545
|
+
(js-call indices :push i)
|
|
546
|
+
(setq i (1+ i)))
|
|
547
|
+
(let ((j (1- n)))
|
|
548
|
+
(while (> j 0)
|
|
549
|
+
(let* ((k (Math/floor (* (Math/random) (1+ j))))
|
|
550
|
+
(tmp (aref indices j)))
|
|
551
|
+
(put indices j (aref indices k))
|
|
552
|
+
(put indices k tmp))
|
|
553
|
+
(setq j (1- j))))
|
|
554
|
+
indices))
|
|
555
|
+
|
|
556
|
+
(defun alts! (ports &optional options)
|
|
557
|
+
(let* ((normalized (normalize-alts-ports ports))
|
|
558
|
+
(priority? (truthy? (read-opt options "priority" nil false)))
|
|
559
|
+
(has-default? (has-opt? options "default"))
|
|
560
|
+
(default-val (if has-default? (read-opt options "default" nil nil) nil))
|
|
561
|
+
(n (js-length normalized))
|
|
562
|
+
(order (if priority?
|
|
563
|
+
(let ((arr (js-array)) (i 0))
|
|
564
|
+
(while (< i n) (js-call arr :push i) (setq i (1+ i)))
|
|
565
|
+
arr)
|
|
566
|
+
(shuffle-indices n))))
|
|
567
|
+
(new globalThis/Promise
|
|
568
|
+
(lambda (resolve)
|
|
569
|
+
(let ((lock (js-object :active t))
|
|
570
|
+
(settled false)
|
|
571
|
+
(idx 0))
|
|
572
|
+
(while (and (not settled) (< idx n))
|
|
573
|
+
(let* ((spec (aref normalized (aref order idx)))
|
|
574
|
+
(ch (get spec :ch))
|
|
575
|
+
(port (get spec :port)))
|
|
576
|
+
(if (= (get spec :kind) "take")
|
|
577
|
+
(let* ((waiter (make-waiter lock (lambda (v) (funcall resolve (persistentVector v ch)))))
|
|
578
|
+
(res (try-take-with-lock! ch waiter)))
|
|
579
|
+
(when (get res :done)
|
|
580
|
+
(setq settled t)
|
|
581
|
+
(funcall resolve (persistentVector (get res :value) ch))))
|
|
582
|
+
(let* ((waiter (make-waiter lock (lambda (ok) (funcall resolve (persistentVector ok ch))) (get spec :val)))
|
|
583
|
+
(res (try-put-with-lock! ch (get spec :val) waiter)))
|
|
584
|
+
(when (get res :done)
|
|
585
|
+
(setq settled t)
|
|
586
|
+
(funcall resolve (persistentVector (get res :accepted) ch)))
|
|
587
|
+
;; Keep original port ref unused note: standard alts returns winning channel (ch)
|
|
588
|
+
(unless (nullish? port) nil))))
|
|
589
|
+
(setq idx (1+ idx)))
|
|
590
|
+
(when (not settled)
|
|
591
|
+
(if (and has-default? (commit-handler (js-object :lock lock)))
|
|
592
|
+
(funcall resolve (persistentVector default-val KW_DEFAULT))
|
|
593
|
+
(let ((pi 0))
|
|
594
|
+
(while (< pi n)
|
|
595
|
+
(let* ((spec (aref normalized (aref order pi)))
|
|
596
|
+
(ch (get spec :ch)))
|
|
597
|
+
(if (= (get spec :kind) "take")
|
|
598
|
+
(js-call (get ch :takes) :push
|
|
599
|
+
(make-waiter lock (lambda (v) (funcall resolve (persistentVector v ch)))))
|
|
600
|
+
(js-call (get ch :puts) :push
|
|
601
|
+
(make-waiter lock (lambda (ok) (funcall resolve (persistentVector ok ch))) (get spec :val)))))
|
|
602
|
+
(setq pi (1+ pi)))))))))))
|
|
603
|
+
|
|
604
|
+
;; ============================================================================
|
|
605
|
+
;; Structured Concurrency Scopes
|
|
606
|
+
;; ============================================================================
|
|
607
|
+
|
|
608
|
+
(defconst AsyncScope
|
|
609
|
+
(js* "(class AsyncScope { constructor() { this.cancelled = false; this.reason = null; this.tasks = []; this.channels = []; this.listeners = []; this.aborted = typeof AbortController === 'function' ? new AbortController() : null; } })"))
|
|
610
|
+
|
|
611
|
+
(defun scope-cancelled? (scope)
|
|
612
|
+
(get scope :cancelled))
|
|
613
|
+
|
|
614
|
+
(defun scope-signal (scope)
|
|
615
|
+
(let ((ctrl (get scope :aborted)))
|
|
616
|
+
(if (nil? ctrl) nil (get ctrl :signal))))
|
|
617
|
+
|
|
618
|
+
(defun scope-cancel! (scope &optional reason)
|
|
619
|
+
(unless (get scope :cancelled)
|
|
620
|
+
(put scope :cancelled t)
|
|
621
|
+
(put scope :reason (if (nullish? reason) (new globalThis/Error "Async scope cancelled") reason))
|
|
622
|
+
(let ((ctrl (get scope :aborted)))
|
|
623
|
+
(unless (nil? ctrl)
|
|
624
|
+
(js-call ctrl :abort (get scope :reason))))
|
|
625
|
+
(let ((channels (get scope :channels))
|
|
626
|
+
(i 0))
|
|
627
|
+
(while (< i (js-length channels))
|
|
628
|
+
(close! (aref channels i))
|
|
629
|
+
(setq i (1+ i))))
|
|
630
|
+
(let ((listeners (get scope :listeners))
|
|
631
|
+
(j 0))
|
|
632
|
+
(while (< j (js-length listeners))
|
|
633
|
+
(try (funcall (aref listeners j) (get scope :reason))
|
|
634
|
+
(catch _err nil))
|
|
635
|
+
(setq j (1+ j)))))
|
|
636
|
+
nil)
|
|
637
|
+
|
|
638
|
+
(defun scope-channel (scope &optional buf-or-n xform ex-handler)
|
|
639
|
+
(let ((ch (chan buf-or-n xform ex-handler)))
|
|
640
|
+
(if (get scope :cancelled)
|
|
641
|
+
(close! ch)
|
|
642
|
+
(js-call (get scope :channels) :push ch))
|
|
643
|
+
ch))
|
|
644
|
+
|
|
645
|
+
(defun scope-spawn (scope task-fn)
|
|
646
|
+
(unless (= (value-type task-fn) "function")
|
|
647
|
+
(throw (new globalThis/TypeError "scope-spawn expects a function")))
|
|
648
|
+
(let ((captured (current-async-bindings)))
|
|
649
|
+
(if (get scope :cancelled)
|
|
650
|
+
(Promise/resolve nil)
|
|
651
|
+
(let ((p (Promise/resolve)))
|
|
652
|
+
(let ((task-promise
|
|
653
|
+
(js-call
|
|
654
|
+
(js-call p :then
|
|
655
|
+
(lambda ()
|
|
656
|
+
(when (get scope :cancelled)
|
|
657
|
+
(throw (get scope :reason)))
|
|
658
|
+
(run-with-bindings captured task-fn)))
|
|
659
|
+
:catch
|
|
660
|
+
(lambda (err)
|
|
661
|
+
(unless (get scope :cancelled)
|
|
662
|
+
(scope-cancel! scope err)
|
|
663
|
+
(put scope :firstError err))
|
|
664
|
+
(throw err)))))
|
|
665
|
+
(js-call (get scope :tasks) :push task-promise)
|
|
666
|
+
task-promise)))))
|
|
667
|
+
|
|
668
|
+
(defasync with-async-scope (body-fn)
|
|
669
|
+
(unless (= (value-type body-fn) "function")
|
|
670
|
+
(throw (new globalThis/TypeError "with-async-scope expects a function")))
|
|
671
|
+
(let ((scope (new AsyncScope))
|
|
672
|
+
(body-err nil)
|
|
673
|
+
(has-body-err false)
|
|
674
|
+
(result nil))
|
|
675
|
+
(try
|
|
676
|
+
(setq result (await (funcall body-fn scope)))
|
|
677
|
+
(catch err
|
|
678
|
+
(setq body-err err has-body-err t)
|
|
679
|
+
(scope-cancel! scope err)))
|
|
680
|
+
(let ((settled (await (Promise/allSettled (get scope :tasks)))))
|
|
681
|
+
;; Always close owned channels when scope exits so no waiters leak.
|
|
682
|
+
(let ((channels (get scope :channels))
|
|
683
|
+
(ci 0))
|
|
684
|
+
(while (< ci (js-length channels))
|
|
685
|
+
(close! (aref channels ci))
|
|
686
|
+
(setq ci (1+ ci))))
|
|
687
|
+
(when has-body-err
|
|
688
|
+
(throw body-err))
|
|
689
|
+
(unless (nil? (get scope :firstError))
|
|
690
|
+
(throw (get scope :firstError)))
|
|
691
|
+
(let ((i 0))
|
|
692
|
+
(while (< i (js-length settled))
|
|
693
|
+
(let ((s (aref settled i)))
|
|
694
|
+
(when (= (get s :status) "rejected")
|
|
695
|
+
(throw (get s :reason))))
|
|
696
|
+
(setq i (1+ i)))))
|
|
697
|
+
result))
|
|
698
|
+
|
|
699
|
+
;; ============================================================================
|
|
700
|
+
;; Channel Composition Combinators
|
|
701
|
+
;; ============================================================================
|
|
702
|
+
|
|
703
|
+
(defun pipe (from to &optional close?)
|
|
704
|
+
(let ((should-close (if (nullish? close?) t (truthy? close?))))
|
|
705
|
+
(js-call
|
|
706
|
+
((lambda ()
|
|
707
|
+
(let ((step nil))
|
|
708
|
+
(setq step
|
|
709
|
+
(lambda ()
|
|
710
|
+
(js-call (take! from) :then
|
|
711
|
+
(lambda (v)
|
|
712
|
+
(if (nil? v)
|
|
713
|
+
(when should-close (close! to))
|
|
714
|
+
(js-call (put! to v) :then
|
|
715
|
+
(lambda (ok)
|
|
716
|
+
(when ok (funcall step)))))))))
|
|
717
|
+
(funcall step))))
|
|
718
|
+
:catch (lambda (_err) nil))
|
|
719
|
+
to))
|
|
720
|
+
|
|
721
|
+
(defun run-xf-to-array (xf ex-handler v)
|
|
722
|
+
(let ((out (js-array))
|
|
723
|
+
(step-fn (funcall xf (lambda (&rest args)
|
|
724
|
+
(let ((argc (js-length args)))
|
|
725
|
+
(cond
|
|
726
|
+
((= argc 0) (js-array))
|
|
727
|
+
((= argc 1) (aref args 0))
|
|
728
|
+
(t
|
|
729
|
+
(js-call (aref args 0) :push (aref args 1))
|
|
730
|
+
(aref args 0))))))))
|
|
731
|
+
(try
|
|
732
|
+
(let ((r1 (funcall step-fn out v)))
|
|
733
|
+
(let ((unr (if (Collection/isReduced r1) (Collection/unreduced r1) r1)))
|
|
734
|
+
(funcall step-fn unr)))
|
|
735
|
+
(catch err
|
|
736
|
+
(let ((recovered (funcall ex-handler err)))
|
|
737
|
+
(unless (nullish? recovered)
|
|
738
|
+
(js-call out :push recovered)))))
|
|
739
|
+
out))
|
|
740
|
+
|
|
741
|
+
(defun host-map? (x)
|
|
742
|
+
(js* "x instanceof globalThis.Map"))
|
|
743
|
+
|
|
744
|
+
(defun pipeline* (mode parallelism to fn-or-xf from close? ex-handler)
|
|
745
|
+
(unless (and (Number/isSafeInteger parallelism) (>= parallelism 1))
|
|
746
|
+
(throw (new globalThis/RangeError "pipeline parallelism n must be >= 1")))
|
|
747
|
+
(let* ((should-close (if (nullish? close?) t (truthy? close?)))
|
|
748
|
+
(exh (if (nullish? ex-handler) default-ex-handler ex-handler))
|
|
749
|
+
(jobs (chan parallelism))
|
|
750
|
+
(results (chan parallelism))
|
|
751
|
+
(stopped false))
|
|
752
|
+
;; Feeder task
|
|
753
|
+
((lambda ()
|
|
754
|
+
(let ((feed-step nil))
|
|
755
|
+
(setq feed-step
|
|
756
|
+
(lambda ()
|
|
757
|
+
(unless stopped
|
|
758
|
+
(js-call (take! from) :then
|
|
759
|
+
(lambda (v)
|
|
760
|
+
(if (or stopped (nil? v))
|
|
761
|
+
(progn
|
|
762
|
+
(close! jobs)
|
|
763
|
+
(close! results))
|
|
764
|
+
(let ((res-ch (chan 1)))
|
|
765
|
+
(js-call (put! jobs (js-object :val v :res res-ch)) :then
|
|
766
|
+
(lambda (ok1)
|
|
767
|
+
(if ok1
|
|
768
|
+
(js-call (put! results res-ch) :then
|
|
769
|
+
(lambda (ok2)
|
|
770
|
+
(when ok2 (funcall feed-step))))
|
|
771
|
+
(close! res-ch)))))))))))
|
|
772
|
+
(funcall feed-step))))
|
|
773
|
+
;; Spawn n bounded workers
|
|
774
|
+
(let ((wi 0))
|
|
775
|
+
(while (< wi parallelism)
|
|
776
|
+
((lambda ()
|
|
777
|
+
(let ((work-step nil))
|
|
778
|
+
(setq work-step
|
|
779
|
+
(lambda ()
|
|
780
|
+
(js-call (take! jobs) :then
|
|
781
|
+
(lambda (job)
|
|
782
|
+
(unless (nil? job)
|
|
783
|
+
(let ((val (get job :val))
|
|
784
|
+
(res-ch (get job :res)))
|
|
785
|
+
(if (= mode "sync")
|
|
786
|
+
(let ((items (run-xf-to-array fn-or-xf exh val)))
|
|
787
|
+
(js-call (onto-chan! res-ch items t) :then
|
|
788
|
+
(lambda () (funcall work-step))))
|
|
789
|
+
(try
|
|
790
|
+
(let ((maybe-p (funcall fn-or-xf val res-ch)))
|
|
791
|
+
(if (and maybe-p (= (value-type (get maybe-p :then)) "function"))
|
|
792
|
+
(js-call maybe-p :then
|
|
793
|
+
(lambda () (funcall work-step))
|
|
794
|
+
(lambda () (close! res-ch) (funcall work-step)))
|
|
795
|
+
(funcall work-step)))
|
|
796
|
+
(catch _err
|
|
797
|
+
(close! res-ch)
|
|
798
|
+
(funcall work-step))))))))))
|
|
799
|
+
(funcall work-step))))
|
|
800
|
+
(setq wi (1+ wi))))
|
|
801
|
+
;; Ordered collector writing to `to`
|
|
802
|
+
((lambda ()
|
|
803
|
+
(let ((collect-outer nil)
|
|
804
|
+
(drain-res nil))
|
|
805
|
+
(setq drain-res
|
|
806
|
+
(lambda (res-ch)
|
|
807
|
+
(js-call (take! res-ch) :then
|
|
808
|
+
(lambda (item)
|
|
809
|
+
(if (nil? item)
|
|
810
|
+
(funcall collect-outer)
|
|
811
|
+
(js-call (put! to item) :then
|
|
812
|
+
(lambda (ok)
|
|
813
|
+
(if ok
|
|
814
|
+
(funcall drain-res res-ch)
|
|
815
|
+
(progn
|
|
816
|
+
(setq stopped t)
|
|
817
|
+
(close! jobs)
|
|
818
|
+
(close! results))))))))))
|
|
819
|
+
(setq collect-outer
|
|
820
|
+
(lambda ()
|
|
821
|
+
(js-call (take! results) :then
|
|
822
|
+
(lambda (res-ch)
|
|
823
|
+
(if (nil? res-ch)
|
|
824
|
+
(when should-close (close! to))
|
|
825
|
+
(funcall drain-res res-ch))))))
|
|
826
|
+
(funcall collect-outer))))
|
|
827
|
+
to))
|
|
828
|
+
|
|
829
|
+
(defun pipeline (n to xf from &optional close? ex-handler)
|
|
830
|
+
(unless (= (value-type xf) "function")
|
|
831
|
+
(throw (new globalThis/TypeError "pipeline expects a transducer function")))
|
|
832
|
+
(pipeline* "sync" n to xf from close? ex-handler))
|
|
833
|
+
|
|
834
|
+
(defun pipeline-async (n to af from &optional close?)
|
|
835
|
+
(unless (= (value-type af) "function")
|
|
836
|
+
(throw (new globalThis/TypeError "pipeline-async expects an async function")))
|
|
837
|
+
(pipeline* "async" n to af from close? nil))
|
|
838
|
+
|
|
839
|
+
(defun merge (chs &optional buf-or-n)
|
|
840
|
+
(let* ((arr (cond
|
|
841
|
+
((Array/isArray chs) chs)
|
|
842
|
+
((Collection/isSeqable chs)
|
|
843
|
+
(let ((tmp (js-array)))
|
|
844
|
+
(Collection/reduce chs (lambda (_a c) (js-call tmp :push c) nil) nil)
|
|
845
|
+
tmp))
|
|
846
|
+
(t (throw (new globalThis/TypeError "merge expects an array or collection of channels")))))
|
|
847
|
+
(out (chan buf-or-n))
|
|
848
|
+
(remaining (js-length arr)))
|
|
849
|
+
(if (= remaining 0)
|
|
850
|
+
(close! out)
|
|
851
|
+
(let ((i 0))
|
|
852
|
+
(while (< i (js-length arr))
|
|
853
|
+
(let ((src (require-chan (aref arr i) "merge")))
|
|
854
|
+
((lambda (ch-src)
|
|
855
|
+
(let ((pump nil))
|
|
856
|
+
(setq pump
|
|
857
|
+
(lambda ()
|
|
858
|
+
(js-call (take! ch-src) :then
|
|
859
|
+
(lambda (v)
|
|
860
|
+
(if (nil? v)
|
|
861
|
+
(progn
|
|
862
|
+
(setq remaining (1- remaining))
|
|
863
|
+
(when (= remaining 0)
|
|
864
|
+
(close! out)))
|
|
865
|
+
(js-call (put! out v) :then
|
|
866
|
+
(lambda (ok)
|
|
867
|
+
(when ok (funcall pump)))))))))
|
|
868
|
+
(funcall pump)))
|
|
869
|
+
src))
|
|
870
|
+
(setq i (1+ i)))))
|
|
871
|
+
out))
|
|
872
|
+
|
|
873
|
+
(defun split (pred ch &optional t-buf f-buf)
|
|
874
|
+
(unless (= (value-type pred) "function")
|
|
875
|
+
(throw (new globalThis/TypeError "split expects a predicate function")))
|
|
876
|
+
(require-chan ch "split")
|
|
877
|
+
(let ((t-ch (chan t-buf))
|
|
878
|
+
(f-ch (chan f-buf)))
|
|
879
|
+
((lambda ()
|
|
880
|
+
(let ((step nil))
|
|
881
|
+
(setq step
|
|
882
|
+
(lambda ()
|
|
883
|
+
(js-call (take! ch) :then
|
|
884
|
+
(lambda (v)
|
|
885
|
+
(if (nil? v)
|
|
886
|
+
(progn (close! t-ch) (close! f-ch))
|
|
887
|
+
(let ((target (if (truthy? (funcall pred v)) t-ch f-ch)))
|
|
888
|
+
(js-call (put! target v) :then
|
|
889
|
+
(lambda (_ok)
|
|
890
|
+
(unless (and (closed? t-ch) (closed? f-ch))
|
|
891
|
+
(funcall step))))))))))
|
|
892
|
+
(funcall step))))
|
|
893
|
+
(persistentVector t-ch f-ch)))
|
|
894
|
+
|
|
895
|
+
;; ============================================================================
|
|
896
|
+
;; Broadcast: mult / tap / untap
|
|
897
|
+
;; ============================================================================
|
|
898
|
+
|
|
899
|
+
(defconst ChannelMult
|
|
900
|
+
(js* "(class ChannelMult { constructor(ch) { this.ch = ch; this.taps = []; this.done = false; } })"))
|
|
901
|
+
|
|
902
|
+
(defun mult? (value)
|
|
903
|
+
(js* "value instanceof ChannelMult"))
|
|
904
|
+
|
|
905
|
+
(defun mult (ch)
|
|
906
|
+
(require-chan ch "mult")
|
|
907
|
+
(let ((m (new ChannelMult ch)))
|
|
908
|
+
((lambda ()
|
|
909
|
+
(let ((step nil))
|
|
910
|
+
(setq step
|
|
911
|
+
(lambda ()
|
|
912
|
+
(js-call (take! ch) :then
|
|
913
|
+
(lambda (v)
|
|
914
|
+
(if (nil? v)
|
|
915
|
+
(progn
|
|
916
|
+
(put m :done t)
|
|
917
|
+
(let ((taps (js-call (get m :taps) :slice))
|
|
918
|
+
(i 0))
|
|
919
|
+
(while (< i (js-length taps))
|
|
920
|
+
(let ((entry (aref taps i)))
|
|
921
|
+
(when (get entry :close)
|
|
922
|
+
(close! (get entry :ch))))
|
|
923
|
+
(setq i (1+ i)))))
|
|
924
|
+
(let ((taps (js-call (get m :taps) :slice)))
|
|
925
|
+
(if (= (js-length taps) 0)
|
|
926
|
+
(funcall step)
|
|
927
|
+
(let ((puts (js-array))
|
|
928
|
+
(i 0))
|
|
929
|
+
(while (< i (js-length taps))
|
|
930
|
+
(let ((entry (aref taps i)))
|
|
931
|
+
(js-call puts :push
|
|
932
|
+
(js-call (put! (get entry :ch) v) :then
|
|
933
|
+
(lambda (ok)
|
|
934
|
+
(unless ok
|
|
935
|
+
(untap m (get entry :ch)))))))
|
|
936
|
+
(setq i (1+ i)))
|
|
937
|
+
(js-call (Promise/all puts) :then (lambda () (funcall step)))))))))))
|
|
938
|
+
(funcall step))))
|
|
939
|
+
m))
|
|
940
|
+
|
|
941
|
+
(defun tap (m ch &optional close?)
|
|
942
|
+
(unless (mult? m)
|
|
943
|
+
(throw (new globalThis/TypeError "tap expects a mult")))
|
|
944
|
+
(require-chan ch "tap")
|
|
945
|
+
(let ((should-close (if (nullish? close?) t (truthy? close?))))
|
|
946
|
+
(if (get m :done)
|
|
947
|
+
(when should-close (close! ch))
|
|
948
|
+
(let ((taps (get m :taps))
|
|
949
|
+
(found false)
|
|
950
|
+
(i 0))
|
|
951
|
+
(while (< i (js-length taps))
|
|
952
|
+
(when (eq (get (aref taps i) :ch) ch)
|
|
953
|
+
(put (aref taps i) :close should-close)
|
|
954
|
+
(setq found t))
|
|
955
|
+
(setq i (1+ i)))
|
|
956
|
+
(unless found
|
|
957
|
+
(js-call taps :push (js-object :ch ch :close should-close)))))
|
|
958
|
+
ch))
|
|
959
|
+
|
|
960
|
+
(defun untap (m ch)
|
|
961
|
+
(unless (mult? m)
|
|
962
|
+
(throw (new globalThis/TypeError "untap expects a mult")))
|
|
963
|
+
(put m :taps
|
|
964
|
+
(js-call (get m :taps) :filter (lambda (entry) (not (eq (get entry :ch) ch)))))
|
|
965
|
+
nil)
|
|
966
|
+
|
|
967
|
+
(defun untap-all (m)
|
|
968
|
+
(unless (mult? m)
|
|
969
|
+
(throw (new globalThis/TypeError "untap-all expects a mult")))
|
|
970
|
+
(put m :taps (js-array))
|
|
971
|
+
nil)
|
|
972
|
+
|
|
973
|
+
;; ============================================================================
|
|
974
|
+
;; Topic Publish/Subscribe: pub / sub / unsub
|
|
975
|
+
;; ============================================================================
|
|
976
|
+
|
|
977
|
+
(defconst ChannelPub
|
|
978
|
+
(js* "(class ChannelPub { constructor(ch, topicFn, bufFn) { this.ch = ch; this.topicFn = topicFn; this.bufFn = bufFn; this.mults = EMPTY_MAP; this.done = false; } })"))
|
|
979
|
+
|
|
980
|
+
(defun pub? (value)
|
|
981
|
+
(js* "value instanceof ChannelPub"))
|
|
982
|
+
|
|
983
|
+
(defun ensure-topic-mult (p topic)
|
|
984
|
+
(let ((existing (Collection/get (get p :mults) topic nil)))
|
|
985
|
+
(if (not (nil? existing))
|
|
986
|
+
existing
|
|
987
|
+
(let* ((buf (if (nil? (get p :bufFn)) nil (funcall (get p :bufFn) topic)))
|
|
988
|
+
(tch (chan buf))
|
|
989
|
+
(m (mult tch)))
|
|
990
|
+
(put p :mults (Collection/assoc (get p :mults) topic (js-object :ch tch :mult m)))
|
|
991
|
+
(Collection/get (get p :mults) topic)))))
|
|
992
|
+
|
|
993
|
+
(defun pub (ch topic-fn &optional buf-fn)
|
|
994
|
+
(require-chan ch "pub")
|
|
995
|
+
(unless (= (value-type topic-fn) "function")
|
|
996
|
+
(throw (new globalThis/TypeError "pub expects a topic function")))
|
|
997
|
+
(let ((p (new ChannelPub ch topic-fn (if (nullish? buf-fn) nil buf-fn))))
|
|
998
|
+
((lambda ()
|
|
999
|
+
(let ((step nil))
|
|
1000
|
+
(setq step
|
|
1001
|
+
(lambda ()
|
|
1002
|
+
(js-call (take! ch) :then
|
|
1003
|
+
(lambda (v)
|
|
1004
|
+
(if (nil? v)
|
|
1005
|
+
(progn
|
|
1006
|
+
(put p :done t)
|
|
1007
|
+
(let ((topic-chs (js-array))
|
|
1008
|
+
(i 0))
|
|
1009
|
+
(Collection/reduceKV (get p :mults)
|
|
1010
|
+
(lambda (_a _k entry) (js-call topic-chs :push (get entry :ch)) nil)
|
|
1011
|
+
nil)
|
|
1012
|
+
(while (< i (js-length topic-chs))
|
|
1013
|
+
(close! (aref topic-chs i))
|
|
1014
|
+
(setq i (1+ i)))))
|
|
1015
|
+
(let* ((topic (funcall topic-fn v))
|
|
1016
|
+
(entry (Collection/get (get p :mults) topic nil)))
|
|
1017
|
+
(if (nil? entry)
|
|
1018
|
+
(funcall step)
|
|
1019
|
+
(js-call (put! (get entry :ch) v) :then (lambda (_ok) (funcall step))))))))))
|
|
1020
|
+
(funcall step))))
|
|
1021
|
+
p))
|
|
1022
|
+
|
|
1023
|
+
(defun sub (p topic ch &optional close?)
|
|
1024
|
+
(unless (pub? p)
|
|
1025
|
+
(throw (new globalThis/TypeError "sub expects a pub")))
|
|
1026
|
+
(require-chan ch "sub")
|
|
1027
|
+
(let ((should-close (if (nullish? close?) t (truthy? close?))))
|
|
1028
|
+
(if (get p :done)
|
|
1029
|
+
(when should-close (close! ch))
|
|
1030
|
+
(let ((entry (ensure-topic-mult p topic)))
|
|
1031
|
+
(tap (get entry :mult) ch should-close)))
|
|
1032
|
+
ch))
|
|
1033
|
+
|
|
1034
|
+
(defun unsub (p topic ch)
|
|
1035
|
+
(unless (pub? p)
|
|
1036
|
+
(throw (new globalThis/TypeError "unsub expects a pub")))
|
|
1037
|
+
(let ((entry (Collection/get (get p :mults) topic nil)))
|
|
1038
|
+
(unless (nil? entry)
|
|
1039
|
+
(untap (get entry :mult) ch)))
|
|
1040
|
+
nil)
|
|
1041
|
+
|
|
1042
|
+
(defun unsub-all (p &optional topic)
|
|
1043
|
+
(unless (pub? p)
|
|
1044
|
+
(throw (new globalThis/TypeError "unsub-all expects a pub")))
|
|
1045
|
+
(if (not (nullish? topic))
|
|
1046
|
+
(let ((entry (Collection/get (get p :mults) topic nil)))
|
|
1047
|
+
(unless (nil? entry)
|
|
1048
|
+
(untap-all (get entry :mult))))
|
|
1049
|
+
(let ((all-mults (js-array))
|
|
1050
|
+
(i 0))
|
|
1051
|
+
(Collection/reduceKV (get p :mults)
|
|
1052
|
+
(lambda (_a _k entry) (js-call all-mults :push (get entry :mult)) nil)
|
|
1053
|
+
nil)
|
|
1054
|
+
(while (< i (js-length all-mults))
|
|
1055
|
+
(untap-all (aref all-mults i))
|
|
1056
|
+
(setq i (1+ i)))))
|
|
1057
|
+
nil)
|
|
1058
|
+
|
|
1059
|
+
;; ============================================================================
|
|
1060
|
+
;; Dynamic Input Mixer: mix / admix / unmix / toggle / solo-mode
|
|
1061
|
+
;; ============================================================================
|
|
1062
|
+
|
|
1063
|
+
(defconst ChannelMix
|
|
1064
|
+
(js* "(class ChannelMix { constructor(out) { this.out = out; this.inputs = new globalThis.Map(); this.soloMode = 'mute'; this.wakeCh = null; this.running = true; } })"))
|
|
1065
|
+
|
|
1066
|
+
(defun mix? (value)
|
|
1067
|
+
(js* "value instanceof ChannelMix"))
|
|
1068
|
+
|
|
1069
|
+
(defun read-mix-state (opts)
|
|
1070
|
+
(let ((s (js-object :mute false :pause false :solo false)))
|
|
1071
|
+
(when (not (nullish? opts))
|
|
1072
|
+
(put s :mute (truthy? (read-opt opts "mute" nil false)))
|
|
1073
|
+
(put s :pause (truthy? (read-opt opts "pause" nil false)))
|
|
1074
|
+
(put s :solo (truthy? (read-opt opts "solo" nil false))))
|
|
1075
|
+
s))
|
|
1076
|
+
|
|
1077
|
+
(defun mix-wake! (m)
|
|
1078
|
+
(let ((old (get m :wakeCh)))
|
|
1079
|
+
(unless (nil? old)
|
|
1080
|
+
(close! old))
|
|
1081
|
+
(put m :wakeCh (chan))))
|
|
1082
|
+
|
|
1083
|
+
(defun mix-run-loop (m)
|
|
1084
|
+
(let ((step nil))
|
|
1085
|
+
(setq step
|
|
1086
|
+
(lambda ()
|
|
1087
|
+
(when (and (get m :running) (not (closed? (get m :out))))
|
|
1088
|
+
(let ((any-solo false)
|
|
1089
|
+
(solo-mode-val (get m :soloMode))
|
|
1090
|
+
(active-ports (js-array))
|
|
1091
|
+
(state-by-ch (new globalThis.Map))
|
|
1092
|
+
(entries (Array/from (js-call (get m :inputs) :entries)))
|
|
1093
|
+
(i 0))
|
|
1094
|
+
(while (< i (js-length entries))
|
|
1095
|
+
(when (get (aref (aref entries i) 1) :solo)
|
|
1096
|
+
(setq any-solo t))
|
|
1097
|
+
(setq i (1+ i)))
|
|
1098
|
+
(setq i 0)
|
|
1099
|
+
(while (< i (js-length entries))
|
|
1100
|
+
(let* ((pair (aref entries i))
|
|
1101
|
+
(ch (aref pair 0))
|
|
1102
|
+
(st (aref pair 1))
|
|
1103
|
+
(solo? (get st :solo))
|
|
1104
|
+
(paused? (get st :pause))
|
|
1105
|
+
(effectively-paused (or paused? (and any-solo (not solo?) (= solo-mode-val "pause"))))
|
|
1106
|
+
(effectively-muted (or (get st :mute) (and any-solo (not solo?) (= solo-mode-val "mute")))))
|
|
1107
|
+
(unless effectively-paused
|
|
1108
|
+
(js-call state-by-ch :set ch (js-object :muted effectively-muted))
|
|
1109
|
+
(js-call active-ports :push ch)))
|
|
1110
|
+
(setq i (1+ i)))
|
|
1111
|
+
(let ((wake (get m :wakeCh)))
|
|
1112
|
+
(js-call active-ports :push wake)
|
|
1113
|
+
(js-call (alts! active-ports) :then
|
|
1114
|
+
(lambda (pair)
|
|
1115
|
+
(let ((val (js-call pair :nth 0))
|
|
1116
|
+
(win-ch (js-call pair :nth 1)))
|
|
1117
|
+
(cond
|
|
1118
|
+
((eq win-ch wake) (funcall step))
|
|
1119
|
+
((nil? val)
|
|
1120
|
+
(js-call (get m :inputs) :delete win-ch)
|
|
1121
|
+
(funcall step))
|
|
1122
|
+
(t
|
|
1123
|
+
(let ((info (js-call state-by-ch :get win-ch)))
|
|
1124
|
+
(if (and info (get info :muted))
|
|
1125
|
+
(funcall step)
|
|
1126
|
+
(js-call (put! (get m :out) val) :then
|
|
1127
|
+
(lambda (ok)
|
|
1128
|
+
(when ok (funcall step))))))))))))))))
|
|
1129
|
+
(funcall step)))
|
|
1130
|
+
|
|
1131
|
+
(defun mix (out)
|
|
1132
|
+
(require-chan out "mix")
|
|
1133
|
+
(let ((m (new ChannelMix out)))
|
|
1134
|
+
(put m :wakeCh (chan))
|
|
1135
|
+
(mix-run-loop m)
|
|
1136
|
+
m))
|
|
1137
|
+
|
|
1138
|
+
(defun admix (m ch)
|
|
1139
|
+
(unless (mix? m) (throw (new globalThis/TypeError "admix expects a mix")))
|
|
1140
|
+
(require-chan ch "admix")
|
|
1141
|
+
(unless (js-call (get m :inputs) :has ch)
|
|
1142
|
+
(js-call (get m :inputs) :set ch (read-mix-state nil))
|
|
1143
|
+
(mix-wake! m))
|
|
1144
|
+
m)
|
|
1145
|
+
|
|
1146
|
+
(defun unmix (m ch)
|
|
1147
|
+
(unless (mix? m) (throw (new globalThis/TypeError "unmix expects a mix")))
|
|
1148
|
+
(when (js-call (get m :inputs) :delete ch)
|
|
1149
|
+
(mix-wake! m))
|
|
1150
|
+
m)
|
|
1151
|
+
|
|
1152
|
+
(defun unmix-all (m)
|
|
1153
|
+
(unless (mix? m) (throw (new globalThis/TypeError "unmix-all expects a mix")))
|
|
1154
|
+
(js-call (get m :inputs) :clear)
|
|
1155
|
+
(mix-wake! m)
|
|
1156
|
+
m)
|
|
1157
|
+
|
|
1158
|
+
(defun solo-mode (m mode)
|
|
1159
|
+
(unless (mix? m) (throw (new globalThis/TypeError "solo-mode expects a mix")))
|
|
1160
|
+
(let ((name (cond
|
|
1161
|
+
((= mode "mute") "mute")
|
|
1162
|
+
((= mode "pause") "pause")
|
|
1163
|
+
((= (str mode) ":mute") "mute")
|
|
1164
|
+
((= (str mode) ":pause") "pause")
|
|
1165
|
+
(t (throw (new globalThis/RangeError "solo-mode must be :mute or :pause"))))))
|
|
1166
|
+
(put m :soloMode name)
|
|
1167
|
+
(mix-wake! m))
|
|
1168
|
+
m)
|
|
1169
|
+
|
|
1170
|
+
(defun toggle (m state-map)
|
|
1171
|
+
(unless (mix? m) (throw (new globalThis/TypeError "toggle expects a mix")))
|
|
1172
|
+
(let ((pairs (js-array))
|
|
1173
|
+
(i 0))
|
|
1174
|
+
(cond
|
|
1175
|
+
((Collection/isMap state-map)
|
|
1176
|
+
(Collection/reduceKV state-map
|
|
1177
|
+
(lambda (_a k v) (js-call pairs :push (js-array k v)) nil)
|
|
1178
|
+
nil))
|
|
1179
|
+
((host-map? state-map)
|
|
1180
|
+
(js-call state-map :forEach (lambda (v k) (js-call pairs :push (js-array k v)))))
|
|
1181
|
+
(t (throw (new globalThis/TypeError "toggle expects a map of channels to state maps"))))
|
|
1182
|
+
(while (< i (js-length pairs))
|
|
1183
|
+
(let* ((pair (aref pairs i))
|
|
1184
|
+
(ch (require-chan (aref pair 0) "toggle"))
|
|
1185
|
+
(next-st (read-mix-state (aref pair 1))))
|
|
1186
|
+
(js-call (get m :inputs) :set ch next-st))
|
|
1187
|
+
(setq i (1+ i))))
|
|
1188
|
+
(mix-wake! m)
|
|
1189
|
+
m)
|
|
1190
|
+
|
|
1191
|
+
(export
|
|
1192
|
+
AsyncChannel
|
|
1193
|
+
AsyncScope
|
|
1194
|
+
ChannelBuffer
|
|
1195
|
+
ChannelMix
|
|
1196
|
+
ChannelMult
|
|
1197
|
+
ChannelPub
|
|
1198
|
+
admix
|
|
1199
|
+
alts!
|
|
1200
|
+
buffer
|
|
1201
|
+
buffer-count
|
|
1202
|
+
buffer-full?
|
|
1203
|
+
buffer?
|
|
1204
|
+
chan
|
|
1205
|
+
chan?
|
|
1206
|
+
close!
|
|
1207
|
+
closed?
|
|
1208
|
+
current-async-bindings
|
|
1209
|
+
dropping-buffer
|
|
1210
|
+
merge
|
|
1211
|
+
mix
|
|
1212
|
+
mix?
|
|
1213
|
+
mult
|
|
1214
|
+
mult?
|
|
1215
|
+
offer!
|
|
1216
|
+
onto-chan!
|
|
1217
|
+
pipe
|
|
1218
|
+
pipeline
|
|
1219
|
+
pipeline-async
|
|
1220
|
+
poll!
|
|
1221
|
+
pub
|
|
1222
|
+
pub?
|
|
1223
|
+
put!
|
|
1224
|
+
scope-cancel!
|
|
1225
|
+
scope-cancelled?
|
|
1226
|
+
scope-channel
|
|
1227
|
+
scope-signal
|
|
1228
|
+
scope-spawn
|
|
1229
|
+
sliding-buffer
|
|
1230
|
+
solo-mode
|
|
1231
|
+
split
|
|
1232
|
+
sub
|
|
1233
|
+
take!
|
|
1234
|
+
tap
|
|
1235
|
+
timeout
|
|
1236
|
+
to-chan!
|
|
1237
|
+
toggle
|
|
1238
|
+
unmix
|
|
1239
|
+
unmix-all
|
|
1240
|
+
unsub
|
|
1241
|
+
unsub-all
|
|
1242
|
+
untap
|
|
1243
|
+
untap-all
|
|
1244
|
+
with-async-bindings
|
|
1245
|
+
with-async-scope))
|