Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript source code generation library for scala [closed]

I'm looking for a library in scala that generates JavaScript from an abstract syntax tree. I want to use it in an compiler that generates JavaScript at the end and I don't want to implement the pretty printing part myself.

like image 542
schlicht Avatar asked Dec 15 '22 15:12

schlicht


2 Answers

I spent half of yesterday researching this. It seems there is no standard, standalone solution. Every project I looked at rolled their own:

  • Scala-JS: The JavaScript AST classes in Sébastien Doeraene's Scala-JS project are here: https://github.com/sjrd/scala-js/blob/master/compiler/src/main/scala/scala/tools/nsc/backend/js/JSTrees.scala and the conversion to strings is here: https://github.com/sjrd/scala-js/blob/master/compiler/src/main/scala/scala/tools/nsc/backend/js/JSPrinters.scala

  • JScala: The JavaScript AST classes in Alexander Nemish's JScala project are here: https://github.com/nau/jscala/blob/master/jscala/src/main/scala/org/jscala/model.scala and the conversion to strings is here: https://github.com/nau/jscala/blob/master/jscala/src/main/scala/org/jscala/JavascriptPrinter.scala

  • s2js: The JavaScript AST classes in Alvaro Carrasco's s2js project are here: https://github.com/alvaroc1/s2js/blob/master/plugin/src/main/scala/com/gravitydev/s2js/Trees.scala and the conversion to strings is here: https://github.com/alvaroc1/s2js/blob/master/plugin/src/main/scala/com/gravitydev/s2js/JsPrinter.scala

  • Lift (see @thoredge's answer) combines AST classes with string generation in a single file, here: https://github.com/lift/framework/blob/master/web/webkit/src/main/scala/net/liftweb/http/js/JsCommands.scala

How standalone and reusable might these various classes be? Just giving the code a visual onceover, it appears to me that the AST classes in JScala and s2js are standalone and could easily be borrowed by another project. The Scala-JS AST classes seem somewhat more entangled with their surroundings; Lift, even more so. (I welcome edits that improve on my informal impressions.)

How mature and battle-tested are these various projects? My informal impression is that they rank in descending order as follows: Lift, Scala-JS, JScala, s2js.

For my own project, I think I'm going to copy-and-paste the two source files from JScala and see how it goes.

P.S. I also mention, for completeness' sake:

  • js-scala generates JavaScript strings directly from Scala ASTs, rather than going through intermediate JavaScript ASTs. (See for example https://github.com/js-scala/js-scala/blob/master/core/src/main/scala/scala/js/gen/js/Compile.scala) There is a very good blog post comparing js-scala and scala.js here.

  • I didn't look at Scala-GWT.

like image 115
Seth Tisue Avatar answered Feb 16 '23 08:02

Seth Tisue


The Lift web framework generates JavaScript from an AST. However, I've never looked at the generation end of it. The AST is described loosely here http://exploring.liftweb.net/master/index-10.html

like image 20
thoredge Avatar answered Feb 16 '23 08:02

thoredge