Open dundalek opened 6 years ago
Thanks for the report. The issue seems to be coming from here, if you wanna dig deeper: https://github.com/anmonteiro/lumo/blob/dc2f4f7fa21bec272b4d3c50e8b1d7a71a8d450c/src/cljs/snapshot/lumo/repl.cljs#L860
Awesome, thanks for the directions @anmonteiro. I did some digging and found out that some bindings were getting missing. Then I noticed the execute
functions did re-binding in every call so I tried out following workaround to capture the values and rebind them:
(ns repro.core
(:require [cljs.reader]
[lumo.repl]
[cljs.env :as env]
[cljs.js :as cljs]))
(def ns *ns*)
(def compiler env/*compiler*)
(def eval-fn cljs/*eval-fn*)
(def load-fn cljs/*load-fn*)
(defn my-eval []
(binding [cljs/*eval-fn* eval-fn
cljs/*load-fn* load-fn]
(-> "(+ 2 3)"
(cljs.reader/read-string)
(lumo.repl/eval ns compiler)
(pr-str)
(js/console.log))))
(my-eval)
(js/setTimeout my-eval 1)
Now it works as expected. I assume that fix would be to do something similar inside repl/eval
function.
I'm not certain it would work if we do the same inside the lumo.repl/eval
function. I'm pretty sure the bindings are lost in the async setTimeout
call, so they wouldn't be there anyway.
Long time ago @darwin came up with a library for solving this exact problem. I cannot find it now but I will link it if I do.
I did some more digging today and found out that lumo.repl/execute-text
works even inside setTimeout
:
(ns repro.core
(:require [cljs.reader]
[lumo.repl]))
(defn my-eval []
(-> "(+ 2 3)"
(lumo.repl/execute-text {:expression? true})))
(my-eval)
(js/setTimeout my-eval 1)
It seems the difference is that it first sets the state with set-session-state-for-session-id!
and then sets the necessary bindings.
I don't know if still relevant, but the project is https://github.com/binaryage/cljs-zones and it basically does exactly that: save context somewhere before giving up control and then restoring it back afterwards.
Hi, I am experimenting with enhancing a cljs repl and I need to pass the user input through a custom function to modify behavior. I am trying to leverage Lumo for that. I am using lumo.core.eval in a script and am running into a strange behavior. Here is a distilled down repro case
repro.cljs
:I run it with
lumo repro.cljs
and expect this output:Actual output in Lumo 1.8.0-beta (on Ubuntu Linux 16.04):
The first invocation works but the second one which is deferred throws. Seems like a bug but maybe I just need to set up things differently?