JintArrayExpression.cs 3.8 KB

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