How does Scala-JS works?

216 views
Skip to first unread message

Artūras Šlajus

unread,
Mar 12, 2014, 10:00:22 AM3/12/14
to scal...@googlegroups.com
Hi there.

I've been stumbling around in a sad state of game development - ActionScript 3 or even Haxe is a far worse language than Scala and I had an idea: Scala to AS3 compiler.

Unfortunately I'm not quite sure how all that works. Is there any overview on how scala-js works? Does it work on JVM bytecode? On Scala source code? How does it translate to JS source code? Etc.?

Perhaps anyone could enlighten me here? :)

Sébastien Doeraene

unread,
Mar 12, 2014, 10:36:45 AM3/12/14
to Artūras Šlajus, scal...@googlegroups.com
Hi Artūras, and welcome!

I'm not sure why you would still want to use Flash at all anymore, when we can use HTML5, but here a few stabs at your questions about Scala.js.

Basically, it is a Scala compiler plugin, that turns a Scala AST that has been simplified by most of the Scala-to-JVM pipeline until a point where it is relatively straightforward to translate it to a JavaScript AST. The phase that does this translation is in GenJSCode.scala:
https://github.com/scala-js/scala-js/blob/master/compiler/src/main/scala/scala/scalajs/compiler/GenJSCode.scala
There are 2 big parts in this file. The first part deals with translating everything that's "outside methods", i.e., classes, traits, implementation classes, fields, module accessors, and the method signatures. That part ends about here:
https://github.com/scala-js/scala-js/blob/master/compiler/src/main/scala/scala/scalajs/compiler/GenJSCode.scala#L939
at which point starts the second part, which deals with code generation, i.e., translating the body of a method, which is a Scala expression, into JavaScript statements that are inside the JavaScript "method".
Further than this high-level view, the comments in the code should give a clear overview of how it works.

So, it does not work either on the JVM bytecode nor on the Scala source code, but somewhere in-between. It translates first to a ~JavaScript AST, and then a printer emits JS source code corresponding to this AST, along with source maps.

That's the basic idea, and to someone with the knowledge of the Scala compiler, a prototype that supports all-but-the-corner-cases can be implemented in a few months, possibly weeks. Then comes "all the rest": corner cases, tooling, integration with sbt and IDE, unit testing capabilities, testing of the compiler itself, libraries, optimizer, etc. This takes, well, years, and the support of a community, which I've been very lucky to get quite early for Scala.js.

Cheers,
Sébastien


--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

EECOLOR

unread,
Mar 12, 2014, 4:08:00 PM3/12/14
to Sébastien Doeraene, Artūras Šlajus, scal...@googlegroups.com
I'm not sure why you would still want to use Flash at all anymore, when we can use HTML5

There is quite a few reasons to still use Flash:

- Supporting older browser versions, most of the HTML 5 related stuff exists since Flash 6
- Working with video (and also live streams)
- 3D in the browser
- Socket support (websocket alternative in older browsers, complete clients for all types of protocols)
- Webcam related stuff
- Sound related stuff
- Advanced text flow
- Support for better file uploads in older browsers


Erik

Haoyi Li

unread,
Mar 12, 2014, 4:09:47 PM3/12/14
to EECOLOR, Sébastien Doeraene, Artūras Šlajus, scal...@googlegroups.com
- WeakReferences, because Javascript doesn't have them and isn't going to get them anytime soon

Artūras Šlajus

unread,
Mar 13, 2014, 6:06:07 AM3/13/14
to scal...@googlegroups.com, Artūras Šlajus
Hey, thanks for the explanation :)

As for Flash - it's still the best option for simple web game content delivery with best monetization and distribution options. Too bad that AS3 is a language that makes me cringe :)

Justin du coeur

unread,
Mar 13, 2014, 8:31:49 AM3/13/14
to Artūras Šlajus, scal...@googlegroups.com
On Thu, Mar 13, 2014 at 6:06 AM, Artūras Šlajus <arturas...@gmail.com> wrote:
As for Flash - it's still the best option for simple web game content delivery with best monetization and distribution options. Too bad that AS3 is a language that makes me cringe :)

I take it you never had to do serious work in AS1?  About ten years ago, I was working at a company (Convoq) that built a full-scale IM/chat/webconferencing platform on AS1, and that was *horrifying*.  I mean, debugging in an environment that had no concept of errors -- brr.

After that, AS3 (which is approximately what Javascript was supposed to become, had politics not gotten in the way) was a breath of fresh air -- a far cry from working in Scala, but I actually prefer it to Java in many ways...
Reply all
Reply to author
Forward
0 new messages