use of org.mozilla.javascript.Context in project gocd by gocd.
the class JsonTester method javascriptParse.
public static Object javascriptParse(String other) {
try {
Context ctx = Context.enter();
ScriptableObject scope = ctx.initStandardObjects();
return parse(ctx.evaluateString(scope, "json = " + other, "JsonTester", 1, null));
} catch (Exception evaluator) {
evaluator.printStackTrace();
System.err.println("Invalid json:\n" + other);
throw bomb("Invalid javascript", evaluator);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project sling by apache.
the class AsyncExtractor method decodeJSPromise.
private void decodeJSPromise(final Scriptable promise, final UnaryCallback callback) {
try {
Context context = Context.enter();
final AsyncContainer errorContainer = new AsyncContainer();
final Function errorHandler = createErrorHandler(errorContainer);
final Function successHandler = convertCallback(callback);
EventLoopInterop.schedule(context, new Runnable() {
@Override
public void run() {
ScriptableObject.callMethod(promise, THEN_METHOD, new Object[] { successHandler, errorHandler });
}
});
if (errorContainer.isCompleted()) {
throw new SightlyException("Promise has completed with failure: " + Context.toString(errorContainer.getResult()));
}
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.Context in project sling by apache.
the class AsyncExtractor method convertCallback.
private static Function convertCallback(final UnaryCallback unaryCallback) {
return new BaseFunction() {
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
Object arg = (args.length == 0) ? Context.getUndefinedValue() : args[0];
unaryCallback.invoke(arg);
return Context.getUndefinedValue();
}
};
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method parse.
public static Scriptable parse(File source, String encoding, Action<Context> contextConfig) {
Context context = Context.enter();
if (contextConfig != null) {
contextConfig.execute(context);
}
Scriptable scope = context.initStandardObjects();
try {
Reader reader = new InputStreamReader(new FileInputStream(source), encoding);
try {
context.evaluateReader(scope, reader, source.getName(), 0, null);
} finally {
reader.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
} finally {
Context.exit();
}
return scope;
}
use of org.mozilla.javascript.Context in project gradle by gradle.
the class RhinoWorkerUtils method childScope.
public static <R> R childScope(Scriptable parentScope, ScopeOperation<R> operation) {
Context context = Context.enter();
try {
operation.initContext(context);
Scriptable childScope = context.newObject(parentScope);
childScope.setParentScope(parentScope);
return operation.action(childScope, context);
} finally {
Context.exit();
}
}
Aggregations