Skip to content

Commit 1b75f77

Browse files
authored
[#610] Fixes for JS literals (#622)
1 parent a0dd7fa commit 1b75f77

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

deps.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{:paths ["resources" "src"]
2-
:deps {borkdude/edamame {:mvn/version "0.0.11"}
2+
:deps {borkdude/edamame {:mvn/version "0.0.12"}
33
borkdude/sci.impl.reflector {:mvn/version "0.0.1"}}
44
:aliases
55
{:examples {:extra-paths ["examples"]}

src/sci/impl/analyzer.cljc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
[sci.impl.utils :as utils :refer
1818
[ana-macros constant? ctx-fn kw-identical? macro?
1919
maybe-destructured rethrow-with-location-of-node set-namespace!]]
20-
[sci.impl.vars :as vars])
20+
[sci.impl.vars :as vars]
21+
#?(:cljs [cljs.tagged-literals :refer [JSValue]]))
2122
#?(:clj (:import [sci.impl Reflector]))
2223
#?(:cljs
2324
(:require-macros
@@ -1294,12 +1295,23 @@
12941295
analyzed-coll))
12951296

12961297
#?(:cljs
1297-
(defn analyze-js-obj [ctx expr]
1298-
(let [cljv (js->clj expr)
1299-
ana (analyze ctx cljv)]
1300-
(ctx-fn (fn [ctx bindings]
1301-
(clj->js (eval/eval ctx bindings ana)))
1302-
expr))))
1298+
(defn analyze-js-obj [ctx js-val]
1299+
(let [v (.-val js-val)]
1300+
(if (map? v)
1301+
(let [ks (keys v)
1302+
ks (map name ks)
1303+
vs (vals v)
1304+
vs (analyze-children ctx vs)]
1305+
(ctx-fn (fn [ctx bindings]
1306+
(apply js-obj (interleave ks (map #(eval/eval ctx bindings %) vs))))
1307+
js-val))
1308+
(let [vs (analyze-children ctx v)]
1309+
(ctx-fn (fn [ctx bindings]
1310+
(let [arr (array)]
1311+
(doseq [x vs]
1312+
(.push arr (eval/eval ctx bindings x)))
1313+
arr))
1314+
js-val))))))
13031315

13041316
(defn analyze
13051317
([ctx expr]
@@ -1326,7 +1338,7 @@
13261338
;; since a record is also a map
13271339
(record? expr) expr
13281340
(map? expr) (analyze-map ctx expr m)
1329-
#?@(:cljs [(object? expr) (analyze-js-obj ctx expr)])
1341+
#?@(:cljs [(instance? JSValue expr) (analyze-js-obj ctx expr)])
13301342
(vector? expr) (analyze-vec-or-set ctx
13311343
;; relying on analyze-children to
13321344
;; return a vector

src/sci/impl/interop.cljc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns sci.impl.interop
22
{:no-doc true}
33
#?(:clj (:import [sci.impl Reflector]))
4-
(:require #?(:cljs [goog.object :as gobj])
4+
(:require #?(:cljs [goog.object :as gobject])
55
#?(:cljs [clojure.string :as str])
66
[sci.impl.vars :as vars]))
77

@@ -12,7 +12,7 @@
1212

1313
(defn invoke-instance-method
1414
#?@(:cljs [[obj _target-class method-name args]
15-
;; gobj/get didn't work here
15+
;; gobject/get didn't work here
1616
(if (identical? \- (.charAt method-name 0))
1717
(aget obj (subs method-name 1))
1818
(if-let [method (aget obj method-name)]
@@ -31,8 +31,8 @@
3131
:cljs [[class field-name-sym]])
3232
#?(:clj (Reflector/getStaticField class (str field-name-sym))
3333
:cljs (if (str/includes? (str field-name-sym) ".")
34-
(apply gobj/getValueByKeys class (str/split (str field-name-sym) #"\."))
35-
(gobj/get class field-name-sym))))
34+
(apply gobject/getValueByKeys class (str/split (str field-name-sym) #"\."))
35+
(gobject/get class field-name-sym))))
3636

3737
#?(:cljs
3838
(defn invoke-js-constructor [constructor args]
@@ -59,7 +59,7 @@
5959
:cljs [[class method-name] args])
6060
#?(:clj
6161
(Reflector/invokeStaticMethod class (str method-name) (object-array args))
62-
:cljs (if-let [method (gobj/get class method-name)]
62+
:cljs (if-let [method (gobject/get class method-name)]
6363
(.apply method class (into-array args))
6464
(let [method-name (str method-name)
6565
field (get-static-field [class method-name])]

test/sci/core_test.cljc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,10 @@
11861186

11871187
#?(:cljs
11881188
(deftest eval-js-obj-test
1189+
(is (= 1 (sci/eval-string "(def o #js {:a 1}) (.-a o) "
1190+
{:classes {'js goog/global :allow :all}})))
1191+
(is (= :a (sci/eval-string "(def o #js {:a :a}) (.-a o) "
1192+
{:classes {'js goog/global :allow :all}})))
11891193
(is (= 1 (sci/eval-string "(def o #js {:a (fn [] 1)}) (.a o) "
11901194
{:classes {'js goog/global :allow :all}})))
11911195
(testing "js objects are not instantiated at read time, but at runtime, rendering new objects each time"

0 commit comments

Comments
 (0)