Skip to content

Commit

Permalink
[#610] Fixes for JS literals (#622)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Sep 30, 2021
1 parent a0dd7fa commit 1b75f77
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{:paths ["resources" "src"]
:deps {borkdude/edamame {:mvn/version "0.0.11"}
:deps {borkdude/edamame {:mvn/version "0.0.12"}
borkdude/sci.impl.reflector {:mvn/version "0.0.1"}}
:aliases
{:examples {:extra-paths ["examples"]}
Expand Down
28 changes: 20 additions & 8 deletions src/sci/impl/analyzer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
[sci.impl.utils :as utils :refer
[ana-macros constant? ctx-fn kw-identical? macro?
maybe-destructured rethrow-with-location-of-node set-namespace!]]
[sci.impl.vars :as vars])
[sci.impl.vars :as vars]
#?(:cljs [cljs.tagged-literals :refer [JSValue]]))
#?(:clj (:import [sci.impl Reflector]))
#?(:cljs
(:require-macros
Expand Down Expand Up @@ -1294,12 +1295,23 @@
analyzed-coll))

#?(:cljs
(defn analyze-js-obj [ctx expr]
(let [cljv (js->clj expr)
ana (analyze ctx cljv)]
(ctx-fn (fn [ctx bindings]
(clj->js (eval/eval ctx bindings ana)))
expr))))
(defn analyze-js-obj [ctx js-val]
(let [v (.-val js-val)]
(if (map? v)
(let [ks (keys v)
ks (map name ks)
vs (vals v)
vs (analyze-children ctx vs)]
(ctx-fn (fn [ctx bindings]
(apply js-obj (interleave ks (map #(eval/eval ctx bindings %) vs))))
js-val))
(let [vs (analyze-children ctx v)]
(ctx-fn (fn [ctx bindings]
(let [arr (array)]
(doseq [x vs]
(.push arr (eval/eval ctx bindings x)))
arr))
js-val))))))

(defn analyze
([ctx expr]
Expand All @@ -1326,7 +1338,7 @@
;; since a record is also a map
(record? expr) expr
(map? expr) (analyze-map ctx expr m)
#?@(:cljs [(object? expr) (analyze-js-obj ctx expr)])
#?@(:cljs [(instance? JSValue expr) (analyze-js-obj ctx expr)])
(vector? expr) (analyze-vec-or-set ctx
;; relying on analyze-children to
;; return a vector
Expand Down
10 changes: 5 additions & 5 deletions src/sci/impl/interop.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns sci.impl.interop
{:no-doc true}
#?(:clj (:import [sci.impl Reflector]))
(:require #?(:cljs [goog.object :as gobj])
(:require #?(:cljs [goog.object :as gobject])
#?(:cljs [clojure.string :as str])
[sci.impl.vars :as vars]))

Expand All @@ -12,7 +12,7 @@

(defn invoke-instance-method
#?@(:cljs [[obj _target-class method-name args]
;; gobj/get didn't work here
;; gobject/get didn't work here
(if (identical? \- (.charAt method-name 0))
(aget obj (subs method-name 1))
(if-let [method (aget obj method-name)]
Expand All @@ -31,8 +31,8 @@
:cljs [[class field-name-sym]])
#?(:clj (Reflector/getStaticField class (str field-name-sym))
:cljs (if (str/includes? (str field-name-sym) ".")
(apply gobj/getValueByKeys class (str/split (str field-name-sym) #"\."))
(gobj/get class field-name-sym))))
(apply gobject/getValueByKeys class (str/split (str field-name-sym) #"\."))
(gobject/get class field-name-sym))))

#?(:cljs
(defn invoke-js-constructor [constructor args]
Expand All @@ -59,7 +59,7 @@
:cljs [[class method-name] args])
#?(:clj
(Reflector/invokeStaticMethod class (str method-name) (object-array args))
:cljs (if-let [method (gobj/get class method-name)]
:cljs (if-let [method (gobject/get class method-name)]
(.apply method class (into-array args))
(let [method-name (str method-name)
field (get-static-field [class method-name])]
Expand Down
4 changes: 4 additions & 0 deletions test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,10 @@

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

0 comments on commit 1b75f77

Please sign in to comment.