3

I have a Clojurescript program running in the browser.

It imports a number of libraries, and then I want to allow the user to enter some small clojurescript "glue-code" that calls those libraries.

I can see (from https://cljs.github.io/api/cljs.js/eval) that you call eval with four arguments, the first being the state of the environment, which is an atom. But can I actually turn my current environment with all the functions I've required from elsewhere, into an appropriate argument to eval?

Update :

I thought that maybe I could set the namesspace for the eval using the :ns option of the third, opts-map, argument. I set it to the namespace of my application :

:ns "fig-pat.core"

But no difference.

Looking at the console, it's definitely the case that it's trying to do the evaluation, but it's complaining that names referenced in the eval-ed code are NOT recognised :

WARNING: Use of undeclared Var /square

for example. (square is a function I'm requiring. It's visible in the application itself ie. the fig-pat.core namespace)

I then get :

SyntaxError: expected expression, got '.'[Learn More]

Which I'm assuming this the failure of eval-ed expression as a whole.

Update 2 :

I'm guessing this problem might actually be related to : How can I get the Clojurescript namespace I am in from within a clojurescript program?

(println *ns*)

is just printing nil. So maybe Clojurescript can't see its own namespace.

And therefore the :ns in eval doesn't work?

akond
  • 15,865
  • 4
  • 35
  • 55
interstar
  • 26,048
  • 36
  • 112
  • 180

1 Answers1

3

Calling eval inside a clojurescript program is part of what is called "self-hosted clojurescript". In self-hosted clojurescript, namespaces are not available unless you implement a resolve policy. It means that have to let the browser know how to resolve the namespace e.g. loads a cljs file from a cdn.

It's not so trivial to implement namespace resolving properly. This is explained in a cryptic way in the docstring of load-fn from cljs.js namespace.

Several tools support namespaces resolving in self-host cljs running in the browser e.g Klipse and crepl

viebel
  • 19,372
  • 10
  • 49
  • 83