<strong>Warning</strong>: This doesn't allow to resolve primitive
prototype properly when many top scopes are involved

@deprecated Use {@link #toObjectOrNull(Context, Object, Scriptable)} instead
339627
@Deprecated
    public static Scriptable toObjectOrNull(Context cx, Object obj)
    {
        if (obj instanceof Scriptable) {
            return (Scriptable)obj;
        } else if (obj != null && obj != Undefined.instance) {
            return toObject(cx, getTopCallScope(cx), obj);
        }
        return null;
    }
247396
public static <T, IV, P> IV injectIntoWith(
            IV injectValue,
            Iterable<T> iterable,
            Function3<? super IV, ? super T, ? super P, ? extends IV> function,
            P parameter)
    {
        if (iterable instanceof MutableCollection)
        {
            return ((MutableCollection<T>) iterable).injectIntoWith(injectValue, function, parameter);
        }
        if (iterable instanceof ArrayList)
        {
            return ArrayListIterate.injectIntoWith(injectValue, (ArrayList<T>) iterable, function, parameter);
        }
        if (iterable instanceof RandomAccess)
        {
            return RandomAccessListIterate.injectIntoWith(injectValue, (List<T>) iterable, function, parameter);
        }
        if (iterable != null)
        {
            return IterableIterate.injectIntoWith(injectValue, iterable, function, parameter);
        }
        throw new IllegalArgumentException("Cannot perform an injectIntoWith on null");
    }
1163430
function() {
      return extend(this.callSuper('toObject'), {
        x1: this.get('x1'),
        y1: this.get('y1'),
        x2: this.get('x2'),
        y2: this.get('y2')
      });
    }
833227
@Deprecated
    public static Object setObjectIndex(Object obj, double dblIndex,
                                        Object value, Context cx)
    {
        return setObjectIndex(obj, dblIndex, value, cx, getTopCallScope(cx));
    }
71115
@Override
  public boolean isUnknownType() {
    // If the object is unknown now, check the supertype again,
    // because it might have been resolved since the last check.
    if (unknown) {
      ObjectType implicitProto = getImplicitPrototype();
      if (implicitProto == null || implicitProto.isNativeObjectType()) {
        unknown = false;
        for (ObjectType interfaceType : getCtorExtendedInterfaces()) {
          if (interfaceType.isUnknownType()) {
            unknown = true;
            break;
          }
        }
      } else {
        unknown = implicitProto.isUnknownType();
      }
    }
    return unknown;
  }
314405
void expectSuperType(Node n, ObjectType superObject, ObjectType subObject) {
    FunctionType subCtor = subObject.getConstructor();
    ObjectType implicitProto = subObject.getImplicitPrototype();
    ObjectType declaredSuper =
        implicitProto == null ? null : implicitProto.getImplicitPrototype();
    if (declaredSuper != null && declaredSuper.isTemplatizedType()) {
      declaredSuper =
          declaredSuper.toMaybeTemplatizedType().getReferencedType();
    }
    if (declaredSuper != null
        && !(superObject instanceof UnknownType)
        && !declaredSuper.isEquivalentTo(superObject)) {
      if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
        registerMismatch(
            superObject,
            declaredSuper,
            report(JSError.make(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
      } else {
        mismatch(n, "mismatch in declaration of superclass type", superObject, declaredSuper);
      }

      // Correct the super type.
      if (!subCtor.hasCachedValues()) {
        subCtor.setPrototypeBasedOn(superObject);
      }
    }
  }
1296569
private boolean isSupportedCallType(Node callNode) {
    if (!callNode.getFirstChild().isName()) {
      if (NodeUtil.isFunctionObjectCall(callNode)) {
        if (!assumeStrictThis) {
          Node thisValue = callNode.getSecondChild();
          if (thisValue == null || !thisValue.isThis()) {
            return false;
          }
        }
      } else if (NodeUtil.isFunctionObjectApply(callNode)) {
        return false;
      }
    }

    return true;
  }
56637
public boolean isJavaScriptObject( JType type ) {
        return null != type.isClass() && type.isClass().isAssignableTo( jsoType );
    }
1028147
private static Object castObject(
        Class<?> toType, Class<?> fromType, Object fromValue,
        int operation, boolean checkOnly )
        throws UtilEvalError
    {
        /*
            Lots of preconditions checked here...
            Once things are running smoothly we might comment these out
            (That's what assertions are for).
        */
        if ( checkOnly && fromValue != null )
            throw new InterpreterError("bad cast params 1");
        if ( !checkOnly && fromValue == null )
            throw new InterpreterError("bad cast params 2");
        if ( fromType == Primitive.class )
            throw new InterpreterError("bad from Type, need to unwrap");
        if ( fromValue == Primitive.NULL && fromType != null )
            throw new InterpreterError("inconsistent args 1");
        if ( fromValue == Primitive.VOID && fromType != Void.TYPE )
            throw new InterpreterError("inconsistent args 2");
        if ( toType == Void.TYPE )
            throw new InterpreterError("loose toType should be null");

        // assignment to loose type, void type, or exactly same type
        if ( toType == null || arrayElementType(toType) == arrayElementType(fromType) )
            return checkOnly ? VALID_CAST :
                fromValue;

        if ( null != fromType && fromType.isArray() )
            if ( operation == Types.CAST
                    || isJavaAssignable(Collection.class, toType)
                    || isJavaAssignable(Map.class, toType) )
                return checkOnly ? VALID_CAST : BshArray.castArray(
                        toType, fromType, fromValue );

        // Casting to primitive type
        if ( toType.isPrimitive() )
        {
            if ( fromType == Void.TYPE || fromType == null
                || fromType.isPrimitive() )
            {
                // Both primitives, do primitive cast
                return Primitive.castPrimitive(
                    toType, fromType, (Primitive)fromValue,
                    checkOnly, operation );
            } else
            {
                if ( Primitive.isWrapperType( fromType ) )
                {
                    // wrapper to primitive
                    // Convert value to Primitive and check/cast it.

                    //Object r = checkOnly ? VALID_CAST :
                    Class unboxedFromType = Primitive.unboxType( fromType );
                    Primitive primFromValue;
                    if ( checkOnly )
                        primFromValue = null; // must be null in checkOnly
                    else
                        primFromValue = (Primitive)Primitive.wrap(
                            fromValue, unboxedFromType );

                    return Primitive.castPrimitive(
                        toType, unboxedFromType, primFromValue,
                        checkOnly, operation );
                } else
                {
                    // Cannot cast from arbitrary object to primitive
                    if ( checkOnly )
                        return INVALID_CAST;
                    else
                        throw castError( toType, fromType, operation );
                }
            }
        }

        // Else, casting to reference type

        // Casting from primitive or void (to reference type)
        if ( fromType == Void.TYPE || fromType == null
            || fromType.isPrimitive() )
        {
            // cast from primitive to wrapper type
            if ( Primitive.isWrapperType( toType )
                && fromType != Void.TYPE && fromType != null )
            {
                // primitive to wrapper type
                return checkOnly ? VALID_CAST :
                    Primitive.castWrapper(
                        Primitive.unboxType(toType),
                        ((Primitive)fromValue).getValue() );
            }

            // Primitive (not null or void) to Object.class type
            if ( toType == Object.class
                && fromType != Void.TYPE && fromType != null )
            {
                // box it
                return checkOnly ? VALID_CAST :
                    ((Primitive)fromValue).getValue();
            }

            // Primitive to arbitrary object type.
            // Allow Primitive.castToType() to handle it as well as cases of
            // Primitive.NULL and Primitive.VOID
            return Primitive.castPrimitive(
                toType, fromType, (Primitive)fromValue, checkOnly, operation );
        }

        // If type already assignable no cast necessary
        // We do this last to allow various errors above to be caught.
        // e.g cast Primitive.Void to Object would pass this
        if ( toType.isAssignableFrom( fromType ) )
            return checkOnly ? VALID_CAST :
                fromValue;

        // Can we use the proxy mechanism to cast a bsh.This to
        // the correct interface?
        if ( toType.isInterface()
            && bsh.This.class.isAssignableFrom( fromType )
        )
            return checkOnly ? VALID_CAST :
                ((bsh.This)fromValue).getInterface( toType );

        // Both numeric wrapper types?
        // Try numeric style promotion wrapper cast
        if ( Primitive.isWrapperType( toType )
            && Primitive.isWrapperType( fromType ) )
            return checkOnly ? VALID_CAST :
                Primitive.castWrapper( toType, fromValue );

        if ( checkOnly )
            return INVALID_CAST;
        else
            throw castError( toType, fromType , operation  );
    }
367576
public static void main(String[] args)
    {
        Context cx = Context.enter();
        try {
            // Set version to JavaScript1.2 so that we get object-literal style
            // printing instead of "[object Object]"
            cx.setLanguageVersion(Context.VERSION_1_2);

            // Initialize the standard objects (Object, Function, etc.)
            // This must be done before scripts can be executed.
            Scriptable scope = cx.initStandardObjects();

            // Now we can evaluate a script. Let's create a new object
            // using the object literal notation.
            Object result = cx.evaluateString(scope, "obj = {a:1, b:['x','y']}",
                                              "MySource", 1, null);

            Scriptable obj = (Scriptable) scope.get("obj", scope);

            // Should print "obj == result" (Since the result of an assignment
            // expression is the value that was assigned)
            System.out.println("obj " + (obj == result ? "==" : "!=") +
                               " result");

            // Should print "obj.a == 1"
            System.out.println("obj.a == " + obj.get("a", obj));

            Scriptable b = (Scriptable) obj.get("b", obj);

            // Should print "obj.b[0] == x"
            System.out.println("obj.b[0] == " + b.get(0, b));

            // Should print "obj.b[1] == y"
            System.out.println("obj.b[1] == " + b.get(1, b));

            // Should print {a:1, b:["x", "y"]}
            Function fn = (Function) ScriptableObject.getProperty(obj, "toString");
            System.out.println(fn.call(cx, scope, obj, new Object[0]));
        } finally {
            Context.exit();
        }
    }
441231
function() {
       if ($caller !== null) {
            for(var $s = $caller.boundTo.$parent; $s !== null; $s = $s.$parent) {
                var m = $s.prototype[$caller.methodName];
                if (m !== undefined) {
                    return m.apply(this, arguments);
                }
            }
        } else {
            throw new Error("$super is called outside of class context");
        }
    }
157049
static void loadFromIterable(Context cx, Scriptable scope, ScriptableObject set, Object arg1)
    {
        if ((arg1 == null) || Undefined.instance.equals(arg1)) {
            return;
        }

        // Call the "[Symbol.iterator]" property as a function.
        Object ito = ScriptRuntime.callIterator(arg1, cx, scope);
        if (Undefined.instance.equals(ito)) {
            // Per spec, ignore if the iterator returns undefined
            return;
        }

        // Find the "add" function of our own prototype, since it might have
        // been replaced. Since we're not fully constructed yet, create a dummy instance
        // so that we can get our own prototype.
        ScriptableObject dummy = ensureScriptableObject(cx.newObject(scope, set.getClassName()));
        final Callable add =
            ScriptRuntime.getPropFunctionAndThis(dummy.getPrototype(), "add", cx, scope);
        // Clean up the value left around by the previous function
        ScriptRuntime.lastStoredScriptable(cx);

        // Finally, run through all the iterated values and add them!
        try (IteratorLikeIterable it = new IteratorLikeIterable(cx, scope, ito)) {
            for (Object val : it) {
                final Object finalVal = val == Scriptable.NOT_FOUND ? Undefined.instance : val;
                add.call(cx, scope, set, new Object[]{finalVal});
            }
        }
    }
65233
function(callback, ctx){
      var k = 0;

      // 1. Let O be the result of calling ToObject passing the |this| value as the argument.
      var O = Object(this);

      var len = O.length >>> 0; 

      if ( typeof callback !== "function" ) {
        throw new TypeError( callback + " is not a function" );
      }

      // 7. Repeat, while k < len
      while( k < len ) {

        var kValue;

        if ( k in O ) {

          kValue = O[ k ];

          callback.call( ctx, kValue, k, O );
        }
        k++;
      }
    }
51982
private boolean setPrototypeNoCheck(ObjectType prototype, Node propertyNode) {
    ObjectType oldPrototype = prototypeSlot == null ? null : (ObjectType) prototypeSlot.getType();
    boolean replacedPrototype = oldPrototype != null;

    this.prototypeSlot =
        new Property("prototype", prototype, true, propertyNode == null ? source : propertyNode);
    prototype.setOwnerFunction(this);

    if (oldPrototype != null) {
      // Disassociating the old prototype makes this easier to debug--
      // we don't have to worry about two prototypes running around.
      oldPrototype.setOwnerFunction(null);
    }

    if (isConstructor() || isInterface()) {
      FunctionType superClass = getSuperClassConstructor();
      if (superClass != null) {
        superClass.addSubType(this);
        wasAddedToExtendedConstructorSubtypes = true;
      }

      if (isInterface()) {
        for (ObjectType interfaceType : getExtendedInterfaces()) {
          if (interfaceType.getConstructor() != null) {
            interfaceType.getConstructor().addSubType(this);
          }
        }
      }
    }

    if (replacedPrototype) {
      clearCachedValues();
    }

    return true;
  }
562609
@Nullable
  private String deepestResolvedTypeNameOf(ObjectType objType) {
    if (!objType.isResolved() || !(objType instanceof ProxyObjectType)) {
      return objType.getReferenceName();
    }

    ObjectType internal = ((ProxyObjectType) objType).getReferencedObjTypeInternal();
    return (internal != null && internal.isNominalType())
        ? deepestResolvedTypeNameOf(internal)
        : null;
  }
124010
@Override
  public String toObjectExpr(String columnName)
  {
    if (_value == null) {
      return "null";
    }
    else if (_value instanceof String) {
      return "'" + _value + "'";
    }
    else {
      return String.valueOf(_value);
    }
  }
453720
public static Object setObjectProp(Object obj, String property,
                                       Object value, Context cx,
                                       Scriptable scope)
    {
        Scriptable sobj = toObjectOrNull(cx, obj, scope);
        if (sobj == null) {
            throw undefWriteError(obj, property, value);
        }
        return setObjectProp(sobj, property, value, cx);
    }