Search in sources :

Example 1 with NativeFunction

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

the class DebugFrameImpl method getFunctionName.

/**
 * Returns the name of the function corresponding to this frame, if it is a function and it has
 * a name. If the function does not have a name, this method will try to return the name under
 * which it was referenced. See <a
 * href="http://www.digital-web.com/articles/scope_in_javascript/">this page</a> for a good
 * explanation of how the <tt>thisObj</tt> plays into this guess.
 *
 * @param thisObj the object via which the function was referenced, used to try to guess a
 *        function name if the function is anonymous
 * @return the name of the function corresponding to this frame
 */
private String getFunctionName(final Scriptable thisObj) {
    if (functionOrScript_.isFunction()) {
        final String name = functionOrScript_.getFunctionName();
        if (name != null && !name.isEmpty()) {
            // A named function -- we can just return the name.
            return name;
        }
        // on our HtmlUnitScriptable we need to avoid looking at the properties we have defined => TODO: improve it
        if (thisObj instanceof HtmlUnitScriptable) {
            return "[anonymous]";
        }
        Scriptable obj = thisObj;
        while (obj != null) {
            for (final Object id : obj.getIds()) {
                if (id instanceof String) {
                    final String s = (String) id;
                    if (obj instanceof ScriptableObject) {
                        Object o = ((ScriptableObject) obj).getGetterOrSetter(s, 0, thisObj, false);
                        if (o == null) {
                            o = ((ScriptableObject) obj).getGetterOrSetter(s, 0, thisObj, true);
                            if (o != null) {
                                return "__defineSetter__ " + s;
                            }
                        } else {
                            return "__defineGetter__ " + s;
                        }
                    }
                    final Object o;
                    try {
                        // within a try block as this sometimes throws (not sure why)
                        o = obj.get(s, obj);
                    } catch (final Exception e) {
                        return "[anonymous]";
                    }
                    if (o instanceof NativeFunction) {
                        final NativeFunction f = (NativeFunction) o;
                        if (f.getDebuggableView() == functionOrScript_) {
                            return s;
                        }
                    }
                }
            }
            obj = obj.getPrototype();
        }
        // Unable to intuit a name -- doh!
        return "[anonymous]";
    }
    // Just a script -- no function name.
    return "[script]";
}
Also used : ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) IdFunctionObject(net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)

Example 2 with NativeFunction

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

the class XPathEvaluator method evaluate.

/**
 * Evaluates an XPath expression string and returns a result of the specified type if possible.
 * @param expression the XPath expression string to be parsed and evaluated
 * @param contextNodeObj the context node for the evaluation of this XPath expression
 * @param resolver the resolver permits translation of all prefixes, including the XML namespace prefix,
 *        within the XPath expression into appropriate namespace URIs.
 * @param type If a specific type is specified, then the result will be returned as the corresponding type
 * @param result the result object which may be reused and returned by this method
 * @return the result of the evaluation of the XPath expression
 */
@JsxFunction
public XPathResult evaluate(final String expression, final Object contextNodeObj, final Object resolver, final int type, final Object result) {
    XPathResult xPathResult = (XPathResult) result;
    if (xPathResult == null) {
        xPathResult = new XPathResult();
        xPathResult.setParentScope(getParentScope());
        xPathResult.setPrototype(getPrototype(xPathResult.getClass()));
    }
    // contextNodeObj can be either a node or an array with the node as the first element.
    if (!(contextNodeObj instanceof Node)) {
        throw Context.reportRuntimeError("Illegal value for parameter 'context'");
    }
    final Node contextNode = (Node) contextNodeObj;
    PrefixResolver prefixResolver = null;
    if (resolver instanceof PrefixResolver) {
        prefixResolver = (PrefixResolver) resolver;
    } else if (resolver instanceof NativeFunction) {
        prefixResolver = new NativeFunctionPrefixResolver((NativeFunction) resolver, contextNode.getParentScope());
    }
    xPathResult.init(contextNode.getDomNodeOrDie().getByXPath(expression, prefixResolver), type);
    return xPathResult;
}
Also used : NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) PrefixResolver(org.apache.xml.utils.PrefixResolver) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with NativeFunction

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

the class ProxyAutoConfig method evaluate.

/**
 * Evaluates the <tt>FindProxyForURL</tt> method of the specified content.
 * @param content the JavaScript content
 * @param url the URL to be retrieved
 * @return semicolon-separated result
 */
public static String evaluate(final String content, final URL url) {
    try (Context cx = Context.enter()) {
        final ProxyAutoConfig config = new ProxyAutoConfig();
        final Scriptable scope = cx.initSafeStandardObjects();
        config.defineMethod("isPlainHostName", scope);
        config.defineMethod("dnsDomainIs", scope);
        config.defineMethod("localHostOrDomainIs", scope);
        config.defineMethod("isResolvable", scope);
        config.defineMethod("isInNet", scope);
        config.defineMethod("dnsResolve", scope);
        config.defineMethod("myIpAddress", scope);
        config.defineMethod("dnsDomainLevels", scope);
        config.defineMethod("shExpMatch", scope);
        config.defineMethod("weekdayRange", scope);
        config.defineMethod("dateRange", scope);
        config.defineMethod("timeRange", scope);
        cx.evaluateString(scope, "var ProxyConfig = function() {}; ProxyConfig.bindings = {}", "<init>", 1, null);
        cx.evaluateString(scope, content, "<Proxy Auto-Config>", 1, null);
        final Object[] functionArgs = { url.toExternalForm(), url.getHost() };
        final Object fObj = scope.get("FindProxyForURL", scope);
        final NativeFunction f = (NativeFunction) fObj;
        final Object result = f.call(cx, scope, scope, functionArgs);
        return Context.toString(result);
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 4 with NativeFunction

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

the class Document method evaluate.

/**
 * Evaluates an XPath expression string and returns a result of the specified type if possible.
 * @param expression the XPath expression string to be parsed and evaluated
 * @param contextNode the context node for the evaluation of this XPath expression
 * @param resolver the resolver permits translation of all prefixes, including the XML namespace prefix,
 *        within the XPath expression into appropriate namespace URIs.
 * @param type If a specific type is specified, then the result will be returned as the corresponding type
 * @param result the result object which may be reused and returned by this method
 * @return the result of the evaluation of the XPath expression
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public XPathResult evaluate(final String expression, final Node contextNode, final Object resolver, final int type, final Object result) {
    XPathResult xPathResult = (XPathResult) result;
    if (xPathResult == null) {
        xPathResult = new XPathResult();
        xPathResult.setParentScope(getParentScope());
        xPathResult.setPrototype(getPrototype(xPathResult.getClass()));
    }
    PrefixResolver prefixResolver = null;
    if (resolver instanceof NativeFunction) {
        prefixResolver = new NativeFunctionPrefixResolver((NativeFunction) resolver, contextNode.getParentScope());
    } else if (resolver instanceof PrefixResolver) {
        prefixResolver = (PrefixResolver) resolver;
    }
    xPathResult.init(contextNode.getDomNodeOrDie().getByXPath(expression, prefixResolver), type);
    return xPathResult;
}
Also used : NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) NativeFunctionPrefixResolver(com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver) PrefixResolver(org.apache.xml.utils.PrefixResolver) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

NativeFunction (net.sourceforge.htmlunit.corejs.javascript.NativeFunction)4 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)2 NativeFunctionPrefixResolver (com.gargoylesoftware.htmlunit.javascript.host.NativeFunctionPrefixResolver)2 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 PrefixResolver (org.apache.xml.utils.PrefixResolver)2 Context (net.sourceforge.htmlunit.corejs.javascript.Context)1 FunctionObject (net.sourceforge.htmlunit.corejs.javascript.FunctionObject)1 IdFunctionObject (net.sourceforge.htmlunit.corejs.javascript.IdFunctionObject)1 JavaScriptException (net.sourceforge.htmlunit.corejs.javascript.JavaScriptException)1