eliscript 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +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 (js* \"new WeakMap()\"))\n (defconst protocol-missing (js* \"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 (js* \"(description) => Symbol(description)\"))\n (defconst protocol-array? (js* \"Array.isArray\"))\n (defconst protocol-map (js* \"() => new Map()\"))\n (defconst protocol-null-object (js* \"() => Object.create(null)\"))\n (defconst protocol-own-descriptors\n (js* \"Object.getOwnPropertyDescriptors\"))\n (defconst protocol-own-keys (js* \"Reflect.ownKeys\"))\n (defconst protocol-has-own\n (js* \"(value, key) => Object.prototype.hasOwnProperty.call(value, key)\"))\n (defconst protocol-property-present\n (js* \"(value, key) => { if (value === null || value === undefined) return false; return Reflect.has(Object(value), key); }\"))\n (defconst protocol-property-value\n (js* \"(value, key) => Object(value)[key]\"))\n (defconst protocol-host-type (js* \"(value) => typeof value\"))\n (defconst protocol-exact-prototype\n (js* \"(value, missing) => ((typeof value === \\\"object\\\" && value !== null) || typeof value === \\\"function\\\") ? Object.getPrototypeOf(value) : 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 (js* \"(target, key, value) => { Object.defineProperty(target, key, { value, enumerable: true }); return target; }\"))\n (defconst protocol-define-metadata\n (js* \"(target, protocol, operation, slot) => { Object.defineProperties(target, { protocol: { value: protocol }, operation: { value: operation }, slot: { value: slot } }); return 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 define-protocol (name operations)\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 (funcall protocol-symbol\n (str \"eliscript.protocol.\"\n protocol-name \"/\" operation)))\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 (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 (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 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 (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 (export define-protocol define-protocol-from-definition\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;AAC1B,yBAA2B;AAC3B,iCACE,CAAU,QAAO,aAAY,WAAU,UAAS,UAAS,UACxD,UAAS,YAAW;AACvB,mCAAqC;AACrC,oCAAsC;AACtC,gCAAkC;AAIlC,wBAA0B;AAC1B,8BAA0B;AAC1B,qBAAuB;AACvB,6BAA+B;AAC/B,iCACE;AACF,0BAA4B;AAC5B,yBACE;AACF,kCACE;AACF,gCACE;AACF,2BAA6B;AAC7B,iCACE;AACF,kCAAoC;AACpC,+BAAiC;AACjC,wBAA0B;AAC1B,mCACE;AACF,iCACE;AAEF,uBAAsB;SACpB,0DAAO,IAAK,qBAAqB;;AAEnC,wBAAuB;SACrB,EAAM;WACJ,oBAAI,CAAY,wBACX,cAAc,oCACjB;KAHS,CAAS,iBAAgB,OAAK;;AAK7C,wBAAuB,OAAM;SAC3B,oBAAI,2FACI,CAAG,EAAW,yBAAO,KADrB,qBAAK,CAAG,s7BAAY,WAAO,gBAE9B,cAAc,QAAK,gBAAM,mCAC5B;;AAEJ,uCAAsC;EACpC,oBAAM,qBAAc,sBAAgB,iBACjC,cAAc;SACjB,EAAM;IACJ,oBAAM,CAAG,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sC,qCAAjC,CAAG,QAAM,EAAW;gBAC9B,EAAO;2BAEA;2BACL,oBAAM,8FACI,uEACK,CAAI,gBAAgB,OAAM,QAAO,WAC9B,2CAFH,CAAG,WAAO,qBADf,CAAM,qBAId,CAAM,CAAA,cAAY;qBALN,cAAc,QAAO;mBAD7B,CAAS,CAAK,OAAM,eAAa,OAAK,IAAc,mBAAN;gBAOtD,CAAM,CAAA,QAAM,CAAI;;;;mBAClB;;WAXkB;SADN;OADK,aAAa,CAAK,UAAS;KAD/B,eAAe;;AAgBhC,QAAQ,iBAAgB,iCAChB,0BAAyB,yBACzB,sBAAqB,sCACrB,4BAAqB,qBAAoB,wBACzC,iBAAgB"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { ProtocolDispatchError } from "./protocol-error.mjs";
|
|
2
|
+
export {
|
|
3
|
+
define_protocol as defineProtocol,
|
|
4
|
+
define_protocol_from_definition as defineProtocolFromDefinition,
|
|
5
|
+
extend_protocol_category as extendProtocolCategory,
|
|
6
|
+
extend_protocol_default as extendProtocolDefault,
|
|
7
|
+
extend_protocol_type as extendProtocolType,
|
|
8
|
+
implements_protocol_operation_QMARK_ as implementsProtocolOperation,
|
|
9
|
+
implements_protocol_QMARK_ as implementsProtocol,
|
|
10
|
+
protocol_definition as protocolDefinition,
|
|
11
|
+
protocol_host_category as protocolHostCategory,
|
|
12
|
+
protocol_method as protocolMethod,
|
|
13
|
+
protocol_slot as protocolSlot,
|
|
14
|
+
} from "./protocol-impl.mjs";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const QUEUE_CONSTRUCTOR_TOKEN = Symbol("eliscript.queue.constructor");
|
|
2
|
+
export const QUEUE_STATE = Symbol("eliscript.queue.state");
|
|
3
|
+
|
|
4
|
+
const metrics = {
|
|
5
|
+
queueAllocations: 0,
|
|
6
|
+
frontPromotions: 0,
|
|
7
|
+
promotedValues: 0,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function recordQueueAllocation() {
|
|
11
|
+
metrics.queueAllocations += 1;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function recordQueueFrontPromotion(count) {
|
|
15
|
+
metrics.frontPromotions += 1;
|
|
16
|
+
metrics.promotedValues += count;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function resetQueueMetrics() {
|
|
20
|
+
metrics.queueAllocations = 0;
|
|
21
|
+
metrics.frontPromotions = 0;
|
|
22
|
+
metrics.promotedValues = 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function readQueueMetrics() {
|
|
26
|
+
return Object.freeze({ ...metrics });
|
|
27
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COLLECTION_CONJ,
|
|
3
|
+
COLLECTION_COUNT,
|
|
4
|
+
COLLECTION_EMPTY,
|
|
5
|
+
COLLECTION_PEEK,
|
|
6
|
+
COLLECTION_POP,
|
|
7
|
+
COLLECTION_REDUCE,
|
|
8
|
+
COLLECTION_SEQ,
|
|
9
|
+
reduceIterable,
|
|
10
|
+
sequenceView,
|
|
11
|
+
} from "./collection-internals.mjs";
|
|
12
|
+
import {
|
|
13
|
+
METADATA_READ,
|
|
14
|
+
METADATA_WITH,
|
|
15
|
+
} from "./metadata-internals.mjs";
|
|
16
|
+
import {
|
|
17
|
+
QUEUE_CONSTRUCTOR_TOKEN,
|
|
18
|
+
QUEUE_STATE,
|
|
19
|
+
recordQueueAllocation,
|
|
20
|
+
recordQueueFrontPromotion,
|
|
21
|
+
} from "./queue-internals.mjs";
|
|
22
|
+
import {
|
|
23
|
+
VALUE_EQUAL,
|
|
24
|
+
VALUE_HASH,
|
|
25
|
+
orderedCollectionHash,
|
|
26
|
+
} from "./value-internals.mjs";
|
|
27
|
+
import {
|
|
28
|
+
EMPTY_VECTOR,
|
|
29
|
+
PersistentVector,
|
|
30
|
+
persistentVector,
|
|
31
|
+
subvec,
|
|
32
|
+
} from "./vector.mjs";
|
|
33
|
+
|
|
34
|
+
const MAX_COUNT = 0x7fff_ffff;
|
|
35
|
+
const QUEUE_HASH_TAG = 0x6b3d_1f27;
|
|
36
|
+
|
|
37
|
+
function makeQueue(count, front, rear, metadata = null) {
|
|
38
|
+
recordQueueAllocation();
|
|
39
|
+
return new PersistentQueue(
|
|
40
|
+
QUEUE_CONSTRUCTOR_TOKEN,
|
|
41
|
+
count,
|
|
42
|
+
front,
|
|
43
|
+
rear,
|
|
44
|
+
metadata,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function emptyQueueWithMetadata(metadata) {
|
|
49
|
+
return metadata === null
|
|
50
|
+
? EMPTY_QUEUE
|
|
51
|
+
: makeQueue(0, EMPTY_VECTOR, EMPTY_VECTOR, metadata);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export class PersistentQueue {
|
|
55
|
+
constructor(token, count, front, rear, metadata = null) {
|
|
56
|
+
if (token !== QUEUE_CONSTRUCTOR_TOKEN) {
|
|
57
|
+
throw new TypeError(
|
|
58
|
+
"PersistentQueue values must be created with persistentQueue or PersistentQueue.from",
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
if (!Number.isSafeInteger(count) || count < 0 || count > MAX_COUNT ||
|
|
62
|
+
!(front instanceof PersistentVector) ||
|
|
63
|
+
!(rear instanceof PersistentVector) ||
|
|
64
|
+
front.count + rear.count !== count ||
|
|
65
|
+
(count === 0 ? front.count !== 0 || rear.count !== 0 : front.count === 0)) {
|
|
66
|
+
throw new TypeError("invalid persistent queue state");
|
|
67
|
+
}
|
|
68
|
+
this[QUEUE_STATE] = Object.freeze({
|
|
69
|
+
count,
|
|
70
|
+
front,
|
|
71
|
+
rear,
|
|
72
|
+
metadata,
|
|
73
|
+
});
|
|
74
|
+
Object.freeze(this);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static empty() {
|
|
78
|
+
return EMPTY_QUEUE;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static from(iterable) {
|
|
82
|
+
if (iterable instanceof PersistentQueue) return iterable;
|
|
83
|
+
let result = EMPTY_QUEUE;
|
|
84
|
+
for (const value of iterable) result = result.conj(value);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
get count() {
|
|
89
|
+
return this[QUEUE_STATE].count;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get size() {
|
|
93
|
+
return this[QUEUE_STATE].count;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get isEmpty() {
|
|
97
|
+
return this[QUEUE_STATE].count === 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
conj(value) {
|
|
101
|
+
const state = this[QUEUE_STATE];
|
|
102
|
+
if (state.count === MAX_COUNT) {
|
|
103
|
+
throw new RangeError(`persistent queue cannot exceed ${MAX_COUNT} values`);
|
|
104
|
+
}
|
|
105
|
+
return state.count === 0
|
|
106
|
+
? makeQueue(1, persistentVector(value), EMPTY_VECTOR, state.metadata)
|
|
107
|
+
: makeQueue(
|
|
108
|
+
state.count + 1,
|
|
109
|
+
state.front,
|
|
110
|
+
state.rear.conj(value),
|
|
111
|
+
state.metadata,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
peek(notFound = null) {
|
|
116
|
+
const state = this[QUEUE_STATE];
|
|
117
|
+
return state.count === 0 ? notFound : state.front.nth(0);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
pop() {
|
|
121
|
+
const state = this[QUEUE_STATE];
|
|
122
|
+
if (state.count === 0) {
|
|
123
|
+
throw new RangeError("cannot pop an empty persistent queue");
|
|
124
|
+
}
|
|
125
|
+
if (state.front.count > 1) {
|
|
126
|
+
return makeQueue(
|
|
127
|
+
state.count - 1,
|
|
128
|
+
subvec(state.front, 1),
|
|
129
|
+
state.rear,
|
|
130
|
+
state.metadata,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (state.rear.count === 0) {
|
|
134
|
+
return emptyQueueWithMetadata(state.metadata);
|
|
135
|
+
}
|
|
136
|
+
recordQueueFrontPromotion(state.rear.count);
|
|
137
|
+
return makeQueue(
|
|
138
|
+
state.count - 1,
|
|
139
|
+
state.rear,
|
|
140
|
+
EMPTY_VECTOR,
|
|
141
|
+
state.metadata,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
reduce(reducer, ...initial) {
|
|
146
|
+
return reduceIterable(this, reducer, ...initial);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
toArray() {
|
|
150
|
+
return [...this];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
[COLLECTION_COUNT]() {
|
|
154
|
+
return this.count;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
[COLLECTION_EMPTY]() {
|
|
158
|
+
return emptyQueueWithMetadata(this[QUEUE_STATE].metadata);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
[COLLECTION_CONJ](value) {
|
|
162
|
+
return this.conj(value);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
[COLLECTION_PEEK]() {
|
|
166
|
+
return this.peek();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
[COLLECTION_POP]() {
|
|
170
|
+
return this.pop();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
[COLLECTION_SEQ]() {
|
|
174
|
+
return this.isEmpty
|
|
175
|
+
? null
|
|
176
|
+
: sequenceView(() => this[Symbol.iterator](), this.count);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
[COLLECTION_REDUCE](reducer, ...initial) {
|
|
180
|
+
return reduceIterable(this, reducer, ...initial);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
[VALUE_EQUAL](other, equal) {
|
|
184
|
+
if (!(other instanceof PersistentQueue) || other.count !== this.count) {
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
const right = other[Symbol.iterator]();
|
|
188
|
+
for (const value of this) {
|
|
189
|
+
if (!equal(value, right.next().value)) return false;
|
|
190
|
+
}
|
|
191
|
+
return true;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
[VALUE_HASH](hash) {
|
|
195
|
+
return orderedCollectionHash(this, hash, QUEUE_HASH_TAG);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
[METADATA_READ]() {
|
|
199
|
+
return this[QUEUE_STATE].metadata;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
[METADATA_WITH](metadata) {
|
|
203
|
+
const state = this[QUEUE_STATE];
|
|
204
|
+
return metadata === state.metadata
|
|
205
|
+
? this
|
|
206
|
+
: makeQueue(
|
|
207
|
+
state.count,
|
|
208
|
+
state.front,
|
|
209
|
+
state.rear,
|
|
210
|
+
metadata,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
*[Symbol.iterator]() {
|
|
215
|
+
const state = this[QUEUE_STATE];
|
|
216
|
+
yield* state.front;
|
|
217
|
+
yield* state.rear;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
get [Symbol.toStringTag]() {
|
|
221
|
+
return "EliscriptPersistentQueue";
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const EMPTY_QUEUE = new PersistentQueue(
|
|
226
|
+
QUEUE_CONSTRUCTOR_TOKEN,
|
|
227
|
+
0,
|
|
228
|
+
EMPTY_VECTOR,
|
|
229
|
+
EMPTY_VECTOR,
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
export function persistentQueue(...values) {
|
|
233
|
+
return PersistentQueue.from(values);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function isPersistentQueue(value) {
|
|
237
|
+
return value instanceof PersistentQueue;
|
|
238
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COLLECTION_ASSOC,
|
|
3
|
+
COLLECTION_CONJ,
|
|
4
|
+
COLLECTION_CONTAINS,
|
|
5
|
+
COLLECTION_COUNT,
|
|
6
|
+
COLLECTION_DISSOC,
|
|
7
|
+
COLLECTION_EMPTY,
|
|
8
|
+
COLLECTION_GET,
|
|
9
|
+
COLLECTION_REDUCE,
|
|
10
|
+
COLLECTION_REDUCE_KV,
|
|
11
|
+
COLLECTION_SEQ,
|
|
12
|
+
isReducedValue,
|
|
13
|
+
readCollectionEntry,
|
|
14
|
+
reduceIterable,
|
|
15
|
+
sequenceView,
|
|
16
|
+
unreducedValue,
|
|
17
|
+
} from "./collection-internals.mjs";
|
|
18
|
+
import {
|
|
19
|
+
identifierName,
|
|
20
|
+
identifierNamespace,
|
|
21
|
+
isKeyword,
|
|
22
|
+
keyword,
|
|
23
|
+
} from "./identifier.mjs";
|
|
24
|
+
import { EMPTY_MAP, PersistentHashMap } from "./map.mjs";
|
|
25
|
+
import { METADATA_READ, METADATA_WITH } from "./metadata-internals.mjs";
|
|
26
|
+
import {
|
|
27
|
+
VALUE_EQUAL,
|
|
28
|
+
VALUE_HASH,
|
|
29
|
+
finishHash,
|
|
30
|
+
hashString,
|
|
31
|
+
mixHash,
|
|
32
|
+
} from "./value-internals.mjs";
|
|
33
|
+
|
|
34
|
+
const MAX_FIELDS = 1024;
|
|
35
|
+
const RECORD_CONSTRUCTOR_TOKEN = Symbol("eliscript.record.constructor-token");
|
|
36
|
+
const RECORD_STATE = Symbol("eliscript.record.state");
|
|
37
|
+
const RECORD_HASH_TAG = 0x4d83_a92f;
|
|
38
|
+
const recordTypes = new WeakSet();
|
|
39
|
+
|
|
40
|
+
function fail(message) {
|
|
41
|
+
throw new TypeError(message);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function normalizeTypeName(name) {
|
|
45
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
46
|
+
fail("record type name must be a non-empty string");
|
|
47
|
+
}
|
|
48
|
+
return name;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function normalizeFieldNames(fields) {
|
|
52
|
+
if (!Array.isArray(fields) || fields.length > MAX_FIELDS) {
|
|
53
|
+
fail(`record fields must be a dense array of at most ${MAX_FIELDS} names`);
|
|
54
|
+
}
|
|
55
|
+
const descriptors = Object.getOwnPropertyDescriptors(fields);
|
|
56
|
+
if (Reflect.ownKeys(descriptors).length !== fields.length + 1) {
|
|
57
|
+
fail("record fields must be a dense array with no extra properties");
|
|
58
|
+
}
|
|
59
|
+
const seen = new Set();
|
|
60
|
+
const result = [];
|
|
61
|
+
for (let index = 0; index < fields.length; index += 1) {
|
|
62
|
+
const descriptor = descriptors[String(index)];
|
|
63
|
+
const field = descriptor?.value;
|
|
64
|
+
if (!descriptor?.enumerable || typeof field !== "string" ||
|
|
65
|
+
field.length === 0 || field.includes("/")) {
|
|
66
|
+
fail("record field names must be non-empty unqualified strings");
|
|
67
|
+
}
|
|
68
|
+
if (seen.has(field)) {
|
|
69
|
+
fail(`record declares duplicate field ${field}`);
|
|
70
|
+
}
|
|
71
|
+
seen.add(field);
|
|
72
|
+
result.push(field);
|
|
73
|
+
}
|
|
74
|
+
return Object.freeze(result);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function declaredFieldIndex(typeState, key) {
|
|
78
|
+
if (!isKeyword(key) || identifierNamespace(key) !== null) return -1;
|
|
79
|
+
return typeState.fieldIndexes.get(identifierName(key)) ?? -1;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function mapWithMetadata(map, metadata) {
|
|
83
|
+
return metadata === null ? map : map[METADATA_WITH](metadata);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function defineFieldAccessors(RecordValue, typeState) {
|
|
87
|
+
for (let index = 0; index < typeState.fieldNames.length; index += 1) {
|
|
88
|
+
const fieldName = typeState.fieldNames[index];
|
|
89
|
+
if (fieldName in RecordValue.prototype) {
|
|
90
|
+
fail(`record field ${fieldName} conflicts with a record member`);
|
|
91
|
+
}
|
|
92
|
+
const fieldKey = typeState.fieldKeys[index];
|
|
93
|
+
Object.defineProperty(RecordValue.prototype, fieldName, {
|
|
94
|
+
configurable: false,
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get() {
|
|
97
|
+
return this[RECORD_STATE].map.get(fieldKey);
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function makeInitialMap(typeState, values) {
|
|
104
|
+
let map = EMPTY_MAP;
|
|
105
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
106
|
+
map = map.assoc(typeState.fieldKeys[index], values[index]);
|
|
107
|
+
}
|
|
108
|
+
return map;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function normalizeSourceMap(typeState, source) {
|
|
112
|
+
let map;
|
|
113
|
+
try {
|
|
114
|
+
map = PersistentHashMap.from(source);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw new TypeError(
|
|
117
|
+
`map->${typeState.name} expects an iterable collection of entries`,
|
|
118
|
+
{ cause: error },
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
for (const fieldKey of typeState.fieldKeys) {
|
|
122
|
+
if (!map.has(fieldKey)) map = map.assoc(fieldKey, null);
|
|
123
|
+
}
|
|
124
|
+
return map;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function defineRecordType(name, fields) {
|
|
128
|
+
const typeName = normalizeTypeName(name);
|
|
129
|
+
const fieldNames = normalizeFieldNames(fields);
|
|
130
|
+
const fieldKeys = Object.freeze(fieldNames.map((field) => keyword(field)));
|
|
131
|
+
const fieldIndexes = new Map(
|
|
132
|
+
fieldNames.map((field, index) => [field, index]),
|
|
133
|
+
);
|
|
134
|
+
const typeState = Object.freeze({
|
|
135
|
+
name: typeName,
|
|
136
|
+
fieldNames,
|
|
137
|
+
fieldKeys,
|
|
138
|
+
fieldIndexes,
|
|
139
|
+
signature: `${typeName}[${fieldNames.join(",")}]`,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
class RecordValue {
|
|
143
|
+
constructor(token, map, metadata = null) {
|
|
144
|
+
if (token !== RECORD_CONSTRUCTOR_TOKEN ||
|
|
145
|
+
!(map instanceof PersistentHashMap)) {
|
|
146
|
+
fail(`${typeName} values must be created with its record constructors`);
|
|
147
|
+
}
|
|
148
|
+
this[RECORD_STATE] = Object.freeze({ map, metadata });
|
|
149
|
+
Object.freeze(this);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
static create(...values) {
|
|
153
|
+
if (values.length !== fieldNames.length) {
|
|
154
|
+
fail(
|
|
155
|
+
`->${typeName} expects ${fieldNames.length} values, received ${values.length}`,
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
return new RecordValue(
|
|
159
|
+
RECORD_CONSTRUCTOR_TOKEN,
|
|
160
|
+
makeInitialMap(typeState, values),
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static fromMap(source) {
|
|
165
|
+
return new RecordValue(
|
|
166
|
+
RECORD_CONSTRUCTOR_TOKEN,
|
|
167
|
+
normalizeSourceMap(typeState, source),
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static isInstance(value) {
|
|
172
|
+
return value instanceof RecordValue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
get count() {
|
|
176
|
+
return this[RECORD_STATE].map.count;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
get size() {
|
|
180
|
+
return this.count;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
get(key, notFound = null) {
|
|
184
|
+
return this[RECORD_STATE].map.get(key, notFound);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
has(key) {
|
|
188
|
+
return this[RECORD_STATE].map.has(key);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
assoc(key, value) {
|
|
192
|
+
const state = this[RECORD_STATE];
|
|
193
|
+
const map = state.map.assoc(key, value);
|
|
194
|
+
return map === state.map
|
|
195
|
+
? this
|
|
196
|
+
: new RecordValue(RECORD_CONSTRUCTOR_TOKEN, map, state.metadata);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
dissoc(key) {
|
|
200
|
+
const state = this[RECORD_STATE];
|
|
201
|
+
if (declaredFieldIndex(typeState, key) >= 0) {
|
|
202
|
+
return mapWithMetadata(state.map.dissoc(key), state.metadata);
|
|
203
|
+
}
|
|
204
|
+
const map = state.map.dissoc(key);
|
|
205
|
+
return map === state.map
|
|
206
|
+
? this
|
|
207
|
+
: new RecordValue(RECORD_CONSTRUCTOR_TOKEN, map, state.metadata);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
entries() {
|
|
211
|
+
return this[RECORD_STATE].map.entries();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
keys() {
|
|
215
|
+
return this[RECORD_STATE].map.keys();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
values() {
|
|
219
|
+
return this[RECORD_STATE].map.values();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
toPersistentMap() {
|
|
223
|
+
return mapWithMetadata(
|
|
224
|
+
this[RECORD_STATE].map,
|
|
225
|
+
this[RECORD_STATE].metadata,
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
[COLLECTION_COUNT]() {
|
|
230
|
+
return this.count;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
[COLLECTION_EMPTY]() {
|
|
234
|
+
return mapWithMetadata(EMPTY_MAP, this[RECORD_STATE].metadata);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
[COLLECTION_CONJ](entry) {
|
|
238
|
+
const [key, value] = readCollectionEntry(entry);
|
|
239
|
+
return this.assoc(key, value);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
[COLLECTION_GET](key, notFound = null) {
|
|
243
|
+
return this.get(key, notFound);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
[COLLECTION_ASSOC](key, value) {
|
|
247
|
+
return this.assoc(key, value);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
[COLLECTION_CONTAINS](key) {
|
|
251
|
+
return this.has(key);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
[COLLECTION_DISSOC](key) {
|
|
255
|
+
return this.dissoc(key);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
[COLLECTION_SEQ]() {
|
|
259
|
+
return this.count === 0
|
|
260
|
+
? null
|
|
261
|
+
: sequenceView(() => this.entries(), this.count);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
[COLLECTION_REDUCE](reducer, ...initial) {
|
|
265
|
+
return reduceIterable(this, reducer, ...initial);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
[COLLECTION_REDUCE_KV](reducer, initial) {
|
|
269
|
+
let result = initial;
|
|
270
|
+
for (const [key, value] of this.entries()) {
|
|
271
|
+
result = reducer(result, key, value);
|
|
272
|
+
if (isReducedValue(result)) return unreducedValue(result);
|
|
273
|
+
}
|
|
274
|
+
return result;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
[VALUE_EQUAL](other, equal) {
|
|
278
|
+
return other instanceof RecordValue &&
|
|
279
|
+
equal(this[RECORD_STATE].map, other[RECORD_STATE].map);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
[VALUE_HASH](hash) {
|
|
283
|
+
return finishHash(
|
|
284
|
+
mixHash(
|
|
285
|
+
mixHash(RECORD_HASH_TAG, hashString(typeState.signature)),
|
|
286
|
+
hash(this[RECORD_STATE].map),
|
|
287
|
+
),
|
|
288
|
+
this.count,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
[METADATA_READ]() {
|
|
293
|
+
return this[RECORD_STATE].metadata;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
[METADATA_WITH](metadata) {
|
|
297
|
+
const state = this[RECORD_STATE];
|
|
298
|
+
return metadata === state.metadata
|
|
299
|
+
? this
|
|
300
|
+
: new RecordValue(RECORD_CONSTRUCTOR_TOKEN, state.map, metadata);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
[Symbol.iterator]() {
|
|
304
|
+
return this.entries();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
get [Symbol.toStringTag]() {
|
|
308
|
+
return `EliscriptRecord:${typeName}`;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
Object.defineProperty(RecordValue, "name", { value: typeName });
|
|
313
|
+
Object.defineProperties(RecordValue, {
|
|
314
|
+
recordName: { enumerable: true, value: typeName },
|
|
315
|
+
recordFields: { enumerable: true, value: fieldNames },
|
|
316
|
+
});
|
|
317
|
+
defineFieldAccessors(RecordValue, typeState);
|
|
318
|
+
Object.freeze(RecordValue.prototype);
|
|
319
|
+
Object.freeze(RecordValue);
|
|
320
|
+
recordTypes.add(RecordValue);
|
|
321
|
+
return RecordValue;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export function isRecordType(value) {
|
|
325
|
+
return typeof value === "function" && recordTypes.has(value);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
export function isRecord(value) {
|
|
329
|
+
return value !== null && typeof value === "object" &&
|
|
330
|
+
isRecordType(value.constructor);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function recordType(value) {
|
|
334
|
+
if (!isRecord(value)) fail("expected an Eliscript record");
|
|
335
|
+
return value.constructor;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export function recordTypeName(value) {
|
|
339
|
+
const type = isRecordType(value) ? value : recordType(value);
|
|
340
|
+
return type.recordName;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
export function recordFieldNames(value) {
|
|
344
|
+
const type = isRecordType(value) ? value : recordType(value);
|
|
345
|
+
return type.recordFields;
|
|
346
|
+
}
|