Skip to content

Commit

Permalink
Merge pull request #490 from jannehietamaki/coffeescript_compiler_opt…
Browse files Browse the repository at this point in the history
…imization

Precompile CoffeeScript to reduce startup time
  • Loading branch information
purplefox committed Jan 13, 2013
2 parents f11813d + 9aad95e commit e80092a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
14 changes: 14 additions & 0 deletions vertx-lang/vertx-lang-rhino/build.gradle
Expand Up @@ -15,6 +15,7 @@
*/

apply from: "$rootDir/gradle/maven.gradle"
apply plugin: 'java'

sourceSets {
main {
Expand All @@ -32,4 +33,17 @@ dependencies {

artifacts {
platform jar
}

compileJava {
javaexec {
classpath sourceSets.main.compileClasspath
main = "org.mozilla.javascript.tools.jsc.Main"
args = [
'-opt', '9',
'-d', 'build/classes/main/',
'-package', 'org.vertx.java.deploy.impl.rhino',
'src/main/coffeescript/coffee-script.js'
]
}
}
Expand Up @@ -18,6 +18,7 @@

import org.mozilla.javascript.Context;
import org.mozilla.javascript.JavaScriptException;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;

import java.io.*;
Expand All @@ -35,30 +36,18 @@ public class CoffeeScriptCompiler {
private final Scriptable globalScope;

public CoffeeScriptCompiler(ClassLoader classLoader) {
InputStream inputStream = classLoader.getResourceAsStream("coffee-script.js");
try {
Reader reader = new InputStreamReader(inputStream, "UTF-8");
Context context = Context.enter();
try {
Context context = Context.enter();
context.setOptimizationLevel(-1); // Without this, Rhino hits a 64K bytecode limit and fails
try {
globalScope = context.initStandardObjects();
context.evaluateReader(globalScope, reader, "coffee-script.js", 0, null);
} finally {
Context.exit();
}
globalScope = context.initStandardObjects();
Script coffeeCompiler = (Script) Class.forName("org.vertx.java.deploy.impl.rhino.coffee_script").newInstance();
coffeeCompiler.exec(context, globalScope);
} finally {
reader.close();
Context.exit();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
}
}

}

public URI coffeeScriptToJavaScript(URI coffeeScript) throws JavaScriptException,
Expand Down

0 comments on commit e80092a

Please sign in to comment.