CreateRawObject() static private method

Creates an Object instance (use ObjectConstructor.Construct rather than this).
static private CreateRawObject ( ObjectInstance prototype ) : ObjectInstance
prototype ObjectInstance The next object in the prototype chain.
return ObjectInstance
示例#1
0
        /// <summary>
        /// Creates the array iterator prototype object.
        /// </summary>
        /// <param name="engine"> The script environment. </param>
        internal static ObjectInstance CreatePrototype(ScriptEngine engine)
        {
            var result     = ObjectInstance.CreateRawObject(engine.BaseIteratorPrototype);
            var properties = GetDeclarativeProperties(engine);

            result.FastSetProperties(properties);
            return(result);
        }
示例#2
0
        /// <summary>
        /// Creates an object, using this function as the constructor.
        /// </summary>
        /// <param name="argumentValues"> An array of argument values. </param>
        /// <returns> The object that was created. </returns>
        public virtual ObjectInstance ConstructLateBound(params object[] argumentValues)
        {
            // Create a new object and set the prototype to the instance prototype of the function.
            var newObject = ObjectInstance.CreateRawObject(this.InstancePrototype);

            // Run the function, with the new object as the "this" keyword.
            var result = CallLateBound(newObject, argumentValues);

            // Return the result of the function if it is an object.
            if (result is ObjectInstance)
            {
                return((ObjectInstance)result);
            }

            // Otherwise, return the new object.
            return(newObject);
        }
示例#3
0
        public static ObjectInstance Create(ScriptEngine engine, object prototype, [DefaultParameterValue(null)] ObjectInstance properties = null)
        {
            if ((prototype is ObjectInstance) == false && prototype != Null.Value)
            {
                throw new JavaScriptException(engine, "TypeError", "object prototype must be an object or null");
            }
            ObjectInstance result;

            if (prototype == Null.Value)
            {
                result = ObjectInstance.CreateRootObject(engine);
            }
            else
            {
                result = ObjectInstance.CreateRawObject((ObjectInstance)prototype);
            }
            if (properties != null)
            {
                DefineProperties(result, properties);
            }
            return(result);
        }
示例#4
0
        /// <summary>
        /// Creates an object, using this function as the constructor.
        /// </summary>
        /// <param name="newTarget"> The value of 'new.target'. </param>
        /// <param name="argumentValues"> An array of argument values. </param>
        /// <returns> The object that was created. </returns>
        public override ObjectInstance ConstructLateBound(FunctionInstance newTarget, params object[] argumentValues)
        {
            // Create a new object and set the prototype to the instance prototype of the function.
            var newObject = ObjectInstance.CreateRawObject(newTarget.InstancePrototype);

            // Run the function, with the new object as the "this" keyword.
            var context = ExecutionContext.CreateConstructContext(
                engine: this.Engine,
                parentScope: this.ParentScope,
                thisValue: newObject,
                executingFunction: this,
                newTarget: newTarget,
                functionContainer: null);
            var result = this.body(context, argumentValues);

            // Return the result of the function if it is an object.
            if (result is ObjectInstance)
            {
                return((ObjectInstance)result);
            }

            // Otherwise, return the new object.
            return(newObject);
        }
示例#5
0
 public ObjectInstance Construct()
 {
     return(ObjectInstance.CreateRawObject(this.InstancePrototype));
 }
示例#6
0
        /// <summary>
        /// Creates an object, using this function as the constructor.
        /// </summary>
        /// <param name="newTarget"> The value of 'new.target'. </param>
        /// <param name="argumentValues"> An array of argument values. </param>
        /// <returns> The object that was created. </returns>
        public override ObjectInstance ConstructLateBound(FunctionInstance newTarget, params object[] argumentValues)
        {
            if (this.constructor != null)
            {
                // This class has a constructor.
                ExecutionContext context;

                if (Prototype != null && Prototype != Engine.Function.InstancePrototype)
                {
                    // This class extends another. In that case 'this' is unavailable.
                    context = ExecutionContext.CreateDerivedContext(
                        engine: this.Engine,
                        parentScope: this.constructor.ParentScope,
                        executingFunction: this.constructor,
                        newTarget: newTarget,
                        functionContainer: this);
                }
                else
                {
                    // This class doesn't extend from another class. Create a new object and set
                    // 'this' equal to the newly created object.
                    context = ExecutionContext.CreateConstructContext(
                        engine: this.Engine,
                        parentScope: this.constructor.ParentScope,
                        thisValue: ObjectInstance.CreateRawObject(newTarget.InstancePrototype),
                        executingFunction: this.constructor,
                        newTarget: newTarget,
                        functionContainer: this);
                }

                // Call the function.
                var result = this.constructor.Body(context, argumentValues);

                // If the constructor returned an object, use that, otherwise use the 'this' value.
                if (result is ObjectInstance resultObject)
                {
                    return(resultObject);
                }

                // Make sure super() was called in derived classes.
                if (context.ThisBindingStatus == ExecutionContext.BindingStatus.Uninitialized)
                {
                    throw new JavaScriptException(ErrorType.ReferenceError, "Must call super constructor in derived class before returning.");
                }
                return((ObjectInstance)context.ThisValue);   // Must be ObjectInstance since it is only set in ExecutionContext.CallSuperClass().
            }
            else if (Prototype != Engine.Function.InstancePrototype)
            {
                // Call the base class constructor.
                if (Prototype is FunctionInstance super)
                {
                    return(super.ConstructLateBound(newTarget, argumentValues));
                }
                else
                {
                    throw new JavaScriptException(ErrorType.TypeError, $"Super constructor {Prototype} of {Name} is not a constructor.");
                }
            }
            else
            {
                // There's no constructor and no base class.
                return(ObjectInstance.CreateRawObject(newTarget.InstancePrototype));
            }
        }