Skip to main content

Sorcerer 0.5 posted

Posted by kohsuke on October 2, 2006 at 8:21 AM PDT

I just posted a new version of sorcerer. It's been a combination of fun and frustration to work with JavaScript. It's fun, because JavaScript is really a different language from Java. It's nice to be able to write higher-order functions (functions that take functions and return functions, etc), and JavaScript's literal syntax goes very nicely with its prototype-based programing paradigm.

The biggest change in 0.5 is also related to JavaScript. I used to just have the tool generate colorized and annotated HTML files, then at runtime (when you are browing pages), JavaScript is only used for doing some dynamic stuff, like creating menus, etc. It turns out that this makes it hard to do reasoning about AST, and not really flexible. For example, even though AST has enough information to produce outline view on its own, it's rather hard to do if all you have is HTML (not to mention that file size bloat.) Another problem with static HTML is the cross-domain protection issue, which prevents sorcerer from showing/linking source files from multiple projects seamlessly.

So I thought perhaps it would be interesting if I generate AST as JavaScript, and generate source HTML, outline, and everything else from there, on the browser. For example, a Java program new Integer(5) would yield JavaScript fragment with(f) { run(n,_,T(1),P(L("5"))) where n='new' keyword, _=one space, T=reference to a type (defined in tables elsewhere), P=parenthesis, L=literal, and so on. I can execute this with 'f' that has different function definitions, and have it do different things. You can pack the fair amount of information in relatively small space this way. For example, 12 sequence of whitespace (often used in indent) is represented as just ws(12). The source and outline views are generated this way in 0.5, and I plan to do more with this — such as code folding, usage search of local variables, etc.

(To be fair, this does have some performance penalty. Even though the fully annotated static HTML is bigger, you still get it loaded progressively, which reduces the perceived loading time. JavaScript version is worse in that sense.)

But there's a lot of frustrating moments to work with JavaScript.

The top on my list is that JavaScript obscures its very nature, that is the prototype-based programming. For example, the basic operation of "creating a new object by using an existing object as a prototype" is not a primitive in JavaScript. Instead, you get the weird "new" keyword, which is simply out of place. I mean, when you don't have any "class", what does "new T()" really mean? But after reading the ECMAScript spec and finding this document, I felt much better.

Then there is a lack of tools. Some IDEs provide OK JavaScript support, but it's far inferior to what's available to Java developers when editing Java source files. I think the real problem here is the JavaScript language, not tools. Given that enough people want to mimic classical-OO in JavaScript, those should be available as language primitives (like maybe 'class' keyword or 'extends' keyword.) They are really just a syntax sugar, but I'd imagine those would greatly help tools, as they make JavaScript much easier to reason with. It helps human readers, too. It's somewhat analogous to how every program flow is a branch statement in assembly languages. You gain a lot by providing "syntax sugars" like for/while/if.

There're other tools that are missing, too. A good debugger is the one thing. FireBug is nice, but again it leaves much to be desired compared to Java debuggers (for example, conditional breakpoints, exception breakpoints, dropping stack frame, just to name a few.) Profilers would be nice to have, too. Testing framework is another.

All in all, I'm looking for another language that can be translated into JavaScript. Something like this (so that I can use all functional programming goodies), but I want static typing, so that tools can do a better job of making me productive. Maybe something like ML (but then I'm asking for great tool support, so I guess it can't be a new language.) I don't know.

So I'm thinking about playing with GWT. While Java isn't a functional programming language and so not necessarily a good fit here, it does have excellent tool support. It would be interesting how it plays out. In the mean time, please have fun with sorcerer! (and if you are interested in becoming a committer, let me know!)

Related Topics >>