use of org.mozilla.javascript.ContextAction in project hackpad by dropbox.
the class StackTraceTest method runWithExpectedStackTrace.
private void runWithExpectedStackTrace(final String _source, final String _expectedStackTrace) {
final ContextAction action = new ContextAction() {
public Object run(Context cx) {
final Scriptable scope = cx.initStandardObjects();
try {
cx.evaluateString(scope, _source, "test.js", 0, null);
} catch (final JavaScriptException e) {
assertEquals(_expectedStackTrace, e.getScriptStackTrace());
return null;
}
throw new RuntimeException("Exception expected!");
}
};
Utils.runWithOptimizationLevel(action, -1);
}
use of org.mozilla.javascript.ContextAction in project jslint4java by happygiraffe.
the class JSLint method optionsAsJavaScriptObject.
/**
* Turn the set of options into a JavaScript object, where the key is the
* name of the option and the value is true.
*/
@NeedsContext
private Scriptable optionsAsJavaScriptObject() {
return (Scriptable) contextFactory.call(new ContextAction() {
public Object run(Context cx) {
applyDefaultOptions();
Scriptable opts = cx.newObject(lintFunc);
for (Entry<Option, Object> entry : options.entrySet()) {
String key = entry.getKey().getLowerName();
// Use our "custom" version in order to get native arrays.
Object value = Util.javaToJS(entry.getValue(), opts);
opts.put(key, opts, value);
}
return opts;
}
});
}
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 BaseObjectColumnValueSelector[] 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 BaseObjectColumnValueSelector selector = selectorList[i];
if (selector != null) {
final Object arg = selector.getObject();
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.
args[i + 1] = cx.newArray(scope, arrayToObjectArray(arg));
} else if (arg instanceof List) {
// Using toArray(Object[]), instead of just toArray(), because Arrays.asList()'s impl and similar List
// impls could clone the underlying array in toArray(), that could be not Object[], but e. g. String[].
args[i + 1] = cx.newArray(scope, ((List) arg).toArray(ObjectArrays.EMPTY_ARRAY));
} else {
args[i + 1] = Context.javaToJS(arg, scope);
}
}
}
final Object res = fnAggregate.call(cx, scope, scope, args);
return Context.toNumber(res);
}
private Object[] arrayToObjectArray(Object array) {
int len = Array.getLength(array);
final Object[] objectArray = new Object[len];
for (int j = 0; j < len; j++) {
objectArray[j] = Array.get(array, j);
}
return objectArray;
}
@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();
}
}
};
}
Aggregations