JintArrayExpression.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Jint.Native;
  2. using Jint.Native.Iterator;
  3. namespace Jint.Runtime.Interpreter.Expressions
  4. {
  5. internal sealed class JintArrayExpression : JintExpression
  6. {
  7. private JintExpression?[] _expressions = Array.Empty<JintExpression?>();
  8. private bool _hasSpreads;
  9. private bool _initialized;
  10. private JintArrayExpression(ArrayExpression expression) : base(expression)
  11. {
  12. }
  13. public static JintExpression Build(ArrayExpression expression)
  14. {
  15. return expression.Elements.Count == 0
  16. ? JintEmptyArrayExpression.Instance
  17. : new JintArrayExpression(expression);
  18. }
  19. private void Initialize()
  20. {
  21. ref readonly var elements = ref ((ArrayExpression) _expression).Elements;
  22. var expressions = _expressions = new JintExpression[((ArrayExpression) _expression).Elements.Count];
  23. for (var n = 0; n < expressions.Length; n++)
  24. {
  25. var expr = elements[n];
  26. if (expr != null)
  27. {
  28. var expression = Build(expr);
  29. expressions[n] = expression;
  30. _hasSpreads |= expr.Type == NodeType.SpreadElement;
  31. }
  32. }
  33. // we get called from nested spread expansion in call
  34. _initialized = true;
  35. }
  36. protected override object EvaluateInternal(EvaluationContext context)
  37. {
  38. if (!_initialized)
  39. {
  40. Initialize();
  41. _initialized = true;
  42. }
  43. var engine = context.Engine;
  44. var a = engine.Realm.Intrinsics.Array.ArrayCreate(_hasSpreads ? 0 : (uint) _expressions.Length);
  45. uint arrayIndexCounter = 0;
  46. foreach (var expr in _expressions)
  47. {
  48. if (expr == null)
  49. {
  50. a.SetIndexValue(arrayIndexCounter++, null, updateLength: false);
  51. }
  52. else if (_hasSpreads && expr is JintSpreadExpression jse)
  53. {
  54. jse.GetValueAndCheckIterator(context, out var objectInstance, out var iterator);
  55. // optimize for array
  56. if (objectInstance is JsArray ai)
  57. {
  58. var length = ai.GetLength();
  59. var newLength = arrayIndexCounter + length;
  60. a.EnsureCapacity(newLength);
  61. a.CopyValues(ai, sourceStartIndex: 0, targetStartIndex: arrayIndexCounter, length);
  62. arrayIndexCounter += length;
  63. a.SetLength(newLength);
  64. }
  65. else
  66. {
  67. var protocol = new ArraySpreadProtocol(engine, a, iterator!, arrayIndexCounter);
  68. protocol.Execute();
  69. arrayIndexCounter += protocol._addedCount;
  70. }
  71. }
  72. else
  73. {
  74. var value = expr.GetValue(context);
  75. a.SetIndexValue(arrayIndexCounter++, value, updateLength: false);
  76. }
  77. }
  78. if (_hasSpreads)
  79. {
  80. a.SetLength(arrayIndexCounter);
  81. }
  82. return a;
  83. }
  84. private sealed class ArraySpreadProtocol : IteratorProtocol
  85. {
  86. private readonly JsArray _instance;
  87. private long _index;
  88. internal uint _addedCount;
  89. public ArraySpreadProtocol(
  90. Engine engine,
  91. JsArray instance,
  92. IteratorInstance iterator,
  93. long startIndex) : base(engine, iterator, 0)
  94. {
  95. _instance = instance;
  96. _index = startIndex - 1;
  97. }
  98. protected override void ProcessItem(JsValue[] arguments, JsValue currentValue)
  99. {
  100. _index++;
  101. _addedCount++;
  102. _instance.SetIndexValue((uint) _index, currentValue, updateLength: false);
  103. }
  104. }
  105. internal sealed class JintEmptyArrayExpression : JintExpression
  106. {
  107. public static JintEmptyArrayExpression Instance = new(new ArrayExpression(NodeList.Create(System.Linq.Enumerable.Empty<Expression?>())));
  108. private JintEmptyArrayExpression(Expression expression) : base(expression)
  109. {
  110. }
  111. protected override object EvaluateInternal(EvaluationContext context)
  112. {
  113. return new JsArray(context.Engine, Array.Empty<JsValue>());
  114. }
  115. public override JsValue GetValue(EvaluationContext context)
  116. {
  117. return new JsArray(context.Engine, Array.Empty<JsValue>());
  118. }
  119. }
  120. }
  121. }