Create a gist now

Instantly share code, notes, and snippets.

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