JsArray.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Diagnostics;
  2. using Jint.Native.Array;
  3. namespace Jint.Native;
  4. [DebuggerTypeProxy(typeof(JsArrayDebugView))]
  5. [DebuggerDisplay("Count = {Length}")]
  6. public sealed class JsArray : ArrayInstance
  7. {
  8. /// <summary>
  9. /// Creates a new array instance with defaults.
  10. /// </summary>
  11. /// <param name="engine">The engine that this array is bound to.</param>
  12. /// <param name="capacity">The initial size of underlying data structure, if you know you're going to add N items, provide N.</param>
  13. /// <param name="length">Sets the length of the array.</param>
  14. public JsArray(Engine engine, uint capacity = 0, uint length = 0) : base(engine, capacity, length)
  15. {
  16. }
  17. /// <summary>
  18. /// Possibility to construct valid array fast.
  19. /// The array will be owned and modified by Jint afterwards.
  20. /// </summary>
  21. public JsArray(Engine engine, JsValue[] items) : base(engine, items)
  22. {
  23. }
  24. public uint Length => GetLength();
  25. private sealed class JsArrayDebugView
  26. {
  27. private readonly JsArray _array;
  28. public JsArrayDebugView(JsArray array)
  29. {
  30. _array = array;
  31. }
  32. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  33. public JsValue[] Values
  34. {
  35. get
  36. {
  37. var values = new JsValue[_array.GetLength()];
  38. var i = 0;
  39. foreach (var value in _array)
  40. {
  41. values[i++] = value;
  42. }
  43. return values;
  44. }
  45. }
  46. }
  47. }