ArrayConstructor.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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, false)
  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. // Array prototype functions
  19. this.Prototype.DefineOwnProperty("push", new ClrDataDescriptor<ArrayInstance, object>(engine, Push), false);
  20. this.Prototype.DefineOwnProperty("pop", new ClrDataDescriptor<ArrayInstance, object>(engine, Pop), false);
  21. }
  22. public override object Call(object thisObject, object[] arguments)
  23. {
  24. return Construct(arguments);
  25. }
  26. public ObjectInstance Construct(object[] arguments)
  27. {
  28. var instance = new ArrayInstance(Prototype);
  29. foreach (var arg in arguments)
  30. {
  31. instance.Push(arg);
  32. }
  33. return instance;
  34. }
  35. private static object Push(ArrayInstance thisObject, object[] arguments)
  36. {
  37. thisObject.Push(arguments[0]);
  38. return arguments[0];
  39. }
  40. private static object Pop(ArrayInstance thisObject, object[] arguments)
  41. {
  42. return thisObject.Pop();
  43. }
  44. }
  45. }