planck-repl / planck

Stand-alone ClojureScript REPL
https://planck-repl.org
Eclipse Public License 1.0
1.03k stars 68 forks source link

Printing cyclic js objects overflows stack #427

Open brandonbloom opened 7 years ago

brandonbloom commented 7 years ago
cljs.user=> js/window
Maximum call stack size exceeded.

I realize that fipp doesn't help you here, but maybe the right thing to do for js objects is to use the underlying platform's inspection/printing?

mfikes commented 7 years ago

Perhaps Planck can monkey-patch this bit of code https://github.com/clojure/clojurescript/blob/47fa30dd95c850aa0643aae9c274c09bc202065c/src/main/cljs/cljs/core.cljs#L9297-L9302 or something could be revised in ClojureScript core to detect cycles in this case.

An interesting workaround:

(set! *print-level* 2)
cljs.user=> (set! *print-level* 2)
2
cljs.user=> js/window
#js {
...
:PLANCK_PRINT_FN #object[PLANCK_PRINT_FN "function PLANCK_PRINT_FN() {
    [native code]
}"], :PLANCK_PRINT_ERR_FN #object[PLANCK_PRINT_ERR_FN "function PLANCK_PRINT_ERR_FN() {
    [native code]
}"]}
cljs.user=>

If you use 3 for *print-level* and interesting Cannot convert a symbol to a string error is encountered.

mfikes commented 6 years ago

With Planck 2.8.1, js/window now produces a different "Cannot convert a symbol to a string" error when printed. So to repo, something like the following can now be done:

cljs.user=> (def x #js {:a 1})
#'cljs.user/x
cljs.user=> (goog.object/set x "me" x)
nil
cljs.user=> x
Maximum call stack size exceeded.