JsArray.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. private sealed class JsArrayDebugView
  25. {
  26. private readonly JsArray _array;
  27. public JsArrayDebugView(JsArray array)
  28. {
  29. _array = array;
  30. }
  31. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  32. public JsValue[] Values
  33. {
  34. get
  35. {
  36. var values = new JsValue[_array.Length];
  37. var i = 0;
  38. foreach (var value in _array)
  39. {
  40. values[i++] = value;
  41. }
  42. return values;
  43. }
  44. }
  45. }
  46. }