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,616 @@
|
|
|
1
|
+
(module eliscript.interop.js
|
|
2
|
+
(import-portable "../persistent-list.eli"
|
|
3
|
+
persistent-list?
|
|
4
|
+
persistent-list-from-array
|
|
5
|
+
persistent-list-to-array)
|
|
6
|
+
(import-portable "../persistent-vector.eli"
|
|
7
|
+
persistent-vector?
|
|
8
|
+
persistent-vector-count
|
|
9
|
+
persistent-vector-from-array
|
|
10
|
+
persistent-vector-nth)
|
|
11
|
+
(import-portable "../persistent-map.eli"
|
|
12
|
+
persistent-map?
|
|
13
|
+
persistent-map-assoc
|
|
14
|
+
persistent-map-has?
|
|
15
|
+
persistent-map-reduce)
|
|
16
|
+
(import-portable "../persistent-set.eli"
|
|
17
|
+
persistent-set?
|
|
18
|
+
persistent-set-conj
|
|
19
|
+
persistent-set-has?
|
|
20
|
+
persistent-set-reduce)
|
|
21
|
+
(import-portable "../value.eli" empty-value-map empty-value-set)
|
|
22
|
+
(import "../../runtime/core/vector.mjs" :as RuntimeVector)
|
|
23
|
+
(import "../../runtime/core/map.mjs" :as RuntimeMap)
|
|
24
|
+
(import "../../runtime/core/set.mjs" :as RuntimeSet)
|
|
25
|
+
|
|
26
|
+
(defconst interop-array-predicate (js* "Array.isArray"))
|
|
27
|
+
(defconst interop-map-predicate
|
|
28
|
+
(js* "(() => { const sentinel = Object.freeze({}); return (value) => { try { Map.prototype.has.call(value, sentinel); return true; } catch { return false; } }; })()"))
|
|
29
|
+
(defconst interop-set-predicate
|
|
30
|
+
(js* "(() => { const sentinel = Object.freeze({}); return (value) => { try { Set.prototype.has.call(value, sentinel); return true; } catch { return false; } }; })()"))
|
|
31
|
+
(defconst interop-object-predicate
|
|
32
|
+
(js* "(value) => { if (value === null || typeof value !== \"object\") return false; try { const prototype = Object.getPrototypeOf(value); return prototype === null || Object.getPrototypeOf(prototype) === null; } catch { return false; } }"))
|
|
33
|
+
(defconst interop-safe-integer-predicate (js* "Number.isSafeInteger"))
|
|
34
|
+
(defconst interop-map-factory (js* "() => new Map()"))
|
|
35
|
+
(defconst interop-set-factory (js* "() => new Set()"))
|
|
36
|
+
(defconst interop-object-factory (js* "() => ({})"))
|
|
37
|
+
(defconst interop-weak-map-factory (js* "() => new WeakMap()"))
|
|
38
|
+
(defconst interop-map-entries
|
|
39
|
+
(js* "(value) => Array.from(Map.prototype.entries.call(value))"))
|
|
40
|
+
(defconst interop-set-values
|
|
41
|
+
(js* "(value) => Array.from(Set.prototype.values.call(value))"))
|
|
42
|
+
(defconst interop-iterable-values (js* "(value) => Array.from(value)"))
|
|
43
|
+
(defconst interop-json-string (js* "JSON.stringify"))
|
|
44
|
+
(defconst interop-enumerable-own-keys
|
|
45
|
+
(js* "(value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key))"))
|
|
46
|
+
(defconst interop-own-data-property
|
|
47
|
+
(js* "(value, key) => { const descriptor = Object.getOwnPropertyDescriptor(value, key); return descriptor !== undefined && Object.prototype.hasOwnProperty.call(descriptor, \"value\") ? { ok: true, value: descriptor.value } : { ok: false, value: undefined }; }"))
|
|
48
|
+
(defconst interop-define-property
|
|
49
|
+
(js* "(target, key, value) => { Object.defineProperty(target, key, { value, enumerable: true, configurable: true, writable: true }); return target; }"))
|
|
50
|
+
(defconst interop-map-set
|
|
51
|
+
(js* "(target, key, value) => { Map.prototype.set.call(target, key, value); return target; }"))
|
|
52
|
+
(defconst interop-set-add
|
|
53
|
+
(js* "(target, value) => { Set.prototype.add.call(target, value); return target; }"))
|
|
54
|
+
|
|
55
|
+
(defun interop-fail (code message path &optional origin)
|
|
56
|
+
(throw
|
|
57
|
+
(js-object :kind "eliscript/interop-error"
|
|
58
|
+
:code code
|
|
59
|
+
:message message
|
|
60
|
+
:path path
|
|
61
|
+
:origin origin)))
|
|
62
|
+
|
|
63
|
+
(defun array? (value)
|
|
64
|
+
(funcall interop-array-predicate value))
|
|
65
|
+
|
|
66
|
+
(defun object? (value)
|
|
67
|
+
(funcall interop-object-predicate value))
|
|
68
|
+
|
|
69
|
+
(defun js-map? (value)
|
|
70
|
+
(funcall interop-map-predicate value))
|
|
71
|
+
|
|
72
|
+
(defun js-set? (value)
|
|
73
|
+
(funcall interop-set-predicate value))
|
|
74
|
+
|
|
75
|
+
(defun js-array (&rest values) values)
|
|
76
|
+
|
|
77
|
+
(defun js-object (&rest key-values)
|
|
78
|
+
(when (/= (mod (js-length key-values) 2) 0)
|
|
79
|
+
(interop-fail
|
|
80
|
+
"ELI-INTEROP-INVALID-ENTRY"
|
|
81
|
+
"js-object requires an even number of key/value arguments"
|
|
82
|
+
"$"))
|
|
83
|
+
(let ((result (funcall interop-object-factory))
|
|
84
|
+
(index 0))
|
|
85
|
+
(while (< index (js-length key-values))
|
|
86
|
+
(let ((key (js-nth index key-values)))
|
|
87
|
+
(when (not (= (value-type key) "string"))
|
|
88
|
+
(interop-fail
|
|
89
|
+
"ELI-INTEROP-INVALID-KEY"
|
|
90
|
+
"js-object keys must be strings"
|
|
91
|
+
(str "$[" index "]")))
|
|
92
|
+
(funcall interop-define-property
|
|
93
|
+
result key (js-nth (1+ index) key-values)))
|
|
94
|
+
(setq index (+ index 2)))
|
|
95
|
+
result))
|
|
96
|
+
|
|
97
|
+
(defun js-map (&rest entries)
|
|
98
|
+
(let ((result (funcall interop-map-factory))
|
|
99
|
+
(index 0))
|
|
100
|
+
(while (< index (js-length entries))
|
|
101
|
+
(let ((entry (js-nth index entries)))
|
|
102
|
+
(when (not (and (array? entry) (= (js-length entry) 2)))
|
|
103
|
+
(interop-fail
|
|
104
|
+
"ELI-INTEROP-INVALID-ENTRY"
|
|
105
|
+
"js-map entries must be two-value native arrays"
|
|
106
|
+
(str "$[" index "]")))
|
|
107
|
+
(funcall interop-map-set result (js-nth 0 entry) (js-nth 1 entry)))
|
|
108
|
+
(setq index (1+ index)))
|
|
109
|
+
result))
|
|
110
|
+
|
|
111
|
+
(defun js-set (&rest values)
|
|
112
|
+
(let ((result (funcall interop-set-factory))
|
|
113
|
+
(index 0))
|
|
114
|
+
(while (< index (js-length values))
|
|
115
|
+
(funcall interop-set-add result (js-nth index values))
|
|
116
|
+
(setq index (1+ index)))
|
|
117
|
+
result))
|
|
118
|
+
|
|
119
|
+
(defun interop-option-value (options key fallback)
|
|
120
|
+
(if (not (object-has? options key))
|
|
121
|
+
fallback
|
|
122
|
+
(let ((property (funcall interop-own-data-property options key)))
|
|
123
|
+
(when (not (get property "ok" false))
|
|
124
|
+
(interop-fail
|
|
125
|
+
"ELI-INTEROP-ACCESSOR"
|
|
126
|
+
"interop option accessors are not evaluated"
|
|
127
|
+
(interop-object-path "$" key)))
|
|
128
|
+
(get property "value" nil))))
|
|
129
|
+
|
|
130
|
+
(defun interop-options (options)
|
|
131
|
+
(if (nullish? options)
|
|
132
|
+
(js-object :deep false :max-depth 256 :max-values 1000000)
|
|
133
|
+
(progn
|
|
134
|
+
(when (not (object? options))
|
|
135
|
+
(interop-fail
|
|
136
|
+
"ELI-INTEROP-INVALID-OPTIONS"
|
|
137
|
+
"interop options must be a plain object or nil"
|
|
138
|
+
"$"))
|
|
139
|
+
(let ((keys (funcall interop-enumerable-own-keys options))
|
|
140
|
+
(index 0))
|
|
141
|
+
(while (< index (js-length keys))
|
|
142
|
+
(let ((key (js-nth index keys)))
|
|
143
|
+
(when (not (or (= key "deep")
|
|
144
|
+
(= key "maxDepth")
|
|
145
|
+
(= key "maxValues")))
|
|
146
|
+
(interop-fail
|
|
147
|
+
"ELI-INTEROP-INVALID-OPTIONS"
|
|
148
|
+
(if (= (value-type key) "string")
|
|
149
|
+
(str "unknown interop option: " key)
|
|
150
|
+
"unknown interop Symbol option")
|
|
151
|
+
"$")))
|
|
152
|
+
(setq index (1+ index))))
|
|
153
|
+
(let ((deep (interop-option-value options "deep" false))
|
|
154
|
+
(max-depth (interop-option-value options "maxDepth" 256))
|
|
155
|
+
(max-values (interop-option-value options "maxValues" 1000000)))
|
|
156
|
+
(when (not (= (value-type deep) "boolean"))
|
|
157
|
+
(interop-fail
|
|
158
|
+
"ELI-INTEROP-INVALID-OPTIONS"
|
|
159
|
+
"deep must be a boolean"
|
|
160
|
+
"$"))
|
|
161
|
+
(when (not (and (funcall interop-safe-integer-predicate max-depth)
|
|
162
|
+
(>= max-depth 0)))
|
|
163
|
+
(interop-fail
|
|
164
|
+
"ELI-INTEROP-INVALID-OPTIONS"
|
|
165
|
+
"maxDepth must be a non-negative safe integer"
|
|
166
|
+
"$"))
|
|
167
|
+
(when (not (and (funcall interop-safe-integer-predicate max-values)
|
|
168
|
+
(> max-values 0)))
|
|
169
|
+
(interop-fail
|
|
170
|
+
"ELI-INTEROP-INVALID-OPTIONS"
|
|
171
|
+
"maxValues must be a positive safe integer"
|
|
172
|
+
"$"))
|
|
173
|
+
(js-object :deep deep
|
|
174
|
+
:max-depth max-depth
|
|
175
|
+
:max-values max-values)))))
|
|
176
|
+
|
|
177
|
+
(defun interop-context (options direction)
|
|
178
|
+
(let ((normalized (interop-options options)))
|
|
179
|
+
(js-object :deep (get normalized "deep" false)
|
|
180
|
+
:max-depth (get normalized "max-depth" 256)
|
|
181
|
+
:max-values (get normalized "max-values" 1000000)
|
|
182
|
+
:count 0
|
|
183
|
+
:direction direction
|
|
184
|
+
:memo (funcall interop-weak-map-factory)
|
|
185
|
+
:active (funcall interop-weak-map-factory))))
|
|
186
|
+
|
|
187
|
+
(defun interop-step (context path depth)
|
|
188
|
+
(when (> depth (get context "max-depth" 256))
|
|
189
|
+
(interop-fail
|
|
190
|
+
"ELI-INTEROP-DEPTH-LIMIT"
|
|
191
|
+
"deep conversion exceeded maxDepth"
|
|
192
|
+
path))
|
|
193
|
+
(let ((count (1+ (get context "count" 0))))
|
|
194
|
+
(put context "count" count)
|
|
195
|
+
(when (> count (get context "max-values" 1000000))
|
|
196
|
+
(interop-fail
|
|
197
|
+
"ELI-INTEROP-VALUE-LIMIT"
|
|
198
|
+
"deep conversion exceeded maxValues"
|
|
199
|
+
path))))
|
|
200
|
+
|
|
201
|
+
(defun interop-scalar? (value)
|
|
202
|
+
(let ((type (value-type value)))
|
|
203
|
+
(or (= type "null")
|
|
204
|
+
(= type "undefined")
|
|
205
|
+
(= type "boolean")
|
|
206
|
+
(= type "number")
|
|
207
|
+
(= type "bigint")
|
|
208
|
+
(= type "string")
|
|
209
|
+
(= type "keyword")
|
|
210
|
+
(= type "symbol"))))
|
|
211
|
+
|
|
212
|
+
(defun interop-kind (value)
|
|
213
|
+
(cond
|
|
214
|
+
((persistent-list? value) "portable-list")
|
|
215
|
+
((persistent-vector? value) "portable-vector")
|
|
216
|
+
((persistent-map? value) "portable-map")
|
|
217
|
+
((persistent-set? value) "portable-set")
|
|
218
|
+
((RuntimeVector/isPersistentVector value) "runtime-vector")
|
|
219
|
+
((RuntimeMap/isPersistentHashMap value) "runtime-map")
|
|
220
|
+
((RuntimeSet/isPersistentHashSet value) "runtime-set")
|
|
221
|
+
((array? value) "host-array")
|
|
222
|
+
((js-map? value) "host-map")
|
|
223
|
+
((js-set? value) "host-set")
|
|
224
|
+
((object? value) "host-object")
|
|
225
|
+
(t nil)))
|
|
226
|
+
|
|
227
|
+
(defun interop-persistent-kind? (kind)
|
|
228
|
+
(or (= kind "portable-list")
|
|
229
|
+
(= kind "portable-vector")
|
|
230
|
+
(= kind "portable-map")
|
|
231
|
+
(= kind "portable-set")
|
|
232
|
+
(= kind "runtime-vector")
|
|
233
|
+
(= kind "runtime-map")
|
|
234
|
+
(= kind "runtime-set")))
|
|
235
|
+
|
|
236
|
+
(defun interop-index-path (path index)
|
|
237
|
+
(str path "[" index "]"))
|
|
238
|
+
|
|
239
|
+
(defun interop-map-key-path (path index)
|
|
240
|
+
(str path "<key:" index ">"))
|
|
241
|
+
|
|
242
|
+
(defun interop-map-value-path (path index)
|
|
243
|
+
(str path "<value:" index ">"))
|
|
244
|
+
|
|
245
|
+
(defun interop-set-path (path index)
|
|
246
|
+
(str path "<set:" index ">"))
|
|
247
|
+
|
|
248
|
+
(defun interop-object-path (path key)
|
|
249
|
+
(str path "[" (funcall interop-json-string key) "]"))
|
|
250
|
+
|
|
251
|
+
(defun interop-convert-container (source context path convert)
|
|
252
|
+
(let ((memo (get context "memo" nil))
|
|
253
|
+
(active (get context "active" nil)))
|
|
254
|
+
(when (js-call active :has source)
|
|
255
|
+
(interop-fail
|
|
256
|
+
"ELI-INTEROP-CYCLE"
|
|
257
|
+
"deep conversion encountered a cycle"
|
|
258
|
+
path
|
|
259
|
+
(js-call active :get source)))
|
|
260
|
+
(if (js-call memo :has source)
|
|
261
|
+
(js-call memo :get source)
|
|
262
|
+
(progn
|
|
263
|
+
(js-call active :set source path)
|
|
264
|
+
(try
|
|
265
|
+
(let ((result (funcall convert)))
|
|
266
|
+
(js-call memo :set source result)
|
|
267
|
+
result)
|
|
268
|
+
(finally (js-call active :delete source)))))))
|
|
269
|
+
|
|
270
|
+
(defun interop-sequence-source (source kind)
|
|
271
|
+
(cond
|
|
272
|
+
((= kind "portable-list")
|
|
273
|
+
(persistent-list-to-array source))
|
|
274
|
+
((= kind "portable-vector")
|
|
275
|
+
(let ((values (js-array))
|
|
276
|
+
(index 0))
|
|
277
|
+
(while (< index (persistent-vector-count source))
|
|
278
|
+
(js-call values :push (persistent-vector-nth source index nil))
|
|
279
|
+
(setq index (1+ index)))
|
|
280
|
+
values))
|
|
281
|
+
((= kind "runtime-vector")
|
|
282
|
+
(funcall interop-iterable-values source))
|
|
283
|
+
(t source)))
|
|
284
|
+
|
|
285
|
+
(defun interop-map-source (source kind context path)
|
|
286
|
+
(cond
|
|
287
|
+
((= kind "portable-map")
|
|
288
|
+
(persistent-map-reduce
|
|
289
|
+
(lambda (entries key value)
|
|
290
|
+
(js-call entries :push (js-array key value))
|
|
291
|
+
entries)
|
|
292
|
+
(js-array)
|
|
293
|
+
source))
|
|
294
|
+
((= kind "runtime-map")
|
|
295
|
+
(funcall interop-iterable-values source))
|
|
296
|
+
((= kind "host-map")
|
|
297
|
+
(funcall interop-map-entries source))
|
|
298
|
+
(t
|
|
299
|
+
(let ((keys (funcall interop-enumerable-own-keys source))
|
|
300
|
+
(entries (js-array))
|
|
301
|
+
(index 0))
|
|
302
|
+
(while (< index (js-length keys))
|
|
303
|
+
(let* ((key (js-nth index keys))
|
|
304
|
+
(_valid-key
|
|
305
|
+
(when (not (= (value-type key) "string"))
|
|
306
|
+
(interop-fail
|
|
307
|
+
"ELI-INTEROP-INVALID-KEY"
|
|
308
|
+
"plain object keys must be strings"
|
|
309
|
+
path)))
|
|
310
|
+
(key-path (interop-object-path path key))
|
|
311
|
+
(property
|
|
312
|
+
(funcall interop-own-data-property source key)))
|
|
313
|
+
(when (not (get property "ok" false))
|
|
314
|
+
(interop-fail
|
|
315
|
+
"ELI-INTEROP-ACCESSOR"
|
|
316
|
+
"plain object accessors are not converted"
|
|
317
|
+
key-path))
|
|
318
|
+
(js-call entries :push (js-array key (get property "value" nil))))
|
|
319
|
+
(setq index (1+ index)))
|
|
320
|
+
entries))))
|
|
321
|
+
|
|
322
|
+
(defun interop-set-source (source kind)
|
|
323
|
+
(cond
|
|
324
|
+
((= kind "portable-set")
|
|
325
|
+
(persistent-set-reduce
|
|
326
|
+
(lambda (values value)
|
|
327
|
+
(js-call values :push value)
|
|
328
|
+
values)
|
|
329
|
+
(js-array)
|
|
330
|
+
source))
|
|
331
|
+
((= kind "runtime-set")
|
|
332
|
+
(funcall interop-iterable-values source))
|
|
333
|
+
(t (funcall interop-set-values source))))
|
|
334
|
+
|
|
335
|
+
(defun interop-child-to-js (value context path depth)
|
|
336
|
+
(if (get context "deep" false)
|
|
337
|
+
(interop-to-js-node value context path depth)
|
|
338
|
+
value))
|
|
339
|
+
|
|
340
|
+
(defun interop-child-from-js (value context path depth)
|
|
341
|
+
(if (get context "deep" false)
|
|
342
|
+
(interop-from-js-node value context path depth)
|
|
343
|
+
value))
|
|
344
|
+
|
|
345
|
+
(defun interop-to-js-sequence (source kind context path depth)
|
|
346
|
+
(let ((source-values (interop-sequence-source source kind))
|
|
347
|
+
(result (js-array))
|
|
348
|
+
(index 0))
|
|
349
|
+
(while (< index (js-length source-values))
|
|
350
|
+
(js-call
|
|
351
|
+
result
|
|
352
|
+
:push
|
|
353
|
+
(interop-child-to-js
|
|
354
|
+
(js-nth index source-values)
|
|
355
|
+
context
|
|
356
|
+
(interop-index-path path index)
|
|
357
|
+
(1+ depth)))
|
|
358
|
+
(setq index (1+ index)))
|
|
359
|
+
result))
|
|
360
|
+
|
|
361
|
+
(defun interop-to-js-map (source kind context path depth)
|
|
362
|
+
(let ((entries (interop-map-source source kind context path))
|
|
363
|
+
(result (funcall interop-map-factory))
|
|
364
|
+
(index 0))
|
|
365
|
+
(while (< index (js-length entries))
|
|
366
|
+
(let ((entry (js-nth index entries)))
|
|
367
|
+
(funcall
|
|
368
|
+
interop-map-set
|
|
369
|
+
result
|
|
370
|
+
(interop-child-to-js
|
|
371
|
+
(js-nth 0 entry) context
|
|
372
|
+
(interop-map-key-path path index) (1+ depth))
|
|
373
|
+
(interop-child-to-js
|
|
374
|
+
(js-nth 1 entry) context
|
|
375
|
+
(interop-map-value-path path index) (1+ depth))))
|
|
376
|
+
(setq index (1+ index)))
|
|
377
|
+
result))
|
|
378
|
+
|
|
379
|
+
(defun interop-to-js-set (source kind context path depth)
|
|
380
|
+
(let ((values (interop-set-source source kind))
|
|
381
|
+
(result (funcall interop-set-factory))
|
|
382
|
+
(index 0))
|
|
383
|
+
(while (< index (js-length values))
|
|
384
|
+
(funcall
|
|
385
|
+
interop-set-add
|
|
386
|
+
result
|
|
387
|
+
(interop-child-to-js
|
|
388
|
+
(js-nth index values) context
|
|
389
|
+
(interop-set-path path index) (1+ depth)))
|
|
390
|
+
(setq index (1+ index)))
|
|
391
|
+
result))
|
|
392
|
+
|
|
393
|
+
(defun interop-to-js-plain-object (source context path depth)
|
|
394
|
+
(let ((entries (interop-map-source source "host-object" context path))
|
|
395
|
+
(result (funcall interop-object-factory))
|
|
396
|
+
(index 0))
|
|
397
|
+
(while (< index (js-length entries))
|
|
398
|
+
(let* ((entry (js-nth index entries))
|
|
399
|
+
(key (js-nth 0 entry)))
|
|
400
|
+
(funcall
|
|
401
|
+
interop-define-property
|
|
402
|
+
result key
|
|
403
|
+
(interop-child-to-js
|
|
404
|
+
(js-nth 1 entry) context
|
|
405
|
+
(interop-object-path path key) (1+ depth))))
|
|
406
|
+
(setq index (1+ index)))
|
|
407
|
+
result))
|
|
408
|
+
|
|
409
|
+
(defun interop-to-js-node (value context path depth)
|
|
410
|
+
(interop-step context path depth)
|
|
411
|
+
(if (interop-scalar? value)
|
|
412
|
+
value
|
|
413
|
+
(let ((kind (interop-kind value)))
|
|
414
|
+
(if (nil? kind)
|
|
415
|
+
(if (get context "deep" false)
|
|
416
|
+
(interop-fail
|
|
417
|
+
"ELI-INTEROP-UNSUPPORTED"
|
|
418
|
+
(str "unsupported deep to-js value: " (value-type value))
|
|
419
|
+
path)
|
|
420
|
+
value)
|
|
421
|
+
(if (and (not (get context "deep" false))
|
|
422
|
+
(or (= kind "host-array")
|
|
423
|
+
(= kind "host-map")
|
|
424
|
+
(= kind "host-set")
|
|
425
|
+
(= kind "host-object")))
|
|
426
|
+
value
|
|
427
|
+
(interop-convert-container
|
|
428
|
+
value context path
|
|
429
|
+
(lambda ()
|
|
430
|
+
(cond
|
|
431
|
+
((or (= kind "portable-list")
|
|
432
|
+
(= kind "portable-vector")
|
|
433
|
+
(= kind "runtime-vector")
|
|
434
|
+
(= kind "host-array"))
|
|
435
|
+
(interop-to-js-sequence value kind context path depth))
|
|
436
|
+
((or (= kind "portable-map")
|
|
437
|
+
(= kind "runtime-map")
|
|
438
|
+
(= kind "host-map"))
|
|
439
|
+
(interop-to-js-map value kind context path depth))
|
|
440
|
+
((or (= kind "portable-set")
|
|
441
|
+
(= kind "runtime-set")
|
|
442
|
+
(= kind "host-set"))
|
|
443
|
+
(interop-to-js-set value kind context path depth))
|
|
444
|
+
(t
|
|
445
|
+
(interop-to-js-plain-object
|
|
446
|
+
value context path depth))))))))))
|
|
447
|
+
|
|
448
|
+
(defun interop-from-js-sequence (source kind context path depth)
|
|
449
|
+
(let ((source-values (interop-sequence-source source kind))
|
|
450
|
+
(values (js-array))
|
|
451
|
+
(index 0))
|
|
452
|
+
(while (< index (js-length source-values))
|
|
453
|
+
(js-call
|
|
454
|
+
values
|
|
455
|
+
:push
|
|
456
|
+
(interop-child-from-js
|
|
457
|
+
(js-nth index source-values) context
|
|
458
|
+
(interop-index-path path index) (1+ depth)))
|
|
459
|
+
(setq index (1+ index)))
|
|
460
|
+
(cond
|
|
461
|
+
((= kind "portable-list") (persistent-list-from-array values))
|
|
462
|
+
((= kind "portable-vector") (persistent-vector-from-array values))
|
|
463
|
+
(t (apply RuntimeVector/persistentVector values)))))
|
|
464
|
+
|
|
465
|
+
(defun interop-from-js-map (source kind context path depth)
|
|
466
|
+
(let ((entries (interop-map-source source kind context path))
|
|
467
|
+
(portable (= kind "portable-map"))
|
|
468
|
+
(result
|
|
469
|
+
(if (= kind "portable-map")
|
|
470
|
+
(empty-value-map)
|
|
471
|
+
RuntimeMap/EMPTY_MAP))
|
|
472
|
+
(index 0))
|
|
473
|
+
(while (< index (js-length entries))
|
|
474
|
+
(let* ((entry (js-nth index entries))
|
|
475
|
+
(key-path
|
|
476
|
+
(if (= kind "host-object")
|
|
477
|
+
(interop-object-path path (js-nth 0 entry))
|
|
478
|
+
(interop-map-key-path path index)))
|
|
479
|
+
(value-path
|
|
480
|
+
(if (= kind "host-object")
|
|
481
|
+
(interop-object-path path (js-nth 0 entry))
|
|
482
|
+
(interop-map-value-path path index)))
|
|
483
|
+
(key
|
|
484
|
+
(interop-child-from-js
|
|
485
|
+
(js-nth 0 entry) context
|
|
486
|
+
key-path (1+ depth)))
|
|
487
|
+
(item-value
|
|
488
|
+
(interop-child-from-js
|
|
489
|
+
(js-nth 1 entry) context
|
|
490
|
+
value-path (1+ depth)))
|
|
491
|
+
(present
|
|
492
|
+
(if portable
|
|
493
|
+
(persistent-map-has? result key)
|
|
494
|
+
(js-call result :has key))))
|
|
495
|
+
(when present
|
|
496
|
+
(interop-fail
|
|
497
|
+
"ELI-INTEROP-DUPLICATE"
|
|
498
|
+
"converted Map contains duplicate value-equal keys"
|
|
499
|
+
key-path))
|
|
500
|
+
(setq result
|
|
501
|
+
(if portable
|
|
502
|
+
(persistent-map-assoc result key item-value)
|
|
503
|
+
(js-call result :assoc key item-value))))
|
|
504
|
+
(setq index (1+ index)))
|
|
505
|
+
result))
|
|
506
|
+
|
|
507
|
+
(defun interop-from-js-set (source kind context path depth)
|
|
508
|
+
(let ((values (interop-set-source source kind))
|
|
509
|
+
(portable (= kind "portable-set"))
|
|
510
|
+
(result
|
|
511
|
+
(if (= kind "portable-set")
|
|
512
|
+
(empty-value-set)
|
|
513
|
+
RuntimeSet/EMPTY_SET))
|
|
514
|
+
(index 0))
|
|
515
|
+
(while (< index (js-length values))
|
|
516
|
+
(let* ((value
|
|
517
|
+
(interop-child-from-js
|
|
518
|
+
(js-nth index values) context
|
|
519
|
+
(interop-set-path path index) (1+ depth)))
|
|
520
|
+
(present
|
|
521
|
+
(if portable
|
|
522
|
+
(persistent-set-has? result value)
|
|
523
|
+
(js-call result :has value))))
|
|
524
|
+
(when present
|
|
525
|
+
(interop-fail
|
|
526
|
+
"ELI-INTEROP-DUPLICATE"
|
|
527
|
+
"converted Set contains duplicate value-equal members"
|
|
528
|
+
(interop-set-path path index)))
|
|
529
|
+
(setq result
|
|
530
|
+
(if portable
|
|
531
|
+
(persistent-set-conj result value)
|
|
532
|
+
(js-call result :conj value))))
|
|
533
|
+
(setq index (1+ index)))
|
|
534
|
+
result))
|
|
535
|
+
|
|
536
|
+
(defun interop-from-js-node (value context path depth)
|
|
537
|
+
(interop-step context path depth)
|
|
538
|
+
(if (interop-scalar? value)
|
|
539
|
+
value
|
|
540
|
+
(let ((kind (interop-kind value)))
|
|
541
|
+
(if (nil? kind)
|
|
542
|
+
(if (get context "deep" false)
|
|
543
|
+
(interop-fail
|
|
544
|
+
"ELI-INTEROP-UNSUPPORTED"
|
|
545
|
+
(str "unsupported deep from-js value: " (value-type value))
|
|
546
|
+
path)
|
|
547
|
+
value)
|
|
548
|
+
(if (and (not (get context "deep" false))
|
|
549
|
+
(interop-persistent-kind? kind))
|
|
550
|
+
value
|
|
551
|
+
(interop-convert-container
|
|
552
|
+
value context path
|
|
553
|
+
(lambda ()
|
|
554
|
+
(cond
|
|
555
|
+
((or (= kind "portable-list")
|
|
556
|
+
(= kind "portable-vector")
|
|
557
|
+
(= kind "runtime-vector")
|
|
558
|
+
(= kind "host-array"))
|
|
559
|
+
(interop-from-js-sequence value kind context path depth))
|
|
560
|
+
((or (= kind "portable-map")
|
|
561
|
+
(= kind "runtime-map")
|
|
562
|
+
(= kind "host-map")
|
|
563
|
+
(= kind "host-object"))
|
|
564
|
+
(interop-from-js-map value kind context path depth))
|
|
565
|
+
(t
|
|
566
|
+
(interop-from-js-set
|
|
567
|
+
value kind context path depth))))))))))
|
|
568
|
+
|
|
569
|
+
(defun interop-to-js-object-map (source kind context path depth)
|
|
570
|
+
(let ((entries (interop-map-source source kind context path))
|
|
571
|
+
(result (funcall interop-object-factory))
|
|
572
|
+
(index 0))
|
|
573
|
+
(while (< index (js-length entries))
|
|
574
|
+
(let* ((entry (js-nth index entries))
|
|
575
|
+
(key (js-nth 0 entry)))
|
|
576
|
+
(when (not (= (value-type key) "string"))
|
|
577
|
+
(interop-fail
|
|
578
|
+
"ELI-INTEROP-INVALID-KEY"
|
|
579
|
+
"to-js-object requires string Map keys"
|
|
580
|
+
(interop-map-key-path path index)))
|
|
581
|
+
(funcall
|
|
582
|
+
interop-define-property
|
|
583
|
+
result key
|
|
584
|
+
(interop-child-to-js
|
|
585
|
+
(js-nth 1 entry) context
|
|
586
|
+
(interop-object-path path key) (1+ depth))))
|
|
587
|
+
(setq index (1+ index)))
|
|
588
|
+
result))
|
|
589
|
+
|
|
590
|
+
(defun to-js (value &optional options)
|
|
591
|
+
(interop-to-js-node value (interop-context options "to-js") "$" 0))
|
|
592
|
+
|
|
593
|
+
(defun to-js-object (value &optional options)
|
|
594
|
+
(let* ((context (interop-context options "to-js-object"))
|
|
595
|
+
(kind (interop-kind value)))
|
|
596
|
+
(interop-step context "$" 0)
|
|
597
|
+
(when (not (or (= kind "portable-map")
|
|
598
|
+
(= kind "runtime-map")
|
|
599
|
+
(= kind "host-map")
|
|
600
|
+
(= kind "host-object")))
|
|
601
|
+
(interop-fail
|
|
602
|
+
"ELI-INTEROP-UNSUPPORTED"
|
|
603
|
+
"to-js-object requires a persistent Map, native Map, or plain object"
|
|
604
|
+
"$"))
|
|
605
|
+
(interop-convert-container
|
|
606
|
+
value context "$"
|
|
607
|
+
(lambda ()
|
|
608
|
+
(if (= kind "host-object")
|
|
609
|
+
(interop-to-js-plain-object value context "$" 0)
|
|
610
|
+
(interop-to-js-object-map value kind context "$" 0))))))
|
|
611
|
+
|
|
612
|
+
(defun from-js (value &optional options)
|
|
613
|
+
(interop-from-js-node value (interop-context options "from-js") "$" 0))
|
|
614
|
+
|
|
615
|
+
(export array? from-js js-array js-map js-map? js-object js-set js-set?
|
|
616
|
+
object? to-js to-js-object))
|