Permalink
Browse files

Precompile CoffeeScript compiler to reduce startup time

  • Loading branch information...
1 parent f11813d commit 9aad95e80000eaa7169842c185a6f1d58dd2ad3d @jannehietamaki committed Jan 10, 2013
@@ -15,6 +15,7 @@
*/
apply from: "$rootDir/gradle/maven.gradle"
+apply plugin: 'java'
sourceSets {
main {
@@ -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'
+ ]
+ }
}
@@ -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.*;
@@ -35,30 +36,18 @@
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,

0 comments on commit 9aad95e

Please sign in to comment.