ArrayConstructor.cs 2.0 KB

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