Hacker News new | ask | show | jobs
by bsuh 3481 days ago
Can I write Clojure that gets compiled to JavaScript with ClojureScript and run that on JVM's JavaScript engine Nashorn?

Obviously turning code into JavaScript at least once is a mandatory requirement for fulfilling the "web" part of "web scale" and running the JavaScript on JVM gives me the "scale" part.

/snark

4 comments

I see the /snark, but I'll answer seriously.

Most JS code assumes it's targeting either a browser or node.js. Nashorn is very barebones, and can't pass for either. trying to run core.async on Nashorn didn't work for me because it expects either Window.setTimeout (which browsers use) or whatever-node-js uses for timers. I expect a large percentage of the libraries you'd want to use will be similarly affected.

Yes. Recent cljs using the nashorn repl anecdotally performs as well as unprofiled and unoptimized clojure for a bunch of common stuff I've tested. Theoretically you could reexpose the missing parts of the JVM clojure api to cljs on nashorn given that most of the missing parts of clojure are written in java. That would let you have things like the STM if you wanted them.
Oracle started a project called avatar-js that aims to clone the Node runtime in Nashorn. I've only POC'd with it a while back, not run in prod.