Skip to content

Commit 41326b9

Browse files
committed
CLJS-1998: Printing an Object with a null prototype throws an error
Allow Objects with null constructors to be printable.
1 parent 4dd8c87 commit 41326b9

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9606,15 +9606,17 @@ reduces them without incurring seq initialization"
96069606
(regexp? obj) (write-all writer "#\"" (.-source obj) "\"")
96079607

96089608
:else
9609-
(if (.. obj -constructor -cljs$lang$ctorStr)
9609+
(if (some-> obj .-constructor .-cljs$lang$ctorStr)
96109610
(write-all writer
96119611
"#object[" (.replace (.. obj -constructor -cljs$lang$ctorStr)
96129612
(js/RegExp. "/" "g") ".") "]")
9613-
(let [name (.. obj -constructor -name)
9614-
name (if (or (nil? name) (gstring/isEmpty name))
9615-
"Object"
9616-
name)]
9617-
(write-all writer "#object[" name " " (str obj) "]")))))))
9613+
(let [name (some-> obj .-constructor .-name)
9614+
name (if (or (nil? name) (gstring/isEmpty name))
9615+
"Object"
9616+
name)]
9617+
(if (nil? (. obj -constructor))
9618+
(write-all writer "#object[" name "]")
9619+
(write-all writer "#object[" name " " (str obj) "]"))))))))
96189620

96199621
(defn- pr-writer
96209622
"Prefer this to pr-seq, because it makes the printing function

src/test/cljs/cljs/core_test.cljs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,10 @@
13591359
(testing "Resolve should return valid var"
13601360
(is (= 1 ((resolve 'first) [1 2 3])))))
13611361

1362+
(deftest test-cljs-1998
1363+
(testing "printing an Object with a null constructor"
1364+
(is (= "#object[Object]" (pr-str (.create js/Object nil))))))
1365+
13621366
(comment
13631367
;; ObjMap
13641368
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)