From 117e2d1390b727f256bd3493b7ef23111ceb533e Mon Sep 17 00:00:00 2001 From: Brandon Bloom Date: Mon, 4 Jun 2012 15:41:58 -0700 Subject: [PATCH] Fix printing and instance? checking RegExp objects --- src/clj/cljs/core.clj | 11 ++++++++--- src/cljs/cljs/core.cljs | 6 ++++-- test/cljs/cljs/core_test.cljs | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/clj/cljs/core.clj b/src/clj/cljs/core.clj index 1220ce1..7f7d3db 100644 --- a/src/clj/cljs/core.clj +++ b/src/clj/cljs/core.clj @@ -15,7 +15,7 @@ memfn ns or proxy proxy-super pvalues refer-clojure reify sync time when when-first when-let when-not while with-bindings with-in-str with-loading-context with-local-vars with-open with-out-str with-precision with-redefs - satisfies? identical? true? false? nil? str get + satisfies? instance? identical? true? false? nil? str get aget aset + - * / < <= > >= == zero? pos? neg? inc dec max min mod @@ -100,6 +100,11 @@ (defmacro undefined? [x] (bool-expr (list 'js* "(void 0 === ~{})" x))) +(defmacro instance? [t o] + (bool-expr (if (symbol? t) + (list 'js* "(~{} instanceof ~{})" o t) + `(let [t# ~t] (~'js* "(~{} instanceof ~{})" ~o t#))))) + (defmacro identical? [a b] (bool-expr (list 'js* "(~{} === ~{})" a b))) @@ -740,7 +745,7 @@ (cond ~@(mapcat (fn [[_ type name & cb]] - `[(instance? ~type ~e) (let [~name ~e] ~@cb)]) + `[(core/instance? ~type ~e) (let [~name ~e] ~@cb)]) catches) :else (throw ~e))) ~@fin) @@ -1029,4 +1034,4 @@ (if (zero? ~'argc) (~'f) ~(gen-apply-to-helper)))) - (set! ~'*unchecked-if* false))) \ No newline at end of file + (set! ~'*unchecked-if* false))) diff --git a/src/cljs/cljs/core.cljs b/src/cljs/cljs/core.cljs index c7d18ca..bc5eee6 100644 --- a/src/cljs/cljs/core.cljs +++ b/src/cljs/cljs/core.cljs @@ -954,7 +954,7 @@ reduces them without incurring seq initialization" (cljs.core/undefined? x)) (defn ^boolean instance? [t o] - (js* "(~{o} instanceof ~{t})")) + (cljs.core/instance? t o)) (defn ^boolean seq? "Return true if s satisfies ISeq" @@ -6093,10 +6093,12 @@ reduces them without incurring seq initialization" ;; handle CLJS ctors (and (not (nil? obj)) ^boolean (.-cljs$lang$type obj)) - (.cljs$lang$ctorPrSeq obj obj) + (.cljs$lang$ctorPrSeq obj obj) (satisfies? IPrintable obj) (-pr-seq obj opts) + (instance? js/RegExp obj) (list "#\"" (.-source obj) "\"") + :else (list "#<" (str obj) ">"))))) (defn- pr-sb [objs opts] diff --git a/test/cljs/cljs/core_test.cljs b/test/cljs/cljs/core_test.cljs index 792ed7e..f31d543 100644 --- a/test/cljs/cljs/core_test.cljs +++ b/test/cljs/cljs/core_test.cljs @@ -213,8 +213,8 @@ (assoc (cljs.core.ObjMap. nil (array) (js-obj)) :foo 5))) (assert (= "\"asdf\"" (pr-str "asdf"))) - (assert (= "[1 true {:a 2, :b 42} #]" - (pr-str [1 true {:a 2 :b 42} (array 3 4)]))) + (assert (= "[1 true {:a 2, :b #\"x\\\"y\"} #]" + (pr-str [1 true {:a 2 :b #"x\"y"} (array 3 4)]))) (assert (= "\"asdf\"\n" (prn-str "asdf"))) (assert (= "[1 true {:a 2, :b 42} #]\n" -- 1.7.9.1