ArrayConstructor.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 ArrayPrototype 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 void Configure()
  26. {
  27. }
  28. public override object Call(object thisObject, object[] arguments)
  29. {
  30. return Construct(arguments);
  31. }
  32. public ObjectInstance Construct(object[] arguments)
  33. {
  34. var instance = new ArrayInstance(Engine);
  35. instance.Prototype = PrototypeObject;
  36. instance.Extensible = true;
  37. if (arguments.Length == 1 && TypeConverter.GetType(arguments[0]) == TypeCode.Double)
  38. {
  39. var length = TypeConverter.ToNumber(arguments[0]);
  40. instance.FastAddProperty("length", length, true, false, true);
  41. }
  42. else
  43. {
  44. instance.FastAddProperty("length", 0, true, false, true);
  45. ((ArrayPrototype)PrototypeObject).Push(instance, arguments);
  46. }
  47. return instance;
  48. }
  49. }
  50. }