JintArrayExpression.cs 3.8 KB

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