eliscript 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +13 -11
  2. package/bootstrap/README.md +13 -8
  3. package/bootstrap/host/build.eli +2 -0
  4. package/bootstrap/host/bun.eli +2 -0
  5. package/bootstrap/host/check-cli-legacy.eli +2 -0
  6. package/bootstrap/host/evaluation-cli.eli +2 -0
  7. package/bootstrap/host/evaluation.eli +2 -0
  8. package/bootstrap/host/format-cli.eli +2 -0
  9. package/bootstrap/host/project-cli-legacy.eli +2 -0
  10. package/bootstrap/host/project-legacy.eli +2 -0
  11. package/bootstrap/host/watch-cli.eli +2 -0
  12. package/bootstrap/host/watch.eli +2 -0
  13. package/browser/README.md +33 -0
  14. package/browser/host.eli +2 -0
  15. package/browser/host.mjs +643 -11
  16. package/browser/worker.eli +2 -0
  17. package/browser/worker.mjs +6 -3
  18. package/dist/browser/host.mjs +643 -11
  19. package/dist/browser/worker.mjs +6 -3
  20. package/dist/compiler-artifact.json +1 -1
  21. package/dist/literal-sources/runtime/core/collection.eli +76 -16
  22. package/dist/literal-sources/runtime/core/metadata.eli +25 -8
  23. package/dist/literal-sources/runtime/core/vector.eli +7 -2
  24. package/dist/literal-sources/stdlib/managed-literal-protocols.eli +18 -7
  25. package/dist/literal-sources/stdlib/managed-literals.eli +121 -66
  26. package/dist/stage3-release.json +1 -1
  27. package/package.json +11 -2
  28. package/platform/README.md +33 -12
  29. package/platform/browser.eli +2 -0
  30. package/platform/http.eli +2 -0
  31. package/platform/worker.eli +2 -0
  32. package/runtime/README.md +34 -14
  33. package/runtime/array-text.eli +2 -0
  34. package/runtime/core/collection-internals.eli +2 -0
  35. package/runtime/core/collection.eli +2 -0
  36. package/runtime/core/collection.mjs +99 -17
  37. package/runtime/core/data-text.eli +2 -0
  38. package/runtime/core/data.eli +2 -0
  39. package/runtime/core/data.mjs +7 -5
  40. package/runtime/core/identifier.eli +2 -0
  41. package/runtime/core/lazy-sequence.eli +2 -0
  42. package/runtime/core/list-internals.eli +2 -0
  43. package/runtime/core/list.eli +2 -0
  44. package/runtime/core/list.mjs +3 -0
  45. package/runtime/core/map-internals.eli +2 -0
  46. package/runtime/core/map.eli +2 -0
  47. package/runtime/core/map.mjs +5 -0
  48. package/runtime/core/metadata-internals.eli +2 -0
  49. package/runtime/core/metadata.eli +2 -0
  50. package/runtime/core/metadata.mjs +14 -0
  51. package/runtime/core/object.eli +2 -0
  52. package/runtime/core/order.eli +2 -0
  53. package/runtime/core/order.mjs +35 -6
  54. package/runtime/core/persistent-kind.eli +2 -0
  55. package/runtime/core/persistent-kind.mjs +6 -0
  56. package/runtime/core/protocol-error.eli +2 -0
  57. package/runtime/core/protocol.eli +2 -0
  58. package/runtime/core/queue-internals.eli +2 -0
  59. package/runtime/core/queue.eli +2 -0
  60. package/runtime/core/queue.mjs +3 -0
  61. package/runtime/core/record.eli +2 -0
  62. package/runtime/core/regex.eli +2 -0
  63. package/runtime/core/sequence.eli +2 -0
  64. package/runtime/core/sequence.mjs +75 -13
  65. package/runtime/core/set-algebra.eli +2 -0
  66. package/runtime/core/set-internals.eli +2 -0
  67. package/runtime/core/set.eli +2 -0
  68. package/runtime/core/sorted-internals.eli +2 -0
  69. package/runtime/core/sorted.eli +2 -0
  70. package/runtime/core/text.eli +2 -0
  71. package/runtime/core/transducer.eli +2 -0
  72. package/runtime/core/transient-internals.eli +2 -0
  73. package/runtime/core/transient.eli +2 -0
  74. package/runtime/core/transient.mjs +59 -10
  75. package/runtime/core/truth.eli +2 -0
  76. package/runtime/core/type.eli +2 -0
  77. package/runtime/core/value-internals.eli +2 -0
  78. package/runtime/core/value.eli +2 -0
  79. package/runtime/core/vector-internals.eli +2 -0
  80. package/runtime/core/vector-internals.mjs +3 -6
  81. package/runtime/core/vector.eli +2 -0
  82. package/runtime/core/vector.mjs +125 -7
  83. package/runtime/literals.eli +2 -0
  84. package/runtime/number-radix.eli +2 -0
  85. package/runtime/source-mapping.eli +2 -0
  86. package/runtime/testing/list.eli +2 -0
  87. package/runtime/testing/map.eli +2 -0
  88. package/runtime/testing/queue.eli +2 -0
  89. package/runtime/testing/set.eli +2 -0
  90. package/runtime/testing/sorted.eli +2 -0
  91. package/runtime/testing/value.eli +2 -0
  92. package/runtime/testing/vector.eli +2 -0
  93. package/runtime/wire/http-rpc.eli +2 -0
  94. package/runtime/wire/markdown.eli +2 -0
  95. package/runtime/wire/schema.eli +2 -0
  96. package/runtime/worker-value-codec.eli +2 -0
  97. package/runtime/worker-value-stream.eli +2 -0
  98. package/runtime/worker.eli +2 -0
  99. package/stdlib/README.md +2 -2
  100. package/stdlib/core/order.eli +36 -10
  101. package/stdlib/core/seq.eli +104 -75
  102. package/stdlib/core/zipper.eli +274 -101
  103. package/stdlib/managed-literal-protocols.eli +18 -7
  104. package/stdlib/managed-literals.eli +121 -66
  105. package/stdlib/managed-runtime-collection.eli +76 -16
  106. package/stdlib/managed-runtime-metadata.eli +25 -8
  107. package/stdlib/managed-runtime-order.eli +32 -10
  108. package/stdlib/managed-runtime-sequence.eli +69 -28
  109. package/stdlib/managed-runtime-vector.eli +7 -2
  110. package/tools/acceptance/check.eli +13 -3
  111. package/tools/backends/compiler-build.mjs +140 -140
  112. package/tools/backends/dist/release-cli.mjs +2 -0
  113. package/tools/backends/dist/release-cli.mjs.map +1 -1
  114. package/tools/backends/stage3-targets.mjs +121 -114
  115. package/bootstrap/host/eliscript-cli-legacy.mjs +0 -37
@@ -3,6 +3,9 @@
3
3
  extend-protocol-category extend-protocol-type implements-protocol? protocol-method)
4
4
  (import "../../stdlib/managed-literal-protocols.eli"
5
5
  I_COUNTED I_EMPTYABLE I_CONJ I_INDEXED I_LOOKUP I_ASSOCIATIVE I_REDUCE I_KV_REDUCE I_MAP I_SET I_STACK I_REVERSIBLE I_SEQABLE
6
+ COLLECTION_COUNT COLLECTION_EMPTY COLLECTION_CONJ COLLECTION_GET COLLECTION_ASSOC
7
+ COLLECTION_CONTAINS COLLECTION_NTH COLLECTION_SEQ COLLECTION_REDUCE COLLECTION_REDUCE_KV COLLECTION_DISSOC COLLECTION_DISJ
8
+ COLLECTION_PEEK COLLECTION_POP COLLECTION_RSEQ
6
9
  managed-literal-is-reduced managed-literal-reduced managed-literal-unreduced
7
10
  managed-literal-reduce-iterable managed-literal-sequence-view managed-literal-unbounded-sequence-view
8
11
  managed-literal-install-slot managed-literal-checked-count ManagedLiteralSequenceView
@@ -478,7 +481,30 @@
478
481
  (extend-protocol-category IStack "null" (js-object :peek (lambda (&rest ignored) nil) :pop (lambda (&rest ignored) nil)))
479
482
  (extend-protocol-category IReversible "null" (js-object :rseq (lambda (&rest ignored) nil)))
480
483
 
481
- (defun count (collection) (managed-literal-checked-count (invoke ICounted "count" collection)))
484
+ (defconst count-method (protocol-method ICounted "count"))
485
+ (defconst empty-method (protocol-method IEmptyable "empty"))
486
+ (defconst conj-method (protocol-method IConj "conj"))
487
+ (defconst get-method (protocol-method ILookup "get"))
488
+ (defconst assoc-method (protocol-method IAssociative "assoc"))
489
+ (defconst contains-method (protocol-method IAssociative "contains"))
490
+ (defconst nth-method (protocol-method IIndexed "nth"))
491
+ (defconst seq-method (protocol-method ISeqable "seq"))
492
+ (defconst reduce-method (protocol-method IReduce "reduce"))
493
+ (defconst reduce-kv-method (protocol-method IKVReduce "reduceKV"))
494
+ (defconst dissoc-method (protocol-method IMap "dissoc"))
495
+ (defconst disj-method (protocol-method ISet "disj"))
496
+ (defconst peek-method (protocol-method IStack "peek"))
497
+ (defconst pop-method (protocol-method IStack "pop"))
498
+ (defconst rseq-method (protocol-method IReversible "rseq"))
499
+
500
+ (defun directSlot (value slot)
501
+ (when (and (not (nullish? value))
502
+ (or (= (js* "typeof value") "object") (= (js* "typeof value") "function")))
503
+ (let ((method (aref value slot)))
504
+ (when (= (js* "typeof method") "function") method))))
505
+ (defun count (collection)
506
+ (let ((slot (directSlot collection COLLECTION_COUNT)))
507
+ (managed-literal-checked-count (if (not (nullish? slot)) (js-call slot :call collection) (funcall count-method collection)))))
482
508
  (defun isCounted (collection) (implements-protocol? ICounted collection))
483
509
  (defun isIndexed (collection) (implements-protocol? IIndexed collection))
484
510
  (defun isSeqable (collection) (implements-protocol? ISeqable collection))
@@ -516,50 +542,84 @@
516
542
  (let ((value (aref values index)))
517
543
  (if (truth (js-call seen :has value)) (setq distinct false) (setq seen (js-call seen :conj value))))
518
544
  (setq index (1+ index))) distinct))
519
- (defun empty (collection) (invoke IEmptyable "empty" collection))
545
+ (defun empty (collection)
546
+ (let ((slot (directSlot collection COLLECTION_EMPTY)))
547
+ (if (not (nullish? slot)) (js-call slot :call collection) (funcall empty-method collection))))
520
548
  (defun conj (collection &rest values)
521
549
  (let ((result collection) (index 0))
522
- (while (< index (js-length values)) (setq result (invoke IConj "conj" result (aref values index)) index (1+ index))) result))
550
+ (while (< index (js-length values))
551
+ (let ((slot (directSlot result COLLECTION_CONJ)) (value (aref values index)))
552
+ (setq result (if (not (nullish? slot)) (js-call slot :call result value) (funcall conj-method result value))
553
+ index (1+ index))))
554
+ result))
523
555
  (defun get (collection key &optional not-found)
524
- (invoke ILookup "get" collection key (if (undefined? not-found) nil not-found)))
556
+ (let ((slot (directSlot collection COLLECTION_GET)) (fallback (if (undefined? not-found) nil not-found)))
557
+ (if (not (nullish? slot)) (js-call slot :call collection key fallback) (funcall get-method collection key fallback))))
525
558
  (defun assoc (&rest arguments)
526
559
  (unless (and (>= (js-length arguments) 3) (= (% (- (js-length arguments) 1) 2) 0))
527
560
  (fail "assoc requires a collection followed by one or more key/value pairs"))
528
561
  (let ((result (aref arguments 0)) (index 1))
529
562
  (while (< index (js-length arguments))
530
- (setq result (invoke IAssociative "assoc" result (aref arguments index) (aref arguments (1+ index))) index (+ index 2))) result))
563
+ (let ((slot (directSlot result COLLECTION_ASSOC))
564
+ (key (aref arguments index)) (value (aref arguments (1+ index))))
565
+ (setq result (if (not (nullish? slot)) (js-call slot :call result key value) (funcall assoc-method result key value))
566
+ index (+ index 2)))) result))
531
567
  (defun contains (collection key)
532
- (let ((result (invoke IAssociative "contains" collection key)))
568
+ (let* ((slot (directSlot collection COLLECTION_CONTAINS))
569
+ (result (if (not (nullish? slot)) (js-call slot :call collection key) (funcall contains-method collection key))))
533
570
  (unless (= (js* "typeof result") "boolean") (fail "IAssociative/contains must return a boolean")) result))
534
571
  (defun nth (collection index &rest not-found)
535
- (if (= (js-length not-found) 0) (invoke IIndexed "nth" collection index) (invoke IIndexed "nth" collection index (aref not-found 0))))
572
+ (let ((slot (directSlot collection COLLECTION_NTH)))
573
+ (if (= (js-length not-found) 0)
574
+ (if (not (nullish? slot)) (js-call slot :call collection index) (funcall nth-method collection index))
575
+ (if (not (nullish? slot)) (js-call slot :call collection index (aref not-found 0)) (funcall nth-method collection index (aref not-found 0))))))
536
576
  (defun checkedSequence (result message)
537
577
  (if (nil? result) nil
538
578
  (progn (when (or (undefined? result) (not (callable? (aref result (aref globalThis/Symbol "iterator"))))) (fail message)) result)))
539
- (defun seq (collection) (checkedSequence (invoke ISeqable "seq" collection) "ISeqable/seq must return null or an iterable sequence view"))
579
+ (defun seq (collection)
580
+ (let ((slot (directSlot collection COLLECTION_SEQ)))
581
+ (checkedSequence (if (not (nullish? slot)) (js-call slot :call collection) (funcall seq-method collection))
582
+ "ISeqable/seq must return null or an iterable sequence view")))
540
583
  (defun isEmpty (collection) (nil? (seq collection)))
541
584
  (defun notEmpty (collection) (if (isEmpty collection) nil collection))
542
585
  (defun reduce (collection reducer &rest initial)
543
586
  (unless (callable? reducer) (fail "collection reducer must be a function"))
544
- (managed-literal-unreduced (if (= (js-length initial) 0)
545
- (invoke IReduce "reduce" collection reducer) (invoke IReduce "reduce" collection reducer (aref initial 0)))))
587
+ (let ((slot (directSlot collection COLLECTION_REDUCE)))
588
+ (managed-literal-unreduced (if (= (js-length initial) 0)
589
+ (if (not (nullish? slot)) (js-call slot :call collection reducer) (funcall reduce-method collection reducer))
590
+ (if (not (nullish? slot)) (js-call slot :call collection reducer (aref initial 0)) (funcall reduce-method collection reducer (aref initial 0)))))))
546
591
  (defun reduceKV (&rest arguments)
547
592
  (unless (= (js-length arguments) 3) (fail "reduceKV requires a collection, reducer, and initial value"))
548
593
  (unless (callable? (aref arguments 1)) (fail "key/value reducer must be a function"))
549
- (managed-literal-unreduced (invoke IKVReduce "reduceKV" (aref arguments 0) (aref arguments 1) (aref arguments 2))))
594
+ (let* ((collection (aref arguments 0)) (slot (directSlot collection COLLECTION_REDUCE_KV)))
595
+ (managed-literal-unreduced (if (not (nullish? slot))
596
+ (js-call slot :call collection (aref arguments 1) (aref arguments 2))
597
+ (funcall reduce-kv-method collection (aref arguments 1) (aref arguments 2))))))
550
598
  (defun dissoc (collection &rest keys)
551
599
  (let ((result collection) (index 0))
552
- (while (< index (js-length keys)) (setq result (invoke IMap "dissoc" result (aref keys index)) index (1+ index))) result))
600
+ (while (< index (js-length keys))
601
+ (let ((slot (directSlot result COLLECTION_DISSOC)) (key (aref keys index)))
602
+ (setq result (if (not (nullish? slot)) (js-call slot :call result key) (funcall dissoc-method result key))
603
+ index (1+ index)))) result))
553
604
  (defun disj (collection &rest values)
554
605
  (let ((result collection) (index 0))
555
- (while (< index (js-length values)) (setq result (invoke ISet "disj" result (aref values index)) index (1+ index))) result))
606
+ (while (< index (js-length values))
607
+ (let ((slot (directSlot result COLLECTION_DISJ)) (value (aref values index)))
608
+ (setq result (if (not (nullish? slot)) (js-call slot :call result value) (funcall disj-method result value))
609
+ index (1+ index)))) result))
556
610
  (defun peek (&rest arguments)
557
- (unless (= (js-length arguments) 1) (fail "peek requires exactly one collection")) (invoke IStack "peek" (aref arguments 0)))
611
+ (unless (= (js-length arguments) 1) (fail "peek requires exactly one collection"))
612
+ (let* ((collection (aref arguments 0)) (slot (directSlot collection COLLECTION_PEEK)))
613
+ (if (not (nullish? slot)) (js-call slot :call collection) (funcall peek-method collection))))
558
614
  (defun pop (&rest arguments)
559
- (unless (= (js-length arguments) 1) (fail "pop requires exactly one collection")) (invoke IStack "pop" (aref arguments 0)))
615
+ (unless (= (js-length arguments) 1) (fail "pop requires exactly one collection"))
616
+ (let* ((collection (aref arguments 0)) (slot (directSlot collection COLLECTION_POP)))
617
+ (if (not (nullish? slot)) (js-call slot :call collection) (funcall pop-method collection))))
560
618
  (defun rseq (&rest arguments)
561
619
  (unless (= (js-length arguments) 1) (fail "rseq requires exactly one collection"))
562
- (checkedSequence (invoke IReversible "rseq" (aref arguments 0)) "IReversible/rseq must return null or an iterable sequence view"))
620
+ (let* ((collection (aref arguments 0)) (slot (directSlot collection COLLECTION_RSEQ)))
621
+ (checkedSequence (if (not (nullish? slot)) (js-call slot :call collection) (funcall rseq-method collection))
622
+ "IReversible/rseq must return null or an iterable sequence view")))
563
623
  (defun reduced (value) (managed-literal-reduced value))
564
624
  (defun isReduced (value) (managed-literal-is-reduced value))
565
625
  (defun unreduced (value) (managed-literal-unreduced value))
@@ -1,21 +1,38 @@
1
1
  (module eliscript.managed-runtime-metadata
2
2
  (import "./map.mjs" isPersistentHashMap)
3
3
  (import "../../stdlib/core/protocol.eli" implements-protocol-operation? protocol-method)
4
- (import "../../stdlib/managed-literal-protocols.eli" I_META I_WITH_META)
4
+ (import "../../stdlib/managed-literal-protocols.eli" I_META I_WITH_META METADATA_READ METADATA_WITH)
5
5
  (defconst IMeta I_META)
6
6
  (defconst IWithMeta I_WITH_META)
7
+ (defconst meta-method (protocol-method IMeta "meta"))
8
+ (defconst with-meta-method (protocol-method IWithMeta "withMeta"))
9
+ (defun directMetaSlot (value slot)
10
+ (when (and (not (nullish? value))
11
+ (or (= (js* "typeof value") "object") (= (js* "typeof value") "function")))
12
+ (let ((method (aref value slot)))
13
+ (when (= (js* "typeof method") "function") method))))
7
14
  (defun supportsMetadata (value)
8
- (and (implements-protocol-operation? IMeta "meta" value)
9
- (implements-protocol-operation? IWithMeta "withMeta" value)))
15
+ (if (and (not (nullish? (directMetaSlot value METADATA_READ)))
16
+ (not (nullish? (directMetaSlot value METADATA_WITH))))
17
+ t
18
+ (and (implements-protocol-operation? IMeta "meta" value)
19
+ (implements-protocol-operation? IWithMeta "withMeta" value))))
10
20
  (defun meta (value)
11
- (if (implements-protocol-operation? IMeta "meta" value)
12
- (funcall (protocol-method IMeta "meta") value) nil))
21
+ (let ((slot (directMetaSlot value METADATA_READ)))
22
+ (if (not (nullish? slot))
23
+ (js-call slot :call value)
24
+ (if (implements-protocol-operation? IMeta "meta" value)
25
+ (funcall meta-method value) nil))))
13
26
  (defun withMeta (value metadata)
14
27
  (unless (or (nil? metadata) (isPersistentHashMap metadata))
15
28
  (throw (new globalThis/TypeError "metadata must be null or a persistent hash map")))
16
- (unless (implements-protocol-operation? IWithMeta "withMeta" value)
17
- (throw (new globalThis/TypeError "value does not support metadata")))
18
- (funcall (protocol-method IWithMeta "withMeta") value metadata))
29
+ (let ((slot (directMetaSlot value METADATA_WITH)))
30
+ (if (not (nullish? slot))
31
+ (js-call slot :call value metadata)
32
+ (progn
33
+ (unless (implements-protocol-operation? IWithMeta "withMeta" value)
34
+ (throw (new globalThis/TypeError "value does not support metadata")))
35
+ (funcall with-meta-method value metadata)))))
19
36
  (defun varyMeta (value transform &rest arguments)
20
37
  (unless (= (js* "typeof transform") "function")
21
38
  (throw (new globalThis/TypeError "metadata transform must be a function")))
@@ -1,10 +1,15 @@
1
1
  (module eliscript.managed-runtime-vector
2
2
  (import "../../stdlib/managed-literals.eli"
3
- PersistentVector literal-vector literal-subvec literal-instance? literal-type literal-export-value)
3
+ PersistentVector literal-vector literal-vector-from-array-values literal-subvec literal-instance? literal-type literal-export-value)
4
4
 
5
5
  (defconst EMPTY_VECTOR (literal-vector))
6
6
  (defconst view-states (new globalThis/WeakMap))
7
- (defun persistentVector (&rest values) (js-call PersistentVector :from values))
7
+ (defconst freeze Object/freeze)
8
+ (defun persistentVector (&rest values)
9
+ (if (= (js-length values) 0) EMPTY_VECTOR
10
+ (progn
11
+ (when (<= (js-length values) 32) (funcall freeze values))
12
+ (literal-vector-from-array-values values))))
8
13
  (defun isPersistentVector (value) (literal-instance? value PersistentVector))
9
14
  (defun subvec (vector start &optional end) (literal-subvec vector start end))
10
15
  (defun readPersistentVectorView (value)
@@ -109,16 +109,27 @@
109
109
  ((or (nil? left) (nil? right)
110
110
  (and (/= (js* "typeof left") "object") (/= (js* "typeof left") "function"))
111
111
  (and (/= (js* "typeof right") "object") (/= (js* "typeof right") "function"))) false)
112
- ((and (implements-protocol-operation? I_EQUIV "equal" left)
113
- (implements-protocol-operation? I_EQUIV "equal" right))
114
- (eq (funcall (protocol-method I_EQUIV "equal") left right managed-literal-equal-values) t))
115
- (t false)))
112
+ (t
113
+ (let ((left-slot (aref left VALUE_EQUAL))
114
+ (right-slot (aref right VALUE_EQUAL)))
115
+ (if (and (= (js* "typeof left-slot") "function")
116
+ (or (= (js* "typeof right-slot") "function")
117
+ (implements-protocol-operation? I_EQUIV "equal" right)))
118
+ (eq (js-call left-slot :call left right managed-literal-equal-values) t)
119
+ (if (and (implements-protocol-operation? I_EQUIV "equal" left)
120
+ (implements-protocol-operation? I_EQUIV "equal" right))
121
+ (eq (funcall (protocol-method I_EQUIV "equal") left right managed-literal-equal-values) t)
122
+ false))))))
116
123
  (defun managed-literal-hash-value (value)
117
124
  (recordHashValueCall)
118
125
  (if (and (not (nil? value))
119
- (or (= (js* "typeof value") "object") (= (js* "typeof value") "function"))
120
- (implements-protocol-operation? I_HASH "hash" value))
121
- (cachedProtocolHash value (lambda () (funcall (protocol-method I_HASH "hash") value managed-literal-hash-value)))
126
+ (or (= (js* "typeof value") "object") (= (js* "typeof value") "function")))
127
+ (let ((slot (aref value VALUE_HASH)))
128
+ (if (= (js* "typeof slot") "function")
129
+ (cachedProtocolHash value (lambda () (js-call slot :call value managed-literal-hash-value)))
130
+ (if (implements-protocol-operation? I_HASH "hash" value)
131
+ (cachedProtocolHash value (lambda () (funcall (protocol-method I_HASH "hash") value managed-literal-hash-value)))
132
+ (hashPrimitiveValue value))))
122
133
  (hashPrimitiveValue value)))
123
134
  (defun managed-literal-is-reduced (value)
124
135
  (and (not (nullish? value))
@@ -57,7 +57,28 @@
57
57
  (defconst literal-prototypes-ready (js-object))
58
58
  (defconst literal-keywords (new globalThis/Map))
59
59
  (defconst literal-transports (new globalThis/Map))
60
- (defconst literal-token (Object/freeze (js-object)))
60
+ (defconst literal-object-freeze Object/freeze)
61
+ (defconst literal-object-is-frozen Object/isFrozen)
62
+ (defconst literal-object-has-own Object/hasOwn)
63
+ (defconst literal-object-create Object/create)
64
+ (defconst literal-object-get-prototype-of Object/getPrototypeOf)
65
+ (defconst literal-object-get-own-property-descriptor Object/getOwnPropertyDescriptor)
66
+ (defconst literal-object-prototype (get globalThis/Object :prototype))
67
+ (defconst literal-array-is-array Array/isArray)
68
+ (defconst literal-symbol-iterator (get globalThis/Symbol :iterator))
69
+ (defconst literal-symbol-has-instance (get globalThis/Symbol :hasInstance))
70
+ (defconst literal-symbol-to-string-tag (get globalThis/Symbol :toStringTag))
71
+ (defconst literal-data-descriptor
72
+ (let ((descriptor (funcall literal-object-create nil)))
73
+ (put descriptor :value undefined)
74
+ (put descriptor :writable t)
75
+ (put descriptor :enumerable t)
76
+ (put descriptor :configurable t)
77
+ descriptor))
78
+ (defvar literal-has-transients false)
79
+ (defun literal-node-owner (node)
80
+ (if literal-has-transients (get node :owner nil) nil))
81
+ (defconst literal-token (funcall literal-object-freeze (js-object)))
61
82
  (defconst literal-missing (funcall globalThis/Symbol "eliscript.collection.missing"))
62
83
  (defconst literal-value-type (js-call globalThis/Symbol :for "eliscript.value.type"))
63
84
  (defconst literal-list-state (funcall globalThis/Symbol "eliscript.list.state"))
@@ -71,7 +92,10 @@
71
92
  (defun literal-fail (message) (throw (new globalThis/TypeError message)))
72
93
  (defun literal-range (message) (throw (new globalThis/RangeError message)))
73
94
  (defun literal-own-data (target key value)
74
- (Object/defineProperty target key (js-object :value value :writable t :enumerable t :configurable t)) target)
95
+ (put literal-data-descriptor :value value)
96
+ (Reflect/defineProperty target key literal-data-descriptor)
97
+ (put literal-data-descriptor :value undefined)
98
+ target)
75
99
  (defun literal-install-method (prototype key name arity implementation)
76
100
  (let ((method (literal-make-method implementation name)))
77
101
  (Object/defineProperty method "length" (js-object :value arity :configurable t))
@@ -108,27 +132,27 @@
108
132
  (literal-fail "PersistentList values must be created with persistentList or PersistentList.from"))
109
133
  (let ((receiver (js* "this")))
110
134
  (literal-own-data receiver literal-list-state
111
- (Object/freeze (js-object :count count :empty empty :value value :rest rest :metadata metadata)))
112
- (Object/freeze receiver)))
135
+ (funcall literal-object-freeze (js-object :count count :empty empty :value value :rest rest :metadata metadata)))
136
+ (funcall literal-object-freeze receiver)))
113
137
  (defun PersistentVector (token count shift root tail &optional metadata)
114
138
  (when (not (eq token literal-token))
115
139
  (literal-fail "PersistentVector values must be created with persistentVector or PersistentVector.from"))
116
140
  (let ((receiver (js* "this")))
117
141
  (literal-own-data receiver literal-vector-state
118
- (Object/freeze (js-object :count count :shift shift :root root :tail tail :metadata metadata)))
119
- (Object/freeze receiver)))
142
+ (funcall literal-object-freeze (js-object :count count :shift shift :root root :tail tail :metadata metadata)))
143
+ (funcall literal-object-freeze receiver)))
120
144
  (defun PersistentHashMap (token count root &optional metadata)
121
145
  (when (not (eq token literal-token))
122
146
  (literal-fail "PersistentHashMap values must be created with persistentHashMap or PersistentHashMap.from"))
123
147
  (let ((receiver (js* "this")))
124
- (literal-own-data receiver literal-map-state (Object/freeze (js-object :count count :root root :metadata metadata)))
125
- (Object/freeze receiver)))
148
+ (literal-own-data receiver literal-map-state (funcall literal-object-freeze (js-object :count count :root root :metadata metadata)))
149
+ (funcall literal-object-freeze receiver)))
126
150
  (defun PersistentHashSet (token map &optional metadata)
127
151
  (when (or (not (eq token literal-token)) (not (= (literal-type map) 3)))
128
152
  (literal-fail "PersistentHashSet values must be created with persistentHashSet or PersistentHashSet.from"))
129
153
  (let ((receiver (js* "this")))
130
- (literal-own-data receiver literal-set-state (Object/freeze (js-object :map map :metadata metadata)))
131
- (Object/freeze receiver)))
154
+ (literal-own-data receiver literal-set-state (funcall literal-object-freeze (js-object :map map :metadata metadata)))
155
+ (funcall literal-object-freeze receiver)))
132
156
  (defun PersistentQueue (token count front rear &optional metadata)
133
157
  (when (not (eq token literal-token))
134
158
  (literal-fail "PersistentQueue values must be created with persistentQueue or PersistentQueue.from"))
@@ -140,38 +164,38 @@
140
164
  (literal-fail "invalid persistent queue state"))
141
165
  (let ((receiver (js* "this")))
142
166
  (literal-own-data receiver literal-queue-state
143
- (Object/freeze (js-object :count count :front front :rear rear :metadata metadata)))
144
- (Object/freeze receiver)))
167
+ (funcall literal-object-freeze (js-object :count count :front front :rear rear :metadata metadata)))
168
+ (funcall literal-object-freeze receiver)))
145
169
  (defun VectorNode (&optional slots owner)
146
170
  (let ((receiver (js* "this")))
147
171
  (put receiver :owner owner) (put receiver :slots (if (nil? slots) (js-array) slots))
148
- (when (nil? owner) (Object/freeze (get receiver :slots)) (Object/freeze receiver)) receiver))
172
+ (when (nil? owner) (funcall literal-object-freeze (get receiver :slots)) (funcall literal-object-freeze receiver)) receiver))
149
173
  (defun MapEntry (key value hash)
150
174
  (let ((receiver (js* "this")))
151
- (put receiver :key key) (put receiver :value value) (put receiver :hash (uint32 hash)) (Object/freeze receiver)))
175
+ (put receiver :key key) (put receiver :value value) (put receiver :hash (uint32 hash)) (funcall literal-object-freeze receiver)))
152
176
  (defun BitmapIndexedNode (&optional bitmap items owner)
153
177
  (let ((receiver (js* "this")))
154
178
  (put receiver :bitmap (uint32 (if (nil? bitmap) 0 bitmap))) (put receiver :owner owner)
155
179
  (put receiver :items (if (nil? items) (js-array) items))
156
- (when (nil? owner) (Object/freeze (get receiver :items)) (Object/freeze receiver)) receiver))
180
+ (when (nil? owner) (funcall literal-object-freeze (get receiver :items)) (funcall literal-object-freeze receiver)) receiver))
157
181
  (defun ArrayNode (count children &optional owner)
158
182
  (let ((receiver (js* "this")))
159
183
  (put receiver :count count) (put receiver :owner owner) (put receiver :children children)
160
- (when (nil? owner) (Object/freeze children) (Object/freeze receiver)) receiver))
184
+ (when (nil? owner) (funcall literal-object-freeze children) (funcall literal-object-freeze receiver)) receiver))
161
185
  (defun HashCollisionNode (hash entries &optional owner)
162
186
  (let ((receiver (js* "this")))
163
187
  (put receiver :hash (uint32 hash)) (put receiver :owner owner) (put receiver :entries entries)
164
- (when (nil? owner) (Object/freeze entries) (Object/freeze receiver)) receiver))
188
+ (when (nil? owner) (funcall literal-object-freeze entries) (funcall literal-object-freeze receiver)) receiver))
165
189
  (defun literal-vector-node-view (node level)
166
190
  (let ((cached (js-call literal-node-views :get node)))
167
191
  (if (not (undefined? cached))
168
192
  (progn
169
- (unless (nil? (get node :owner nil))
193
+ (unless (nil? (literal-node-owner node))
170
194
  (let ((source (get node :slots)) (slots (get cached :slots)) (index 0))
171
195
  (put slots :length (js-length source))
172
196
  (while (< index (js-length source))
173
197
  (let ((item (get source index)))
174
- (if (Object/hasOwn source index)
198
+ (if (funcall literal-object-has-own source index)
175
199
  (put slots index (if (and (> level 0) (not (nullish? item)))
176
200
  (literal-vector-node-view item (- level 5)) item))
177
201
  (Reflect/deleteProperty slots index)))
@@ -183,14 +207,14 @@
183
207
  (put values index (if (and (> level 0) (not (nil? value)))
184
208
  (literal-vector-node-view value (- level 5)) value))))
185
209
  (setq index (1+ index)))
186
- (let ((view (new VectorNode values (get node :owner nil))))
210
+ (let ((view (new VectorNode values (literal-node-owner node))))
187
211
  (js-call literal-node-views :set node view) view)))))
188
212
  (defun literal-map-node-view (node)
189
213
  (if (nil? node) literal-empty-bitmap
190
214
  (let ((cached (js-call literal-node-views :get node)))
191
215
  (if (not (undefined? cached))
192
216
  (progn
193
- (unless (or (= (get node :kind) "entry") (nil? (get node :owner nil)))
217
+ (unless (or (= (get node :kind) "entry") (nil? (literal-node-owner node)))
194
218
  (let* ((kind (get node :kind)) (field (if (= kind "bitmap") "items" (if (= kind "array") "children" "entries")))
195
219
  (source (get node field)) (items (get cached field)) (index 0))
196
220
  (put items :length (js-length source))
@@ -208,9 +232,9 @@
208
232
  (let ((child (get source index)))
209
233
  (put items index (if (nil? child) undefined (literal-map-node-view child))))
210
234
  (setq index (1+ index)))
211
- (cond ((= kind "bitmap") (new BitmapIndexedNode (get node :bitmap) items (get node :owner nil)))
212
- ((= kind "array") (new ArrayNode (get node :count) items (get node :owner nil)))
213
- (t (new HashCollisionNode (get node :hash) items (get node :owner nil))))))))
235
+ (cond ((= kind "bitmap") (new BitmapIndexedNode (get node :bitmap) items (literal-node-owner node)))
236
+ ((= kind "array") (new ArrayNode (get node :count) items (literal-node-owner node)))
237
+ (t (new HashCollisionNode (get node :hash) items (literal-node-owner node))))))))
214
238
  (js-call literal-node-views :set node view) view)))))
215
239
  (defun literal-map-root-view (root)
216
240
  (if (nil? root) literal-empty-bitmap
@@ -218,7 +242,7 @@
218
242
  (let ((child (literal-map-node-view root)) (cached (js-call literal-map-root-views :get root)))
219
243
  (if (not (undefined? cached)) cached
220
244
  (let ((view (new BitmapIndexedNode (uint32 (bit-shift-left 1 (bit-and (get root :hash) 31)))
221
- (js-array child) (get root :owner nil))))
245
+ (js-array child) (literal-node-owner root))))
222
246
  (js-call literal-map-root-views :set root view) view)))
223
247
  (literal-map-node-view root))))
224
248
 
@@ -256,8 +280,10 @@
256
280
  (cond
257
281
  ((= type 2) (new PersistentVector literal-token (persistent-vector-count data) (get data :shift)
258
282
  (literal-vector-node-view (get data :root) (get data :shift))
259
- (if (js-call literal-editable-results :has data) (get data :tail)
260
- (Object/freeze (js-call (get data :tail) :slice))) (persistent-vector-meta data)))
283
+ (let ((tail (get data :tail)))
284
+ (if (js-call literal-editable-results :has data)
285
+ tail
286
+ (funcall literal-object-freeze tail))) (persistent-vector-meta data)))
261
287
  ((= type 3) (new PersistentHashMap literal-token (persistent-map-count data)
262
288
  (literal-map-root-view (get data :root)) (persistent-map-meta data)))
263
289
  ((= type 4) (new PersistentHashSet literal-token (literal-wrap 3 (get data :map)) (persistent-set-meta data)))
@@ -281,7 +307,9 @@
281
307
  (defun literal-list (&rest values)
282
308
  (literal-initialize-prototype 1) (js-call PersistentList :from values))
283
309
  (defun literal-vector (&rest values)
284
- (literal-initialize-prototype 2) (literal-vector-from-array-values values))
310
+ (literal-initialize-prototype 2)
311
+ (when (<= (js-length values) 32) (funcall literal-object-freeze values))
312
+ (literal-vector-from-array-values values))
285
313
  (defun literal-map (&rest values)
286
314
  (when (/= (% (js-length values) 2) 0) (literal-fail "hash-map expects complete key/value pairs"))
287
315
  (let ((entries (js-array)) (index 0) (pair 0))
@@ -298,12 +326,27 @@
298
326
  (let ((type (literal-type collection)))
299
327
  (cond ((or (and (>= type 1) (<= type 5)) (= type 8)) (get collection :count))
300
328
  ((nullish? collection) 0)
301
- ((or (Array/isArray collection) (= (js* "typeof collection") "string")) (js-length collection))
329
+ ((or (funcall literal-array-is-array collection) (= (js* "typeof collection") "string")) (js-length collection))
302
330
  ((or (literal-instance? collection globalThis/Map) (literal-instance? collection globalThis/Set)) (get collection :size))
303
- ((= (Object/getPrototypeOf collection) (get globalThis/Object :prototype)) (js-length (Object/keys collection)))
331
+ ((= (funcall literal-object-get-prototype-of collection) literal-object-prototype) (js-length (Object/keys collection)))
304
332
  (t (literal-fail "value does not implement ICounted/count")))))
305
333
  (defun literal-instance? (value constructor)
306
- (js-call (get constructor (get globalThis/Symbol :hasInstance)) :call constructor value))
334
+ (cond
335
+ ((or (eq constructor PersistentVector) (eq constructor PersistentList)
336
+ (eq constructor PersistentHashMap) (eq constructor PersistentHashSet)
337
+ (eq constructor PersistentQueue))
338
+ (if (or (nullish? value) (and (/= (js* "typeof value") "object") (/= (js* "typeof value") "function")))
339
+ false
340
+ (let ((type (literal-type value)))
341
+ (if (> type 0)
342
+ (if (eq constructor PersistentVector)
343
+ (or (= type 2) (= type 8))
344
+ (= type (cond ((eq constructor PersistentList) 1) ((eq constructor PersistentHashMap) 3)
345
+ ((eq constructor PersistentHashSet) 4) (t 5))))
346
+ (if (funcall literal-array-is-array value)
347
+ false
348
+ (js-call (get constructor literal-symbol-has-instance) :call constructor value))))))
349
+ (t (js-call (get constructor literal-symbol-has-instance) :call constructor value))))
307
350
  (defun literal-nth (collection index &rest not-found)
308
351
  (let ((type (literal-type collection)) (fallback (if (and (> (js-length not-found) 0) (not (undefined? (get not-found 0))))
309
352
  (get not-found 0) literal-missing)))
@@ -878,12 +921,12 @@
878
921
  (let ((out (if (= type 1) (persistent-list-nth data index not-found) (persistent-vector-nth data index not-found))))
879
922
  (if (eq out literal-missing)
880
923
  (literal-range (str "nth index " (funcall globalThis/String index) " is outside [0, " (literal-data-count type data) ")")) out)))
881
- (defun literal-list-nth-method (index &rest not-found)
924
+ (defun literal-list-nth-method (index &optional not-found)
882
925
  (literal-nth-data 1 (literal-data (js* "this") 1) index
883
- (if (or (= (js-length not-found) 0) (undefined? (get not-found 0))) literal-missing (get not-found 0))))
884
- (defun literal-vector-nth-method (index &rest not-found)
926
+ (if (undefined? not-found) literal-missing not-found)))
927
+ (defun literal-vector-nth-method (index &optional not-found)
885
928
  (literal-nth-data 2 (literal-data (js* "this") 2) index
886
- (if (or (= (js-length not-found) 0) (undefined? (get not-found 0))) literal-missing (get not-found 0))))
929
+ (if (undefined? not-found) literal-missing not-found)))
887
930
  (defun literal-list-first-method (&optional not-found)
888
931
  (let ((state (aref (js* "this") literal-list-state))) (if (get state :empty) not-found (get state :value))))
889
932
  (defun literal-list-rest-method ()
@@ -959,39 +1002,50 @@
959
1002
  (let ((length (js-length values)))
960
1003
  (when (> length 2147483647)
961
1004
  (literal-range "persistent collection cannot exceed 2147483647 values"))
962
- (if (= length 0) (literal-vector-empty)
963
- (let ((count 0) (shift 5) (root (js-object :slots (js-array))) (tail (js-array)) (tail-len 0))
964
- (while (< count length)
965
- (let ((value (get values count)))
966
- (if (< tail-len 32)
967
- (progn (put tail tail-len value) (setq tail-len (1+ tail-len)))
968
- (let ((tail-node (js-object :slots tail)))
969
- (if (> (unsigned-bit-shift-right count 5) (bit-shift-left 1 shift))
970
- (setq root (js-object :slots (js-array root (literal-vector-new-path shift tail-node)))
971
- shift (+ shift 5))
972
- (literal-vector-push-tail-mutable shift root tail-node count))
973
- (setq tail (js-array value) tail-len 1))))
974
- (setq count (1+ count)))
1005
+ (cond
1006
+ ((= length 0) (literal-vector-empty))
1007
+ ((<= length 32)
975
1008
  (literal-wrap 2 (js-object :kind "eliscript/persistent-vector"
976
- :count count :shift shift :root root :tail tail :metadata nil))))))
1009
+ :count length :shift 5 :root (get literal-empty-vector-data :root)
1010
+ :tail (if (funcall literal-object-is-frozen values) values (funcall literal-object-freeze (js-call values :slice)))
1011
+ :metadata nil)))
1012
+ (t
1013
+ (let ((count 0) (shift 5) (root (js-object :slots (js-array))) (tail (js-array)) (tail-len 0))
1014
+ (while (< count length)
1015
+ (let ((value (get values count)))
1016
+ (if (< tail-len 32)
1017
+ (progn (put tail tail-len value) (setq tail-len (1+ tail-len)))
1018
+ (let ((tail-node (js-object :slots tail)))
1019
+ (if (> (unsigned-bit-shift-right count 5) (bit-shift-left 1 shift))
1020
+ (setq root (js-object :slots (js-array root (literal-vector-new-path shift tail-node)))
1021
+ shift (+ shift 5))
1022
+ (literal-vector-push-tail-mutable shift root tail-node count))
1023
+ (setq tail (js-array value) tail-len 1))))
1024
+ (setq count (1+ count)))
1025
+ (literal-wrap 2 (js-object :kind "eliscript/persistent-vector"
1026
+ :count count :shift shift :root root :tail (funcall literal-object-freeze tail) :metadata nil)))))))
977
1027
  (defun literal-vector-from (iterable)
978
1028
  (if (or (= (literal-type iterable) 2) (= (literal-type iterable) 8)) iterable
979
- (let ((count 0) (shift 5) (root (js-object :slots (js-array))) (tail (js-array)) (tail-len 0))
980
- (literal-each iterable (lambda (value)
981
- (when (= count 2147483647)
982
- (literal-range "persistent collection cannot exceed 2147483647 values"))
983
- (if (< tail-len 32)
984
- (progn (put tail tail-len value) (setq tail-len (1+ tail-len)))
985
- (let ((tail-node (js-object :slots tail)))
986
- (if (> (unsigned-bit-shift-right count 5) (bit-shift-left 1 shift))
987
- (setq root (js-object :slots (js-array root (literal-vector-new-path shift tail-node)))
988
- shift (+ shift 5))
989
- (literal-vector-push-tail-mutable shift root tail-node count))
990
- (setq tail (js-array value) tail-len 1)))
991
- (setq count (1+ count))))
992
- (if (= count 0) (literal-vector-empty)
993
- (literal-wrap 2 (js-object :kind "eliscript/persistent-vector"
994
- :count count :shift shift :root root :tail tail :metadata nil))))))
1029
+ (if (and (funcall literal-array-is-array iterable) (not (funcall literal-object-has-own iterable literal-symbol-iterator)))
1030
+ (literal-vector-from-array-values iterable)
1031
+ (let ((count 0) (shift 5) (root (get literal-empty-vector-data :root)) (tail (js-array)) (tail-len 0))
1032
+ (literal-each iterable (lambda (value)
1033
+ (when (= count 2147483647)
1034
+ (literal-range "persistent collection cannot exceed 2147483647 values"))
1035
+ (if (< tail-len 32)
1036
+ (progn (put tail tail-len value) (setq tail-len (1+ tail-len)))
1037
+ (let ((tail-node (js-object :slots tail)))
1038
+ (when (eq root (get literal-empty-vector-data :root))
1039
+ (setq root (js-object :slots (js-array))))
1040
+ (if (> (unsigned-bit-shift-right count 5) (bit-shift-left 1 shift))
1041
+ (setq root (js-object :slots (js-array root (literal-vector-new-path shift tail-node)))
1042
+ shift (+ shift 5))
1043
+ (literal-vector-push-tail-mutable shift root tail-node count))
1044
+ (setq tail (js-array value) tail-len 1)))
1045
+ (setq count (1+ count))))
1046
+ (if (= count 0) (literal-vector-empty)
1047
+ (literal-wrap 2 (js-object :kind "eliscript/persistent-vector"
1048
+ :count count :shift shift :root root :tail (funcall literal-object-freeze tail) :metadata nil)))))))
995
1049
  (defun literal-map-from (iterable)
996
1050
  (if (= (literal-type iterable) 3) iterable
997
1051
  (let ((data literal-empty-map-data))
@@ -1127,6 +1181,7 @@
1127
1181
  (put view :tail (get state :tail)))
1128
1182
  (put view :root (literal-map-root-view (get state :root)))))) receiver))
1129
1183
  (defun literal-transient-build (receiver type source state)
1184
+ (setq literal-has-transients t)
1130
1185
  (literal-initialize-transient-prototype type)
1131
1186
  (let ((view (if (= type 4)
1132
1187
  (js-object :active t :source source :sourceMap (get (aref source literal-set-state) :map)
@@ -1402,7 +1457,7 @@
1402
1457
  (js-object :kind "protocol-method" :protocol (get owner :protocol) :operation (get owner :operation))))
1403
1458
  (lambda (record) (get (get (get record :protocol) :operations) (get record :operation))))
1404
1459
  (defun literal-protocol-for-role (role) (managed-literal-protocol-for-role role))
1405
- (export literal-list literal-vector literal-map literal-set literal-queue literal-keyword literal-symbol literal-binding-lookup
1460
+ (export literal-list literal-vector literal-vector-from-array-values literal-map literal-set literal-queue literal-keyword literal-symbol literal-binding-lookup
1406
1461
  literal-count literal-nth literal-get literal-first literal-rest literal-cons literal-equal literal-hash
1407
1462
  literal-mix literal-finish literal-string-hash literal-entry literal-each
1408
1463
  literal-conj literal-assoc literal-dissoc literal-disj literal-peek literal-pop literal-meta literal-with-meta