use of org.mozilla.javascript.Function 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.Function in project jslint4java by happygiraffe.
the class JSLint method callReport.
/**
* Construct a JSLint error report. This is in two parts: a list of errors, and an optional
* function report.
*
* @param errorsOnly
* if the function report should be omitted.
* @return the report, an HTML string.
*/
@NeedsContext
private String callReport(final boolean errorsOnly) {
return (String) contextFactory.call(new ContextAction() {
// TODO: This would probably benefit from injecting an API to manage JSLint.
public Object run(Context cx) {
Function fn = null;
Object value = null;
StringBuilder sb = new StringBuilder();
// Look up JSLINT.data.
value = lintFunc.get("data", lintFunc);
if (value == UniqueTag.NOT_FOUND) {
return "";
}
fn = (Function) value;
// Call JSLINT.data(). This returns a JS data structure that we need below.
Object data = fn.call(cx, lintFunc, null, Context.emptyArgs);
// Look up JSLINT.error_report.
value = lintFunc.get("error_report", lintFunc);
// Shouldn't happen ordinarily, but some of my tests don't have it.
if (value != UniqueTag.NOT_FOUND) {
fn = (Function) value;
// Call JSLint.report().
sb.append(fn.call(cx, lintFunc, null, new Object[] { data }));
}
if (!errorsOnly) {
// Look up JSLINT.report.
value = lintFunc.get("report", lintFunc);
// Shouldn't happen ordinarily, but some of my tests don't have it.
if (value != UniqueTag.NOT_FOUND) {
fn = (Function) value;
// Call JSLint.report().
sb.append(fn.call(cx, lintFunc, null, new Object[] { data }));
}
}
return sb.toString();
}
});
}
use of org.mozilla.javascript.Function in project jslint4java by happygiraffe.
the class JSLint method buildResults.
/**
* Assemble the {@link JSLintResult} object.
*/
@NeedsContext
private JSLintResult buildResults(final String systemId, final long startNanos, final long endNanos) {
return (JSLintResult) contextFactory.call(new ContextAction() {
public Object run(Context cx) {
ResultBuilder b = new JSLintResult.ResultBuilder(systemId);
b.duration(TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos));
for (Issue issue : readErrors(systemId)) {
b.addIssue(issue);
}
// Collect a report on what we've just linted.
b.report(callReport(false));
// Extract JSLINT.data() output and set it on the result.
Object o = lintFunc.get("data", lintFunc);
// Real JSLINT will always have this, but some of my test stubs don't.
if (o != UniqueTag.NOT_FOUND) {
Function reportFunc = (Function) o;
Scriptable data = (Scriptable) reportFunc.call(cx, lintFunc, null, Context.emptyArgs);
for (String global : Util.listValueOfType("global", String.class, data)) {
b.addGlobal(global);
}
b.json(Util.booleanValue("json", data));
for (JSFunction f : Util.listValue("functions", data, new JSFunctionConverter())) {
b.addFunction(f);
}
}
// Extract the list of properties. Note that we don't expose the counts, as it
// doesn't seem that useful.
Object properties = lintFunc.get("property", lintFunc);
if (properties != UniqueTag.NOT_FOUND) {
for (Object id : ScriptableObject.getPropertyIds((Scriptable) properties)) {
b.addProperty(id.toString());
}
}
return b.build();
}
});
}
use of org.mozilla.javascript.Function in project cxf by apache.
the class JavascriptTestUtilities method rhinoCallExpectingException.
/**
* Call a Javascript function, identified by name, on a set of arguments. Optionally, expect it to throw
* an exception.
*
* @param expectingException
* @param functionName
* @param args
* @return
*/
public Object rhinoCallExpectingException(final Object expectingException, final String functionName, final Object... args) {
Object fObj = rhinoScope.get(functionName, rhinoScope);
if (!(fObj instanceof Function)) {
throw new RuntimeException("Missing test function " + functionName);
}
Function function = (Function) fObj;
try {
return function.call(rhinoContext, rhinoScope, rhinoScope, args);
} catch (RhinoException angryRhino) {
if (expectingException != null && angryRhino instanceof JavaScriptException) {
JavaScriptException jse = (JavaScriptException) angryRhino;
Assert.assertEquals(jse.getValue(), expectingException);
return null;
}
String trace = angryRhino.getScriptStackTrace();
Assert.fail("JavaScript error: " + angryRhino.toString() + " " + trace);
} catch (JavaScriptAssertionFailed assertion) {
Assert.fail(assertion.getMessage());
}
return null;
}
use of org.mozilla.javascript.Function in project cxf by apache.
the class JsXMLHttpRequest method notifyReadyStateChangeListener.
private void notifyReadyStateChangeListener() {
if (readyStateChangeListener instanceof Function) {
LOG.fine("notify " + readyState);
// for now, call with no args.
Function listenerFunction = (Function) readyStateChangeListener;
listenerFunction.call(Context.getCurrentContext(), getParentScope(), null, new Object[] {});
}
}
Aggregations