ecmascript for guile
Ladies, gentlemen: behold, an ECMAScript compiler for Guile!
$ guile scheme@(guile-user)> ,language ecmascript Guile ECMAScript interpreter 3.0 on Guile 1.9.0 Copyright (C) 2001-2008 Free Software Foundation, Inc. Enter `,help' for help. ecmascript@(guile-user)> 42 + " good times!"; $1 = "42 good times!" ecmascript@(guile-user)> [0,1,2,3,4,5].length * 7; $2 = 42 ecmascript@(guile-user)> var zoink = { qux: 12, frobate: function (x) { return random(x * 2.0) * this.qux; } }; ecmascript@(guile-user)> zoink.frobate("4.2") $3 = 37.3717848761822
The REPL above parses ECMAScript expressions from the current input port, compiling them for Guile's virtual machine, then runs them -- just like any other Guile program.
Above you can see some of the elements of ECMAScript in action. The compiler implements most of ECMAScript 3.0, and with another few days' effort should implement the whole thing. (It's easier to implement a specification than to document differences to what people expect.)
The "frobate" example also shows integration with Guile -- the random function comes from current module, which is helpfully printed out in the prompt, (guile-user) above.
In addition, we can import other Guile modules as JavaScript, oops, I mean ECMAScript objects:
ecmascript@(guile-user)> require ('cairo'); $1 = #<<js-module-object> b7192810> ecmascript@(guile-user)> $1['cairo-version'](); $2 = 10800
I could automatically rename everything to names that are valid ES identifiers, but I figured that it's less confusing just to leave them as they are, and require ['%strange-names!'] to be accessed in brackets. Of course if the user wants, she can just rename them herself.
Neat hack, eh?
what the hell?
I realize that my readers might have a number of questions, especially those that have other things to do than to obsessively refresh my weblog. Well, since I had myself at my disposal, I decided to put some of these questions to me.
So, Andy, why did you implement this?
Well, I've been hacking on a compiler for Guile for the last year or so, and realized at some point that the compiler tower I had implemented gave me multi-language support for free.
But obviously I couldn't claim that Guile supported multiple languages without actually implementing another language, so that's what I did.
I chose ECMAScript because it's a relatively clean language, and one that doesn't have too large of a standard library -- because implementing standard libraries is a bit of a drag. Even this one isn't complete yet.
How does it perform? Is it as fast as those arachno-fish implementations I keep hearing about?
It's tough to tell, but it seems to be good enough. It's probably not as fast as compilers that produce native code, but because it hooks into Guile's compiler at a high level, as Guile's compiler improves and eventually gets native code compilation, it will be plenty fast. For now it feels snappy.
There is another way in which it feels much faster though, and that's development time -- having a real REPL with readline, a big set of library functions (Guile's), and fast compilation all make it seem like you're talking directly with the demon on the other side of the cursor.
It actually implements ECMAScript? And what about ES4?
ES3 is the current goal, though there are some bits that are lacking -- unimplemented parts of the core libraries, mainly. Probably there are some semantic differences as well, but those are considered bugs, not features. I'm just one man, except in interviews!
Well, there is one difference: how could you deny the full numeric tower to a language?
ecmascript@(guile-user)> 2 / 3 - 1 / 6; $3 = 1/2
And regarding future standards of ECMAScript, who knows what will happen. ES4 looks like a much larger language. Still, Guile is well-positioned to implement it -- we already have a powerful object system with multimethod support, and a compiler and runtime written in a high-level language, which count for a lot.
Awesome! So I can run my jQuery comet stuff on Guile!!1!!
You certainly could, in theory -- if you implemented XMLHttpRequest and the DOM and all the other things that JavaScript-in-a-web-browser implements. But that's not what I'm interested in, so you won't get that implementation from me!
Where do you see this going?
I see this compiler leading to me publishing a paper at some conference!
More seriously, I think it will have several effects. One will be that users of applications with Guile extensions will now be able to extend their applications in ECMAScript in addition to Scheme. Many more people know ECMAScript than Scheme, so this is a good thing.
Also, developers that want to support ES extensions don't have to abandon Scheme to do so. There are actually many people like this, who prefer Scheme, but who have some users that prefer ECMAScript. I'm a lover, not a fighter.
The compiler will also be an example for upcoming Elisp support. I think that Guile is the best chance we have at modernizing Emacs -- we can compile Elisp to Guile's VM, write some C shims so that all existing C code works, then we replace the Elisp engine with Guile. That would bring Scheme and ECMAScript and whatever other languages are implemented to be peers of Elisp -- and provide better typing, macros, modules, etc to Emacs. Everyone wins.
So how do I give this thing a spin?
Well, it's on a branch at the moment. Either you wait the 3 or 6 months for a Guile 2.0 release, or you check it out from git:
git clone git://git.sv.gnu.org/guile.git guile cd guile git fetch git checkout -b vm origin/vm ./autogen.sh && ./configure && make ./pre-inst-guile
Once you're in Guile, type ,language ecmascript to switch languages. This will be better integrated in the future.
Why haven't you answered my mail?
Mom, I've been hacking on a compiler! I'll call tonight ;-)
13 responses
Comments are closed.
Regardless of the performance of the implementation, this wins easily as best performance for the announcement! Congratulations.
Congrats Andy - this is great news. It seems the grand visions for Guile may come true after all.
Doing great cool stuff, that is Andy Wingo. :)
Nice work, but hasn't this already been done? I'm sure I already saw a JS -> Guile compiler a few years ago. Or maybe it was Python. Hmm.
Oh boy, you are crazy. And I like that :)
Keep up your rockarific crazyness :)
You're my favorite rockstar hacker. Emacs extensible in Scheme and Javascript? Heaven.
Beautiful hack!
Fantastic. This is an exciting news. Thanks Andy for your great work!
I love this awesome cracksmoke! That's really really neat.
Wow, this is excellent in just so many ways that I don't know how to fit them into one short comment!
You know, theoretically, Guile *does* have a DOM implementation.
Neat. Very cool, in fact. The Emacs Lisp thing looks particularly awesome.
The existing code would probably end up in one thread(and obviously one namespace) unless converted, I assume?
Andrew, why haven't you called yet?