JintArrayExpression.cs 4.8 KB

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