ArrayConstructor.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using Jint.Native.Function;
  3. using Jint.Native.Object;
  4. using Jint.Runtime;
  5. namespace Jint.Native.Array
  6. {
  7. public sealed class ArrayConstructor : FunctionInstance, IConstructor
  8. {
  9. private ArrayConstructor(Engine engine) : base(engine, null, null, false)
  10. {
  11. }
  12. public ObjectInstance PrototypeObject { get; private set; }
  13. public static ArrayConstructor CreateArrayConstructor(Engine engine)
  14. {
  15. var obj = new ArrayConstructor(engine);
  16. obj.Extensible = true;
  17. // The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object
  18. obj.Prototype = engine.Function.PrototypeObject;
  19. obj.PrototypeObject = ArrayPrototype.CreatePrototypeObject(engine, obj);
  20. obj.FastAddProperty("length", 1, false, false, false);
  21. // The initial value of Array.prototype is the Array prototype object
  22. obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
  23. return obj;
  24. }
  25. public override object Call(object thisObject, object[] arguments)
  26. {
  27. return Construct(arguments);
  28. }
  29. public ObjectInstance Construct(object[] arguments)
  30. {
  31. var instance = new ArrayInstance(Engine);
  32. instance.Prototype = PrototypeObject;
  33. instance.Extensible = true;
  34. if (arguments.Length == 1 && TypeConverter.GetType(arguments[0]) == TypeCode.Double)
  35. {
  36. var length = TypeConverter.ToNumber(arguments[0]);
  37. instance.FastAddProperty("length", length, true, false, true);
  38. }
  39. else
  40. {
  41. instance.FastAddProperty("length", 0, true, false, true);
  42. ((ArrayPrototype)PrototypeObject).Push(instance, arguments);
  43. }
  44. return instance;
  45. }
  46. }
  47. }