use of org.mozilla.javascript.Context in project hackpad by dropbox.
the class RequireTest method testRelativeId.
public void testRelativeId() throws Exception {
final Context cx = createContext();
final Scriptable scope = cx.initStandardObjects();
final Require require = getSandboxedRequire(cx, scope, false);
require.install(scope);
cx.evaluateReader(scope, getReader("testRelativeId.js"), "testRelativeId.js", 1, null);
}
use of org.mozilla.javascript.Context in project hackpad by dropbox.
the class RequireTest method testSetMainForAlreadyLoadedModule.
public void testSetMainForAlreadyLoadedModule() throws Exception {
final Context cx = createContext();
final Scriptable scope = cx.initStandardObjects();
final Require require = getSandboxedRequire(cx, scope, false);
require.install(scope);
cx.evaluateReader(scope, getReader("testSetMainForAlreadyLoadedModule.js"), "testSetMainForAlreadyLoadedModule.js", 1, null);
try {
require.requireMain(cx, "assert");
fail();
} catch (IllegalStateException e) {
assertEquals(e.getMessage(), "Attempt to set main module after it was loaded");
}
}
use of org.mozilla.javascript.Context in project druid by druid-io.
the class JavaScriptPostAggregator method compile.
private static Function compile(String function) {
final ContextFactory contextFactory = ContextFactory.getGlobal();
final Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
final ScriptableObject scope = context.initStandardObjects();
final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
Context.exit();
return new Function() {
public double apply(Object[] args) {
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
return Context.toNumber(fn.call(cx, scope, scope, args));
}
};
}
use of org.mozilla.javascript.Context in project druid by druid-io.
the class JavaScriptExtractionFn method compile.
private static Function<Object, String> compile(String function) {
final ContextFactory contextFactory = ContextFactory.getGlobal();
final Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
final ScriptableObject scope = context.initStandardObjects();
final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
Context.exit();
return new Function<Object, String>() {
public String apply(Object input) {
// ideally we need a close() function to discard the context once it is not used anymore
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
}
final Object res = fn.call(cx, scope, scope, new Object[] { input });
return res != null ? Context.toString(res) : null;
}
};
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class CoffeeScriptCompilerWorker method process.
public void process(SerializableCoffeeScriptCompileSpec spec) {
Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
public void execute(Context context) {
context.setOptimizationLevel(-1);
}
});
String encoding = spec.getOptions().getEncoding();
CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());
for (RelativeFile target : spec.getSource()) {
String source = readFile(target.getFile(), encoding);
String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
}
}
Aggregations