eliscript 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -8
- package/bootstrap/compiler/emitter.eli +161 -63
- package/bootstrap/host/check-cli.mjs +0 -1
- package/bootstrap/host/check-cli.mjs.map +1 -1
- package/bootstrap/host/compiler-session.eli +2 -1
- package/bootstrap/host/eliscript-cli.mjs +2 -9
- package/bootstrap/host/eliscript-cli.mjs.map +1 -1
- package/bootstrap/host/project-cli.mjs +1 -11
- package/bootstrap/host/project-cli.mjs.map +1 -1
- package/bootstrap/host/project.mjs +2 -8
- package/bootstrap/host/project.mjs.map +1 -1
- package/browser/README.md +1 -1
- package/compiler/README.md +2 -2
- package/compiler/eliscript-emitter.el +229 -85
- package/compiler/eliscript-ir-emitter.el +64 -34
- package/compiler/eliscript-project.el +20 -5
- package/dist/backends/async.mjs +1 -90
- package/dist/backends/async.mjs.map +1 -1
- package/dist/backends/binary.mjs +1 -91
- package/dist/backends/binary.mjs.map +1 -1
- package/dist/backends/capabilities.mjs +1 -90
- package/dist/backends/capabilities.mjs.map +1 -1
- package/dist/backends/elf.mjs +1 -90
- package/dist/backends/elf.mjs.map +1 -1
- package/dist/backends/managed.mjs +1 -94
- package/dist/backends/managed.mjs.map +1 -1
- package/dist/backends/memory.mjs +1 -90
- package/dist/backends/memory.mjs.map +1 -1
- package/dist/backends/native.mjs +1 -91
- package/dist/backends/native.mjs.map +1 -1
- package/dist/backends/numeric.mjs +1 -90
- package/dist/backends/numeric.mjs.map +1 -1
- package/dist/backends/optimize.mjs +1 -90
- package/dist/backends/optimize.mjs.map +1 -1
- package/dist/backends/regexp.mjs +1 -91
- package/dist/backends/regexp.mjs.map +1 -1
- package/dist/backends/sha256.mjs +2 -96
- package/dist/backends/sha256.mjs.map +1 -1
- package/dist/backends/wasm.mjs +1 -90
- package/dist/backends/wasm.mjs.map +1 -1
- package/dist/bootstrap/analyzer.mjs +1 -95
- package/dist/bootstrap/analyzer.mjs.map +1 -1
- package/dist/bootstrap/compiler.mjs +1 -105
- package/dist/bootstrap/compiler.mjs.map +1 -1
- package/dist/bootstrap/emitter.mjs +848 -807
- package/dist/bootstrap/emitter.mjs.map +1 -1
- package/dist/bootstrap/expander.mjs +1 -96
- package/dist/bootstrap/expander.mjs.map +1 -1
- package/dist/bootstrap/formatter.mjs +1 -104
- package/dist/bootstrap/formatter.mjs.map +1 -1
- package/dist/bootstrap/ir.mjs +18 -172
- package/dist/bootstrap/ir.mjs.map +1 -1
- package/dist/bootstrap/lower.mjs +1 -92
- package/dist/bootstrap/lower.mjs.map +1 -1
- package/dist/bootstrap/project.mjs +23 -209
- package/dist/bootstrap/project.mjs.map +1 -1
- package/dist/bootstrap/reader.mjs +1 -96
- package/dist/bootstrap/reader.mjs.map +1 -1
- package/dist/bootstrap/source-map.mjs +7 -112
- package/dist/bootstrap/source-map.mjs.map +1 -1
- package/dist/bootstrap/symbol.mjs +7 -114
- package/dist/bootstrap/symbol.mjs.map +1 -1
- package/dist/bootstrap/syntax.mjs +10 -176
- package/dist/bootstrap/syntax.mjs.map +1 -1
- package/dist/bootstrap/transient-analysis.mjs +1 -93
- package/dist/bootstrap/transient-analysis.mjs.map +1 -1
- package/dist/browser/compiler.js +105 -104
- package/dist/browser/loader.js +1 -1
- package/dist/browser/loader.mjs +5 -124
- package/dist/compiler-artifact.json +1 -1
- package/dist/host/compiler-session.mjs +205 -297
- package/dist/host/compiler-session.mjs.map +1 -1
- package/dist/host/compiler-tools.mjs +1 -91
- package/dist/host/compiler-tools.mjs.map +1 -1
- package/dist/literal-sources/runtime/core/order.eli +32 -10
- package/dist/literal-sources/runtime/core/sequence.eli +69 -28
- package/dist/literal-sources/runtime/core/vector.eli +2 -4
- package/dist/literal-sources/stdlib/managed-literal-protocols.eli +5 -5
- package/dist/literal-sources/stdlib/managed-literals.eli +1 -1
- package/dist/package.json +1 -0
- package/dist/runtime/math.mjs +62 -252
- package/dist/runtime/math.mjs.map +1 -1
- package/dist/runtime/prelude.mjs +95 -0
- package/dist/runtime/prelude.mjs.macro-origins.json +1 -0
- package/dist/runtime/prelude.mjs.map +1 -0
- package/dist/runtime/unicode-case-data.mjs +1 -95
- package/dist/runtime/unicode-case-data.mjs.map +1 -1
- package/dist/runtime/unicode-case.mjs +3 -97
- package/dist/runtime/unicode-case.mjs.map +1 -1
- package/dist/stage3-release.json +1 -1
- package/editor/README.md +9 -4
- package/editor/eliscript-compat.el +164 -2
- package/editor/eliscript-mode.el +1 -1
- package/editor/eliscript-repl.el +1 -1
- package/package.json +2 -1
- package/runtime/README.md +21 -1
- package/runtime/core/protocol-impl.mjs +1 -115
- package/runtime/core/protocol-impl.mjs.map +1 -1
- package/runtime/core/text-impl.mjs +1 -90
- package/runtime/core/text-impl.mjs.map +1 -1
- package/runtime/core/vector.eli +1 -1
- package/runtime/core/vector.mjs +0 -9
- package/runtime/emacs-compat.eli +5957 -2
- package/runtime/emacs-compat.mjs +13522 -2295
- package/runtime/emacs-compat.mjs.map +1 -0
- package/runtime/math.mjs +62 -252
- package/runtime/math.mjs.map +1 -1
- package/runtime/prelude.eli +4 -0
- package/runtime/prelude.mjs +95 -0
- package/runtime/prelude.mjs.map +1 -0
- package/runtime/unicode-case-data.mjs +1 -95
- package/runtime/unicode-case-data.mjs.map +1 -1
- package/runtime/unicode-case.mjs +3 -97
- package/runtime/unicode-case.mjs.map +1 -1
- package/stdlib/managed-literals.eli +1 -1
- package/stdlib/managed-runtime-vector.eli +2 -4
- package/tools/backends/compiler-build.mjs +3 -93
- package/tools/backends/compiler-cli.mjs +2 -15
- package/tools/backends/compiler-corpus-bit.mjs +2 -95
- package/tools/backends/compiler-corpus-cli-adapters.mjs +1 -97
- package/tools/backends/compiler-corpus-debugging.mjs +2 -95
- package/tools/backends/compiler-corpus-emitter.mjs +1 -97
- package/tools/backends/compiler-corpus-execution.mjs +2 -108
- package/tools/backends/compiler-corpus-fuzz.mjs +2 -95
- package/tools/backends/compiler-corpus-host-context.mjs +2 -99
- package/tools/backends/compiler-corpus-identity-host.mjs +18 -123
- package/tools/backends/compiler-corpus-identity-value-adapters.mjs +0 -9
- package/tools/backends/compiler-corpus-literal-provider.mjs +1 -91
- package/tools/backends/compiler-corpus-managed-adapters.mjs +1 -99
- package/tools/backends/compiler-corpus-managed-fixtures.mjs +2 -113
- package/tools/backends/compiler-corpus-matchers.mjs +3 -106
- package/tools/backends/compiler-corpus-original-program.mjs +1 -91
- package/tools/backends/compiler-corpus-process.mjs +1 -92
- package/tools/backends/compiler-corpus-program-driver.mjs +1 -3
- package/tools/backends/compiler-corpus-project-adapters.mjs +1 -95
- package/tools/backends/compiler-corpus-project-entry-host.mjs +2 -92
- package/tools/backends/compiler-corpus-project-transport.mjs +1 -90
- package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1 -91
- package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1 -92
- package/tools/backends/compiler-corpus-raw-math-fixture.mjs +1 -91
- package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +1 -91
- package/tools/backends/compiler-corpus-reference.mjs +1 -90
- package/tools/backends/compiler-corpus-runtime-adapters.mjs +2 -110
- package/tools/backends/compiler-corpus-tools-cli.mjs +1 -95
- package/tools/backends/compiler-corpus-tools-reference.mjs +22 -163
- package/tools/backends/compiler-corpus.mjs +3 -125
- package/tools/backends/compiler-matrix.mjs +1 -92
- package/tools/backends/compiler-package-browser.mjs +1 -90
- package/tools/backends/compiler-package.mjs +3 -95
- package/tools/backends/compiler-unicode-profile-host.mjs +1 -91
- package/tools/backends/dist/bootstrap-js-stages.mjs +1 -91
- package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
- package/tools/backends/dist/managed-ir-inventory.mjs +1 -91
- package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
- package/tools/backends/dist/record-grid.mjs +1 -91
- package/tools/backends/dist/record-grid.mjs.map +1 -1
- package/tools/backends/dist/release-cli.mjs +4 -94
- package/tools/backends/dist/release-cli.mjs.map +1 -1
- package/tools/backends/dist/run-artifact.mjs +1 -91
- package/tools/backends/dist/run-artifact.mjs.map +1 -1
- package/tools/backends/eliscript/compiler-corpus-bit.eli +1 -0
- package/tools/backends/managed-foreign-binding.mjs +1 -95
- package/tools/backends/managed-host-lifecycle.mjs +1 -91
- package/tools/backends/managed-host-object-provider.mjs +18 -59
- package/tools/backends/managed-host-object-realm.mjs +1 -91
- package/tools/backends/managed-host-proxy.mjs +9 -18
- package/tools/backends/managed-host-runtime.mjs +3 -105
- package/tools/backends/managed-host.mjs +1 -104
- package/tools/backends/managed-module-graph.mjs +1 -97
- package/tools/backends/managed-runtime.mjs +1 -96
- package/tools/backends/managed-source-view.mjs +13 -108
- package/tools/backends/native-managed-runtime.mjs +1 -91
- package/tools/backends/native-memory.mjs +1 -91
- package/tools/backends/stage3-targets.mjs +1 -91
- package/tools/bootstrap-runtime.el +3 -2
- package/tools/project/project.eli +1 -1
- package/tools/testing/stdlib-bit-corpus-host.mjs +1 -90
- package/tools/testing/stdlib-bit.mjs +1 -90
- package/dist/bootstrap/package.json +0 -1
- package/dist/bootstrap/runtime/math.mjs +0 -1117
- package/dist/bootstrap/runtime/math.mjs.map +0 -1
- package/dist/bootstrap/runtime/unicode-case-data.mjs +0 -10125
- package/dist/bootstrap/runtime/unicode-case-data.mjs.map +0 -1
- package/dist/bootstrap/runtime/unicode-case.mjs +0 -281
- package/dist/bootstrap/runtime/unicode-case.mjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"protocol-impl.mjs","sources":["../../stdlib/core/protocol.eli"],"sourcesContent":["(module eliscript.core.protocol\n (import \"../../runtime/core/protocol-error.mjs\" ProtocolDispatchError)\n\n (defconst protocol-states (new globalThis/WeakMap))\n (defconst protocol-operation-owners (new globalThis/WeakMap))\n (defconst protocol-method-owners (new globalThis/WeakMap))\n (defconst protocol-missing\n (funcall globalThis/Symbol \"eliscript.protocol.missing\"))\n (defconst protocol-host-categories\n (js-array \"null\" \"undefined\" \"boolean\" \"number\" \"bigint\" \"string\"\n \"symbol\" \"function\" \"object\"))\n (defconst protocol-definition-format \"eliscript-protocol-definition\")\n (defconst protocol-definition-version 1)\n (defconst protocol-max-operations 1024)\n\n ;; These capabilities isolate host reflection from dispatch policy. They are\n ;; deliberately private and contain no protocol-specific decisions.\n (defconst protocol-symbol\n (lambda (description) (funcall globalThis/Symbol description)))\n (defconst protocol-array? (js* \"Array.isArray\"))\n (defconst protocol-map (lambda () (new globalThis/Map)))\n (defconst protocol-null-object\n (lambda () (js-call globalThis/Object :create nil)))\n (defconst protocol-own-descriptors\n (js* \"Object.getOwnPropertyDescriptors\"))\n (defconst protocol-own-keys (js* \"Reflect.ownKeys\"))\n (defconst protocol-has-own\n (lambda (value key)\n (js-call (aref (aref globalThis/Object \"prototype\") \"hasOwnProperty\")\n :call value key)))\n (defconst protocol-property-present\n (lambda (value key)\n (if (or (nil? value) (undefined? value))\n false\n (js-call globalThis/Reflect :has (funcall globalThis/Object value) key))))\n (defconst protocol-property-value\n (lambda (value key) (aref (funcall globalThis/Object value) key)))\n (defconst protocol-host-type (lambda (value) (js* \"typeof value\")))\n (defconst protocol-exact-prototype\n (lambda (value missing)\n (if (or (and (= (js* \"typeof value\") \"object\") (not (nil? value)))\n (= (js* \"typeof value\") \"function\"))\n (js-call globalThis/Object :getPrototypeOf value)\n missing)))\n (defconst protocol-object-prototype (js* \"Object.prototype\"))\n (defconst protocol-get-prototype (js* \"Object.getPrototypeOf\"))\n (defconst protocol-freeze (js* \"Object.freeze\"))\n (defconst protocol-define-enumerable\n (lambda (target key value)\n (js-call globalThis/Object :defineProperty target key\n (js-object :value value :enumerable t))\n target))\n (defconst protocol-define-metadata\n (lambda (target protocol operation slot)\n (js-call globalThis/Object :defineProperties target\n (js-object :protocol (js-object :value protocol)\n :operation (js-object :value operation)\n :slot (js-object :value slot)))\n target))\n\n (defun protocol-fail (message)\n (throw (new globalThis/TypeError message)))\n\n (defun protocol-state (protocol)\n (let ((state (js-call protocol-states :get protocol)))\n (if (undefined? state)\n (protocol-fail \"expected an Eliscript protocol\")\n state)))\n\n (defun normalize-name (value label)\n (if (or (not (= (value-type value) \"string\"))\n (= (js-length value) 0))\n (protocol-fail (str label \" must be a non-empty string\"))\n value))\n\n (defun normalize-protocol-operations (operations)\n (when (not (funcall protocol-array? operations))\n (protocol-fail \"protocol operations must be a non-empty dense array\"))\n (let ((length (js-length operations)))\n (when (= length 0)\n (protocol-fail \"protocol operations must be a non-empty dense array\"))\n (when (> length protocol-max-operations)\n (protocol-fail\n (str \"protocol operations exceed \" protocol-max-operations)))\n (let* ((descriptors (funcall protocol-own-descriptors operations))\n (keys (funcall protocol-own-keys descriptors))\n (normalized (js-array))\n (index 0))\n ;; A dense Array has one own descriptor per item plus \"length\". This\n ;; also rejects Symbol keys and named side-channel properties.\n (when (/= (js-length keys) (1+ length))\n (protocol-fail\n \"protocol operations must be a dense array with no extra properties\"))\n (while (< index length)\n (let ((descriptor (get descriptors (str index) undefined)))\n (when (or (undefined? descriptor)\n (not (funcall protocol-has-own descriptor \"value\"))\n (not (get descriptor :enumerable false)))\n (protocol-fail\n \"protocol operations must contain enumerable data values\"))\n (js-call normalized :push\n (normalize-name (get descriptor :value)\n \"protocol operation\")))\n (setq index (1+ index)))\n normalized)))\n\n (defun protocol-definition-field (descriptors name)\n (let ((descriptor (get descriptors name undefined)))\n (when (or (undefined? descriptor)\n (not (funcall protocol-has-own descriptor \"value\"))\n (not (get descriptor :enumerable false)))\n (protocol-fail\n (str \"protocol definition field \" name\n \" must be an enumerable data property\")))\n (get descriptor :value)))\n\n (defun normalize-protocol-definition (definition)\n (when (or (nil? definition)\n (not (= (value-type definition) \"object\"))\n (funcall protocol-array? definition))\n (protocol-fail \"protocol definition must be a plain object\"))\n (let ((prototype (funcall protocol-get-prototype definition)))\n (when (and (/= prototype protocol-object-prototype)\n (not (nil? prototype)))\n (protocol-fail \"protocol definition must be a plain object\")))\n (let* ((descriptors (funcall protocol-own-descriptors definition))\n (keys (funcall protocol-own-keys descriptors)))\n (when (/= (js-length keys) 4)\n (protocol-fail\n \"protocol definition must contain exactly format, version, name, and operations\"))\n (let ((format (protocol-definition-field descriptors \"format\"))\n (version (protocol-definition-field descriptors \"version\"))\n (name (protocol-definition-field descriptors \"name\"))\n (operations (protocol-definition-field descriptors \"operations\")))\n (when (/= format protocol-definition-format)\n (protocol-fail (str \"unsupported protocol definition format \" format)))\n (when (/= version protocol-definition-version)\n (protocol-fail (str \"unsupported protocol definition version \" version)))\n (js-object :name (normalize-name name \"protocol name\")\n :operations (normalize-protocol-operations operations)))))\n\n (defun operation-record (protocol operation)\n (let* ((state (protocol-state protocol))\n (record (js-call (get state :operations) :get operation)))\n (if (undefined? record)\n (protocol-fail\n (str \"protocol \" (get protocol :name) \" has no operation \" operation))\n record)))\n\n (defun protocol-host-category (value)\n (if (nil? value) \"null\" (funcall protocol-host-type value)))\n\n (defun known-host-category? (category)\n (let ((index 0)\n (found false))\n (while (and (< index (js-length protocol-host-categories)) (not found))\n (when (= category (js-nth index protocol-host-categories))\n (setq found t))\n (setq index (1+ index)))\n found))\n\n (defun normalize-implementations (protocol implementations)\n (when (or (nil? implementations)\n (not (= (value-type implementations) \"object\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" implementations must be an object\")))\n (let* ((descriptors (funcall protocol-own-descriptors implementations))\n (keys (funcall protocol-own-keys descriptors))\n (normalized (funcall protocol-null-object))\n (index 0))\n (when (= (js-length keys) 0)\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" extension must implement at least one operation\")))\n (while (< index (js-length keys))\n (let* ((key (js-nth index keys))\n (descriptor (get descriptors key)))\n (when (not (= (value-type key) \"string\"))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" implementation names must be strings\")))\n (operation-record protocol key)\n (when (or (not (funcall protocol-has-own descriptor \"value\"))\n (not (= (value-type (get descriptor :value)) \"function\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name) \"/\" key\n \" implementation must be a function\")))\n (funcall protocol-define-enumerable\n normalized key (get descriptor :value)))\n (setq index (1+ index)))\n (funcall protocol-freeze normalized)))\n\n (defun merge-implementations (current incoming)\n (let ((merged (funcall protocol-null-object))\n (copy nil))\n (setq copy\n (lambda (source)\n (unless (nullish? source)\n (let ((keys (object-keys source))\n (index 0))\n (while (< index (js-length keys))\n (let ((key (js-nth index keys)))\n (funcall protocol-define-enumerable\n merged key (get source key)))\n (setq index (1+ index)))))))\n (funcall copy current)\n (funcall copy incoming)\n (funcall protocol-freeze merged)))\n\n (defun direct-method (record receiver)\n (if (or (nil? receiver) (undefined? receiver))\n protocol-missing\n (let ((slot (get record :slot)))\n (if (not (funcall protocol-property-present receiver slot))\n protocol-missing\n (let ((method (funcall protocol-property-value receiver slot)))\n (if (= (value-type method) \"function\") method nil))))))\n\n (defun external-method (state record receiver)\n (let* ((prototype\n (funcall protocol-exact-prototype receiver protocol-missing))\n (exact-implementations\n (if (= prototype protocol-missing)\n undefined\n (js-call (get state :types) :get prototype)))\n (exact\n (if (undefined? exact-implementations)\n undefined\n (get exact-implementations (get record :name) undefined))))\n (if (not (undefined? exact))\n exact\n (let* ((category-implementations\n (js-call (get state :categories) :get\n (protocol-host-category receiver)))\n (category\n (if (undefined? category-implementations)\n undefined\n (get category-implementations\n (get record :name) undefined))))\n (if (not (undefined? category))\n category\n (get (get state :defaults)\n (get record :name) protocol-missing))))))\n\n (defun dispatch (protocol record receiver arguments)\n (let ((direct (direct-method record receiver)))\n (when (nil? direct)\n (throw\n (new ProtocolDispatchError\n protocol (get record :name) receiver \"invalid-direct-slot\")))\n (if (/= direct protocol-missing)\n (js-call direct :apply receiver arguments)\n (let ((external\n (external-method (protocol-state protocol) record receiver)))\n (if (= external protocol-missing)\n (throw\n (new ProtocolDispatchError protocol (get record :name) receiver))\n (apply external (js-cons receiver arguments)))))))\n\n (defun make-protocol-with-slots (name operations slots)\n (let ((protocol-name (normalize-name name \"protocol name\"))\n (protocol-operations (normalize-protocol-operations operations)))\n (let ((records (funcall protocol-map))\n (operation-table (funcall protocol-null-object))\n (protocol nil)\n (index 0))\n (while (< index (js-length protocol-operations))\n (let ((operation\n (js-nth index protocol-operations)))\n (when (js-call records :has operation)\n (protocol-fail\n (str \"protocol \" protocol-name\n \" declares duplicate operation \" operation)))\n (let* ((slot\n (if (nil? slots)\n (funcall protocol-symbol\n (str \"eliscript.protocol.\" protocol-name \"/\" operation))\n (get slots index)))\n (method\n (lambda (&rest invocation)\n (when (= (js-length invocation) 0)\n (protocol-fail\n (str \"protocol \" protocol-name \"/\" operation\n \" requires a dispatch value\")))\n (dispatch protocol\n (js-call records :get operation)\n (aref invocation 0)\n (js-call invocation :slice 1))))\n (record\n (js-object :name operation :slot slot :method method)))\n (js-call records :set operation record)\n (funcall protocol-define-enumerable\n operation-table operation method)))\n (setq index (1+ index)))\n\n (setq protocol\n (funcall protocol-freeze\n (js-object :name protocol-name\n :operations\n (funcall protocol-freeze operation-table))))\n (js-call protocol-operation-owners :set operation-table protocol)\n (setq index 0)\n (while (< index (js-length protocol-operations))\n (let* ((operation (js-nth index protocol-operations))\n (record (js-call records :get operation))\n (method (get record :method)))\n (funcall protocol-define-metadata\n method protocol operation (get record :slot))\n (js-call protocol-method-owners :set method\n (funcall protocol-freeze\n (js-object :protocol protocol :operation operation)))\n (funcall protocol-freeze method)\n (funcall protocol-freeze record))\n (setq index (1+ index)))\n\n (js-call protocol-states :set protocol\n (js-object :operations records\n :types (funcall protocol-map)\n :categories (funcall protocol-map)\n :defaults\n (funcall protocol-freeze\n (funcall protocol-null-object))))\n protocol)))\n\n (defun define-protocol (name operations)\n (make-protocol-with-slots name operations nil))\n\n (defun protocol-definition (protocol)\n (protocol-state protocol)\n (let ((operations\n (js-call (object-keys (get protocol :operations)) :slice)))\n (funcall protocol-freeze operations)\n (funcall protocol-freeze\n (js-object :format protocol-definition-format\n :version protocol-definition-version\n :name (get protocol :name)\n :operations operations))))\n\n (defun define-protocol-from-definition (definition)\n (let ((normalized (normalize-protocol-definition definition)))\n (define-protocol (get normalized :name)\n (get normalized :operations))))\n\n ;; Bulk materialization reuses already transported Symbols, so observing a\n ;; slot before its protocol does not create two identities for one slot.\n (defun materialize-protocol-from-definition (definition slots)\n (let* ((normalized (normalize-protocol-definition definition))\n (operations (get normalized :operations)))\n (unless (and (funcall protocol-array? slots) (= (js-length slots) (js-length operations)))\n (protocol-fail \"protocol slots must be a dense array matching its operations\"))\n (let* ((descriptors (funcall protocol-own-descriptors slots))\n (keys (funcall protocol-own-keys descriptors))\n (seen (new globalThis/Set)) (symbols (js-array)) (index 0))\n (unless (= (js-length keys) (1+ (js-length slots)))\n (protocol-fail \"protocol slots must be a dense array with no extra properties\"))\n (while (< index (js-length slots))\n (let* ((descriptor (get descriptors (str index) undefined))\n (value (if (undefined? descriptor) undefined (get descriptor :value))))\n (unless (and (not (undefined? descriptor))\n (funcall protocol-has-own descriptor \"value\")\n (get descriptor :enumerable false)\n (= (funcall protocol-host-type value) \"symbol\"))\n (protocol-fail \"protocol slots must contain enumerable Symbol data values\"))\n (when (js-call seen :has value) (protocol-fail \"protocol slots must be distinct Symbols\"))\n (js-call seen :add value) (js-call symbols :push value))\n (setq index (1+ index)))\n (make-protocol-with-slots (get normalized :name) operations symbols))))\n\n (defun protocol-method (protocol operation)\n (get (operation-record protocol operation) :method))\n\n (defun protocol-slot (protocol operation)\n (get (operation-record protocol operation) :slot))\n\n (defun extend-protocol-type (protocol constructor implementations)\n (let ((state (protocol-state protocol)))\n (when (not (= (value-type constructor) \"function\"))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" exact type must be a constructor with a prototype\")))\n (let ((prototype (get constructor :prototype nil)))\n (when (or (nil? prototype)\n (not (= (value-type prototype) \"object\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" exact type must be a constructor with a prototype\")))\n (let* ((normalized\n (normalize-implementations protocol implementations))\n (current (js-call (get state :types) :get prototype)))\n (js-call (get state :types) :set prototype\n (merge-implementations current normalized)))\n protocol)))\n\n (defun extend-protocol-category (protocol category implementations)\n (let ((state (protocol-state protocol)))\n (when (not (known-host-category? category))\n (protocol-fail (str \"unknown protocol host category \" category)))\n (let* ((normalized\n (normalize-implementations protocol implementations))\n (current (js-call (get state :categories) :get category)))\n (js-call (get state :categories) :set category\n (merge-implementations current normalized)))\n protocol))\n\n (defun extend-protocol-default (protocol implementations)\n (let* ((state (protocol-state protocol))\n (normalized (normalize-implementations protocol implementations)))\n (put state :defaults\n (merge-implementations (get state :defaults) normalized))\n protocol))\n\n (defun implements-protocol-operation? (protocol operation value)\n (let* ((record (operation-record protocol operation))\n (direct (direct-method record value)))\n (and (not (nil? direct))\n (or (/= direct protocol-missing)\n (/= (external-method (protocol-state protocol) record value)\n protocol-missing)))))\n\n (defun implements-protocol? (protocol value)\n (let* ((state (protocol-state protocol))\n (operations (object-keys (get protocol :operations)))\n (index 0)\n (implemented t))\n (while (and (< index (js-length operations)) implemented)\n (let* ((record\n (js-call (get state :operations) :get (js-nth index operations)))\n (direct (direct-method record value)))\n (when (or (nil? direct)\n (and (= direct protocol-missing)\n (= (external-method state record value)\n protocol-missing)))\n (setq implemented false)))\n (setq index (1+ index)))\n implemented))\n\n (defun protocol? (value) (js-call protocol-states :has value))\n\n (defun read-protocol-operations-owner (value)\n (js-call protocol-operation-owners :get value))\n\n (defun read-protocol-method-owner (value)\n (js-call protocol-method-owners :get value))\n\n (export protocol? define-protocol define-protocol-from-definition materialize-protocol-from-definition\n read-protocol-operations-owner read-protocol-method-owner\n extend-protocol-category extend-protocol-default\n extend-protocol-type implements-protocol-operation?\n implements-protocol? protocol-definition protocol-host-category\n protocol-method protocol-slot))\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AsB,CAAK,OAAM,aAAW;IAClD,OAAA;;;AAEJ,8CAAuC,UAAS,WAAU;EACxD;QAAO,SAAS,iBAAiB,UAAS;QACnC,SAAS,cAAc,QAAO;IACnC,OAAA,CAAK,EAAK,CAAM,qBACX,CAAI,kGAAI,QAAO,+BACX,kGAAK,gBAAiB,eAAe,WAAU,QAAO,QAClD;;;AAEjB,oCAA6B,UAAS;EACpC;QAAO,QAAQ,eAAe;QACvB,aAAY,aAAa,CAAK,UAAS;QACvC,QAAO;QACP,cAAa;IAClB,0BAAO,uEAAsC,qCAAjC,kCAAG,OAAM,EAAW;MAC9B;YAAO,SACC,CAAS,CAAK,OAAM,eAAa,OAAK,IAAc,mBAAN;YAC/C,SAAS,cAAc,QAAO;QACnC,IAAM,CAAI,CAAM,oBACN,CAAK,CAAG,WAAO,+BACV,CAAI,gBAAgB,OAAM,QAAO,WAC9B;UAChB,CAAM,CAAA,cAAY;;;MACtB,CAAM,CAAA,QAAM,CAAI;;IAClB,OAAA;;;AAEJ,yBAAkB;EAAO,OAAA,CAAS,4BAAgB,OAAK;;AAEvD,wCAAuC;EACrC,OAAA,CAAS,4BAA0B,OAAK;;AAE1C,oCAAmC;EACjC,OAAA,CAAS,4BAAuB,OAAK;;AAEvC,QAAQ,iBAAU,iBAAgB,iCAAgC,sCAC1D,gCAA+B,4BAC/B,0BAAyB,yBACzB,sBAAqB,sCACrB,4BAAqB,qBAAoB,wBACzC,iBAAgB"}
|
|
1
|
+
{"version":3,"file":"protocol-impl.mjs","sources":["../../stdlib/core/protocol.eli"],"sourcesContent":["(module eliscript.core.protocol\n (import \"../../runtime/core/protocol-error.mjs\" ProtocolDispatchError)\n\n (defconst protocol-states (new globalThis/WeakMap))\n (defconst protocol-operation-owners (new globalThis/WeakMap))\n (defconst protocol-method-owners (new globalThis/WeakMap))\n (defconst protocol-missing\n (funcall globalThis/Symbol \"eliscript.protocol.missing\"))\n (defconst protocol-host-categories\n (js-array \"null\" \"undefined\" \"boolean\" \"number\" \"bigint\" \"string\"\n \"symbol\" \"function\" \"object\"))\n (defconst protocol-definition-format \"eliscript-protocol-definition\")\n (defconst protocol-definition-version 1)\n (defconst protocol-max-operations 1024)\n\n ;; These capabilities isolate host reflection from dispatch policy. They are\n ;; deliberately private and contain no protocol-specific decisions.\n (defconst protocol-symbol\n (lambda (description) (funcall globalThis/Symbol description)))\n (defconst protocol-array? (js* \"Array.isArray\"))\n (defconst protocol-map (lambda () (new globalThis/Map)))\n (defconst protocol-null-object\n (lambda () (js-call globalThis/Object :create nil)))\n (defconst protocol-own-descriptors\n (js* \"Object.getOwnPropertyDescriptors\"))\n (defconst protocol-own-keys (js* \"Reflect.ownKeys\"))\n (defconst protocol-has-own\n (lambda (value key)\n (js-call (aref (aref globalThis/Object \"prototype\") \"hasOwnProperty\")\n :call value key)))\n (defconst protocol-property-present\n (lambda (value key)\n (if (or (nil? value) (undefined? value))\n false\n (js-call globalThis/Reflect :has (funcall globalThis/Object value) key))))\n (defconst protocol-property-value\n (lambda (value key) (aref (funcall globalThis/Object value) key)))\n (defconst protocol-host-type (lambda (value) (js* \"typeof value\")))\n (defconst protocol-exact-prototype\n (lambda (value missing)\n (if (or (and (= (js* \"typeof value\") \"object\") (not (nil? value)))\n (= (js* \"typeof value\") \"function\"))\n (js-call globalThis/Object :getPrototypeOf value)\n missing)))\n (defconst protocol-object-prototype (js* \"Object.prototype\"))\n (defconst protocol-get-prototype (js* \"Object.getPrototypeOf\"))\n (defconst protocol-freeze (js* \"Object.freeze\"))\n (defconst protocol-define-enumerable\n (lambda (target key value)\n (js-call globalThis/Object :defineProperty target key\n (js-object :value value :enumerable t))\n target))\n (defconst protocol-define-metadata\n (lambda (target protocol operation slot)\n (js-call globalThis/Object :defineProperties target\n (js-object :protocol (js-object :value protocol)\n :operation (js-object :value operation)\n :slot (js-object :value slot)))\n target))\n\n (defun protocol-fail (message)\n (throw (new globalThis/TypeError message)))\n\n (defun protocol-state (protocol)\n (let ((state (js-call protocol-states :get protocol)))\n (if (undefined? state)\n (protocol-fail \"expected an Eliscript protocol\")\n state)))\n\n (defun normalize-name (value label)\n (if (or (not (= (value-type value) \"string\"))\n (= (js-length value) 0))\n (protocol-fail (str label \" must be a non-empty string\"))\n value))\n\n (defun normalize-protocol-operations (operations)\n (when (not (funcall protocol-array? operations))\n (protocol-fail \"protocol operations must be a non-empty dense array\"))\n (let ((length (js-length operations)))\n (when (= length 0)\n (protocol-fail \"protocol operations must be a non-empty dense array\"))\n (when (> length protocol-max-operations)\n (protocol-fail\n (str \"protocol operations exceed \" protocol-max-operations)))\n (let* ((descriptors (funcall protocol-own-descriptors operations))\n (keys (funcall protocol-own-keys descriptors))\n (normalized (js-array))\n (index 0))\n ;; A dense Array has one own descriptor per item plus \"length\". This\n ;; also rejects Symbol keys and named side-channel properties.\n (when (/= (js-length keys) (1+ length))\n (protocol-fail\n \"protocol operations must be a dense array with no extra properties\"))\n (while (< index length)\n (let ((descriptor (get descriptors (str index) undefined)))\n (when (or (undefined? descriptor)\n (not (funcall protocol-has-own descriptor \"value\"))\n (not (get descriptor :enumerable false)))\n (protocol-fail\n \"protocol operations must contain enumerable data values\"))\n (js-call normalized :push\n (normalize-name (get descriptor :value)\n \"protocol operation\")))\n (setq index (1+ index)))\n normalized)))\n\n (defun protocol-definition-field (descriptors name)\n (let ((descriptor (get descriptors name undefined)))\n (when (or (undefined? descriptor)\n (not (funcall protocol-has-own descriptor \"value\"))\n (not (get descriptor :enumerable false)))\n (protocol-fail\n (str \"protocol definition field \" name\n \" must be an enumerable data property\")))\n (get descriptor :value)))\n\n (defun normalize-protocol-definition (definition)\n (when (or (nil? definition)\n (not (= (value-type definition) \"object\"))\n (funcall protocol-array? definition))\n (protocol-fail \"protocol definition must be a plain object\"))\n (let ((prototype (funcall protocol-get-prototype definition)))\n (when (and (/= prototype protocol-object-prototype)\n (not (nil? prototype)))\n (protocol-fail \"protocol definition must be a plain object\")))\n (let* ((descriptors (funcall protocol-own-descriptors definition))\n (keys (funcall protocol-own-keys descriptors)))\n (when (/= (js-length keys) 4)\n (protocol-fail\n \"protocol definition must contain exactly format, version, name, and operations\"))\n (let ((format (protocol-definition-field descriptors \"format\"))\n (version (protocol-definition-field descriptors \"version\"))\n (name (protocol-definition-field descriptors \"name\"))\n (operations (protocol-definition-field descriptors \"operations\")))\n (when (/= format protocol-definition-format)\n (protocol-fail (str \"unsupported protocol definition format \" format)))\n (when (/= version protocol-definition-version)\n (protocol-fail (str \"unsupported protocol definition version \" version)))\n (js-object :name (normalize-name name \"protocol name\")\n :operations (normalize-protocol-operations operations)))))\n\n (defun operation-record (protocol operation)\n (let* ((state (protocol-state protocol))\n (record (js-call (get state :operations) :get operation)))\n (if (undefined? record)\n (protocol-fail\n (str \"protocol \" (get protocol :name) \" has no operation \" operation))\n record)))\n\n (defun protocol-host-category (value)\n (if (nil? value) \"null\" (funcall protocol-host-type value)))\n\n (defun known-host-category? (category)\n (let ((index 0)\n (found false))\n (while (and (< index (js-length protocol-host-categories)) (not found))\n (when (= category (js-nth index protocol-host-categories))\n (setq found t))\n (setq index (1+ index)))\n found))\n\n (defun normalize-implementations (protocol implementations)\n (when (or (nil? implementations)\n (not (= (value-type implementations) \"object\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" implementations must be an object\")))\n (let* ((descriptors (funcall protocol-own-descriptors implementations))\n (keys (funcall protocol-own-keys descriptors))\n (normalized (funcall protocol-null-object))\n (index 0))\n (when (= (js-length keys) 0)\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" extension must implement at least one operation\")))\n (while (< index (js-length keys))\n (let* ((key (js-nth index keys))\n (descriptor (get descriptors key)))\n (when (not (= (value-type key) \"string\"))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" implementation names must be strings\")))\n (operation-record protocol key)\n (when (or (not (funcall protocol-has-own descriptor \"value\"))\n (not (= (value-type (get descriptor :value)) \"function\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name) \"/\" key\n \" implementation must be a function\")))\n (funcall protocol-define-enumerable\n normalized key (get descriptor :value)))\n (setq index (1+ index)))\n (funcall protocol-freeze normalized)))\n\n (defun merge-implementations (current incoming)\n (let ((merged (funcall protocol-null-object))\n (copy nil))\n (setq copy\n (lambda (source)\n (unless (nullish? source)\n (let ((keys (object-keys source))\n (index 0))\n (while (< index (js-length keys))\n (let ((key (js-nth index keys)))\n (funcall protocol-define-enumerable\n merged key (get source key)))\n (setq index (1+ index)))))))\n (funcall copy current)\n (funcall copy incoming)\n (funcall protocol-freeze merged)))\n\n (defun direct-method (record receiver)\n (if (or (nil? receiver) (undefined? receiver))\n protocol-missing\n (let ((slot (get record :slot)))\n (if (not (funcall protocol-property-present receiver slot))\n protocol-missing\n (let ((method (funcall protocol-property-value receiver slot)))\n (if (= (value-type method) \"function\") method nil))))))\n\n (defun external-method (state record receiver)\n (let* ((prototype\n (funcall protocol-exact-prototype receiver protocol-missing))\n (exact-implementations\n (if (= prototype protocol-missing)\n undefined\n (js-call (get state :types) :get prototype)))\n (exact\n (if (undefined? exact-implementations)\n undefined\n (get exact-implementations (get record :name) undefined))))\n (if (not (undefined? exact))\n exact\n (let* ((category-implementations\n (js-call (get state :categories) :get\n (protocol-host-category receiver)))\n (category\n (if (undefined? category-implementations)\n undefined\n (get category-implementations\n (get record :name) undefined))))\n (if (not (undefined? category))\n category\n (get (get state :defaults)\n (get record :name) protocol-missing))))))\n\n (defun dispatch (protocol record receiver arguments)\n (let ((direct (direct-method record receiver)))\n (when (nil? direct)\n (throw\n (new ProtocolDispatchError\n protocol (get record :name) receiver \"invalid-direct-slot\")))\n (if (/= direct protocol-missing)\n (js-call direct :apply receiver arguments)\n (let ((external\n (external-method (protocol-state protocol) record receiver)))\n (if (= external protocol-missing)\n (throw\n (new ProtocolDispatchError protocol (get record :name) receiver))\n (apply external (js-cons receiver arguments)))))))\n\n (defun make-protocol-with-slots (name operations slots)\n (let ((protocol-name (normalize-name name \"protocol name\"))\n (protocol-operations (normalize-protocol-operations operations)))\n (let ((records (funcall protocol-map))\n (operation-table (funcall protocol-null-object))\n (protocol nil)\n (index 0))\n (while (< index (js-length protocol-operations))\n (let ((operation\n (js-nth index protocol-operations)))\n (when (js-call records :has operation)\n (protocol-fail\n (str \"protocol \" protocol-name\n \" declares duplicate operation \" operation)))\n (let* ((slot\n (if (nil? slots)\n (funcall protocol-symbol\n (str \"eliscript.protocol.\" protocol-name \"/\" operation))\n (get slots index)))\n (method\n (lambda (&rest invocation)\n (when (= (js-length invocation) 0)\n (protocol-fail\n (str \"protocol \" protocol-name \"/\" operation\n \" requires a dispatch value\")))\n (dispatch protocol\n (js-call records :get operation)\n (aref invocation 0)\n (js-call invocation :slice 1))))\n (record\n (js-object :name operation :slot slot :method method)))\n (js-call records :set operation record)\n (funcall protocol-define-enumerable\n operation-table operation method)))\n (setq index (1+ index)))\n\n (setq protocol\n (funcall protocol-freeze\n (js-object :name protocol-name\n :operations\n (funcall protocol-freeze operation-table))))\n (js-call protocol-operation-owners :set operation-table protocol)\n (setq index 0)\n (while (< index (js-length protocol-operations))\n (let* ((operation (js-nth index protocol-operations))\n (record (js-call records :get operation))\n (method (get record :method)))\n (funcall protocol-define-metadata\n method protocol operation (get record :slot))\n (js-call protocol-method-owners :set method\n (funcall protocol-freeze\n (js-object :protocol protocol :operation operation)))\n (funcall protocol-freeze method)\n (funcall protocol-freeze record))\n (setq index (1+ index)))\n\n (js-call protocol-states :set protocol\n (js-object :operations records\n :types (funcall protocol-map)\n :categories (funcall protocol-map)\n :defaults\n (funcall protocol-freeze\n (funcall protocol-null-object))))\n protocol)))\n\n (defun define-protocol (name operations)\n (make-protocol-with-slots name operations nil))\n\n (defun protocol-definition (protocol)\n (protocol-state protocol)\n (let ((operations\n (js-call (object-keys (get protocol :operations)) :slice)))\n (funcall protocol-freeze operations)\n (funcall protocol-freeze\n (js-object :format protocol-definition-format\n :version protocol-definition-version\n :name (get protocol :name)\n :operations operations))))\n\n (defun define-protocol-from-definition (definition)\n (let ((normalized (normalize-protocol-definition definition)))\n (define-protocol (get normalized :name)\n (get normalized :operations))))\n\n ;; Bulk materialization reuses already transported Symbols, so observing a\n ;; slot before its protocol does not create two identities for one slot.\n (defun materialize-protocol-from-definition (definition slots)\n (let* ((normalized (normalize-protocol-definition definition))\n (operations (get normalized :operations)))\n (unless (and (funcall protocol-array? slots) (= (js-length slots) (js-length operations)))\n (protocol-fail \"protocol slots must be a dense array matching its operations\"))\n (let* ((descriptors (funcall protocol-own-descriptors slots))\n (keys (funcall protocol-own-keys descriptors))\n (seen (new globalThis/Set)) (symbols (js-array)) (index 0))\n (unless (= (js-length keys) (1+ (js-length slots)))\n (protocol-fail \"protocol slots must be a dense array with no extra properties\"))\n (while (< index (js-length slots))\n (let* ((descriptor (get descriptors (str index) undefined))\n (value (if (undefined? descriptor) undefined (get descriptor :value))))\n (unless (and (not (undefined? descriptor))\n (funcall protocol-has-own descriptor \"value\")\n (get descriptor :enumerable false)\n (= (funcall protocol-host-type value) \"symbol\"))\n (protocol-fail \"protocol slots must contain enumerable Symbol data values\"))\n (when (js-call seen :has value) (protocol-fail \"protocol slots must be distinct Symbols\"))\n (js-call seen :add value) (js-call symbols :push value))\n (setq index (1+ index)))\n (make-protocol-with-slots (get normalized :name) operations symbols))))\n\n (defun protocol-method (protocol operation)\n (get (operation-record protocol operation) :method))\n\n (defun protocol-slot (protocol operation)\n (get (operation-record protocol operation) :slot))\n\n (defun extend-protocol-type (protocol constructor implementations)\n (let ((state (protocol-state protocol)))\n (when (not (= (value-type constructor) \"function\"))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" exact type must be a constructor with a prototype\")))\n (let ((prototype (get constructor :prototype nil)))\n (when (or (nil? prototype)\n (not (= (value-type prototype) \"object\")))\n (protocol-fail\n (str \"protocol \" (get protocol :name)\n \" exact type must be a constructor with a prototype\")))\n (let* ((normalized\n (normalize-implementations protocol implementations))\n (current (js-call (get state :types) :get prototype)))\n (js-call (get state :types) :set prototype\n (merge-implementations current normalized)))\n protocol)))\n\n (defun extend-protocol-category (protocol category implementations)\n (let ((state (protocol-state protocol)))\n (when (not (known-host-category? category))\n (protocol-fail (str \"unknown protocol host category \" category)))\n (let* ((normalized\n (normalize-implementations protocol implementations))\n (current (js-call (get state :categories) :get category)))\n (js-call (get state :categories) :set category\n (merge-implementations current normalized)))\n protocol))\n\n (defun extend-protocol-default (protocol implementations)\n (let* ((state (protocol-state protocol))\n (normalized (normalize-implementations protocol implementations)))\n (put state :defaults\n (merge-implementations (get state :defaults) normalized))\n protocol))\n\n (defun implements-protocol-operation? (protocol operation value)\n (let* ((record (operation-record protocol operation))\n (direct (direct-method record value)))\n (and (not (nil? direct))\n (or (/= direct protocol-missing)\n (/= (external-method (protocol-state protocol) record value)\n protocol-missing)))))\n\n (defun implements-protocol? (protocol value)\n (let* ((state (protocol-state protocol))\n (operations (object-keys (get protocol :operations)))\n (index 0)\n (implemented t))\n (while (and (< index (js-length operations)) implemented)\n (let* ((record\n (js-call (get state :operations) :get (js-nth index operations)))\n (direct (direct-method record value)))\n (when (or (nil? direct)\n (and (= direct protocol-missing)\n (= (external-method state record value)\n protocol-missing)))\n (setq implemented false)))\n (setq index (1+ index)))\n implemented))\n\n (defun protocol? (value) (js-call protocol-states :has value))\n\n (defun read-protocol-operations-owner (value)\n (js-call protocol-operation-owners :get value))\n\n (defun read-protocol-method-owner (value)\n (js-call protocol-method-owners :get value))\n\n (export protocol? define-protocol define-protocol-from-definition materialize-protocol-from-definition\n read-protocol-operations-owner read-protocol-method-owner\n extend-protocol-category extend-protocol-default\n extend-protocol-type implements-protocol-operation?\n implements-protocol? protocol-definition protocol-host-category\n protocol-method protocol-slot))\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACE,QAAgD;AAEhD,wBAA0B,KAAK;;AAC/B,kCAAoC,KAAK;;AACzC,+BAAiC,KAAK;;AACtC,yBACW,kBAAkB;;AAC7B,iCACE,CAAU,QAAO,aAAY,WAAU,UAAS,U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AI,QAAO,+BACX,kGAAK,gBAAiB,eAAe,WAAU,QAAO,QAClD;;;AAEjB,oCAA6B,UAAS;EACpC;QAAO,QAAQ,eAAe;QACvB,aAAY,aAAa,CAAK,UAAS;QACvC,QAAO;QACP,cAAa;IAClB,0BAAO,uEAAsC,qCAAjC,kCAAG,OAAM,EAAW;MAC9B;YAAO,SACC,CAAS,CAAK,OAAM,eAAa,OAAK,IAAc,mBAAN;YAC/C,SAAS,cAAc,QAAO;QACnC,IAAM,CAAI,CAAM,oBACN,CAAK,CAAG,WAAO,+BACV,CAAI,gBAAgB,OAAM,QAAO,WAC9B;UAChB,CAAM,CAAA,cAAY;;;MACtB,CAAM,CAAA,QAAM,CAAI;;IAClB,OAAA;;;AAEJ,yBAAkB;EAAO,OAAA,CAAS,4BAAgB,OAAK;;AAEvD,wCAAuC;EACrC,OAAA,CAAS,4BAA0B,OAAK;;AAE1C,oCAAmC;EACjC,OAAA,CAAS,4BAAuB,OAAK;;AAEvC,QAAQ,iBAAU,iBAAgB,iCAAgC,sCAC1D,gCAA+B,4BAC/B,0BAAyB,yBACzB,sBAAqB,sCACrB,4BAAqB,qBAAoB,wBACzC,iBAAgB"}
|
|
@@ -1,96 +1,7 @@
|
|
|
1
1
|
// Generated by Eliscript. Do not edit.
|
|
2
2
|
import "eliscript/runtime/unicode-case.mjs";
|
|
3
|
+
import { __eliscript_to_number, __eliscript_numeric_decrement, __eliscript_numeric_relation, __eliscript_numeric_sub, __eliscript_numeric_mul, __eliscript_numeric_div, __eliscript_numeric_rem, __eliscript_numeric_and, __eliscript_numeric_or, __eliscript_numeric_xor, __eliscript_numeric_shl, __eliscript_numeric_shr, __eliscript_numeric_ushr, __eliscript_numeric_neg, __eliscript_numeric_int32, __eliscript_numeric_uint32, __eliscript_numeric_not } from "eliscript/runtime/prelude.mjs";
|
|
3
4
|
const __eliscript_truthy = (value) => value !== false && value != null;
|
|
4
|
-
const __eliscript_number_intrinsic = globalThis.Number;
|
|
5
|
-
const __eliscript_bigint_intrinsic = globalThis.BigInt;
|
|
6
|
-
const __eliscript_numeric_apply = globalThis.Reflect.apply;
|
|
7
|
-
const __eliscript_numeric_symbol = globalThis.Symbol.toPrimitive;
|
|
8
|
-
const __eliscript_numeric_error = globalThis.TypeError;
|
|
9
|
-
const __eliscript_numeric_trim = globalThis.String.prototype.trim;
|
|
10
|
-
const __eliscript_numeric_slice = globalThis.String.prototype.slice;
|
|
11
|
-
const __eliscript_numeric_exec = globalThis.RegExp.prototype.exec;
|
|
12
|
-
const __eliscript_numeric_radix = /^(?:0[xX][0-9a-fA-F]+|0[oO][0-7]+|0[bB][01]+)$/;
|
|
13
|
-
const __eliscript_numeric_object = (value) => value !== null && (typeof value === "object" || typeof value === "function" || (typeof value === "undefined" && value !== void 0));
|
|
14
|
-
const __eliscript_numeric_primitive = (value) => {
|
|
15
|
-
if (!__eliscript_numeric_object(value)) return value;
|
|
16
|
-
const exotic = value[__eliscript_numeric_symbol];
|
|
17
|
-
if (exotic !== void 0 && exotic !== null) {
|
|
18
|
-
const result = __eliscript_numeric_apply(exotic, value, ["number"]);
|
|
19
|
-
if (!__eliscript_numeric_object(result)) return result;
|
|
20
|
-
throw new __eliscript_numeric_error("Cannot convert object to primitive value");
|
|
21
|
-
}
|
|
22
|
-
for (let index = 0; index < 2; index++) {
|
|
23
|
-
const method = value[index === 0 ? "valueOf" : "toString"];
|
|
24
|
-
if (typeof method === "function" || (typeof method === "undefined" && method !== void 0)) {
|
|
25
|
-
const result = __eliscript_numeric_apply(method, value, []);
|
|
26
|
-
if (!__eliscript_numeric_object(result)) return result;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
throw new __eliscript_numeric_error("Cannot convert object to primitive value");
|
|
30
|
-
};
|
|
31
|
-
const __eliscript_to_number = (value) => {
|
|
32
|
-
const primitive = __eliscript_numeric_primitive(value);
|
|
33
|
-
if (typeof primitive === "string") {
|
|
34
|
-
const text = __eliscript_numeric_apply(__eliscript_numeric_trim, primitive, []);
|
|
35
|
-
if (__eliscript_numeric_apply(__eliscript_numeric_exec, __eliscript_numeric_radix, [text]) !== null) {
|
|
36
|
-
let first = 2;
|
|
37
|
-
while (first < text.length && text[first] === "0") first++;
|
|
38
|
-
if (first === text.length) return 0;
|
|
39
|
-
const prefix = text[1];
|
|
40
|
-
const limit = prefix === "x" || prefix === "X" ? 256 : prefix === "b" || prefix === "B" ? 1024 : 342;
|
|
41
|
-
if (text.length - first > limit) return 1 / 0;
|
|
42
|
-
const token = __eliscript_numeric_apply(__eliscript_numeric_slice, text, [0, 2]) + __eliscript_numeric_apply(__eliscript_numeric_slice, text, [first]);
|
|
43
|
-
return __eliscript_number_intrinsic(__eliscript_bigint_intrinsic(token));
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return __eliscript_number_intrinsic(primitive);
|
|
47
|
-
};
|
|
48
|
-
const __eliscript_numeric = (value) => {
|
|
49
|
-
if (typeof value === "number" || typeof value === "bigint") return value;
|
|
50
|
-
const primitive = __eliscript_numeric_primitive(value);
|
|
51
|
-
if (typeof primitive === "number" || typeof primitive === "bigint") return primitive;
|
|
52
|
-
return typeof primitive === "string" ? __eliscript_to_number(primitive) : +primitive;
|
|
53
|
-
};
|
|
54
|
-
const __eliscript_implicit_number = (value) => {
|
|
55
|
-
if (typeof value === "number") return value;
|
|
56
|
-
const primitive = __eliscript_numeric_primitive(value);
|
|
57
|
-
if (typeof primitive === "bigint") throw new __eliscript_numeric_error("Cannot convert a BigInt value to a number");
|
|
58
|
-
return typeof primitive === "string" ? __eliscript_to_number(primitive) : +primitive;
|
|
59
|
-
};
|
|
60
|
-
const __eliscript_numeric_decrement = (value) => __eliscript_implicit_number(value) - 1;
|
|
61
|
-
const __eliscript_numeric_sub = (a, b) => { if (typeof a === "number" && typeof b === "number") return a - b; return __eliscript_numeric(a) - __eliscript_numeric(b); };
|
|
62
|
-
const __eliscript_numeric_mul = (a, b) => { if (typeof a === "number" && typeof b === "number") return a * b; return __eliscript_numeric(a) * __eliscript_numeric(b); };
|
|
63
|
-
const __eliscript_numeric_div = (a, b) => { if (typeof a === "number" && typeof b === "number") return a / b; return __eliscript_numeric(a) / __eliscript_numeric(b); };
|
|
64
|
-
const __eliscript_numeric_rem = (a, b) => { if (typeof a === "number" && typeof b === "number") return a % b; return __eliscript_numeric(a) % __eliscript_numeric(b); };
|
|
65
|
-
const __eliscript_numeric_and = (a, b) => { if (typeof a === "number" && typeof b === "number") return a & b; return __eliscript_numeric(a) & __eliscript_numeric(b); };
|
|
66
|
-
const __eliscript_numeric_or = (a, b) => { if (typeof a === "number" && typeof b === "number") return a | b; return __eliscript_numeric(a) | __eliscript_numeric(b); };
|
|
67
|
-
const __eliscript_numeric_xor = (a, b) => { if (typeof a === "number" && typeof b === "number") return a ^ b; return __eliscript_numeric(a) ^ __eliscript_numeric(b); };
|
|
68
|
-
const __eliscript_numeric_shl = (a, b) => { if (typeof a === "number" && typeof b === "number") return a << b; return __eliscript_numeric(a) << __eliscript_numeric(b); };
|
|
69
|
-
const __eliscript_numeric_shr = (a, b) => { if (typeof a === "number" && typeof b === "number") return a >> b; return __eliscript_numeric(a) >> __eliscript_numeric(b); };
|
|
70
|
-
const __eliscript_numeric_ushr = (a, b) => { if (typeof a === "number" && typeof b === "number") return a >>> b; return __eliscript_numeric(a) >>> __eliscript_numeric(b); };
|
|
71
|
-
const __eliscript_numeric_neg = (a) => typeof a === "number" ? -a : -__eliscript_numeric(a);
|
|
72
|
-
const __eliscript_numeric_int32 = (a) => typeof a === "number" ? a | 0 : __eliscript_numeric(a) | 0;
|
|
73
|
-
const __eliscript_numeric_uint32 = (a) => typeof a === "number" ? a >>> 0 : __eliscript_numeric(a) >>> 0;
|
|
74
|
-
const __eliscript_numeric_not = (a) => typeof a === "number" ? ~a : ~__eliscript_numeric(a);
|
|
75
|
-
const __eliscript_numeric_relation = (operator, left, right) => {
|
|
76
|
-
if (typeof left === "number" && typeof right === "number") {
|
|
77
|
-
if (operator === "<") return left < right;
|
|
78
|
-
if (operator === "<=") return left <= right;
|
|
79
|
-
if (operator === ">") return left > right;
|
|
80
|
-
return left >= right;
|
|
81
|
-
}
|
|
82
|
-
let a = __eliscript_numeric_primitive(left);
|
|
83
|
-
let b = __eliscript_numeric_primitive(right);
|
|
84
|
-
const x = typeof a, y = typeof b;
|
|
85
|
-
if (!((x === "string" && y === "string") || (x === "bigint" && y === "string") || (x === "string" && y === "bigint"))) {
|
|
86
|
-
a = __eliscript_numeric(a);
|
|
87
|
-
b = __eliscript_numeric(b);
|
|
88
|
-
}
|
|
89
|
-
if (operator === "<") return a < b;
|
|
90
|
-
if (operator === "<=") return a <= b;
|
|
91
|
-
if (operator === ">") return a > b;
|
|
92
|
-
return a >= b;
|
|
93
|
-
};
|
|
94
5
|
|
|
95
6
|
import * as Collection from "../../runtime/core/collection.mjs";
|
|
96
7
|
function empty_QMARK_(text) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-impl.mjs","sources":["../../stdlib/core/text.eli"],"sourcesContent":["(module eliscript.core.text\n (import \"../../runtime/core/collection.mjs\" :as Collection)\n\n (defun empty? (text)\n (= (Collection/count text) 0))\n\n (defun slice (start end text)\n (let ((result \"\")\n (index (if (< start 0) 0 start))\n (limit\n (if (> end (Collection/count text))\n (Collection/count text)\n end)))\n (while (< index limit)\n (setq result (str result (Collection/nth text index))\n index (1+ index)))\n result))\n\n (defun starts-at? (prefix offset text)\n (let ((matched\n (and (>= offset 0)\n (<= (+ offset (Collection/count prefix))\n (Collection/count text))))\n (index 0))\n (while (and matched (< index (Collection/count prefix)))\n (unless (= (Collection/nth prefix index)\n (Collection/nth text (+ offset index)))\n (setq matched false))\n (setq index (1+ index)))\n matched))\n\n (defun starts-with? (prefix text)\n (starts-at? prefix 0 text))\n\n (defun ends-with? (suffix text)\n (starts-at? suffix\n (- (Collection/count text) (Collection/count suffix))\n text))\n\n (defun contains? (needle text)\n (let ((found false)\n (offset 0)\n (last-offset\n (- (Collection/count text) (Collection/count needle))))\n (while (and (not found) (<= offset last-offset))\n (when (starts-at? needle offset text)\n (setq found t))\n (setq offset (1+ offset)))\n found))\n\n (defun strip-prefix (prefix text)\n (if (starts-with? prefix text)\n (slice (Collection/count prefix) (Collection/count text) text)\n text))\n\n (defun strip-suffix (suffix text)\n (if (ends-with? suffix text)\n (slice 0 (- (Collection/count text) (Collection/count suffix)) text)\n text))\n\n (defun whitespace? (character)\n (or (= character \" \")\n (= character \"\\t\")\n (= character \"\\n\")\n (= character \"\\r\")))\n\n (defun trim (text)\n (let ((start 0)\n (end (1- (Collection/count text))))\n (while (and (<= start end)\n (whitespace? (Collection/nth text start)))\n (setq start (1+ start)))\n (while (and (>= end start)\n (whitespace? (Collection/nth text end)))\n (setq end (1- end)))\n (slice start (1+ end) text)))\n\n (defun blank? (text)\n (empty? (trim text)))\n\n (defun join (separator values)\n (get\n (Collection/reduce\n values\n (lambda (state value)\n (js-object\n :first false\n :text (str (get state :text)\n (if (get state :first) \"\" separator)\n value)))\n (js-object :first t :text \"\"))\n :text))\n\n (defun repeat (count text)\n (let ((result \"\")\n (index 0))\n (while (< index count)\n (setq result (str result text)\n index (1+ index)))\n result))\n\n (export blank? contains? empty? ends-with? join repeat slice starts-at?\n starts-with? strip-prefix strip-suffix trim whitespace?))\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"text-impl.mjs","sources":["../../stdlib/core/text.eli"],"sourcesContent":["(module eliscript.core.text\n (import \"../../runtime/core/collection.mjs\" :as Collection)\n\n (defun empty? (text)\n (= (Collection/count text) 0))\n\n (defun slice (start end text)\n (let ((result \"\")\n (index (if (< start 0) 0 start))\n (limit\n (if (> end (Collection/count text))\n (Collection/count text)\n end)))\n (while (< index limit)\n (setq result (str result (Collection/nth text index))\n index (1+ index)))\n result))\n\n (defun starts-at? (prefix offset text)\n (let ((matched\n (and (>= offset 0)\n (<= (+ offset (Collection/count prefix))\n (Collection/count text))))\n (index 0))\n (while (and matched (< index (Collection/count prefix)))\n (unless (= (Collection/nth prefix index)\n (Collection/nth text (+ offset index)))\n (setq matched false))\n (setq index (1+ index)))\n matched))\n\n (defun starts-with? (prefix text)\n (starts-at? prefix 0 text))\n\n (defun ends-with? (suffix text)\n (starts-at? suffix\n (- (Collection/count text) (Collection/count suffix))\n text))\n\n (defun contains? (needle text)\n (let ((found false)\n (offset 0)\n (last-offset\n (- (Collection/count text) (Collection/count needle))))\n (while (and (not found) (<= offset last-offset))\n (when (starts-at? needle offset text)\n (setq found t))\n (setq offset (1+ offset)))\n found))\n\n (defun strip-prefix (prefix text)\n (if (starts-with? prefix text)\n (slice (Collection/count prefix) (Collection/count text) text)\n text))\n\n (defun strip-suffix (suffix text)\n (if (ends-with? suffix text)\n (slice 0 (- (Collection/count text) (Collection/count suffix)) text)\n text))\n\n (defun whitespace? (character)\n (or (= character \" \")\n (= character \"\\t\")\n (= character \"\\n\")\n (= character \"\\r\")))\n\n (defun trim (text)\n (let ((start 0)\n (end (1- (Collection/count text))))\n (while (and (<= start end)\n (whitespace? (Collection/nth text start)))\n (setq start (1+ start)))\n (while (and (>= end start)\n (whitespace? (Collection/nth text end)))\n (setq end (1- end)))\n (slice start (1+ end) text)))\n\n (defun blank? (text)\n (empty? (trim text)))\n\n (defun join (separator values)\n (get\n (Collection/reduce\n values\n (lambda (state value)\n (js-object\n :first false\n :text (str (get state :text)\n (if (get state :first) \"\" separator)\n value)))\n (js-object :first t :text \"\"))\n :text))\n\n (defun repeat (count text)\n (let ((result \"\")\n (index 0))\n (while (< index count)\n (setq result (str result text)\n index (1+ index)))\n result))\n\n (export blank? contains? empty? ends-with? join repeat slice starts-at?\n starts-with? strip-prefix strip-suffix trim whitespace?))\n"],"names":[],"mappings":";;;;;AACE,YAAgD;AAEhD,sBAAe;EACb,OAAA,CAAI,iBAAiB,UAAM;;AAE7B,eAAc,OAAM,KAAI;EACtB;gCAAc;gCACD,CAAI,kCAAG,OAAM,KAAG,IAAE;gCAExB,CAAI,kCAAG,KAAK,iBAAiB,SACxB,iBAAiB,QAC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ACJ,0BAAO,uEACM,kBAAa,eAAe,MAAK,iCADlC,mCAAI,OAAM;QAEpB,CAAM,CAAA,QAAM,CAAI;;MAClB,0BAAO,uEACM,kBAAa,eAAe,MAAK,+BADlC,mCAAI,KAAI;QAElB,CAAM,CAAA,MAAI,8BAAI;;MAChB,OAAC,MAAM,OAAM,CAAI,UAAK;;;;AAE1B,sBAAe;EACb,OAAC,aAAQ,KAAK;;AAEhB,cAAa,WAAU;EACrB,OAAA,CACG,kBACC,QACA,EAAS,OAAM;IACb,OAAA,EACE,SAAO,OACP,QAAM,QAAK,CAAK,OAAM,kBACX,oBAAI,CAAK,OAAM,YAAQ,KAAG,qBAC1B;MACf,EAAW,SAAO,MAAE,QAAM,OAC5B;;AAEJ,gBAAe,OAAM;EACnB;iCAAc;iCACD;;UADP;UACA;MACJ,OAAO,kCAAG,OAAM;QACd,CAAM,CAAA,SAAO,QAAK,iBAAO,SACnB,CAAA,QAAM,CAAI;;MAClB,OAAA;;;;AAEJ,QAAQ,cAAO,iBAAU,cAAO,kBAAW,MAAK,QAAO,OAAM,kBACrD,oBAAa,cAAa,cAAa,MAAK"}
|
package/runtime/core/vector.eli
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
(module capsules.runtime.core.vector
|
|
2
|
-
(js* "import {\n BRANCH_BITS,\n BRANCH_MASK,\n BRANCH_WIDTH,\n EMPTY_ROOT,\n EMPTY_TAIL,\n TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN,\n TRANSIENT_VECTOR_STATE,\n VECTOR_CONSTRUCTOR_TOKEN,\n VECTOR_STATE,\n VectorNode,\n allocateTail,\n editableVectorNode,\n recordInvalidTransientVectorCall,\n recordRootGrowth,\n recordTransientVectorNodeMutation,\n recordTransientVectorPersistent,\n recordTransientVectorTailCopy,\n recordTransientVectorTailMutation,\n visitNode,\n} from \"./vector-internals.mjs\";\nimport {\n VALUE_EQUAL,\n VALUE_HASH,\n orderedCollectionHash,\n} from \"./value-internals.mjs\";\nimport {\n COLLECTION_COUNT,\n COLLECTION_EMPTY,\n COLLECTION_CONJ,\n COLLECTION_GET,\n COLLECTION_ASSOC,\n COLLECTION_CONTAINS,\n COLLECTION_NTH,\n COLLECTION_REDUCE,\n COLLECTION_REDUCE_KV,\n COLLECTION_PEEK,\n COLLECTION_POP,\n COLLECTION_RSEQ,\n COLLECTION_SEQ,\n isReducedValue,\n reduceIterable,\n sequenceView,\n unreducedValue,\n} from \"./collection-internals.mjs\";\nimport {\n EDITABLE_TRANSIENT,\n TRANSIENT_ASSOC,\n TRANSIENT_CONJ,\n TRANSIENT_PERSISTENT,\n} from \"./transient-internals.mjs\";\nimport {\n METADATA_READ,\n METADATA_WITH,\n} from \"./metadata-internals.mjs\";\n\nconst MAX_COUNT = 0x7fffffff;\nconst MISSING = Symbol(\"eliscript.vector.missing\");\nconst VECTOR_HASH_TAG = 0x4f1b_2c3d;\nconst SUBVECTOR_STATE = new WeakMap();\n\nfunction assertIndex(index, upperBound, operation) {\n if (!Number.isInteger(index) || index < 0 || index >= upperBound) {\n throw new RangeError(\n `${operation} index ${String(index)} is outside [0, ${upperBound})`,\n );\n }\n}\n\nfunction tailOffset(count) {\n return count < BRANCH_WIDTH\n ? 0\n : ((count - 1) >>> BRANCH_BITS) << BRANCH_BITS;\n}\n\nfunction reverseVectorIterator(state) {\n let index = state.count - 1;\n let chunk = EMPTY_TAIL;\n let chunkStart = state.count;\n return {\n next() {\n if (index < 0) return { value: undefined, done: true };\n if (index < chunkStart) {\n chunk = arrayFor(state, index);\n chunkStart = index - (index & BRANCH_MASK);\n }\n const value = chunk[index - chunkStart];\n index -= 1;\n return { value, done: false };\n },\n };\n}\n\nfunction vectorRangeIterator(vector, start, end, reverse = false) {\n let index = reverse ? end - 1 : start;\n return {\n next() {\n if (reverse ? index < start : index >= end) {\n return { value: undefined, done: true };\n }\n const value = vector.nth(index);\n index += reverse ? -1 : 1;\n return { value, done: false };\n },\n };\n}\n\nfunction newPath(level, node) {\n if (level === 0) {\n return node;\n }\n return new VectorNode([newPath(level - BRANCH_BITS, node)]);\n}\n\nfunction pushTail(level, parent, tailNode, count) {\n visitNode(parent);\n const subindex = ((count - 1) >>> level) & BRANCH_MASK;\n const slots = parent.slots.slice();\n\n if (level === BRANCH_BITS) {\n slots[subindex] = tailNode;\n } else {\n const child = parent.slots[subindex];\n slots[subindex] = child === undefined\n ? newPath(level - BRANCH_BITS, tailNode)\n : pushTail(level - BRANCH_BITS, child, tailNode, count);\n }\n return new VectorNode(slots);\n}\n\nfunction assocNode(level, node, index, value) {\n visitNode(node);\n const slots = node.slots.slice();\n if (level === 0) {\n slots[index & BRANCH_MASK] = value;\n } else {\n const subindex = (index >>> level) & BRANCH_MASK;\n slots[subindex] = assocNode(\n level - BRANCH_BITS,\n node.slots[subindex],\n index,\n value,\n );\n }\n return new VectorNode(slots);\n}\n\nfunction transientNewPath(level, node, owner) {\n if (level === 0) {\n return node;\n }\n return new VectorNode(\n [transientNewPath(level - BRANCH_BITS, node, owner)],\n owner,\n );\n}\n\nfunction transientPushTail(level, parent, tailNode, count, owner) {\n visitNode(parent);\n const editable = editableVectorNode(parent, owner);\n const subindex = ((count - 1) >>> level) & BRANCH_MASK;\n if (level === BRANCH_BITS) {\n editable.slots[subindex] = tailNode;\n } else {\n const child = editable.slots[subindex];\n editable.slots[subindex] = child === undefined\n ? transientNewPath(level - BRANCH_BITS, tailNode, owner)\n : transientPushTail(level - BRANCH_BITS, child, tailNode, count, owner);\n }\n recordTransientVectorNodeMutation();\n return editable;\n}\n\nfunction transientAssocNode(level, node, index, value, owner) {\n visitNode(node);\n const editable = editableVectorNode(node, owner);\n if (level === 0) {\n editable.slots[index & BRANCH_MASK] = value;\n } else {\n const subindex = (index >>> level) & BRANCH_MASK;\n editable.slots[subindex] = transientAssocNode(\n level - BRANCH_BITS,\n editable.slots[subindex],\n index,\n value,\n owner,\n );\n }\n recordTransientVectorNodeMutation();\n return editable;\n}\n\nfunction popTail(level, node, count) {\n visitNode(node);\n const subindex = ((count - 2) >>> level) & BRANCH_MASK;\n if (level > BRANCH_BITS) {\n const child = popTail(\n level - BRANCH_BITS,\n node.slots[subindex],\n count,\n );\n if (child === undefined && subindex === 0) {\n return undefined;\n }\n const slots = node.slots.slice();\n slots[subindex] = child;\n return new VectorNode(slots);\n }\n if (subindex === 0) {\n return undefined;\n }\n const slots = node.slots.slice();\n slots[subindex] = undefined;\n return new VectorNode(slots);\n}\n\nfunction arrayFor(state, index) {\n if (index >= tailOffset(state.count)) {\n return state.tail;\n }\n\n let node = state.root;\n for (let level = state.shift; level > 0; level -= BRANCH_BITS) {\n visitNode(node);\n node = node.slots[(index >>> level) & BRANCH_MASK];\n }\n visitNode(node);\n return node.slots;\n}\n\nfunction makeVector(count, shift, root, tail, metadata = null) {\n return new PersistentVector(\n VECTOR_CONSTRUCTOR_TOKEN,\n count,\n shift,\n root,\n tail,\n metadata,\n );\n}\n\nfunction emptyVectorWithMetadata(metadata) {\n return metadata === null\n ? EMPTY_VECTOR\n : makeVector(0, BRANCH_BITS, EMPTY_ROOT, EMPTY_TAIL, metadata);\n}\n\nfunction makeTransientVector(vector) {\n return new TransientVector(TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN, vector);\n}\n\nfunction activeTransientVectorState(vector) {\n const state = vector[TRANSIENT_VECTOR_STATE];\n if (!state.active) {\n recordInvalidTransientVectorCall();\n throw new TypeError(\"transient vector is no longer editable\");\n }\n return state;\n}\n\nfunction editableTransientTail(state) {\n if (!state.tailOwned) {\n state.tail = state.tail.slice();\n state.tailOwned = true;\n recordTransientVectorTailCopy();\n }\n return state.tail;\n}\n\nclass TransientVector {\n constructor(token, vector) {\n if (token !== TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN ||\n !(vector instanceof PersistentVector)) {\n throw new TypeError(\"transient vectors must be created from a persistent vector\");\n }\n const source = vector[VECTOR_STATE];\n this[TRANSIENT_VECTOR_STATE] = {\n active: true,\n changed: false,\n owner: Object.freeze({}),\n source: vector,\n count: source.count,\n shift: source.shift,\n root: source.root,\n tail: source.tail,\n tailOwned: false,\n metadata: source.metadata,\n };\n Object.defineProperty(this, \"__eliscript_transient__\", {\n enumerable: true,\n get() {\n throw new TypeError(\"transient vectors cannot be serialized\");\n },\n });\n Object.freeze(this);\n }\n\n [TRANSIENT_CONJ](value) {\n const state = activeTransientVectorState(this);\n if (state.count === MAX_COUNT) {\n throw new RangeError(`persistent vector cannot exceed ${MAX_COUNT} values`);\n }\n if (state.tail.length < BRANCH_WIDTH) {\n editableTransientTail(state).push(value);\n recordTransientVectorTailMutation();\n } else {\n if (!state.tailOwned) {\n state.tail = state.tail.slice();\n recordTransientVectorTailCopy();\n }\n const tailNode = new VectorNode(state.tail, state.owner);\n if ((state.count >>> BRANCH_BITS) > (1 << state.shift)) {\n state.root = new VectorNode([\n state.root,\n transientNewPath(state.shift, tailNode, state.owner),\n ], state.owner);\n state.shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n state.root = transientPushTail(\n state.shift,\n state.root,\n tailNode,\n state.count,\n state.owner,\n );\n }\n state.tail = [value];\n state.tailOwned = true;\n recordTransientVectorTailMutation();\n }\n state.count += 1;\n state.changed = true;\n return this;\n }\n\n [TRANSIENT_ASSOC](index, value) {\n const state = activeTransientVectorState(this);\n if (index === state.count) {\n return this[TRANSIENT_CONJ](value);\n }\n assertIndex(index, state.count, \"assocBang\");\n if (index >= tailOffset(state.count)) {\n editableTransientTail(state)[index & BRANCH_MASK] = value;\n recordTransientVectorTailMutation();\n } else {\n state.root = transientAssocNode(\n state.shift,\n state.root,\n index,\n value,\n state.owner,\n );\n }\n state.changed = true;\n return this;\n }\n\n [TRANSIENT_PERSISTENT]() {\n const state = activeTransientVectorState(this);\n const result = state.changed\n ? makeVector(\n state.count,\n state.shift,\n state.root,\n state.tail,\n state.metadata,\n )\n : state.source;\n state.active = false;\n state.owner = null;\n recordTransientVectorPersistent();\n return result;\n }\n\n toJSON() {\n throw new TypeError(\"transient vectors cannot be serialized\");\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptTransientVector\";\n }\n}\n\nexport class PersistentVector {\n constructor(token, count, shift, root, tail, metadata = null) {\n if (token !== VECTOR_CONSTRUCTOR_TOKEN) {\n throw new TypeError(\n \"PersistentVector values must be created with persistentVector or PersistentVector.from\",\n );\n }\n this[VECTOR_STATE] = {\n count,\n shift,\n root,\n tail,\n metadata,\n };\n Object.freeze(this);\n }\n\n static empty() {\n return EMPTY_VECTOR;\n }\n\n static single(value) {\n return makeVector(\n 1,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail([value]),\n null,\n );\n }\n\n static pairWithMeta(first, second, metadata = null) {\n return makeVector(\n 2,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail([first, second]),\n metadata,\n );\n }\n\n static rawSlot(vector, index) {\n return vector[VECTOR_STATE].tail[index];\n }\n\n static from(iterable) {\n if (iterable instanceof PersistentVector) {\n return iterable;\n }\n if (\n Array.isArray(iterable) &&\n Object.getPrototypeOf(iterable) === Array.prototype &&\n !Object.hasOwn(iterable, Symbol.iterator) &&\n iterable[Symbol.iterator] === Array.prototype[Symbol.iterator]\n ) {\n const length = iterable.length;\n if (length === 0) return EMPTY_VECTOR;\n if (length <= BRANCH_WIDTH) {\n return makeVector(\n length,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail(iterable.slice()),\n );\n }\n let root = EMPTY_ROOT;\n let shift = BRANCH_BITS;\n let count = BRANCH_WIDTH;\n let tail = allocateTail(iterable.slice(0, BRANCH_WIDTH));\n while (count < length) {\n const nextEnd = Math.min(count + BRANCH_WIDTH, length);\n const tailNode = new VectorNode(tail);\n if ((count >>> BRANCH_BITS) > (1 << shift)) {\n root = new VectorNode([\n root,\n newPath(shift, tailNode),\n ]);\n shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n root = pushTail(shift, root, tailNode, count);\n }\n tail = allocateTail(iterable.slice(count, nextEnd));\n count = nextEnd;\n }\n return makeVector(length, shift, root, tail);\n }\n let result = EMPTY_VECTOR;\n for (const value of iterable) {\n result = result.conj(value);\n }\n return result;\n }\n\n get count() {\n return this[VECTOR_STATE].count;\n }\n\n get size() {\n return this[VECTOR_STATE].count;\n }\n\n nth(index, notFound = MISSING) {\n const state = this[VECTOR_STATE];\n if (((index | 0) === index || Number.isInteger(index)) && index >= 0 && index < state.count) {\n if (state.count <= BRANCH_WIDTH) {\n const value = state.tail[index];\n if (\n index === 1 &&\n state.count === 2 &&\n state.metadata !== null &&\n Array.isArray(value) &&\n value.length === 6 &&\n value[5] === true &&\n !Object.isFrozen(value)\n ) {\n Object.freeze(value);\n }\n return value;\n }\n return arrayFor(state, index)[index & BRANCH_MASK];\n }\n if (notFound !== MISSING) {\n return notFound;\n }\n assertIndex(index, state.count, \"nth\");\n }\n\n assoc(index, value) {\n const state = this[VECTOR_STATE];\n if (index === state.count) {\n return this.conj(value);\n }\n assertIndex(index, state.count, \"assoc\");\n\n if (index >= tailOffset(state.count)) {\n const tail = state.tail.slice();\n tail[index & BRANCH_MASK] = value;\n return makeVector(\n state.count,\n state.shift,\n state.root,\n allocateTail(tail),\n state.metadata,\n );\n }\n return makeVector(\n state.count,\n state.shift,\n assocNode(state.shift, state.root, index, value),\n state.tail,\n state.metadata,\n );\n }\n\n conj(value) {\n const state = this[VECTOR_STATE];\n if (state.count === MAX_COUNT) {\n throw new RangeError(`persistent vector cannot exceed ${MAX_COUNT} values`);\n }\n if (state.tail.length < BRANCH_WIDTH) {\n return makeVector(\n state.count + 1,\n state.shift,\n state.root,\n allocateTail([...state.tail, value]),\n state.metadata,\n );\n }\n\n const tailNode = new VectorNode(state.tail);\n let shift = state.shift;\n let root;\n if ((state.count >>> BRANCH_BITS) > (1 << state.shift)) {\n root = new VectorNode([\n state.root,\n newPath(state.shift, tailNode),\n ]);\n shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n root = pushTail(state.shift, state.root, tailNode, state.count);\n }\n return makeVector(\n state.count + 1,\n shift,\n root,\n allocateTail([value]),\n state.metadata,\n );\n }\n\n peek(notFound = null) {\n const state = this[VECTOR_STATE];\n return state.count === 0 ? notFound : state.tail[state.tail.length - 1];\n }\n\n pop() {\n const state = this[VECTOR_STATE];\n if (state.count === 0) {\n throw new RangeError(\"cannot pop an empty persistent vector\");\n }\n if (state.count === 1) {\n return emptyVectorWithMetadata(state.metadata);\n }\n if (state.tail.length > 1) {\n return makeVector(\n state.count - 1,\n state.shift,\n state.root,\n allocateTail(state.tail.slice(0, -1)),\n state.metadata,\n );\n }\n\n const tail = allocateTail([...arrayFor(state, state.count - 2)]);\n let root = popTail(state.shift, state.root, state.count) ?? EMPTY_ROOT;\n let shift = state.shift;\n if (shift > BRANCH_BITS && root.slots[1] === undefined) {\n root = root.slots[0];\n shift -= BRANCH_BITS;\n }\n return makeVector(\n state.count - 1,\n shift,\n root,\n tail,\n state.metadata,\n );\n }\n\n reduce(reducer, ...initial) {\n if (typeof reducer !== \"function\") {\n throw new TypeError(\"persistent vector reducer must be a function\");\n }\n const state = this[VECTOR_STATE];\n let accumulator;\n let index = 0;\n if (initial.length === 0) {\n if (state.count === 0) {\n throw new TypeError(\"cannot reduce an empty persistent vector without an initial value\");\n }\n accumulator = this.nth(0);\n index = 1;\n } else {\n accumulator = initial[0];\n }\n\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n accumulator = reducer(accumulator, chunk[offset], index);\n index += 1;\n }\n }\n return accumulator;\n }\n\n [COLLECTION_COUNT]() {\n return this.count;\n }\n\n [COLLECTION_EMPTY]() {\n return emptyVectorWithMetadata(this[VECTOR_STATE].metadata);\n }\n\n [COLLECTION_CONJ](value) {\n return this.conj(value);\n }\n\n [COLLECTION_PEEK]() {\n return this.peek();\n }\n\n [COLLECTION_POP]() {\n return this.pop();\n }\n\n [COLLECTION_RSEQ]() {\n const state = this[VECTOR_STATE];\n return state.count === 0\n ? null\n : sequenceView(() => reverseVectorIterator(state), state.count);\n }\n\n [COLLECTION_GET](index, notFound = null) {\n return this.nth(index, notFound);\n }\n\n [COLLECTION_ASSOC](index, value) {\n return this.assoc(index, value);\n }\n\n [COLLECTION_CONTAINS](index) {\n return Number.isInteger(index) && index >= 0 && index < this.count;\n }\n\n [COLLECTION_NTH](index, ...notFound) {\n return notFound.length === 0\n ? this.nth(index)\n : this.nth(index, notFound[0]);\n }\n\n [COLLECTION_SEQ]() {\n return this.count === 0\n ? null\n : sequenceView(() => this[Symbol.iterator](), this.count);\n }\n\n [COLLECTION_REDUCE](reducer, ...initial) {\n return reduceIterable(this, reducer, ...initial);\n }\n\n [COLLECTION_REDUCE_KV](reducer, initial) {\n const state = this[VECTOR_STATE];\n let result = initial;\n let index = 0;\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n result = reducer(result, index, chunk[offset]);\n index += 1;\n if (isReducedValue(result)) return unreducedValue(result);\n }\n }\n return result;\n }\n\n [EDITABLE_TRANSIENT]() {\n return makeTransientVector(this);\n }\n\n toArray() {\n return this.reduce((values, value) => {\n values.push(value);\n return values;\n }, []);\n }\n\n sliceVector(start, end) {\n const state = this[VECTOR_STATE];\n const length = end - start;\n if (length <= 0) return EMPTY_VECTOR;\n if (start === 0 && end >= state.count && state.metadata === null) {\n return this;\n }\n const values = new Array(length);\n let outIndex = 0;\n let index = start;\n while (index < end) {\n const chunk = arrayFor(state, index);\n const chunkOffset = index & BRANCH_MASK;\n const chunkBase = index - chunkOffset;\n const chunkEnd = Math.min(end - chunkBase, chunk.length);\n for (let i = chunkOffset; i < chunkEnd; i += 1) {\n values[outIndex] = chunk[i];\n outIndex += 1;\n }\n index = chunkBase + chunkEnd;\n }\n return PersistentVector.from(values);\n }\n\n [VALUE_EQUAL](other, equal) {\n if (this === other) {\n return true;\n }\n if (!(other instanceof PersistentVector) || other.count !== this.count) {\n return false;\n }\n const right = other[Symbol.iterator]();\n for (const value of this) {\n if (!equal(value, right.next().value)) {\n return false;\n }\n }\n return true;\n }\n\n [VALUE_HASH](hash) {\n return orderedCollectionHash(this, hash, VECTOR_HASH_TAG);\n }\n\n [METADATA_READ]() {\n return this[VECTOR_STATE].metadata;\n }\n\n [METADATA_WITH](metadata) {\n const state = this[VECTOR_STATE];\n return metadata === state.metadata\n ? this\n : makeVector(\n state.count,\n state.shift,\n state.root,\n state.tail,\n metadata,\n );\n }\n\n *[Symbol.iterator]() {\n const state = this[VECTOR_STATE];\n let index = 0;\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n yield chunk[offset];\n index += 1;\n }\n }\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptPersistentVector\";\n }\n}\n\nclass PersistentSubVector extends PersistentVector {\n constructor(vector, start, end, metadata = null) {\n const source = vector[VECTOR_STATE];\n super(\n VECTOR_CONSTRUCTOR_TOKEN,\n source.count,\n source.shift,\n source.root,\n source.tail,\n metadata,\n );\n SUBVECTOR_STATE.set(this, Object.freeze({ vector, start, end, metadata }));\n }\n\n get count() {\n const state = SUBVECTOR_STATE.get(this);\n return state.end - state.start;\n }\n\n get size() {\n return this.count;\n }\n\n sliceVector(start, end) {\n const state = SUBVECTOR_STATE.get(this);\n return state.vector.sliceVector(state.start + start, state.start + end);\n }\n\n nth(index, notFound = MISSING) {\n const state = SUBVECTOR_STATE.get(this);\n const count = state.end - state.start;\n if (!Number.isInteger(index) || index < 0 || index >= count) {\n if (notFound !== MISSING) return notFound;\n assertIndex(index, count, \"subvec nth\");\n }\n return state.vector.nth(state.start + index);\n }\n\n assoc(index, value) {\n const state = SUBVECTOR_STATE.get(this);\n const count = state.end - state.start;\n if (!Number.isInteger(index) || index < 0 || index > count) {\n throw new RangeError(\n `subvec assoc index ${String(index)} is outside [0, ${count}]`,\n );\n }\n const absoluteIndex = state.start + index;\n const vector = absoluteIndex === state.vector.count\n ? state.vector.conj(value)\n : state.vector.assoc(absoluteIndex, value);\n return new PersistentSubVector(\n vector,\n state.start,\n Math.max(state.end, absoluteIndex + 1),\n state.metadata,\n );\n }\n\n conj(value) {\n return this.assoc(this.count, value);\n }\n\n peek(notFound = null) {\n const state = SUBVECTOR_STATE.get(this);\n return state.start === state.end\n ? notFound\n : state.vector.nth(state.end - 1);\n }\n\n pop() {\n const state = SUBVECTOR_STATE.get(this);\n if (state.start === state.end) {\n throw new RangeError(\"cannot pop an empty persistent subvector\");\n }\n return state.end - state.start === 1\n ? emptyVectorWithMetadata(state.metadata)\n : new PersistentSubVector(\n state.vector,\n state.start,\n state.end - 1,\n state.metadata,\n );\n }\n\n reduce(reducer, ...initial) {\n if (typeof reducer !== \"function\") {\n throw new TypeError(\"persistent subvector reducer must be a function\");\n }\n const state = SUBVECTOR_STATE.get(this);\n let index = 0;\n let result;\n if (initial.length === 0) {\n if (state.start === state.end) {\n throw new TypeError(\n \"cannot reduce an empty persistent subvector without an initial value\",\n );\n }\n result = state.vector.nth(state.start);\n index = 1;\n } else {\n result = initial[0];\n }\n while (index < state.end - state.start) {\n result = reducer(result, state.vector.nth(state.start + index), index);\n index += 1;\n }\n return result;\n }\n\n [COLLECTION_COUNT]() {\n return this.count;\n }\n\n [COLLECTION_EMPTY]() {\n return emptyVectorWithMetadata(SUBVECTOR_STATE.get(this).metadata);\n }\n\n [COLLECTION_CONJ](value) {\n return this.conj(value);\n }\n\n [COLLECTION_PEEK]() {\n return this.peek();\n }\n\n [COLLECTION_POP]() {\n return this.pop();\n }\n\n [COLLECTION_RSEQ]() {\n const state = SUBVECTOR_STATE.get(this);\n return state.start === state.end\n ? null\n : sequenceView(\n () => vectorRangeIterator(\n state.vector,\n state.start,\n state.end,\n true,\n ),\n state.end - state.start,\n );\n }\n\n [COLLECTION_GET](index, notFound = null) {\n return this.nth(index, notFound);\n }\n\n [COLLECTION_ASSOC](index, value) {\n return this.assoc(index, value);\n }\n\n [COLLECTION_CONTAINS](index) {\n return Number.isInteger(index) && index >= 0 && index < this.count;\n }\n\n [COLLECTION_NTH](index, ...notFound) {\n return notFound.length === 0\n ? this.nth(index)\n : this.nth(index, notFound[0]);\n }\n\n [COLLECTION_SEQ]() {\n return this.count === 0\n ? null\n : sequenceView(() => this[Symbol.iterator](), this.count);\n }\n\n [COLLECTION_REDUCE](reducer, ...initial) {\n return reduceIterable(this, reducer, ...initial);\n }\n\n [COLLECTION_REDUCE_KV](reducer, initial) {\n const state = SUBVECTOR_STATE.get(this);\n let result = initial;\n let index = 0;\n while (state.start + index < state.end) {\n result = reducer(result, index, state.vector.nth(state.start + index));\n index += 1;\n if (isReducedValue(result)) return unreducedValue(result);\n }\n return result;\n }\n\n [EDITABLE_TRANSIENT]() {\n const state = SUBVECTOR_STATE.get(this);\n const builder = makeTransientVector(\n emptyVectorWithMetadata(state.metadata),\n );\n for (const value of this) {\n builder[TRANSIENT_CONJ](value);\n }\n return builder;\n }\n\n [METADATA_READ]() {\n return SUBVECTOR_STATE.get(this).metadata;\n }\n\n [METADATA_WITH](metadata) {\n const state = SUBVECTOR_STATE.get(this);\n return metadata === state.metadata\n ? this\n : new PersistentSubVector(\n state.vector,\n state.start,\n state.end,\n metadata,\n );\n }\n\n [Symbol.iterator]() {\n const state = SUBVECTOR_STATE.get(this);\n return vectorRangeIterator(state.vector, state.start, state.end);\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptPersistentSubVector\";\n }\n}\n\nexport const EMPTY_VECTOR = makeVector(0, BRANCH_BITS, EMPTY_ROOT, EMPTY_TAIL);\n\nexport function persistentVector(...values) {\n if (values.length === 0) return EMPTY_VECTOR;\n if (values.length <= BRANCH_WIDTH) {\n return makeVector(\n values.length,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail(values),\n );\n }\n return PersistentVector.from(values);\n}\n\nexport function subvec(vector, start, end = undefined) {\n if (!(vector instanceof PersistentVector)) {\n throw new TypeError(\"subvec expects a persistent vector\");\n }\n const count = vector.count;\n const limit = end == null ? count : end;\n if (!Number.isSafeInteger(start) || !Number.isSafeInteger(limit) ||\n start < 0 || limit < start || limit > count) {\n throw new RangeError(\n `subvec range [${String(start)}, ${String(limit)}) is outside [0, ${count}]`,\n );\n }\n if (vector instanceof PersistentSubVector) {\n const state = SUBVECTOR_STATE.get(vector);\n return new PersistentSubVector(\n state.vector,\n state.start + start,\n state.start + limit,\n );\n }\n return new PersistentSubVector(vector, start, limit);\n}\n\nexport function isPersistentVector(value) {\n return value instanceof PersistentVector;\n}\n\n// Bulk transports preserve a view's original base and bounds. Only the\n// constructor-owned WeakMap establishes this brand; user properties do not.\nexport function readPersistentVectorView(value) {\n return SUBVECTOR_STATE.get(value);\n}\n"))
|
|
2
|
+
(js* "import {\n BRANCH_BITS,\n BRANCH_MASK,\n BRANCH_WIDTH,\n EMPTY_ROOT,\n EMPTY_TAIL,\n TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN,\n TRANSIENT_VECTOR_STATE,\n VECTOR_CONSTRUCTOR_TOKEN,\n VECTOR_STATE,\n VectorNode,\n allocateTail,\n editableVectorNode,\n recordInvalidTransientVectorCall,\n recordRootGrowth,\n recordTransientVectorNodeMutation,\n recordTransientVectorPersistent,\n recordTransientVectorTailCopy,\n recordTransientVectorTailMutation,\n visitNode,\n} from \"./vector-internals.mjs\";\nimport {\n VALUE_EQUAL,\n VALUE_HASH,\n orderedCollectionHash,\n} from \"./value-internals.mjs\";\nimport {\n COLLECTION_COUNT,\n COLLECTION_EMPTY,\n COLLECTION_CONJ,\n COLLECTION_GET,\n COLLECTION_ASSOC,\n COLLECTION_CONTAINS,\n COLLECTION_NTH,\n COLLECTION_REDUCE,\n COLLECTION_REDUCE_KV,\n COLLECTION_PEEK,\n COLLECTION_POP,\n COLLECTION_RSEQ,\n COLLECTION_SEQ,\n isReducedValue,\n reduceIterable,\n sequenceView,\n unreducedValue,\n} from \"./collection-internals.mjs\";\nimport {\n EDITABLE_TRANSIENT,\n TRANSIENT_ASSOC,\n TRANSIENT_CONJ,\n TRANSIENT_PERSISTENT,\n} from \"./transient-internals.mjs\";\nimport {\n METADATA_READ,\n METADATA_WITH,\n} from \"./metadata-internals.mjs\";\n\nconst MAX_COUNT = 0x7fffffff;\nconst MISSING = Symbol(\"eliscript.vector.missing\");\nconst VECTOR_HASH_TAG = 0x4f1b_2c3d;\nconst SUBVECTOR_STATE = new WeakMap();\n\nfunction assertIndex(index, upperBound, operation) {\n if (!Number.isInteger(index) || index < 0 || index >= upperBound) {\n throw new RangeError(\n `${operation} index ${String(index)} is outside [0, ${upperBound})`,\n );\n }\n}\n\nfunction tailOffset(count) {\n return count < BRANCH_WIDTH\n ? 0\n : ((count - 1) >>> BRANCH_BITS) << BRANCH_BITS;\n}\n\nfunction reverseVectorIterator(state) {\n let index = state.count - 1;\n let chunk = EMPTY_TAIL;\n let chunkStart = state.count;\n return {\n next() {\n if (index < 0) return { value: undefined, done: true };\n if (index < chunkStart) {\n chunk = arrayFor(state, index);\n chunkStart = index - (index & BRANCH_MASK);\n }\n const value = chunk[index - chunkStart];\n index -= 1;\n return { value, done: false };\n },\n };\n}\n\nfunction vectorRangeIterator(vector, start, end, reverse = false) {\n let index = reverse ? end - 1 : start;\n return {\n next() {\n if (reverse ? index < start : index >= end) {\n return { value: undefined, done: true };\n }\n const value = vector.nth(index);\n index += reverse ? -1 : 1;\n return { value, done: false };\n },\n };\n}\n\nfunction newPath(level, node) {\n if (level === 0) {\n return node;\n }\n return new VectorNode([newPath(level - BRANCH_BITS, node)]);\n}\n\nfunction pushTail(level, parent, tailNode, count) {\n visitNode(parent);\n const subindex = ((count - 1) >>> level) & BRANCH_MASK;\n const slots = parent.slots.slice();\n\n if (level === BRANCH_BITS) {\n slots[subindex] = tailNode;\n } else {\n const child = parent.slots[subindex];\n slots[subindex] = child === undefined\n ? newPath(level - BRANCH_BITS, tailNode)\n : pushTail(level - BRANCH_BITS, child, tailNode, count);\n }\n return new VectorNode(slots);\n}\n\nfunction assocNode(level, node, index, value) {\n visitNode(node);\n const slots = node.slots.slice();\n if (level === 0) {\n slots[index & BRANCH_MASK] = value;\n } else {\n const subindex = (index >>> level) & BRANCH_MASK;\n slots[subindex] = assocNode(\n level - BRANCH_BITS,\n node.slots[subindex],\n index,\n value,\n );\n }\n return new VectorNode(slots);\n}\n\nfunction transientNewPath(level, node, owner) {\n if (level === 0) {\n return node;\n }\n return new VectorNode(\n [transientNewPath(level - BRANCH_BITS, node, owner)],\n owner,\n );\n}\n\nfunction transientPushTail(level, parent, tailNode, count, owner) {\n visitNode(parent);\n const editable = editableVectorNode(parent, owner);\n const subindex = ((count - 1) >>> level) & BRANCH_MASK;\n if (level === BRANCH_BITS) {\n editable.slots[subindex] = tailNode;\n } else {\n const child = editable.slots[subindex];\n editable.slots[subindex] = child === undefined\n ? transientNewPath(level - BRANCH_BITS, tailNode, owner)\n : transientPushTail(level - BRANCH_BITS, child, tailNode, count, owner);\n }\n recordTransientVectorNodeMutation();\n return editable;\n}\n\nfunction transientAssocNode(level, node, index, value, owner) {\n visitNode(node);\n const editable = editableVectorNode(node, owner);\n if (level === 0) {\n editable.slots[index & BRANCH_MASK] = value;\n } else {\n const subindex = (index >>> level) & BRANCH_MASK;\n editable.slots[subindex] = transientAssocNode(\n level - BRANCH_BITS,\n editable.slots[subindex],\n index,\n value,\n owner,\n );\n }\n recordTransientVectorNodeMutation();\n return editable;\n}\n\nfunction popTail(level, node, count) {\n visitNode(node);\n const subindex = ((count - 2) >>> level) & BRANCH_MASK;\n if (level > BRANCH_BITS) {\n const child = popTail(\n level - BRANCH_BITS,\n node.slots[subindex],\n count,\n );\n if (child === undefined && subindex === 0) {\n return undefined;\n }\n const slots = node.slots.slice();\n slots[subindex] = child;\n return new VectorNode(slots);\n }\n if (subindex === 0) {\n return undefined;\n }\n const slots = node.slots.slice();\n slots[subindex] = undefined;\n return new VectorNode(slots);\n}\n\nfunction arrayFor(state, index) {\n if (index >= tailOffset(state.count)) {\n return state.tail;\n }\n\n let node = state.root;\n for (let level = state.shift; level > 0; level -= BRANCH_BITS) {\n visitNode(node);\n node = node.slots[(index >>> level) & BRANCH_MASK];\n }\n visitNode(node);\n return node.slots;\n}\n\nfunction makeVector(count, shift, root, tail, metadata = null) {\n return new PersistentVector(\n VECTOR_CONSTRUCTOR_TOKEN,\n count,\n shift,\n root,\n tail,\n metadata,\n );\n}\n\nfunction emptyVectorWithMetadata(metadata) {\n return metadata === null\n ? EMPTY_VECTOR\n : makeVector(0, BRANCH_BITS, EMPTY_ROOT, EMPTY_TAIL, metadata);\n}\n\nfunction makeTransientVector(vector) {\n return new TransientVector(TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN, vector);\n}\n\nfunction activeTransientVectorState(vector) {\n const state = vector[TRANSIENT_VECTOR_STATE];\n if (!state.active) {\n recordInvalidTransientVectorCall();\n throw new TypeError(\"transient vector is no longer editable\");\n }\n return state;\n}\n\nfunction editableTransientTail(state) {\n if (!state.tailOwned) {\n state.tail = state.tail.slice();\n state.tailOwned = true;\n recordTransientVectorTailCopy();\n }\n return state.tail;\n}\n\nclass TransientVector {\n constructor(token, vector) {\n if (token !== TRANSIENT_VECTOR_CONSTRUCTOR_TOKEN ||\n !(vector instanceof PersistentVector)) {\n throw new TypeError(\"transient vectors must be created from a persistent vector\");\n }\n const source = vector[VECTOR_STATE];\n this[TRANSIENT_VECTOR_STATE] = {\n active: true,\n changed: false,\n owner: Object.freeze({}),\n source: vector,\n count: source.count,\n shift: source.shift,\n root: source.root,\n tail: source.tail,\n tailOwned: false,\n metadata: source.metadata,\n };\n Object.defineProperty(this, \"__eliscript_transient__\", {\n enumerable: true,\n get() {\n throw new TypeError(\"transient vectors cannot be serialized\");\n },\n });\n Object.freeze(this);\n }\n\n [TRANSIENT_CONJ](value) {\n const state = activeTransientVectorState(this);\n if (state.count === MAX_COUNT) {\n throw new RangeError(`persistent vector cannot exceed ${MAX_COUNT} values`);\n }\n if (state.tail.length < BRANCH_WIDTH) {\n editableTransientTail(state).push(value);\n recordTransientVectorTailMutation();\n } else {\n if (!state.tailOwned) {\n state.tail = state.tail.slice();\n recordTransientVectorTailCopy();\n }\n const tailNode = new VectorNode(state.tail, state.owner);\n if ((state.count >>> BRANCH_BITS) > (1 << state.shift)) {\n state.root = new VectorNode([\n state.root,\n transientNewPath(state.shift, tailNode, state.owner),\n ], state.owner);\n state.shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n state.root = transientPushTail(\n state.shift,\n state.root,\n tailNode,\n state.count,\n state.owner,\n );\n }\n state.tail = [value];\n state.tailOwned = true;\n recordTransientVectorTailMutation();\n }\n state.count += 1;\n state.changed = true;\n return this;\n }\n\n [TRANSIENT_ASSOC](index, value) {\n const state = activeTransientVectorState(this);\n if (index === state.count) {\n return this[TRANSIENT_CONJ](value);\n }\n assertIndex(index, state.count, \"assocBang\");\n if (index >= tailOffset(state.count)) {\n editableTransientTail(state)[index & BRANCH_MASK] = value;\n recordTransientVectorTailMutation();\n } else {\n state.root = transientAssocNode(\n state.shift,\n state.root,\n index,\n value,\n state.owner,\n );\n }\n state.changed = true;\n return this;\n }\n\n [TRANSIENT_PERSISTENT]() {\n const state = activeTransientVectorState(this);\n const result = state.changed\n ? makeVector(\n state.count,\n state.shift,\n state.root,\n state.tail,\n state.metadata,\n )\n : state.source;\n state.active = false;\n state.owner = null;\n recordTransientVectorPersistent();\n return result;\n }\n\n toJSON() {\n throw new TypeError(\"transient vectors cannot be serialized\");\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptTransientVector\";\n }\n}\n\nexport class PersistentVector {\n constructor(token, count, shift, root, tail, metadata = null) {\n if (token !== VECTOR_CONSTRUCTOR_TOKEN) {\n throw new TypeError(\n \"PersistentVector values must be created with persistentVector or PersistentVector.from\",\n );\n }\n this[VECTOR_STATE] = {\n count,\n shift,\n root,\n tail,\n metadata,\n };\n Object.freeze(this);\n }\n\n static empty() {\n return EMPTY_VECTOR;\n }\n\n static single(value) {\n return makeVector(\n 1,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail([value]),\n null,\n );\n }\n\n static pairWithMeta(first, second, metadata = null) {\n return makeVector(\n 2,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail([first, second]),\n metadata,\n );\n }\n\n static rawSlot(vector, index) {\n return vector[VECTOR_STATE].tail[index];\n }\n\n static from(iterable) {\n if (iterable instanceof PersistentVector) {\n return iterable;\n }\n if (\n Array.isArray(iterable) &&\n Object.getPrototypeOf(iterable) === Array.prototype &&\n !Object.hasOwn(iterable, Symbol.iterator) &&\n iterable[Symbol.iterator] === Array.prototype[Symbol.iterator]\n ) {\n const length = iterable.length;\n if (length === 0) return EMPTY_VECTOR;\n if (length <= BRANCH_WIDTH) {\n return makeVector(\n length,\n BRANCH_BITS,\n EMPTY_ROOT,\n allocateTail(iterable.slice()),\n );\n }\n let root = EMPTY_ROOT;\n let shift = BRANCH_BITS;\n let count = BRANCH_WIDTH;\n let tail = allocateTail(iterable.slice(0, BRANCH_WIDTH));\n while (count < length) {\n const nextEnd = Math.min(count + BRANCH_WIDTH, length);\n const tailNode = new VectorNode(tail);\n if ((count >>> BRANCH_BITS) > (1 << shift)) {\n root = new VectorNode([\n root,\n newPath(shift, tailNode),\n ]);\n shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n root = pushTail(shift, root, tailNode, count);\n }\n tail = allocateTail(iterable.slice(count, nextEnd));\n count = nextEnd;\n }\n return makeVector(length, shift, root, tail);\n }\n let result = EMPTY_VECTOR;\n for (const value of iterable) {\n result = result.conj(value);\n }\n return result;\n }\n\n get count() {\n return this[VECTOR_STATE].count;\n }\n\n get size() {\n return this[VECTOR_STATE].count;\n }\n\n nth(index, notFound = MISSING) {\n const state = this[VECTOR_STATE];\n if (((index | 0) === index || Number.isInteger(index)) && index >= 0 && index < state.count) {\n if (state.count <= BRANCH_WIDTH) {\n const value = state.tail[index];\n if (\n index === 1 &&\n state.count === 2 &&\n state.metadata !== null &&\n Array.isArray(value) &&\n value.length === 6 &&\n value[5] === true &&\n !Object.isFrozen(value)\n ) {\n Object.freeze(value);\n }\n return value;\n }\n return arrayFor(state, index)[index & BRANCH_MASK];\n }\n if (notFound !== MISSING) {\n return notFound;\n }\n assertIndex(index, state.count, \"nth\");\n }\n\n assoc(index, value) {\n const state = this[VECTOR_STATE];\n if (index === state.count) {\n return this.conj(value);\n }\n assertIndex(index, state.count, \"assoc\");\n\n if (index >= tailOffset(state.count)) {\n const tail = state.tail.slice();\n tail[index & BRANCH_MASK] = value;\n return makeVector(\n state.count,\n state.shift,\n state.root,\n allocateTail(tail),\n state.metadata,\n );\n }\n return makeVector(\n state.count,\n state.shift,\n assocNode(state.shift, state.root, index, value),\n state.tail,\n state.metadata,\n );\n }\n\n conj(value) {\n const state = this[VECTOR_STATE];\n if (state.count === MAX_COUNT) {\n throw new RangeError(`persistent vector cannot exceed ${MAX_COUNT} values`);\n }\n if (state.tail.length < BRANCH_WIDTH) {\n return makeVector(\n state.count + 1,\n state.shift,\n state.root,\n allocateTail([...state.tail, value]),\n state.metadata,\n );\n }\n\n const tailNode = new VectorNode(state.tail);\n let shift = state.shift;\n let root;\n if ((state.count >>> BRANCH_BITS) > (1 << state.shift)) {\n root = new VectorNode([\n state.root,\n newPath(state.shift, tailNode),\n ]);\n shift += BRANCH_BITS;\n recordRootGrowth();\n } else {\n root = pushTail(state.shift, state.root, tailNode, state.count);\n }\n return makeVector(\n state.count + 1,\n shift,\n root,\n allocateTail([value]),\n state.metadata,\n );\n }\n\n peek(notFound = null) {\n const state = this[VECTOR_STATE];\n return state.count === 0 ? notFound : state.tail[state.tail.length - 1];\n }\n\n pop() {\n const state = this[VECTOR_STATE];\n if (state.count === 0) {\n throw new RangeError(\"cannot pop an empty persistent vector\");\n }\n if (state.count === 1) {\n return emptyVectorWithMetadata(state.metadata);\n }\n if (state.tail.length > 1) {\n return makeVector(\n state.count - 1,\n state.shift,\n state.root,\n allocateTail(state.tail.slice(0, -1)),\n state.metadata,\n );\n }\n\n const tail = allocateTail([...arrayFor(state, state.count - 2)]);\n let root = popTail(state.shift, state.root, state.count) ?? EMPTY_ROOT;\n let shift = state.shift;\n if (shift > BRANCH_BITS && root.slots[1] === undefined) {\n root = root.slots[0];\n shift -= BRANCH_BITS;\n }\n return makeVector(\n state.count - 1,\n shift,\n root,\n tail,\n state.metadata,\n );\n }\n\n reduce(reducer, ...initial) {\n if (typeof reducer !== \"function\") {\n throw new TypeError(\"persistent vector reducer must be a function\");\n }\n const state = this[VECTOR_STATE];\n let accumulator;\n let index = 0;\n if (initial.length === 0) {\n if (state.count === 0) {\n throw new TypeError(\"cannot reduce an empty persistent vector without an initial value\");\n }\n accumulator = this.nth(0);\n index = 1;\n } else {\n accumulator = initial[0];\n }\n\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n accumulator = reducer(accumulator, chunk[offset], index);\n index += 1;\n }\n }\n return accumulator;\n }\n\n [COLLECTION_COUNT]() {\n return this.count;\n }\n\n [COLLECTION_EMPTY]() {\n return emptyVectorWithMetadata(this[VECTOR_STATE].metadata);\n }\n\n [COLLECTION_CONJ](value) {\n return this.conj(value);\n }\n\n [COLLECTION_PEEK]() {\n return this.peek();\n }\n\n [COLLECTION_POP]() {\n return this.pop();\n }\n\n [COLLECTION_RSEQ]() {\n const state = this[VECTOR_STATE];\n return state.count === 0\n ? null\n : sequenceView(() => reverseVectorIterator(state), state.count);\n }\n\n [COLLECTION_GET](index, notFound = null) {\n return this.nth(index, notFound);\n }\n\n [COLLECTION_ASSOC](index, value) {\n return this.assoc(index, value);\n }\n\n [COLLECTION_CONTAINS](index) {\n return Number.isInteger(index) && index >= 0 && index < this.count;\n }\n\n [COLLECTION_NTH](index, ...notFound) {\n return notFound.length === 0\n ? this.nth(index)\n : this.nth(index, notFound[0]);\n }\n\n [COLLECTION_SEQ]() {\n return this.count === 0\n ? null\n : sequenceView(() => this[Symbol.iterator](), this.count);\n }\n\n [COLLECTION_REDUCE](reducer, ...initial) {\n return reduceIterable(this, reducer, ...initial);\n }\n\n [COLLECTION_REDUCE_KV](reducer, initial) {\n const state = this[VECTOR_STATE];\n let result = initial;\n let index = 0;\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n result = reducer(result, index, chunk[offset]);\n index += 1;\n if (isReducedValue(result)) return unreducedValue(result);\n }\n }\n return result;\n }\n\n [EDITABLE_TRANSIENT]() {\n return makeTransientVector(this);\n }\n\n toArray() {\n return this.reduce((values, value) => {\n values.push(value);\n return values;\n }, []);\n }\n\n sliceVector(start, end) {\n const state = this[VECTOR_STATE];\n const length = end - start;\n if (length <= 0) return EMPTY_VECTOR;\n if (start === 0 && end >= state.count && state.metadata === null) {\n return this;\n }\n const values = new Array(length);\n let outIndex = 0;\n let index = start;\n while (index < end) {\n const chunk = arrayFor(state, index);\n const chunkOffset = index & BRANCH_MASK;\n const chunkBase = index - chunkOffset;\n const chunkEnd = Math.min(end - chunkBase, chunk.length);\n for (let i = chunkOffset; i < chunkEnd; i += 1) {\n values[outIndex] = chunk[i];\n outIndex += 1;\n }\n index = chunkBase + chunkEnd;\n }\n return PersistentVector.from(values);\n }\n\n [VALUE_EQUAL](other, equal) {\n if (this === other) {\n return true;\n }\n if (!(other instanceof PersistentVector) || other.count !== this.count) {\n return false;\n }\n const right = other[Symbol.iterator]();\n for (const value of this) {\n if (!equal(value, right.next().value)) {\n return false;\n }\n }\n return true;\n }\n\n [VALUE_HASH](hash) {\n return orderedCollectionHash(this, hash, VECTOR_HASH_TAG);\n }\n\n [METADATA_READ]() {\n return this[VECTOR_STATE].metadata;\n }\n\n [METADATA_WITH](metadata) {\n const state = this[VECTOR_STATE];\n return metadata === state.metadata\n ? this\n : makeVector(\n state.count,\n state.shift,\n state.root,\n state.tail,\n metadata,\n );\n }\n\n *[Symbol.iterator]() {\n const state = this[VECTOR_STATE];\n let index = 0;\n while (index < state.count) {\n const chunk = arrayFor(state, index);\n const chunkStart = index & BRANCH_MASK;\n const chunkLength = Math.min(chunk.length, state.count - (index - chunkStart));\n for (let offset = chunkStart; offset < chunkLength; offset += 1) {\n yield chunk[offset];\n index += 1;\n }\n }\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptPersistentVector\";\n }\n}\n\nclass PersistentSubVector extends PersistentVector {\n constructor(vector, start, end, metadata = null) {\n const source = vector[VECTOR_STATE];\n super(\n VECTOR_CONSTRUCTOR_TOKEN,\n source.count,\n source.shift,\n source.root,\n source.tail,\n metadata,\n );\n SUBVECTOR_STATE.set(this, Object.freeze({ vector, start, end, metadata }));\n }\n\n get count() {\n const state = SUBVECTOR_STATE.get(this);\n return state.end - state.start;\n }\n\n get size() {\n return this.count;\n }\n\n sliceVector(start, end) {\n const state = SUBVECTOR_STATE.get(this);\n return state.vector.sliceVector(state.start + start, state.start + end);\n }\n\n nth(index, notFound = MISSING) {\n const state = SUBVECTOR_STATE.get(this);\n const count = state.end - state.start;\n if (!Number.isInteger(index) || index < 0 || index >= count) {\n if (notFound !== MISSING) return notFound;\n assertIndex(index, count, \"subvec nth\");\n }\n return state.vector.nth(state.start + index);\n }\n\n assoc(index, value) {\n const state = SUBVECTOR_STATE.get(this);\n const count = state.end - state.start;\n if (!Number.isInteger(index) || index < 0 || index > count) {\n throw new RangeError(\n `subvec assoc index ${String(index)} is outside [0, ${count}]`,\n );\n }\n const absoluteIndex = state.start + index;\n const vector = absoluteIndex === state.vector.count\n ? state.vector.conj(value)\n : state.vector.assoc(absoluteIndex, value);\n return new PersistentSubVector(\n vector,\n state.start,\n Math.max(state.end, absoluteIndex + 1),\n state.metadata,\n );\n }\n\n conj(value) {\n return this.assoc(this.count, value);\n }\n\n peek(notFound = null) {\n const state = SUBVECTOR_STATE.get(this);\n return state.start === state.end\n ? notFound\n : state.vector.nth(state.end - 1);\n }\n\n pop() {\n const state = SUBVECTOR_STATE.get(this);\n if (state.start === state.end) {\n throw new RangeError(\"cannot pop an empty persistent subvector\");\n }\n return state.end - state.start === 1\n ? emptyVectorWithMetadata(state.metadata)\n : new PersistentSubVector(\n state.vector,\n state.start,\n state.end - 1,\n state.metadata,\n );\n }\n\n reduce(reducer, ...initial) {\n if (typeof reducer !== \"function\") {\n throw new TypeError(\"persistent subvector reducer must be a function\");\n }\n const state = SUBVECTOR_STATE.get(this);\n let index = 0;\n let result;\n if (initial.length === 0) {\n if (state.start === state.end) {\n throw new TypeError(\n \"cannot reduce an empty persistent subvector without an initial value\",\n );\n }\n result = state.vector.nth(state.start);\n index = 1;\n } else {\n result = initial[0];\n }\n while (index < state.end - state.start) {\n result = reducer(result, state.vector.nth(state.start + index), index);\n index += 1;\n }\n return result;\n }\n\n [COLLECTION_COUNT]() {\n return this.count;\n }\n\n [COLLECTION_EMPTY]() {\n return emptyVectorWithMetadata(SUBVECTOR_STATE.get(this).metadata);\n }\n\n [COLLECTION_CONJ](value) {\n return this.conj(value);\n }\n\n [COLLECTION_PEEK]() {\n return this.peek();\n }\n\n [COLLECTION_POP]() {\n return this.pop();\n }\n\n [COLLECTION_RSEQ]() {\n const state = SUBVECTOR_STATE.get(this);\n return state.start === state.end\n ? null\n : sequenceView(\n () => vectorRangeIterator(\n state.vector,\n state.start,\n state.end,\n true,\n ),\n state.end - state.start,\n );\n }\n\n [COLLECTION_GET](index, notFound = null) {\n return this.nth(index, notFound);\n }\n\n [COLLECTION_ASSOC](index, value) {\n return this.assoc(index, value);\n }\n\n [COLLECTION_CONTAINS](index) {\n return Number.isInteger(index) && index >= 0 && index < this.count;\n }\n\n [COLLECTION_NTH](index, ...notFound) {\n return notFound.length === 0\n ? this.nth(index)\n : this.nth(index, notFound[0]);\n }\n\n [COLLECTION_SEQ]() {\n return this.count === 0\n ? null\n : sequenceView(() => this[Symbol.iterator](), this.count);\n }\n\n [COLLECTION_REDUCE](reducer, ...initial) {\n return reduceIterable(this, reducer, ...initial);\n }\n\n [COLLECTION_REDUCE_KV](reducer, initial) {\n const state = SUBVECTOR_STATE.get(this);\n let result = initial;\n let index = 0;\n while (state.start + index < state.end) {\n result = reducer(result, index, state.vector.nth(state.start + index));\n index += 1;\n if (isReducedValue(result)) return unreducedValue(result);\n }\n return result;\n }\n\n [EDITABLE_TRANSIENT]() {\n const state = SUBVECTOR_STATE.get(this);\n const builder = makeTransientVector(\n emptyVectorWithMetadata(state.metadata),\n );\n for (const value of this) {\n builder[TRANSIENT_CONJ](value);\n }\n return builder;\n }\n\n [METADATA_READ]() {\n return SUBVECTOR_STATE.get(this).metadata;\n }\n\n [METADATA_WITH](metadata) {\n const state = SUBVECTOR_STATE.get(this);\n return metadata === state.metadata\n ? this\n : new PersistentSubVector(\n state.vector,\n state.start,\n state.end,\n metadata,\n );\n }\n\n [Symbol.iterator]() {\n const state = SUBVECTOR_STATE.get(this);\n return vectorRangeIterator(state.vector, state.start, state.end);\n }\n\n get [Symbol.toStringTag]() {\n return \"EliscriptPersistentSubVector\";\n }\n}\n\nexport const EMPTY_VECTOR = makeVector(0, BRANCH_BITS, EMPTY_ROOT, EMPTY_TAIL);\n\nexport function persistentVector(...values) {\n return PersistentVector.from(values);\n}\n\nexport function subvec(vector, start, end = undefined) {\n if (!(vector instanceof PersistentVector)) {\n throw new TypeError(\"subvec expects a persistent vector\");\n }\n const count = vector.count;\n const limit = end == null ? count : end;\n if (!Number.isSafeInteger(start) || !Number.isSafeInteger(limit) ||\n start < 0 || limit < start || limit > count) {\n throw new RangeError(\n `subvec range [${String(start)}, ${String(limit)}) is outside [0, ${count}]`,\n );\n }\n if (vector instanceof PersistentSubVector) {\n const state = SUBVECTOR_STATE.get(vector);\n return new PersistentSubVector(\n state.vector,\n state.start + start,\n state.start + limit,\n );\n }\n return new PersistentSubVector(vector, start, limit);\n}\n\nexport function isPersistentVector(value) {\n return value instanceof PersistentVector;\n}\n\n// Bulk transports preserve a view's original base and bounds. Only the\n// constructor-owned WeakMap establishes this brand; user properties do not.\nexport function readPersistentVectorView(value) {\n return SUBVECTOR_STATE.get(value);\n}\n"))
|
package/runtime/core/vector.mjs
CHANGED
|
@@ -1029,15 +1029,6 @@ class PersistentSubVector extends PersistentVector {
|
|
|
1029
1029
|
export const EMPTY_VECTOR = makeVector(0, BRANCH_BITS, EMPTY_ROOT, EMPTY_TAIL);
|
|
1030
1030
|
|
|
1031
1031
|
export function persistentVector(...values) {
|
|
1032
|
-
if (values.length === 0) return EMPTY_VECTOR;
|
|
1033
|
-
if (values.length <= BRANCH_WIDTH) {
|
|
1034
|
-
return makeVector(
|
|
1035
|
-
values.length,
|
|
1036
|
-
BRANCH_BITS,
|
|
1037
|
-
EMPTY_ROOT,
|
|
1038
|
-
allocateTail(values),
|
|
1039
|
-
);
|
|
1040
|
-
}
|
|
1041
1032
|
return PersistentVector.from(values);
|
|
1042
1033
|
}
|
|
1043
1034
|
|