Skip to content

Commit b6dc7f3

Browse files
authored
Fix #1001: regression of #987: interop with reserved JS keyword fails
1 parent bd3555c commit b6dc7f3

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

src/sci/impl/analyzer.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@
957957
(subs method-name 1)
958958
method-name)
959959
meth-name* meth-name
960-
meth-name (munge meth-name)
960+
meth-name (#?(:clj munge :cljs utils/munge-str) meth-name)
961961
stack (assoc (meta expr)
962962
:ns @utils/current-ns
963963
:file @utils/current-file)]

src/sci/impl/resolve.cljc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
[sym (if (and call? #?(:clj (not (str/starts-with? sym-name-str "."))))
7272
(with-meta
7373
[clazz #?(:clj sym-name
74-
:cljs (.split (str sym-name) "."))
74+
:cljs (.split (utils/munge-str (str sym-name)) "."))
7575
sym-ns]
7676
#?(:clj
7777
(if (= "new" sym-name-str)
@@ -87,7 +87,8 @@
8787
(let [stack (assoc (meta sym)
8888
:file @utils/current-file
8989
:ns @utils/current-ns)
90-
path (.split (str sym-name) ".")
90+
sym-name (utils/munge-str (str sym-name))
91+
path (.split sym-name ".")
9192
len (alength path)]
9293
(if (== 1 len)
9394
(->Node
@@ -250,6 +251,7 @@
250251
#?(:cljs
251252
(when-let [[v segments] (resolve-prefix+path ctx sym m)]
252253
(let [v (if (utils/var? v) (deref v) v)
254+
segments (mapv utils/munge-str segments)
253255
segments (into-array segments)]
254256
;; NOTE: there is a reloading implication here...
255257
(if call?

src/sci/impl/utils.cljc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
{:no-doc true}
33
(:refer-clojure :exclude [eval demunge var? macroexpand macroexpand-1])
44
(:require [clojure.string :as str]
5+
#?(:cljs [goog.object :as gobject])
56
[sci.impl.macros :as macros]
67
[sci.impl.types :as t]
78
[sci.impl.vars :as vars]
89
[sci.lang :as lang])
10+
#?(:cljs (:import [goog.string StringBuffer]))
911
#?(:cljs (:require-macros [sci.impl.utils :refer [kw-identical? dotimes+]])))
1012

1113
#?(:clj (set! *warn-on-reflection* true))
@@ -307,3 +309,16 @@
307309
'*unchecked-math* clojure.core/*unchecked-math*
308310
{:ns clojure-core-ns
309311
:doc "While bound to true, compilations of +, -, *, inc, dec and the\n coercions will be done without overflow checks. While bound\n to :warn-on-boxed, same behavior as true, and a warning is emitted\n when compilation uses boxed math. Default: false."})))
312+
313+
#?(:cljs
314+
(defn ^string munge-str [name]
315+
(let [sb (StringBuffer.)]
316+
(loop [i 0]
317+
(when (< i (. name -length))
318+
(let [c (.charAt name i)
319+
sub (gobject/get CHAR_MAP c)]
320+
(if-not (nil? sub)
321+
(.append sb sub)
322+
(.append sb c))
323+
(recur (inc i)))))
324+
(.toString sb))))

test/sci/interop_test.cljc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,19 @@
394394
))
395395

396396
#?(:cljs
397-
(deftest issue-987-munged-property-name-test
397+
(deftest issue-987-munged-method-or-property-name-test
398398
(is (= 1 (sci/eval-string "(def x #js {:foo_bar 1}) (.-foo-bar x)" {:classes {:allow :all}})))
399399
(is (= 1 (sci/eval-string "(def x #js {:foo_bar (fn [] 1)}) (.foo-bar x)" {:classes {:allow :all}})))
400-
(is (= {:foo_bar 1} (sci/eval-string "(js->clj (doto #js {} (set! -foo-bar 1)) :keywordize-keys true)" {:classes {:allow :all}})))))
400+
(testing "reserved keyword munging is bypassed"
401+
(is (= 1 (sci/eval-string "(def x #js {:catch (fn [] 1)}) (.catch x)" {:classes {:allow :all}})))
402+
(is (= 1 (sci/eval-string "d/foo-bar" {:imports {'d 'dude}
403+
:classes {:allow :all 'dude #js {:foo_bar 1}}})))
404+
(is (= 1 (sci/eval-string "(d/foo-bar)" {:imports {'d 'dude}
405+
:classes {:allow :all 'dude #js {:foo_bar (fn [] 1)}}}))))
406+
(is (= {:foo_bar 1} (sci/eval-string "(js->clj (doto #js {} (set! -foo-bar 1)) :keywordize-keys true)" {:classes {:allow :all}})))
407+
(testing "dotted access"
408+
(is (= 2 (sci/eval-string "(def x #js {:a 1 :foo_bar #js {:catch 2}}) x.foo-bar.catch" {:classes {'js goog/global}})))
409+
(is (= 3 (sci/eval-string "(let [a #js {:foo_bar #js {:catch 3}}] a.foo-bar.catch)"))))))
401410

402411
#?(:clj
403412
(deftest issue-987-deftype-munged-fields-test

0 commit comments

Comments
 (0)