Search in sources :

Example 1 with FunctionObject

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

the class JavaScriptEngine method configureFunctions.

private static void configureFunctions(final ClassConfiguration config, final ScriptableObject scriptable) {
    final int attributes = ScriptableObject.EMPTY;
    // the functions
    final Map<String, Method> functionMap = config.getFunctionMap();
    if (functionMap != null) {
        for (final Entry<String, Method> functionInfo : functionMap.entrySet()) {
            final String functionName = functionInfo.getKey();
            final Method method = functionInfo.getValue();
            final FunctionObject functionObject = new FunctionObject(functionName, method, scriptable);
            scriptable.defineProperty(functionName, functionObject, attributes);
        }
    }
}
Also used : Method(java.lang.reflect.Method) IdFunctionObject(net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject)

Example 2 with FunctionObject

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

the class JavaScriptEngine method configureStaticFunctions.

private static void configureStaticFunctions(final ClassConfiguration config, final ScriptableObject scriptable) {
    final Map<String, Method> staticFunctionMap = config.getStaticFunctionMap();
    if (staticFunctionMap != null) {
        for (final Entry<String, Method> staticFunctionInfo : staticFunctionMap.entrySet()) {
            final String functionName = staticFunctionInfo.getKey();
            final Method method = staticFunctionInfo.getValue();
            final FunctionObject staticFunctionObject = new FunctionObject(functionName, method, scriptable);
            scriptable.defineProperty(functionName, staticFunctionObject, ScriptableObject.EMPTY);
        }
    }
}
Also used : Method(java.lang.reflect.Method) IdFunctionObject(net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject)

Example 3 with FunctionObject

use of net.sourceforge.htmlunit.corejs.javascript.FunctionObject 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)

Example 4 with FunctionObject

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

the class Reflect method setParentScope.

/**
 * {@inheritDoc}
 */
@Override
public void setParentScope(final Scriptable scope) {
    super.setParentScope(scope);
    try {
        final FunctionObject functionObject = new FunctionObject("has", getClass().getDeclaredMethod("has", Scriptable.class, String.class), scope);
        defineProperty("has", functionObject, ScriptableObject.DONTENUM);
    } catch (final Exception e) {
        Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)

Example 5 with FunctionObject

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

the class Intl method define.

private void define(final Class<? extends HtmlUnitScriptable> c, final BrowserVersion browserVersion) {
    try {
        final ClassConfiguration config = AbstractJavaScriptConfiguration.getClassConfiguration(c, browserVersion);
        final HtmlUnitScriptable prototype = JavaScriptEngine.configureClass(config, this, browserVersion);
        final FunctionObject functionObject = new RecursiveFunctionObject(c.getSimpleName(), config.getJsConstructor(), this);
        if (c == V8BreakIterator.class) {
            prototype.setClassName("v8BreakIterator");
        }
        functionObject.addAsConstructor(this, prototype);
    } catch (final Exception e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
Also used : HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) RecursiveFunctionObject(com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) RecursiveFunctionObject(com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration)

Aggregations

FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)9 Method (java.lang.reflect.Method)6 IdFunctionObject (net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)2 ClassConfiguration (com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 Page (com.gargoylesoftware.htmlunit.Page)1 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 RecursiveFunctionObject (com.gargoylesoftware.htmlunit.javascript.RecursiveFunctionObject)1 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)1 PropertyInfo (com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration.PropertyInfo)1 ActiveXObject (com.gargoylesoftware.htmlunit.javascript.host.ActiveXObject)1 Reflect (com.gargoylesoftware.htmlunit.javascript.host.Reflect)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1