Skip to content

Commit 1e794f0

Browse files
mfikesdnolen
authored and
dnolen
committed
CLJS-1612: Resolve ns aliases in syntax quote
1 parent 28e040d commit 1e794f0

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@
26262626
:default (str x))))
26272627

26282628
(defn resolve-symbol [s]
2629-
(:name (resolve-var {:ns {:name *cljs-ns*}} s)))
2629+
(:name (resolve-var (assoc @env/*compiler* :ns (get-namespace *cljs-ns*)) s)))
26302630

26312631
#?(:clj
26322632
(defn forms-seq*

src/main/clojure/cljs/core.cljc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@
636636
(core/let [gmap (gensym "map__")
637637
defaults (:or b)]
638638
(core/loop [ret (core/-> bvec (conj gmap) (conj v)
639-
(conj gmap) (conj `(if (implements? ISeq ~gmap) (apply hash-map ~gmap) ~gmap))
639+
(conj gmap) (conj `(if (implements? ISeq ~gmap) (apply cljs.core/hash-map ~gmap) ~gmap))
640640
((core/fn [ret]
641641
(if (:as b)
642642
(conj ret (:as b) gmap)
@@ -654,8 +654,8 @@
654654
bk (val (first bes))
655655
has-default (contains? defaults bb)]
656656
(recur (pb ret bb (if has-default
657-
(core/list `get gmap bk (defaults bb))
658-
(core/list `get gmap bk)))
657+
(core/list 'cljs.core/get gmap bk (defaults bb))
658+
(core/list 'cljs.core/get gmap bk)))
659659
(next bes)))
660660
ret))))]
661661
(core/cond
@@ -1391,7 +1391,7 @@
13911391
`(fn ~[this-sym argsym]
13921392
(this-as ~this-sym
13931393
(.apply (.-call ~this-sym) ~this-sym
1394-
(.concat (array ~this-sym) (aclone ~argsym)))))
1394+
(.concat (array ~this-sym) (cljs.core/aclone ~argsym)))))
13951395
(meta form)))]
13961396
(ifn-invoke-methods type type-sym form))))
13971397

@@ -1714,7 +1714,7 @@
17141714
`(~'-lookup [this# ~ksym else#]
17151715
(case ~ksym
17161716
~@(mapcat (core/fn [f] [(keyword f) f]) base-fields)
1717-
(get ~'__extmap ~ksym else#)))
1717+
(cljs.core/get ~'__extmap ~ksym else#)))
17181718
'ICounted
17191719
`(~'-count [this#] (+ ~(count base-fields) (count ~'__extmap)))
17201720
'ICollection
@@ -2366,13 +2366,13 @@
23662366
`(js/Array. ~size))
23672367
assoc :tag 'array))
23682368
([type size]
2369-
`(make-array ~size))
2369+
`(cljs.core/make-array ~size))
23702370
([type size & more-sizes]
23712371
(vary-meta
23722372
`(let [dims# (list ~@more-sizes)
2373-
dimarray# (make-array ~size)]
2373+
dimarray# (cljs.core/make-array ~size)]
23742374
(dotimes [i# (alength dimarray#)]
2375-
(aset dimarray# i# (apply make-array nil dims#)))
2375+
(aset dimarray# i# (apply cljs.core/make-array nil dims#)))
23762376
dimarray#)
23772377
assoc :tag 'array)))
23782378

@@ -2471,7 +2471,7 @@
24712471
array ret."
24722472
[a idx ret expr]
24732473
`(let [a# ~a
2474-
~ret (aclone a#)]
2474+
~ret (cljs.core/aclone a#)]
24752475
(loop [~idx 0]
24762476
(if (< ~idx (alength a#))
24772477
(do
@@ -2556,7 +2556,7 @@
25562556
prefer-table# (atom {})
25572557
method-cache# (atom {})
25582558
cached-hierarchy# (atom {})
2559-
hierarchy# (get ~options :hierarchy (cljs.core/get-global-hierarchy))]
2559+
hierarchy# (cljs.core/get ~options :hierarchy (cljs.core/get-global-hierarchy))]
25602560
(cljs.core/MultiFn. (cljs.core/symbol ~mm-ns ~(name mm-name)) ~dispatch-fn ~default hierarchy#
25612561
method-table# prefer-table# method-cache# cached-hierarchy#))))))
25622562

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(ns cljs.syntax-quote-test
2+
(:require [cljs.test :as test :refer-macros [deftest is]]))
3+
4+
(deftest test-syntax-quote
5+
(is (= 'cljs.syntax-quote-test/foo `foo))
6+
(is (= 'cljs.test/test-vars `test/test-vars))
7+
(is (= 'cljs.test/deftest `test/deftest))
8+
(is (= 'cljs.test/foo `test/foo)))

src/test/cljs/test_runner.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[cljs.top-level]
1414
[cljs.reducers-test]
1515
[cljs.keyword-test]
16+
[cljs.syntax-quote-test]
1617
[cljs.import-test]
1718
[cljs.ns-test.foo]
1819
[cljs.pprint]))
@@ -32,6 +33,7 @@
3233
'cljs.macro-test
3334
'cljs.top-level
3435
'cljs.keyword-test
36+
'cljs.syntax-quote-test
3537
'cljs.ns-test
3638
'cljs.ns-test.foo
3739
'foo.ns-shadow-test

src/test/self/self_host/test.cljs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@
4747
(defn elide-env [env ast opts]
4848
(dissoc ast :env))
4949

50+
(defn str-evals-to
51+
"Checks that a string evaluates to an expected value."
52+
([st l expected s]
53+
(str-evals-to st l expected nil))
54+
([st l expected s opts]
55+
(cljs/eval-str st
56+
s
57+
nil
58+
(merge
59+
{:context :expr
60+
:eval node-eval}
61+
opts)
62+
(fn [{:keys [error value]}]
63+
(is (nil? error))
64+
(is (= expected value))
65+
(inc! l)))))
66+
5067
;; NOTE: can't set passes because callbacks happen _inside_ binding
5168
;; do so will effect other tests
5269

@@ -663,6 +680,34 @@
663680
(is (nil? error))
664681
(is (= :x value)))))
665682

683+
(deftest test-CLJS-1612
684+
(async done
685+
(let [st (cljs/empty-state)
686+
l (latch 10 done)]
687+
(cljs/eval st '(ns foo.core
688+
(:require [bar.core :as bar]))
689+
{:load (fn [{:keys [macros]} cb]
690+
(if macros
691+
(cb {:lang :clj :source "(ns bar.core) (defmacro add [a b] `(+ ~a ~b))"})
692+
(cb {:lang :clj :source "(ns bar.core (:refer-macros bar.core)) (defn sub [a b] (- a b))"})))}
693+
(fn [_] (inc! l)))
694+
(testing "various syntax quote patterns"
695+
(str-evals-to st l 'foo.core/foo "`foo" {:ns 'foo.core})
696+
(str-evals-to st l 'bar.core/sub "`bar/sub" {:ns 'foo.core})
697+
(str-evals-to st l 'bar.core/add "`bar/add" {:ns 'foo.core})
698+
(str-evals-to st l 'bar.core/undeclared "`bar/undeclared" {:ns 'foo.core}))
699+
(testing "core macros under syntax quote"
700+
(str-evals-to st l 13
701+
"(do (defmulti bar (fn [x y] [x y])) 13)" {:ns 'foo.core})
702+
(str-evals-to st l 17
703+
"(do (deftype FnLikeB [a] IFn (-invoke [_] a)) 17)" {:ns 'foo.core})
704+
(str-evals-to st l [10 4]
705+
"(let [{:keys [a b] :or {b 4}} {:a 10}] [a b])" {:ns 'foo.core})
706+
(str-evals-to st l [[nil]]
707+
"(js->clj (make-array nil 1 1))" {:ns 'foo.core})
708+
(str-evals-to st l [1 1 1 1 1]
709+
"(let [an-array (int-array 5 0)] (js->clj (amap an-array idx ret (+ 1 (aget an-array idx)))))" {:ns 'foo.core})))))
710+
666711
#_(deftest test-eval-str-with-require
667712
(async done
668713
(let [l (latch 3 done)]

0 commit comments

Comments
 (0)