Fork me on GitHub

Hi everyone! I have a question about cljs/eval.. if I run eval on a "defn f ..." in my web app, and then eval the expr "(f ...)" it works! So it is saved somewhere. I thought maybe in the cljs/state object but I pass an empty one in each time. Also interestingly, the console logs a warning "undeclared Var cljs.user/f" but it still works. So my question is - where is the cross-call data being stored? How is it possible to invoke f on a separate call? And can I access this state?


I think it's just using the cljs.user.f value. If you type window.cljs.user.f into the console, does it show a value? If you change window.cljs.user.f to another value, does the call still work?


Ah this is exactly what I wanted to know. I didn't realize there was a window.cljs object for program state. But I can also eval "(dmacs.api/f ..)" and that will call a function f that I defined in the namespace. So eval has access to all program namespaces and functions through this window object?


when you compile without advanced optimizations, the values defined in namespaces are all stored at my.namespace.def_name, eg. dmacs.api/f => window.dmacs.api.f


Wow thank you so much. When I do get to optimizations, is there a way to preserve these namespaces?


in general, yes, you can use ^:export, eg:

(defn ^:export square [x]
  (* x x))


i'm not sure if cljs eval is supported with advanced optimizations


if you can, I would highly recommend avoiding eval usage in clojurescript

phronmophobic18:07:15 you can't avoid eval, you can check out and there's a #bootstrapped-cljs channel


using eval in cljs is neat, but its usage has several caveats and it will dramatically increase the size of your shipped js


Ah I see, it's definitely unavoidable for my application but this is good to know