Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Scriptable#getParentScope()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Scriptable#getParentScope() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecursiveFunctionObject.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
    final Object object = super.call(cx, scope, thisObj, args);
    if (object instanceof Scriptable) {
        final Scriptable result = (Scriptable) object;
        if (result.getPrototype() == null) {
            final Scriptable proto = getClassPrototype();
            if (result != proto) {
                result.setPrototype(proto);
            }
        }
        if (result.getParentScope() == null) {
            final Scriptable parent = getParentScope();
            if (result != parent) {
                result.setParentScope(parent);
            }
        }
    }
    return object;
}
 
Example 2
Source File: RecursiveFunctionObject.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
    final Object object = super.call(cx, scope, thisObj, args);
    if (object instanceof Scriptable) {
        final Scriptable result = (Scriptable) object;
        if (result.getPrototype() == null) {
            final Scriptable proto = getClassPrototype();
            if (result != proto) {
                result.setPrototype(proto);
            }
        }
        if (result.getParentScope() == null) {
            final Scriptable parent = getParentScope();
            if (result != parent) {
                result.setParentScope(parent);
            }
        }
    }
    return object;
}
 
Example 3
Source File: Symbol.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static Symbol getSymbol(final Scriptable thisObj, final String name) {
    final SimpleScriptable scope = (SimpleScriptable) thisObj.getParentScope();
    final BrowserVersion browserVersion = scope.getBrowserVersion();

    Map<String, Symbol> map = SYMBOL_MAP_.get(browserVersion.getNickname());
    if (map == null) {
        map = new HashMap<>();
        SYMBOL_MAP_.put(browserVersion.getNickname(), map);
    }

    Symbol symbol = map.get(name);
    if (symbol == null) {
        symbol = new Symbol();
        symbol.name_ = name;
        symbol.setParentScope(scope);
        symbol.setPrototype(scope.getPrototype(symbol.getClass()));
        map.put(name, symbol);
    }

    return symbol;
}
 
Example 4
Source File: Symbol.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Searches for existing symbols in a runtime-wide symbol registry with the given key and returns it if found.
 * Otherwise a new symbol gets created in the global symbol registry with this key.
 * @param context the context
 * @param thisObj this object
 * @param args the arguments
 * @param function the function
 * @return the symbol
 */
@JsxStaticFunction(functionName = "for")
public static Symbol forFunction(final Context context, final Scriptable thisObj, final Object[] args,
        final Function function) {
    final String key = Context.toString(args.length != 0 ? args[0] : Undefined.instance);

    Symbol symbol = (Symbol) ((ScriptableObject) thisObj).get(key);
    if (symbol == null) {
        final SimpleScriptable parentScope = (SimpleScriptable) thisObj.getParentScope();

        symbol = new Symbol();
        symbol.name_ = key;
        symbol.setParentScope(parentScope);
        symbol.setPrototype(parentScope.getPrototype(symbol.getClass()));
        thisObj.put(key, thisObj, symbol);
    }
    return symbol;
}
 
Example 5
Source File: RecursiveFunctionObject.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the browser version currently used.
 * @return the browser version
 */
public BrowserVersion getBrowserVersion() {
    Scriptable parent = getParentScope();
    while (!(parent instanceof Window)) {
        parent = parent.getParentScope();
    }
    return ((Window) parent).getBrowserVersion();
}
 
Example 6
Source File: DebugFrameImpl.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onEnter(final Context cx, final Scriptable activation, final Scriptable thisObj, final Object[] args) {
    if (LOG.isTraceEnabled()) {
        final StringBuilder sb = new StringBuilder();

        final String line = getFirstLine(cx);
        final String source = getSourceName(cx);
        sb.append(source).append(':').append(line).append(' ');

        Scriptable parent = activation.getParentScope();
        while (parent != null) {
            sb.append("   ");
            parent = parent.getParentScope();
        }
        final String functionName = getFunctionName(thisObj);
        sb.append(functionName).append('(');
        final int nbParams = functionOrScript_.getParamCount();
        for (int i = 0; i < nbParams; i++) {
            final String argAsString;
            if (i < args.length) {
                argAsString = stringValue(args[i]);
            }
            else {
                argAsString = "undefined";
            }
            sb.append(getParamName(i)).append(": ").append(argAsString);
            if (i < nbParams - 1) {
                sb.append(", ");
            }
        }
        sb.append(')');

        LOG.trace(sb);
    }
}
 
Example 7
Source File: RecursiveFunctionObject.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the browser version currently used.
 * @return the browser version
 */
public BrowserVersion getBrowserVersion() {
    Scriptable parent = getParentScope();
    while (!(parent instanceof Window)) {
        parent = parent.getParentScope();
    }
    return ((Window) parent).getBrowserVersion();
}
 
Example 8
Source File: DebugFrameImpl.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onEnter(final Context cx, final Scriptable activation, final Scriptable thisObj, final Object[] args) {
    if (LOG.isTraceEnabled()) {
        final StringBuilder sb = new StringBuilder();

        final String line = getFirstLine(cx);
        final String source = getSourceName(cx);
        sb.append(source).append(":").append(line).append(" ");

        Scriptable parent = activation.getParentScope();
        while (parent != null) {
            sb.append("   ");
            parent = parent.getParentScope();
        }
        final String functionName = getFunctionName(thisObj);
        sb.append(functionName).append("(");
        final int nbParams = functionOrScript_.getParamCount();
        for (int i = 0; i < nbParams; i++) {
            final String argAsString;
            if (i < args.length) {
                argAsString = stringValue(args[i]);
            }
            else {
                argAsString = "undefined";
            }
            sb.append(getParamName(i)).append(": ").append(argAsString);
            if (i < nbParams - 1) {
                sb.append(", ");
            }
        }
        sb.append(")");

        LOG.trace(sb);
    }
}