use of org.mozilla.javascript.ContextAction in project druid by druid-io.
the class JavaScriptAggregatorFactory method compileScript.
@VisibleForTesting
static JavaScriptAggregator.ScriptAggregator compileScript(final String aggregate, final String reset, final String combine) {
final ContextFactory contextFactory = ContextFactory.getGlobal();
Context context = contextFactory.enterContext();
context.setOptimizationLevel(JavaScriptConfig.DEFAULT_OPTIMIZATION_LEVEL);
final ScriptableObject scope = context.initStandardObjects();
final Function fnAggregate = context.compileFunction(scope, aggregate, "aggregate", 1, null);
final Function fnReset = context.compileFunction(scope, reset, "reset", 1, null);
final Function fnCombine = context.compileFunction(scope, combine, "combine", 1, null);
Context.exit();
return new JavaScriptAggregator.ScriptAggregator() {
@Override
public double aggregate(final double current, final ObjectColumnSelector[] selectorList) {
Context cx = Context.getCurrentContext();
if (cx == null) {
cx = contextFactory.enterContext();
// Disable primitive wrapping- we want Java strings and primitives to behave like JS entities.
cx.getWrapFactory().setJavaPrimitiveWrap(false);
}
final int size = selectorList.length;
final Object[] args = new Object[size + 1];
args[0] = current;
for (int i = 0; i < size; i++) {
final ObjectColumnSelector selector = selectorList[i];
if (selector != null) {
final Object arg = selector.get();
if (arg != null && arg.getClass().isArray()) {
// Context.javaToJS on an array sort of works, although it returns false for Array.isArray(...) and
// may have other issues too. Let's just copy the array and wrap that.
final Object[] arrayAsObjectArray = new Object[Array.getLength(arg)];
for (int j = 0; j < Array.getLength(arg); j++) {
arrayAsObjectArray[j] = Array.get(arg, j);
}
args[i + 1] = cx.newArray(scope, arrayAsObjectArray);
} else {
args[i + 1] = Context.javaToJS(arg, scope);
}
}
}
final Object res = fnAggregate.call(cx, scope, scope, args);
return Context.toNumber(res);
}
@Override
public double combine(final double a, final double b) {
final Object res = contextFactory.call(new ContextAction() {
@Override
public Object run(final Context cx) {
return fnCombine.call(cx, scope, scope, new Object[] { a, b });
}
});
return Context.toNumber(res);
}
@Override
public double reset() {
final Object res = contextFactory.call(new ContextAction() {
@Override
public Object run(final Context cx) {
return fnReset.call(cx, scope, scope, new Object[] {});
}
});
return Context.toNumber(res);
}
@Override
public void close() {
if (Context.getCurrentContext() != null) {
Context.exit();
}
}
};
}
use of org.mozilla.javascript.ContextAction in project hackpad by dropbox.
the class GlobalParseXTest method assertEvaluates.
private void assertEvaluates(final Object expected, final String source) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, rep);
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.ContextAction in project hackpad by dropbox.
the class NativeStringTest method assertEvaluates.
private void assertEvaluates(final Object expected, final String source) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
final Object rep = cx.evaluateString(scope, source, "test.js", 0, null);
assertEquals(expected, rep);
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.ContextAction in project hackpad by dropbox.
the class PrimitiveTypeScopeResolutionTest method functionObjectPrimitiveToObject.
/**
* Test that FunctionObject use the right top scope to convert a primitive
* to an object
*/
@Test
public void functionObjectPrimitiveToObject() throws Exception {
final String scriptScope2 = "function f() {\n" + "String.prototype.foo = 'from 2'; \n" + "var s2 = 's2';\n" + "var s2Foo = s2.foo;\n" + "var s2FooReadByFunction = myObject.readPropFoo(s2);\n" + "if (s2Foo != s2FooReadByFunction)\n" + "throw 's2 got: ' + s2FooReadByFunction;\n" + "}";
// define object with custom method
final MyObject myObject = new MyObject();
final String[] functionNames = { "readPropFoo" };
myObject.defineFunctionProperties(functionNames, MyObject.class, ScriptableObject.EMPTY);
final String scriptScope1 = "String.prototype.foo = 'from 1'; scope2.f()";
final ContextAction action = new ContextAction() {
public Object run(final Context cx) {
final Scriptable scope1 = cx.initStandardObjects(new MySimpleScriptableObject("scope1"));
final Scriptable scope2 = cx.initStandardObjects(new MySimpleScriptableObject("scope2"));
scope2.put("myObject", scope2, myObject);
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.ContextAction in project hackpad by dropbox.
the class TypeOfTest method test0.
/**
* ECMA 11.4.3 says that typeof on host object is Implementation-dependent
*/
public void test0() throws Exception {
final Function f = new BaseFunction() {
@Override
public Object call(Context _cx, Scriptable _scope, Scriptable _thisObj, Object[] _args) {
return _args[0].getClass().getName();
}
};
final ContextAction action = new ContextAction() {
public Object run(final Context context) {
final Scriptable scope = context.initStandardObjects();
scope.put("myObj", scope, f);
return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
}
};
doTest("function", action);
}
Aggregations