예제 #1
0
        public static bool SetPrototypeOf(ObjectInstance target, object prototype)
        {
            // The prototype must be null or an object. Note that null in .NET is actually undefined in JS!
            var prototypeObj = prototype as ObjectInstance;

            if (prototypeObj == null && prototype != Null.Value)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Object prototype may only be an Object or null.");
            }
            return(target.SetPrototype(prototypeObj));
        }
예제 #2
0
        public static ObjectInstance SetPrototypeOf(ObjectInstance obj, object prototype)
        {
            // The prototype must be null or an object. Note that null in .NET is actually undefined in JS!
            var prototypeObj = prototype as ObjectInstance;

            if (prototypeObj == null && prototype != Null.Value)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Object prototype may only be an Object or null.");
            }

            // Attempt to set the prototype.
            obj.SetPrototype(prototypeObj, throwOnError: true);

            return(obj);
        }
예제 #3
0
        public static ObjectInstance SetPrototypeOf(ObjectInstance obj, object prototype)
        {
            // The prototype must be null or an object. Note that null in .NET is actually undefined in JS!
            var prototypeObj = prototype as ObjectInstance;

            if (prototypeObj == null && prototype != Null.Value)
            {
                throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Object prototype may only be an Object or null.");
            }

            // Attempt to set the prototype.
            if (!obj.SetPrototype(prototypeObj))
            {
                // Attempt to throw a reasonable error message based on what we know about how SetPrototype works.
                if (!obj.IsExtensible)
                {
                    throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Object is not extensible.");
                }
                throw new JavaScriptException(obj.Engine, ErrorType.TypeError, "Prototype chain contains a cyclic reference.");
            }

            return(obj);
        }
예제 #4
0
        /// <summary>
        /// Sets the next object in the prototype chain. Can be <c>null</c>, which indicates there
        /// are no further objects in the chain.
        /// </summary>
        /// <param name="prototype"> The new prototype. </param>
        /// <param name="throwOnError"> <c>true</c> to throw an exception if the prototype could not
        /// be set.  This can happen if the object is non-extensible or if setting the prototype
        /// would introduce a cyclic dependency. </param>
        /// <returns> <c>true</c> if the prototype was successfully applied; <c>false</c> otherwise. </returns>
        internal override bool SetPrototype(ObjectInstance prototype, bool throwOnError)
        {
            // Check for revocation.
            if (target == null || handler == null)
            {
                throw new JavaScriptException(ErrorType.TypeError, "Cannot call 'setPrototypeOf' on a proxy that has been revoked.");
            }

            // Call the handler, if one exists.
            var trap = handler.GetMethod("setPrototypeOf");

            if (trap == null)
            {
                return(target.SetPrototype(prototype, throwOnError));
            }
            var result = TypeConverter.ToBoolean(trap.CallLateBound(handler, target, prototype));

            // Validate.
            if (result == false)
            {
                if (throwOnError)
                {
                    throw new JavaScriptException(ErrorType.TypeError, "'setPrototypeOf' on proxy: trap returned falsish.");
                }
                return(false);
            }
            if (target.IsExtensible)
            {
                return(true);
            }
            // Trap returned true, and target is non-extensible.
            if (target.Prototype != prototype)
            {
                throw new JavaScriptException(ErrorType.TypeError, "'setPrototypeOf' on proxy: proxy target is non-extensible but the trap did not return its actual prototype.");
            }
            return(true);
        }