JintCallExpression.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. using Jint.Runtime.Environments;
  6. using Jint.Runtime.References;
  7. namespace Jint.Runtime.Interpreter.Expressions
  8. {
  9. internal sealed class JintCallExpression : JintExpression
  10. {
  11. private CachedArgumentsHolder _cachedArguments;
  12. private bool _cached;
  13. private JintExpression _calleeExpression;
  14. private bool _hasSpreads;
  15. public JintCallExpression(Engine engine, CallExpression expression) : base(engine, expression)
  16. {
  17. _initialized = false;
  18. }
  19. protected override void Initialize()
  20. {
  21. var expression = (CallExpression) _expression;
  22. _calleeExpression = Build(_engine, expression.Callee);
  23. var cachedArgumentsHolder = new CachedArgumentsHolder
  24. {
  25. JintArguments = new JintExpression[expression.Arguments.Count]
  26. };
  27. static bool CanSpread(Node e)
  28. {
  29. return e?.Type == Nodes.SpreadElement
  30. || e is AssignmentExpression ae && ae.Right?.Type == Nodes.SpreadElement;
  31. }
  32. bool cacheable = true;
  33. for (var i = 0; i < expression.Arguments.Count; i++)
  34. {
  35. var expressionArgument = expression.Arguments[i];
  36. cachedArgumentsHolder.JintArguments[i] = Build(_engine, expressionArgument);
  37. cacheable &= expressionArgument.Type == Nodes.Literal;
  38. _hasSpreads |= CanSpread(expressionArgument);
  39. if (expressionArgument is ArrayExpression ae)
  40. {
  41. for (var elementIndex = 0; elementIndex < ae.Elements.Count; elementIndex++)
  42. {
  43. _hasSpreads |= CanSpread(ae.Elements[elementIndex]);
  44. }
  45. }
  46. }
  47. if (cacheable)
  48. {
  49. _cached = true;
  50. var arguments = System.Array.Empty<JsValue>();
  51. if (cachedArgumentsHolder.JintArguments.Length > 0)
  52. {
  53. arguments = new JsValue[cachedArgumentsHolder.JintArguments.Length];
  54. BuildArguments(cachedArgumentsHolder.JintArguments, arguments);
  55. }
  56. cachedArgumentsHolder.CachedArguments = arguments;
  57. }
  58. _cachedArguments = cachedArgumentsHolder;
  59. }
  60. protected override object EvaluateInternal()
  61. {
  62. return _calleeExpression is JintSuperExpression
  63. ? SuperCall()
  64. : Call();
  65. }
  66. private object SuperCall()
  67. {
  68. var thisEnvironment = (FunctionEnvironmentRecord) _engine.ExecutionContext.GetThisEnvironment();
  69. var newTarget = _engine.GetNewTarget(thisEnvironment);
  70. var func = GetSuperConstructor(thisEnvironment);
  71. if (!func.IsConstructor)
  72. {
  73. ExceptionHelper.ThrowTypeError(_engine, "Not a constructor");
  74. }
  75. var argList = ArgumentListEvaluation();
  76. var result = ((IConstructor) func).Construct(argList, newTarget);
  77. var thisER = (FunctionEnvironmentRecord) _engine.ExecutionContext.GetThisEnvironment();
  78. return thisER.BindThisValue(result);
  79. }
  80. /// <summary>
  81. /// https://tc39.es/ecma262/#sec-getsuperconstructor
  82. /// </summary>
  83. private ObjectInstance GetSuperConstructor(FunctionEnvironmentRecord thisEnvironment)
  84. {
  85. var envRec = thisEnvironment;
  86. var activeFunction = envRec._functionObject;
  87. var superConstructor = activeFunction.GetPrototypeOf();
  88. return superConstructor;
  89. }
  90. private object Call()
  91. {
  92. var callee = _calleeExpression.Evaluate();
  93. var expression = (CallExpression) _expression;
  94. // todo: implement as in http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.4
  95. var arguments = ArgumentListEvaluation();
  96. var func = _engine.GetValue(callee, false);
  97. var r = callee as Reference;
  98. if (func._type == InternalTypes.Undefined)
  99. {
  100. ExceptionHelper.ThrowTypeError(_engine, r == null ? "" : $"Object has no method '{r.GetReferencedName()}'");
  101. }
  102. if (!func.IsObject())
  103. {
  104. if (!_engine._referenceResolver.TryGetCallable(_engine, callee, out func))
  105. {
  106. ExceptionHelper.ThrowTypeError(_engine,
  107. r == null ? "" : $"Property '{r.GetReferencedName()}' of object is not a function");
  108. }
  109. }
  110. if (!(func is ICallable callable))
  111. {
  112. var message = $"{r?.GetReferencedName() ?? ""} is not a function";
  113. return ExceptionHelper.ThrowTypeError<object>(_engine, message);
  114. }
  115. var thisObject = Undefined.Instance;
  116. if (r != null)
  117. {
  118. var baseValue = r.GetBase();
  119. if ((baseValue._type & InternalTypes.ObjectEnvironmentRecord) == 0)
  120. {
  121. thisObject = r.GetThisValue();
  122. }
  123. else
  124. {
  125. var env = (EnvironmentRecord) baseValue;
  126. thisObject = env.ImplicitThisValue();
  127. }
  128. // is it a direct call to eval ? http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.1.1
  129. if (r.GetReferencedName() == CommonProperties.Eval && callable is EvalFunctionInstance instance)
  130. {
  131. var value = instance.PerformEval(arguments, true);
  132. _engine._referencePool.Return(r);
  133. return value;
  134. }
  135. }
  136. var result = _engine.Call(callable, thisObject, arguments, _calleeExpression);
  137. if (!_cached && arguments.Length > 0)
  138. {
  139. _engine._jsValueArrayPool.ReturnArray(arguments);
  140. }
  141. _engine._referencePool.Return(r);
  142. return result;
  143. }
  144. private JsValue[] ArgumentListEvaluation()
  145. {
  146. var cachedArguments = _cachedArguments;
  147. var arguments = System.Array.Empty<JsValue>();
  148. if (_cached)
  149. {
  150. arguments = cachedArguments.CachedArguments;
  151. }
  152. else
  153. {
  154. if (cachedArguments.JintArguments.Length > 0)
  155. {
  156. if (_hasSpreads)
  157. {
  158. arguments = BuildArgumentsWithSpreads(cachedArguments.JintArguments);
  159. }
  160. else
  161. {
  162. arguments = _engine._jsValueArrayPool.RentArray(cachedArguments.JintArguments.Length);
  163. BuildArguments(cachedArguments.JintArguments, arguments);
  164. }
  165. }
  166. }
  167. return arguments;
  168. }
  169. private class CachedArgumentsHolder
  170. {
  171. internal JintExpression[] JintArguments;
  172. internal JsValue[] CachedArguments;
  173. }
  174. }
  175. }