|
1 | 1 | # js-scala: JavaScript as an embedded DSL in Scala #
|
2 | 2 |
|
3 |
| -### Documentation |
| 3 | +js-scala is a Scala library providing composable JavaScript code generators as embedded DSLs. Generate (optimized) JavaScript code from Scala-like code: |
| 4 | + |
| 5 | +```scala |
| 6 | +import scala.js.language.JS |
| 7 | +trait JavaScriptGreet extends JS { |
| 8 | + |
| 9 | + def greet(name: Rep[String]): Rep[Unit] = { |
| 10 | + println("Hello, " + name + "!") |
| 11 | + } |
| 12 | + |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +`greet` is a JavaScript program generator that produces a program that prints a message in the console. The JavaScript code can be produced as follows: |
| 17 | + |
| 18 | +```scala |
| 19 | +import scala.js.exp.JSExp |
| 20 | +import scala.js.gen.js.GenJS |
| 21 | +object Generator extends App { |
| 22 | + val javaScriptGreet = new JavaScriptGreet with JSExp |
| 23 | + val codeGen = new GenJS { val IR: javaScriptGreet.type = javaScriptGreet } |
| 24 | + codeGen.emitSource(javaScriptGreet.greet, "greet", new java.io.PrintWriter(System.out)) |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +Running the above Scala program will print the following on the standard output: |
| 29 | + |
| 30 | +```javascript |
| 31 | +function greet(x0) { |
| 32 | + var x1 = "Hello, "+x0; |
| 33 | + var x2 = x1+"!"; |
| 34 | + var x3 = console.log(x2); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Publications and talks |
4 | 39 |
|
5 | 40 | * ECOOP 2012 paper ([PDF](http://infoscience.epfl.ch/record/179888/files/js-scala-ecoop.pdf)) and slides ([PDF](http://pldi12.cs.purdue.edu/sites/default/files/slides_ecoop_gkossakowski.pdf))
|
6 | 41 | * [Scala Days 2012 talk](http://skillsmatter.com/podcast/scala/javascript-embedded-dsl-scala)
|
| 42 | +* mloc-js'13 talk ([slides](http://prezi.com/l23gghh7c27t/?utm_campaign=share&utm_medium=copy&rc=ex0share)) |
| 43 | +* GPCE'13 paper ([PDF](https://github.com/js-scala/js-scala/raw/master/papers/gpce2013/gpce19c-foy.pdf)) and [slides](https://docs.google.com/presentation/d/1ErPjZMTheuKwp428QpxWibZlyjK3PKijwKtYEaXWoxQ/pub?start=false&loop=false&delayms=3000) |
7 | 44 |
|
8 |
| -### Setup |
| 45 | +## Setup |
9 | 46 |
|
10 | 47 | 1. Setup [virtualization-lms-core](http://github.com/TiarkRompf/virtualization-lms-core):
|
11 | 48 | - `$ git clone [email protected]:TiarkRompf/virtualization-lms-core.git`
|
|
20 | 57 | - `> test`
|
21 | 58 | 4. Publish it (if you want to use it in your project):
|
22 | 59 | - `> publish-local`
|
23 |
| -5. Run the examples: |
| 60 | +5. Generate the API documentation: |
| 61 | + - `> doc` |
| 62 | + - The documentation is generated in the `core/target/scala-2.10/api/` directory. |
| 63 | +6. Run the examples: |
24 | 64 | - `> project examples`
|
25 | 65 | - `> run`
|
26 | 66 |
|
27 |
| -### Use it in your project |
| 67 | +## Use it in your project |
28 | 68 |
|
29 | 69 | 1. Add a dependency on js-scala 0.4-SNAPSHOT
|
30 | 70 | - `libraryDependencies += "EPFL" %% "js-scala" % "0.4-SNAPSHOT"`
|
|
34 | 74 | 3. Set the `-Yvirtualize` compiler option
|
35 | 75 | - `scalacOptions += "-Yvirtualize"`
|
36 | 76 |
|
37 |
| -### Further projects |
| 77 | +## Further projects |
38 | 78 |
|
39 | 79 | * [play-js-validation](http://github.com/js-scala/play-js-validation) uses this DSL to enable form validation code in Play 2.0 to be written once and checked on both client and server sides.
|
40 | 80 |
|
41 | 81 | * [forest](http://github.com/js-scala/forest) uses this DSL to enable HTML templates to be written once and shared between client and server sides, both for initial rendering and automatic updating.
|
| 82 | + |
| 83 | +## Quick start |
| 84 | + |
| 85 | +First, be sure to be familiar with [LMS tutorials](http://scala-lms.github.io/tutorials). |
| 86 | + |
| 87 | +(More to come!) |
0 commit comments