

Clamato: A Smalltalk Dialect for Javascript - BigZaphod
http://clamato.net/

======
ggchappell
The use of JS as an object code seems to be a big trend lately. This makes me
wonder:

(1) How suitable is JS for use as an object code?

(2) Is this trend taken into account by those designing the future of JS?

~~~
gruseom
_How suitable is JS for use as an object code?_

Highly. I think it's a much nicer language to compile to than to write by hand
(although JS is a pretty good language), especially if you want to use a very
high-level language and have your code run in a web browser. We compile to JS
from Common Lisp using a set of macros built on top of the excellent
Parenscript library. We've built things up incrementally to the point that
we're now able to write most of our app in a subset of Common Lisp that
compiles to efficient JS. The other day I took some Lisp code I wrote a year
ago and generated JS from it without having to change it. That's the first
time that's happened. (We'll probably open-source this eventually as a layer
on top of Parenscript.)

A big advantage of this approach over JS libraries is that you can emit quite
low-level JS, so you don't have to pay for all that abstraction at runtime.

Having said the above, I'll partly take it back. JS isn't completely object
code in the sense that you can generate it and forget about it. You need to be
able to debug it in the web browser, and that means you need to be able to
read it. In that sense, it's not really object code.

~~~
Hexstream
"Having said the above, I'll partly take it back. JS isn't completely object
code in the sense that you can generate it and forget about it. You need to be
able to debug it in the web browser, and that means you need to be able to
read it. In that sense, it's not really object code."

I'm pretty sure it must be possible to 1. preserve the "semantic trace" of
compilation from high-level Lisp to low-level JS and then 2. write a high-
level debugger that, given a piece of object-code that went wrong, points to
the high-level code that generated it.

A complete solution might be intractable but even a partial solution could
greatly help debugging.

~~~
olavk
I compile from ECMAScript 4 to JS, and what I do is create a separate mapping
file, where line/char pos for every token in the generated file is mapped to
file/line/char in the source.

When the JS engine throws a runtime error it typically (depending on the
engine) includes line/char pos for the point of error. A wrapper then extract
line/char from the error message, finds the corresponding position in the
original source, and displays that also. This greatly helps debugging runtime
errors.

It is not perfect, since some engines (e.g. Rhino) doesn't include position
for runtime errors.

------
akkartik
Code: <http://bitbucket.org/avibryant/clamato>

------
tumult
I hate you! I was looking for a Smalltalk->Javascript thing before I started
my current project, but I couldn't find one other than st2js.
Arrrgghhhhhhafuhoasdf

asdfiojafdiojafdsooijfa

Anyway, thanks, this is awesome.

~~~
avibryant
It's not really in the shape I would have wanted for a public release... kudos
to the submitter for finding the website, I guess :). But if you find it
useful, great.

~~~
stcredzero
_There are several departures from Smalltalk’s syntax and semantics. The most
notable of these are: no explicit returns, 0-based indexing, no metaclass
hierarchy, special syntax for instance variables._

I can live with no metaclasses. Special syntax for instance variables isn't
that much of a problem. 0-based indexing -- this should be easy to take care
of with your own collection classes.

However, no explicit returns -- this one kills me.

I guess this has to do with Javascript.

EDIT: JS has exception handling, so this could be used to implement explicit
returns.

~~~
gruseom
The problem with using exception handling for explicit returns is that you'd
need to put a try/catch block at the top level of every function. That seems
yuck. Or am I missing something?

~~~
avibryant
Exactly. For those times that you really need them, I added a #return: method
that you can use explicitly:

self return: [:ret | .... ret value: foo ...].

But you only pay the cost of the try/catch/throw when you actually use it.

FWIW, I've found that the lack of explicit returns really isn't a problem, at
least in the code I've written and ported so far.

~~~
gruseom
I've been offline for a few days and just read this:

 _But you only pay the cost of the try/catch/throw when you actually use it._

Do you mean that the existence of an explicit return is recognized at compile
time, so only the functions that actually contain one need to be wrapped in a
try-catch? Now that I think of it, that seems like the best way to do it. I've
wanted to be able to compile _return-from_ into JS for a while...

------
akkartik
After all these years reading about it, the clamato tutorial was the first
time I got to actually try out a code browser interface. Very cool.

~~~
lucifer
"a browser fit for the gods" <G>

------
extension
The current Ruby to JS project: <http://www.ntecs.de/projects.html>

After some recent ambitious JS development, it's become clear to me that the
language is just unsuited to large scale development and no library is going
to change that.

As such, I am really keen on these compiler projects, but I think it's a
mistake to avoid language features because they are difficult to implement in
JS. Syntax is nice, but it's those language features that are really needed.

~~~
avibryant
It depends on your goals. For me, it's not so much about difficult to
implement as whether the generated JS is idiomatic enough for the various
optimizing JS implementations (V8, SquirrelFish, TraceMonkey) to handle it.
With Clamato, we've shied away from implementing language features until we
could see how to do them in a way that wouldn't hurt optimization (the only
exception I can think of is the Smalltalk cascade syntax, which is implemented
naively and slowly for now, but it's obvious that with a bit more work it
could be sped up).

------
s3graham
Wow, slick! Browser for environment/introspection is definitely a killer
feature. What does "save/commit" do? Is there a git-y thing behind it? It'd be
nice to be able to have it auto-push to github et al.

I get a generic "Error committing to counter.st" when I try to follow the
tutorial though right now, using Chromium nightly on Ubuntu.

(Trying to think of a project that could be called Caesar... ;)

edit: oops, can't read: the browser's already called Caesar.

~~~
avibryant
Save modifies the method in the current environment. Commit does an HTTP PUT
back to the webserver (which, on clamato.net, is not configured to accept
PUTs, so that will always error). You end up with .st files you can manage in
git or anything else.

You should be able to just ignore the commit errors and keep going through the
tutorial, I think? But to get rid of them, just search for the "save" method,
and remove the line that says "self commit", and then save it.

------
jdp
The script hangs and freezes in my browser, even on the tutorial page. I get
the wait/stop script prompt every time I reload it. I'm using Firefox
3/Ubuntu.

~~~
avibryant
Good to know - it definitely works best in Safari and Chrome right now (since
they have the best-optimized JS implementations). I'm surprised Firefox is
hanging that badly but I'll look into it.

~~~
extension
The search filter seems a bit too eager. Type one letter and it tries to
enumerate scrillions of methods, which kills Firefox.

It's probably the DOM manipulation specifically. Are you adding the results in
one big innerHTML assignment?

You can also try breaking the search into small chunks with setTimeouts
between them.

~~~
avibryant
The problem is actually, more or less, building up the search index - this is
only an issue the first time you do a particular kind of search, and it's fast
after that. This involves parsing all the source code, which is a recursive
process, which FF doesn't optimize (whereas Safari and Chrome zip through it
much faster).

------
skybrian
Neat. Is there a class hierarchy browser?

