is anybody interested in clojure to javascript compiler?
I'm fighting javascript last days a bit and it seems as good idea to
try to target javascript instead of jvm for (subset of) clojure.
I think clojure is the most appropriate lisp derivate for such task.
There is also possible future evolution in integration of client and
serverside computing - everything is clojure...
Just gathering ideas... :)
RK
>
> Hello,
>
> is anybody interested in clojure to javascript compiler?
> I'm fighting javascript last days a bit and it seems as good idea to
> try to target javascript instead of jvm for (subset of) clojure.
Yes, count me in as interested.
>
> I think clojure is the most appropriate lisp derivate for such task.
> There is also possible future evolution in integration of client and
> serverside computing - everything is clojure...
I have heard of such a thing, and I know there's one written in Java
(which is ironic). Having a Clojure-to-Javascript compiler could be
good thing. I am curious to see how much benefit can be obtained.
This has been mentioned: http://clojure-log.n01se.net/date/2008-05-29.html#15:26
In fact, cgrand even has a bit of something working:
http://code.google.com/p/clj-stuff/wiki/TemplatingExamples#Javascript_HTML_templating_examples
It seems to me there are at least 3 different levels of
"compatibility" one could shoot for. The simplest (which I think is
what cgrand is attempting) is a sort of Clojure-like JavaScript. The
syntax looks like Clojure, but it's the thinnest layer possible --
something that looks like a Clojure vector might really be a
JavaScript array (perhaps with copy-the-whole-array-on-write).
Strings would be JavaScript strings and therefore not completely
compatible with Java strings.
The most-compatible might try to get most existing Clojure code to
work without changes. This would mean not only a faithful port of all
the persistent data structures to JavaScript, but also similar ports
or wrappers for much of the Java standard libraries (strings, numbers,
etc.). Sounds like a huge project.
In-between might be something that provides the built-in Clojure stuff
(functions, namespaces, persistent data structures, etc.) faithfully
in JavaScript, but all the host syntax stuff (. foo bar) would be
direct JavaScript calls. This would allow one to pretty easily spot
on site what Clojure code could be compiled directly to JavaScript and
what would have to be re-written.
...and there's probably other levels of compatibility in between or
outside this list. Are we sure we're all talking about the same
thing? :-) I'm not even sure what I really want.
--Chouser
Well, that's generally true of this type of problem I think.
> The main difficulty is in getting the control structures converted in
> a way that produces idiomatic javascript. For instance:
>
> (loop [x (if y (do a b c) (do e f g))]
> (if (< x 0)
> (recur (- x 1))
> (alert (do y z))))
Or how about:
(function(x){
(x < 0)
? arguments.callee( x - 1 )
: alert((y,z))
})( y ? (a, b, c) : (e, f, g) )
> You could take the easy way out and make "if" into a javascript
> function, I guess. With liberal use of anonymous functions, you can
> replicate the exact structure of the Clojure code without need for a
> complex compiler. On the down side, it would probably be slow and hard
> to read.
I guess that's the anonymous functions you're talking about, although
it's not *that* many or too terribly slow I'd think. It's unfortunate
the loop init expression comes in a different order. And I guess that
construct will blow the stack unexpectedly.
It's not exactly idiomatic JavaScript, but I don't think the Java that
real Clojure generates is exactly idiomatic either. If it get's the
job done (and especially if the resulting JS lines up line-for-line
with the ClojureScript source), I'd be happy.
Well, as you said -- harder that it first looks.
--Chouser
Actually, this is something I was planning to start with. :)
>
>
> -Stuart
>
> >
>
GWT appears to still compile from .java sources (not .class files). I
flailed around with it a bit today, trying to get some of Clojure's
collections to compile, since they're written in Java. In the end, I
don't think that'll be worth it.
Instead, here a first cut at PersistentVector ported to JavaScript. I
intend to do some timing tests to see if different arrangements might
run faster, but at least this seems to work. To use it, you can do
this sort of thing in JavaScript:
var v = PersistentVector.EMPTY;
for( var i = 0; i < 100; ++i ) {
v = v.cons( i );
}
The other methods work just like the Java version: v.peek(), v.pop(),
v.nth( i ), v.assocN( i, val ), v.count(), etc.
The only create method I wrote takes a JS array:
PersistentVector.create( ["a", "b", "c"] )
--Chouser
Perhaps Clojure-generated bytecode could be decompiled back to Java?
> ...
>
> --Chouser
Randall Schulz
I'd be happy if someone were to get more use out it. Eventually
I hope we have JS generated from a pure-Clojure implementations
of all the collection types, but until then I guess this will
have to do. Thanks for getting it out there.
--Chouser
http://joyofclojure.com/