JintNewExpression.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. namespace Jint.Runtime.Interpreter.Expressions
  4. {
  5. internal sealed class JintNewExpression : JintExpression
  6. {
  7. private JintExpression _calleeExpression = null!;
  8. private JintExpression[] _jintArguments = Array.Empty<JintExpression>();
  9. private bool _hasSpreads;
  10. public JintNewExpression(NewExpression expression) : base(expression)
  11. {
  12. _initialized = false;
  13. }
  14. protected override void Initialize(EvaluationContext context)
  15. {
  16. var engine = context.Engine;
  17. var expression = (NewExpression) _expression;
  18. _calleeExpression = Build(engine, expression.Callee);
  19. if (expression.Arguments.Count <= 0)
  20. {
  21. return;
  22. }
  23. _jintArguments = new JintExpression[expression.Arguments.Count];
  24. for (var i = 0; i < _jintArguments.Length; i++)
  25. {
  26. var argument = expression.Arguments[i];
  27. _jintArguments[i] = Build(engine, argument);
  28. _hasSpreads |= argument.Type == Nodes.SpreadElement;
  29. }
  30. }
  31. protected override ExpressionResult EvaluateInternal(EvaluationContext context)
  32. {
  33. var engine = context.Engine;
  34. // todo: optimize by defining a common abstract class or interface
  35. var jsValue = _calleeExpression.GetValue(context).Value;
  36. JsValue[] arguments;
  37. if (_jintArguments.Length == 0)
  38. {
  39. arguments = Array.Empty<JsValue>();
  40. }
  41. else if (_hasSpreads)
  42. {
  43. arguments = BuildArgumentsWithSpreads(context, _jintArguments);
  44. }
  45. else
  46. {
  47. arguments = engine._jsValueArrayPool.RentArray(_jintArguments.Length);
  48. BuildArguments(context, _jintArguments, arguments);
  49. }
  50. // Reset the location to the "new" keyword so that if an Error object is
  51. // constructed below, the stack trace will capture the correct location.
  52. context.LastSyntaxElement = _expression;
  53. if (!jsValue.IsConstructor)
  54. {
  55. ExceptionHelper.ThrowTypeError(engine.Realm, _calleeExpression.SourceText + " is not a constructor");
  56. }
  57. // construct the new instance using the Function's constructor method
  58. var instance = engine.Construct(jsValue, arguments, jsValue, _calleeExpression);
  59. engine._jsValueArrayPool.ReturnArray(arguments);
  60. return NormalCompletion(instance);
  61. }
  62. }
  63. }