ArrayConstructor.cs 2.2 KB

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