use of org.mozilla.javascript.ContextFactory 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() {
@Override
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.ContextFactory in project cxf by apache.
the class JavascriptTestUtilities method initializeRhino.
public void initializeRhino() {
rhinoContextFactory = new ContextFactory();
if (System.getProperty("cxf.jsdebug") != null && !rhinoDebuggerUp) {
try {
Class<?> debuggerMain = ClassLoaderUtils.loadClass("org.mozilla.javascript.tools.debugger.Main", getClass());
if (debuggerMain != null) {
Method mainMethod = debuggerMain.getMethod("mainEmbedded", ContextFactory.class, Scriptable.class, String.class);
mainMethod.invoke(null, rhinoContextFactory, rhinoScope, "Debug embedded JavaScript.");
rhinoDebuggerUp = true;
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Failed to launch Rhino debugger", e);
}
}
rhinoContext = rhinoContextFactory.enterContext();
rhinoScope = rhinoContext.initStandardObjects();
try {
ScriptableObject.defineClass(rhinoScope, JsAssert.class);
ScriptableObject.defineClass(rhinoScope, Trace.class);
ScriptableObject.defineClass(rhinoScope, Notifier.class);
ScriptableObject.defineClass(rhinoScope, CountDownNotifier.class);
// so that the stock test for IE can gracefully fail.
rhinoContext.evaluateString(rhinoScope, "var window = new Object();", "<internal>", 0, null);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new RuntimeException(e);
} finally {
Context.exit();
}
JsSimpleDomNode.register(rhinoScope);
JsSimpleDomParser.register(rhinoScope);
JsNamedNodeMap.register(rhinoScope);
JsXMLHttpRequest.register(rhinoScope);
}
use of org.mozilla.javascript.ContextFactory in project hackpad by dropbox.
the class CustomSetterAcceptNullScriptableTest method testSetNullForScriptableSetter.
public void testSetNullForScriptableSetter() throws Exception {
final String scriptCode = "foo.myProp = new Foo2();\n" + "foo.myProp = null;";
final ContextFactory factory = new ContextFactory();
final Context cx = factory.enterContext();
try {
final ScriptableObject topScope = cx.initStandardObjects();
final Foo foo = new Foo();
// define custom setter method
final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
topScope.put("foo", topScope, foo);
ScriptableObject.defineClass(topScope, Foo2.class);
cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.ContextFactory in project hackpad by dropbox.
the class DoctestsTest method runDoctest.
@Test
public void runDoctest() throws Exception {
ContextFactory factory = ContextFactory.getGlobal();
Context cx = factory.enterContext();
try {
cx.setOptimizationLevel(optimizationLevel);
Global global = new Global(cx);
// global.runDoctest throws an exception on any failure
int testsPassed = global.runDoctest(cx, global, source, name, 1);
System.out.println(name + "(" + optimizationLevel + "): " + testsPassed + " passed.");
assertTrue(testsPassed > 0);
} catch (Exception ex) {
System.out.println(name + "(" + optimizationLevel + "): FAILED due to " + ex);
throw ex;
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.ContextFactory 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));
}
};
}
Aggregations