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;
}
}
}
}
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);
}
}
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);
}
}
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);
}
}
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.");
}
Aggregations