Calling Clojurescript code from Clojure

269 views
Skip to first unread message

Brent Millare

unread,
Sep 16, 2012, 7:05:41 PM9/16/12
to clo...@googlegroups.com
Background: I'm developing a web based development environment.

Problem: Is there a way to evaluate clojurescript code from clojure? I need to call clojurescript code like (load-file "foo.cljs") from clojure. Note this is different then starting a clojurescript repl and typing in (load-file "foo.cljs"). I'm already doing this. It needs to be more like (eval-cljs cljs-foo-form). How does one do this?

David Nolen

unread,
Sep 17, 2012, 12:04:25 PM9/17/12
to clo...@googlegroups.com
By evaluate do you mean compile and run? What about compiling the code
to JS and running it via Rhino?

David

Brent Millare

unread,
Sep 17, 2012, 11:27:41 PM9/17/12
to clo...@googlegroups.com
I forgot to mention an additional condition, this should work with the browser as an eval environment

Brent Millare

unread,
Sep 18, 2012, 12:03:17 AM9/18/12
to clo...@googlegroups.com
And yes by eval I mean compile and run on the target (browser)

Shantanu Kumar

unread,
Sep 18, 2012, 12:03:18 AM9/18/12
to Clojure


On Sep 18, 8:27 am, Brent Millare <brent.mill...@gmail.com> wrote:
> I forgot to mention an additional condition, this should work with the browser as an eval environment

Quiddity (URL below) may not load an entire file, but you can eval an
S-expression by supplying all values:

https://github.com/kumarshantanu/quiddity

Shantanu

Frank Siebenlist

unread,
Sep 18, 2012, 2:00:08 AM9/18/12
to clo...@googlegroups.com, Frank Siebenlist
I have the same requirement to have a clojurescript-form in my clojure-environment that I want to evaluate in the browser…

To make the following code-snippet work, you're supposed to have a browser-repl session running, and start a new repl-session on that same JVM from where you invoke the following forms to execute either javascript-code or clojurescript-forms in the browser.

---
user=> (require 'cljs.repl)
nil

user=> (require 'cljs.repl.browser)
nil

user=> (cljs.repl.browser/browser-eval "alert('No Way!')")
{:status :success, :value ""}

user=> (def my-repl-env {:port 9000, :optimizations :simple, :working-dir ".lein-cljsbuild-repl", :serve-static true, :static-dir ["." "out/"], :preloaded-libs []})
#'user/my-repl-env

user=> (def my-env {:context :statement :locals {}})
#'user/my-env

user=> (#'cljs.repl/eval-and-print my-repl-env my-env '(js/alert "Yes Way!"))
nil
nil

user=>
---

The public function "cljs.repl.browser/browser-eval" seems to allow you to send javascript code as a string to the browser to execute over the existing browser-repl connection.

The private function "cljs.repl/eval-and-print" will take a clojurescript form, compile it to javascript and send it to the browser for execution. The "my-repl-env" and "my-env" values are artifacts needed that are normally only available within the context of the function.

My apology for this huge&ugly hack… please see it as a proof of principle.

There may be much more elegant solutions available… I've only scratch the surface of understanding how this clojurescript repl works in detail.

-Enjoy, FrankS


On Sep 17, 2012, at 9:03 PM, Brent Millare <brent....@gmail.com> wrote:

> And yes by eval I mean compile and run on the target (browser)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Brent Millare

unread,
Sep 18, 2012, 11:08:10 AM9/18/12
to clo...@googlegroups.com
Shantanu,

Quiddity does not fulfill my requirements since I need to control the environment from interop such as load-file and interop.

Brent Millare

unread,
Sep 18, 2012, 11:16:16 AM9/18/12
to clo...@googlegroups.com, Frank Siebenlist
Hi Frank,

That is pretty much exactly as I wanted. The only problem is that you are calling a private fn and therefore the code is susceptible to change. I think this should be looked into more and become officially supported. This is a great hook that opens up the door to a more interactive development experience for clojurescript.

Brent Millare

unread,
Sep 18, 2012, 11:27:57 AM9/18/12
to clo...@googlegroups.com, Frank Siebenlist
Actually, after looking through the source myself, I noticed eval-and-print just calls evaluate-form, which is better since its public. I can take care of printing myself so this is probably better.
Reply all
Reply to author
Forward
0 new messages