use of org.mozilla.javascript.ScriptableObject in project hackpad by dropbox.
the class WriteReadOnlyPropertyTest method testWriteReadOnly.
void testWriteReadOnly(final boolean acceptWriteReadOnly) throws Exception {
final Method readMethod = Foo.class.getMethod("getMyProp", (Class[]) null);
final Foo foo = new Foo("hello");
foo.defineProperty("myProp", null, readMethod, null, ScriptableObject.EMPTY);
final String script = "foo.myProp = 123; foo.myProp";
final ContextAction action = new ContextAction() {
public Object run(final Context cx) {
final ScriptableObject top = cx.initStandardObjects();
ScriptableObject.putProperty(top, "foo", foo);
cx.evaluateString(top, script, "script", 0, null);
return null;
}
};
final ContextFactory contextFactory = new ContextFactory() {
@Override
protected boolean hasFeature(final Context cx, final int featureIndex) {
if (Context.FEATURE_STRICT_MODE == featureIndex) {
return !acceptWriteReadOnly;
}
return super.hasFeature(cx, featureIndex);
}
};
contextFactory.call(action);
}
use of org.mozilla.javascript.ScriptableObject in project druid by druid-io.
the class JavaScriptParser method compile.
private static Function<Object, Object> compile(String function) {
final ContextFactory contextFactory = ContextFactory.getGlobal();
final Context context = contextFactory.enterContext();
context.setOptimizationLevel(9);
final ScriptableObject scope = context.initStandardObjects();
final org.mozilla.javascript.Function fn = context.compileFunction(scope, function, "fn", 1, null);
Context.exit();
return new Function<Object, Object>() {
public Object 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.toObject(res, scope) : null;
}
};
}
use of org.mozilla.javascript.ScriptableObject 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.ScriptableObject in project hackpad by dropbox.
the class ApplyOnPrimitiveNumberTest method testIt.
public void testIt() {
final String script = "var fn = function() { return this; }\n" + "fn.apply(1)";
final ContextAction action = new ContextAction() {
public Object run(final Context _cx) {
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
assertEquals("object", ScriptRuntime.typeof(result));
assertEquals("1", Context.toString(result));
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
use of org.mozilla.javascript.ScriptableObject in project hackpad by dropbox.
the class ArrayConcatTest method testArrayConcat.
public void testArrayConcat() {
final String script = "var a = ['a0', 'a1'];\n" + "a[3] = 'a3';\n" + "var b = ['b1', 'b2'];\n" + "b.concat(a)";
final ContextAction action = new ContextAction() {
public Object run(final Context _cx) {
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
assertEquals("b1,b2,a0,a1,,a3", Context.toString(result));
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
Aggregations