clojure -> javascript

65 views
Skip to first unread message

Rastislav Kassak

unread,
Aug 18, 2008, 4:58:59 AM8/18/08
to clo...@googlegroups.com
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.

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

Moxley Stratton

unread,
Aug 18, 2008, 9:51:52 AM8/18/08
to clo...@googlegroups.com
On Aug 18, 2008, at 1:58 AM, Rastislav Kassak wrote:

>
> 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.

Stuart Sierra

unread,
Aug 18, 2008, 10:02:15 AM8/18/08
to Clojure
On Aug 18, 4:58 am, "Rastislav Kassak" <kasou...@gmail.com> wrote:
> 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.

There are several projects that do this for Common Lisp and Scheme,
which might provide a useful foundation. Clojure's dot syntax
(.method object) is actually ideal for this sort of translation.

Probably the most complete:
http://common-lisp.net/project/parenscript/

Other attempts:
http://lispscript.sourceforge.net/
http://hop.inria.fr/usr/local/share/hop/weblets/home/articles/scheme2js/article.html
http://www-sop.inria.fr/mimosa/scheme2js/

-Stuart

Chouser

unread,
Aug 18, 2008, 2:32:15 PM8/18/08
to clo...@googlegroups.com
On Aug 18, 4:58 am, "Rastislav Kassak" <kasou...@gmail.com> wrote:
> is anybody interested in clojure to javascript compiler?

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

Stuart Sierra

unread,
Aug 18, 2008, 3:55:46 PM8/18/08
to Clojure
On Aug 18, 2:32 pm, Chouser <chou...@gmail.com> wrote:
> 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.

Even that would be useful, with the addition of macros.

> 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.

I'd say impossible.

> 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.

That's an interesting idea. There are plenty of Scheme interpreters
in JavaScript, but Clojure's data structures are quite a bit more
complicated. I'm guessing that this would be hard to get right,
particularly given the speed of Clojure's evolution.

So my first choice would be a thin wrapper that makes it easy to use
Clojure macros to generate syntactically-correct JavaScript without
worrying about recreating Clojure's semantics.

-Stuart

jim

unread,
Aug 18, 2008, 6:47:52 PM8/18/08
to Clojure
I've got a Javascript generator from Clojure data struct that I was
planning on putting up sometime. I haven't had time to polish it and
I'm working on other parts of my project, but I'd put it out there if
anyone's interested.

Jim

James Reeves

unread,
Aug 19, 2008, 3:35:35 AM8/19/08
to Clojure
On Aug 18, 9:58 am, "Rastislav Kassak" <kasou...@gmail.com> wrote:
> 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 was actually working on this for Compojure, but I scaled it back to
just providing support for JSON. Turns out the problem was more
difficult than I had originally thought!

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))))

Should probably become something like:

var x;
if (y) {
a; b; x = c;
}
else {
e; f; x = g;
}
while (x < 0) x = x - 1;
y;
alert(z);

I suspect you'd first have to do this in a two step process. First,
convert the Clojure code into a "javascript friendly" syntax tree, and
then convert the syntax tree into text. You'd have to work out some
transformation rules around javascript's control structures.

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.

--
James

Chouser

unread,
Aug 19, 2008, 12:25:30 PM8/19/08
to clo...@googlegroups.com
On Tue, Aug 19, 2008 at 3:35 AM, James Reeves
<weave...@googlemail.com> wrote:
> Turns out the problem was more
> difficult than I had originally thought!

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

Rastislav Kassak

unread,
Aug 19, 2008, 3:45:12 PM8/19/08
to clo...@googlegroups.com

Actually, this is something I was planning to start with. :)

>
>
> -Stuart
>
> >
>

levand

unread,
Aug 19, 2008, 9:27:09 AM8/19/08
to Clojure
The Google Web Toolkit does this with Java code - it compiles it to
JavaScript and (ostensibly) writes more efficient JavaScript than can
be written by hand, albeit entirely obfuscated.

It's an open source - it might be worth checking into, particularly
because they already provide JS emulation of most of the core Java
libraries. In fact... I'm not entirely sure how the internals of the
compiler work, but if it compiles the Javascript from the bytecode
rather than from the .java files, all of the heavy lifting to get it
to work with Clojure may already be done.

Something to look into, at any rate.

-Luke

Rastislav Kassak

unread,
Aug 20, 2008, 3:06:00 AM8/20/08
to clo...@googlegroups.com
I'm not sure, if it compiles from class files, at least it probably
wasn't true in the beginning (when I was studying GWT).
If you turn off obfuscation and optimization, translated source
contains the same variable names lines of code as original java
source.
However, it's good idea to dissect GWT sources because of emulated
standard java libraries.

Chouser

unread,
Sep 10, 2008, 1:37:18 AM9/10/08
to clo...@googlegroups.com
On Wed, Aug 20, 2008 at 3:06 AM, Rastislav Kassak <kaso...@gmail.com> wrote:
>
> I'm not sure, if it compiles from class files, at least it probably
> wasn't true in the beginning (when I was studying GWT).

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

PersistentVector.js

Randall R Schulz

unread,
Sep 10, 2008, 9:52:40 AM9/10/08
to clo...@googlegroups.com
On Tuesday 09 September 2008 22:37, Chouser wrote:
> On Wed, Aug 20, 2008 at 3:06 AM, Rastislav Kassak <kaso...@gmail.com>
wrote:
> > I'm not sure, if it compiles from class files, at least it probably
> > wasn't true in the beginning (when I was studying GWT).
>
> 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.

Perhaps Clojure-generated bytecode could be decompiled back to Java?


> ...
>
> --Chouser


Randall Schulz

Message has been deleted
Message has been deleted

Chouser

unread,
Dec 21, 2010, 8:34:32 AM12/21/10
to clo...@googlegroups.com
On Tue, Dec 21, 2010 at 6:51 AM, Shane Daniel <simr...@gmail.com> wrote:
> Hi everybody,
> Just for kicks I took Chouser's good start on the PersistentVector port and
> threw it in a Github gist, and demonstrated using it in jsFiddle. I love the
> cloud these days ;)
> Anyway, I hope you don't mind Chouser. I plan to refactor your code to use
> Javascript's prototype system, but really just wanted it online because it's
> an excellent starting point. Now it could be forked and demo'd easily.

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/

nickik

unread,
Dec 21, 2010, 11:17:18 AM12/21/10
to Clojure
The dream would be to have:

- Everything for clojure in clojure
- A nice compiler in clojure
- java speed clojure
- Collections and multimethodes in clojure
- A js generating backend for the compiler that works with GWT for the
required java stuff

Unfortunately I do not (jet) have the skill to do any of that and this
will takes a very long time (if ever).

In the mean time I think Chouser "In-between" sound pretty good and I
think it would be the most practical. Write clojure code, use the
clojure core functions, be able to use macros and clojure collections
would already be a great project.
Reply all
Reply to author
Forward
0 new messages