ArrayConstructor.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using Jint.Native.Function;
  2. using Jint.Native.Object;
  3. using Jint.Runtime.Descriptors;
  4. using Jint.Runtime.Descriptors.Specialized;
  5. namespace Jint.Native.Array
  6. {
  7. public sealed class ArrayConstructor : FunctionInstance, IConstructor
  8. {
  9. private readonly Engine _engine;
  10. public ArrayConstructor(Engine engine) : base(engine, new ObjectInstance(engine.RootFunction), null, null)
  11. {
  12. _engine = engine;
  13. // the constructor is the function constructor of an object
  14. this.Prototype.DefineOwnProperty("constructor", new DataDescriptor(this) { Writable = true, Enumerable = false, Configurable = false }, false);
  15. this.Prototype.DefineOwnProperty("prototype", new DataDescriptor(this.Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
  16. // Array prototype properties
  17. this.Prototype.DefineOwnProperty("length", new ClrAccessDescriptor<ArrayInstance>(_engine, x => x.Length), false);
  18. this.Prototype.DefineOwnProperty("push", new ClrDataDescriptor<ArrayInstance>(engine, Push), false);
  19. this.Prototype.DefineOwnProperty("pop", new ClrDataDescriptor<ArrayInstance>(engine, Pop), false);
  20. }
  21. public override object Call(object thisObject, object[] arguments)
  22. {
  23. return Construct(arguments);
  24. }
  25. public ObjectInstance Construct(object[] arguments)
  26. {
  27. var instance = new ArrayInstance(Prototype);
  28. foreach (var arg in arguments)
  29. {
  30. instance.Push(arg);
  31. }
  32. return instance;
  33. }
  34. private static object Push(ArrayInstance thisObject, object[] arguments)
  35. {
  36. thisObject.Push(arguments[0]);
  37. return Undefined.Instance;
  38. }
  39. private static object Pop(ArrayInstance thisObject, object[] arguments)
  40. {
  41. return thisObject.Pop();
  42. }
  43. }
  44. }