using System; using Jint.Native.Function; using Jint.Native.Object; using Jint.Runtime; namespace Jint.Native.Array { public sealed class ArrayConstructor : FunctionInstance, IConstructor { private ArrayConstructor(Engine engine) : base(engine, null, null, false) { } public ObjectInstance PrototypeObject { get; private set; } public static ArrayConstructor CreateArrayConstructor(Engine engine) { var obj = new ArrayConstructor(engine); obj.Extensible = true; // The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object obj.Prototype = engine.Function.PrototypeObject; obj.PrototypeObject = ArrayPrototype.CreatePrototypeObject(engine, obj); obj.FastAddProperty("length", 1, false, false, false); // The initial value of Array.prototype is the Array prototype object obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false); return obj; } public override object Call(object thisObject, object[] arguments) { return Construct(arguments); } public ObjectInstance Construct(object[] arguments) { var instance = new ArrayInstance(Engine); instance.Prototype = PrototypeObject; instance.Extensible = true; if (arguments.Length == 1 && TypeConverter.GetType(arguments[0]) == TypeCode.Double) { var length = TypeConverter.ToNumber(arguments[0]); instance.FastAddProperty("length", length, true, false, true); } else { instance.FastAddProperty("length", 0, true, false, true); ((ArrayPrototype)PrototypeObject).Push(instance, arguments); } return instance; } } }