Search in sources :

Example 6 with Function

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();
    }
}
Also used : Context(org.mozilla.javascript.Context) BaseFunction(org.mozilla.javascript.BaseFunction) Function(org.mozilla.javascript.Function) SightlyException(org.apache.sling.scripting.sightly.SightlyException) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 7 with Function

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();
        }
    });
}
Also used : Context(org.mozilla.javascript.Context) Function(org.mozilla.javascript.Function) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 8 with Function

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();
        }
    });
}
Also used : Context(org.mozilla.javascript.Context) ResultBuilder(com.googlecode.jslint4java.JSLintResult.ResultBuilder) Function(org.mozilla.javascript.Function) ContextAction(org.mozilla.javascript.ContextAction) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable)

Example 9 with Function

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;
}
Also used : Function(org.mozilla.javascript.Function) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) JavaScriptException(org.mozilla.javascript.JavaScriptException)

Example 10 with Function

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[] {});
    }
}
Also used : Function(org.mozilla.javascript.Function)

Aggregations

Function (org.mozilla.javascript.Function)21 ScriptableObject (org.mozilla.javascript.ScriptableObject)12 Context (org.mozilla.javascript.Context)10 Scriptable (org.mozilla.javascript.Scriptable)6 ContextAction (org.mozilla.javascript.ContextAction)4 Map (java.util.Map)3 SightlyException (org.apache.sling.scripting.sightly.SightlyException)3 BaseFunction (org.mozilla.javascript.BaseFunction)3 HashMap (java.util.HashMap)2 TimingFunction (org.apache.sling.scripting.sightly.js.impl.async.TimingFunction)2 HybridObject (org.apache.sling.scripting.sightly.js.impl.rhino.HybridObject)2 ContinuationPending (org.mozilla.javascript.ContinuationPending)2 NativeArray (org.mozilla.javascript.NativeArray)2 NativeObject (org.mozilla.javascript.NativeObject)2 RhinoException (org.mozilla.javascript.RhinoException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ResultBuilder (com.googlecode.jslint4java.JSLintResult.ResultBuilder)1 ArgumentReader (com.scratchdisk.script.ArgumentReader)1 StringArgumentReader (com.scratchdisk.script.StringArgumentReader)1 ExtendedJavaClass (com.scratchdisk.script.rhino.ExtendedJavaClass)1