Skip to content

Instantly share code, notes, and snippets.

@rbackhouse
Created March 24, 2011 01:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbackhouse/884396 to your computer and use it in GitHub Desktop.
Save rbackhouse/884396 to your computer and use it in GitHub Desktop.
Example demonstrating calling the commonjs loader via V8
import java.io.IOException;
import org.dojotoolkit.rt.v8.V8JavaBridge;
import org.dojotoolkit.server.util.resource.ResourceLoader;
public class V8CommonJSLoader extends V8JavaBridge {
private ResourceLoader resourceLoader = null;
public V8CommonJSLoader(ResourceLoader resourceLoader) {
super(true);
this.resourceLoader = resourceLoader;
}
public void run(String program) throws IOException {
StringBuffer sb = new StringBuffer();
sb.append("loadJS('/jsutil/commonjs/loader.js');\n");
sb.append("require('"+program+"');\n");
sb.append("result = '{}';\n");
try {
runScript(sb.toString());
} catch (Throwable e) {
if (compileErrors.size() > 0) {
for (Throwable t : compileErrors) {
t.printStackTrace();
}
}
throw new IOException("Exception on compress for ["+sb+"] : "+e.getMessage());
}
}
public String readResource(String path, boolean useCache) throws IOException {
return resourceLoader.readResource(path, useCache);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment