Search in sources :

Example 1 with Function

use of net.sourceforge.htmlunit.corejs.javascript.Function in project htmlunit by HtmlUnit.

the class EventListenersContainer method executeEventListeners.

private void executeEventListeners(final int eventPhase, final Event event, final Object[] args) {
    final DomNode node = jsNode_.getDomNodeOrNull();
    // some event don't apply on all kind of nodes, for instance "blur"
    if (node != null && !node.handles(event)) {
        return;
    }
    final TypeContainer container = getTypeContainer(event.getType());
    final List<Scriptable> listeners = container.getListeners(eventPhase);
    if (!listeners.isEmpty()) {
        event.setCurrentTarget(jsNode_);
        final HtmlPage page;
        if (jsNode_ instanceof Window) {
            page = (HtmlPage) jsNode_.getDomNodeOrDie();
        } else {
            final Scriptable parentScope = jsNode_.getParentScope();
            if (parentScope instanceof Window) {
                page = (HtmlPage) ((Window) parentScope).getDomNodeOrDie();
            } else if (parentScope instanceof HTMLDocument) {
                page = ((HTMLDocument) parentScope).getPage();
            } else {
                page = ((HTMLElement) parentScope).getDomNodeOrDie().getHtmlPageOrNull();
            }
        }
        // no need for a copy, listeners are copy on write
        for (Scriptable listener : listeners) {
            boolean isPropertyHandler = false;
            if (listener == TypeContainer.EVENT_HANDLER_PLACEHOLDER) {
                listener = container.handler_;
                isPropertyHandler = true;
            }
            Function function = null;
            Scriptable thisObject = null;
            if (listener instanceof Function) {
                function = (Function) listener;
                thisObject = jsNode_;
            } else if (listener instanceof NativeObject) {
                final Object handleEvent = ScriptableObject.getProperty(listener, "handleEvent");
                if (handleEvent instanceof Function) {
                    function = (Function) handleEvent;
                    thisObject = listener;
                }
            }
            if (function != null) {
                final ScriptResult result = page.executeJavaScriptFunction(function, thisObject, args, node);
                // Return value is only honored for property handlers (Tested in Chrome/FF/IE11)
                if (isPropertyHandler && !ScriptResult.isUndefined(result)) {
                    event.handlePropertyHandlerReturnValue(result.getJavaScriptResult());
                }
            }
            if (event.isImmediatePropagationStopped()) {
                return;
            }
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) Function(net.sourceforge.htmlunit.corejs.javascript.Function) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 2 with Function

use of net.sourceforge.htmlunit.corejs.javascript.Function in project htmlunit by HtmlUnit.

the class HTMLObjectElement method createAppletMethodAndProperties.

private void createAppletMethodAndProperties() throws Exception {
    final HtmlObject appletNode = (HtmlObject) getDomNodeOrDie();
    final Applet applet = appletNode.getApplet();
    if (applet == null) {
        return;
    }
    // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
    for (final Method method : applet.getClass().getMethods()) {
        final Function f = new BaseFunction() {

            @Override
            public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                final Object[] realArgs = new Object[method.getParameterTypes().length];
                for (int i = 0; i < realArgs.length; i++) {
                    final Object arg;
                    if (i > args.length) {
                        arg = null;
                    } else {
                        arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
                    }
                    realArgs[i] = arg;
                }
                try {
                    return method.invoke(applet, realArgs);
                } catch (final Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        };
        ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) Applet(java.applet.Applet) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) NativeJavaObject(net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 3 with Function

use of net.sourceforge.htmlunit.corejs.javascript.Function in project htmlunit by HtmlUnit.

the class HTMLAppletElement method createAppletMethodAndProperties.

private void createAppletMethodAndProperties() throws Exception {
    final HtmlApplet appletNode = (HtmlApplet) getDomNodeOrDie();
    final Applet applet = appletNode.getApplet();
    if (applet == null) {
        return;
    }
    // Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
    for (final Method method : applet.getClass().getMethods()) {
        final Function f = new BaseFunction() {

            @Override
            public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
                final Object[] realArgs = new Object[method.getParameterTypes().length];
                for (int i = 0; i < realArgs.length; i++) {
                    final Object arg;
                    if (i > args.length) {
                        arg = null;
                    } else {
                        arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
                    }
                    realArgs[i] = arg;
                }
                try {
                    return method.invoke(applet, realArgs);
                } catch (final Exception e) {
                    throw Context.throwAsScriptRuntimeEx(e);
                }
            }
        };
        ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
    }
}
Also used : HtmlApplet(com.gargoylesoftware.htmlunit.html.HtmlApplet) Context(net.sourceforge.htmlunit.corejs.javascript.Context) Function(net.sourceforge.htmlunit.corejs.javascript.Function) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) Applet(java.applet.Applet) HtmlApplet(com.gargoylesoftware.htmlunit.html.HtmlApplet) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 4 with Function

use of net.sourceforge.htmlunit.corejs.javascript.Function in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method triggerOnError.

/**
 * Triggers the {@code onerror} handler, if one has been set.
 * @param e the error that needs to be reported
 */
public void triggerOnError(final ScriptException e) {
    final Object o = getOnerror();
    if (o instanceof Function) {
        final Function f = (Function) o;
        String msg = e.getMessage();
        final String url = e.getPage().getUrl().toExternalForm();
        final int line = e.getFailingLineNumber();
        final int column = e.getFailingColumnNumber();
        Object jsError = e.getMessage();
        if (e.getCause() instanceof JavaScriptException) {
            msg = "uncaught exception: " + e.getCause().getMessage();
            jsError = ((JavaScriptException) e.getCause()).getValue();
        } else if (e.getCause() instanceof EcmaError) {
            msg = "uncaught " + e.getCause().getMessage();
            final EcmaError ecmaError = (EcmaError) e.getCause();
            final Scriptable err = Context.getCurrentContext().newObject(this, "Error");
            ScriptableObject.putProperty(err, "message", ecmaError.getMessage());
            ScriptableObject.putProperty(err, "fileName", ecmaError.sourceName());
            ScriptableObject.putProperty(err, "lineNumber", Integer.valueOf(ecmaError.lineNumber()));
            jsError = err;
        }
        final Object[] args = { msg, url, Integer.valueOf(line), Integer.valueOf(column), jsError };
        f.call(Context.getCurrentContext(), this, this, args);
    }
}
Also used : JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) EcmaError(net.sourceforge.htmlunit.corejs.javascript.EcmaError) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 5 with Function

use of net.sourceforge.htmlunit.corejs.javascript.Function in project htmlunit by HtmlUnit.

the class WindowOrWorkerGlobalScopeMixin method setTimeoutIntervalImpl.

private static int setTimeoutIntervalImpl(final Window window, final Object code, int timeout, final boolean isTimeout, final Object[] params) {
    if (timeout < MIN_TIMER_DELAY) {
        timeout = MIN_TIMER_DELAY;
    }
    final WebWindow webWindow = window.getWebWindow();
    final Page page = (Page) window.getDomNodeOrNull();
    Integer period = null;
    if (!isTimeout) {
        period = timeout;
    }
    if (code instanceof String) {
        final String s = (String) code;
        final String description = "window.set" + (isTimeout ? "Timeout" : "Interval") + "(" + s + ", " + timeout + ")";
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(timeout, period, description, webWindow, s);
        return webWindow.getJobManager().addJob(job, page);
    }
    if (code instanceof Function) {
        final Function f = (Function) code;
        final String functionName;
        if (f instanceof FunctionObject) {
            functionName = ((FunctionObject) f).getFunctionName();
        } else {
            // can this happen?
            functionName = String.valueOf(f);
        }
        final String description = "window.set" + (isTimeout ? "Timeout" : "Interval") + "(" + functionName + ", " + timeout + ")";
        final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavaScriptJob(timeout, period, description, webWindow, f, params);
        return webWindow.getJobManager().addJob(job, page);
    }
    throw Context.reportRuntimeError("Unknown type for function.");
}
Also used : Function(net.sourceforge.htmlunit.corejs.javascript.Function) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) Page(com.gargoylesoftware.htmlunit.Page) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

Function (net.sourceforge.htmlunit.corejs.javascript.Function)13 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)9 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)8 BaseFunction (net.sourceforge.htmlunit.corejs.javascript.BaseFunction)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)4 Context (net.sourceforge.htmlunit.corejs.javascript.Context)4 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)3 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)3 Method (java.lang.reflect.Method)3 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 HtmlObject (com.gargoylesoftware.htmlunit.html.HtmlObject)2 Applet (java.applet.Applet)2 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)2 NativeJavaObject (net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject)2 MockWebConnection (com.gargoylesoftware.htmlunit.MockWebConnection)1 Page (com.gargoylesoftware.htmlunit.Page)1 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)1 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1