예제 #1
0
        //     INITIALIZATION
        //_________________________________________________________________________________________

        /// <summary>
        /// Creates a new instance of a user-defined class.
        /// </summary>
        /// <param name="prototype"> The next object in the prototype chain. </param>
        /// <param name="name"> The name of the class. Can be <c>null</c> if none were supplied. </param>
        /// <param name="instancePrototype"> The value of the 'prototype' property. </param>
        /// <param name="constructor"> A function that represents the constructor, if the class has
        /// one, or <c>null</c> otherwise. </param>
        /// <remarks>
        /// A class that doesn't extend looks like this:
        /// new ClassFunction(engine.Function.InstancePrototype, name, engine.Object.Construct(), constructor)
        ///
        /// A class that extends A looks like this:
        /// new ClassFunction(A, name, ObjectInstance.CreateRawObject(A.InstancePrototype), constructor)
        ///
        /// A class that extends null looks like this:
        /// new ClassFunction(engine.Function.InstancePrototype, name, ObjectInstance.CreateRawObject(null), constructor)
        /// </remarks>
        public ClassFunction(ObjectInstance prototype, string name, ObjectInstance instancePrototype, UserDefinedFunction constructor)
            : base(prototype)
        {
            if (instancePrototype == null)
            {
                throw new ArgumentNullException(nameof(instancePrototype));
            }
            this.constructor = constructor;

            // Initialize the instance prototype.
            instancePrototype.InitializeProperties(new List <PropertyNameAndValue>
            {
                new PropertyNameAndValue("constructor", this, PropertyAttributes.NonEnumerable)
            });

            // Now add properties to this object.
            int length = constructor == null ? 0 : constructor.Length;

            InitializeProperties(new List <PropertyNameAndValue>()
            {
                new PropertyNameAndValue("name", name ?? string.Empty, PropertyAttributes.Configurable),
                new PropertyNameAndValue("length", length, PropertyAttributes.Configurable),
                new PropertyNameAndValue("prototype", instancePrototype, PropertyAttributes.Writable),
            });
        }
예제 #2
0
        /// <summary>
        /// Initializes the prototype properties.
        /// </summary>
        /// <param name="obj"> The object to set the properties on. </param>
        /// <param name="constructor"> A reference to the constructor that owns the prototype. </param>
        internal static void InitializePrototypeProperties(ObjectInstance obj, SymbolConstructor constructor)
        {
            var engine     = obj.Engine;
            var properties = GetDeclarativeProperties(engine);

            properties.Add(new PropertyNameAndValue("constructor", constructor, PropertyAttributes.NonEnumerable));
            properties.Add(new PropertyNameAndValue(Symbol.ToStringTag, "Symbol", PropertyAttributes.Configurable));
            obj.InitializeProperties(properties);
        }
예제 #3
0
        /// <summary>
        /// Initializes the prototype properties.
        /// </summary>
        /// <param name="obj"> The object to set the properties on. </param>
        /// <param name="constructor"> A reference to the constructor that owns the prototype. </param>
        internal static void InitializePrototypeProperties(ObjectInstance obj, FunctionConstructor constructor)
        {
            var engine     = obj.Engine;
            var properties = GetDeclarativeProperties(engine);

            properties.Add(new PropertyNameAndValue("constructor", constructor, PropertyAttributes.NonEnumerable));
            properties.Add(new PropertyNameAndValue("name", "Empty", PropertyAttributes.Configurable));
            properties.Add(new PropertyNameAndValue("length", 0, PropertyAttributes.Configurable));
            obj.InitializeProperties(properties);
        }