JintCallExpression.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Realm, "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 static ObjectInstance GetSuperConstructor(FunctionEnvironmentRecord thisEnvironment)
  84. {
  85. var envRec = thisEnvironment;
  86. var activeFunction = envRec._functionObject;
  87. var superConstructor = activeFunction.GetPrototypeOf();
  88. return superConstructor;
  89. }
  90. /// <summary>
  91. /// https://tc39.es/ecma262/#sec-function-calls
  92. /// </summary>
  93. private object Call()
  94. {
  95. var reference = _calleeExpression.Evaluate();
  96. if (ReferenceEquals(reference, Undefined.Instance))
  97. {
  98. return Undefined.Instance;
  99. }
  100. var func = _engine.GetValue(reference, false);
  101. if (reference is Reference referenceRecord
  102. && !referenceRecord.IsPropertyReference()
  103. && referenceRecord.GetReferencedName() == CommonProperties.Eval
  104. && ReferenceEquals(func, _engine.Realm.Intrinsics.Eval))
  105. {
  106. var argList = ArgumentListEvaluation();
  107. if (argList.Length == 0)
  108. {
  109. return Undefined.Instance;
  110. }
  111. var evalFunctionInstance = (EvalFunctionInstance) func;
  112. var evalArg = argList[0];
  113. var strictCaller = StrictModeScope.IsStrictModeCode;
  114. var evalRealm = evalFunctionInstance._realm;
  115. var direct = !((CallExpression) _expression).Optional;
  116. var value = evalFunctionInstance.PerformEval(evalArg, evalRealm, strictCaller, direct);
  117. _engine._referencePool.Return(referenceRecord);
  118. return value;
  119. }
  120. var thisCall = (CallExpression) _expression;
  121. var tailCall = IsInTailPosition(thisCall);
  122. return EvaluateCall(func, reference, thisCall.Arguments, tailCall);
  123. }
  124. /// <summary>
  125. /// https://tc39.es/ecma262/#sec-evaluatecall
  126. /// </summary>
  127. private object EvaluateCall(JsValue func, object reference, in NodeList<Expression> arguments, bool tailPosition)
  128. {
  129. JsValue thisValue;
  130. var referenceRecord = reference as Reference;
  131. if (referenceRecord is not null)
  132. {
  133. if (referenceRecord.IsPropertyReference())
  134. {
  135. thisValue = referenceRecord.GetThisValue();
  136. }
  137. else
  138. {
  139. var baseValue = referenceRecord.GetBase();
  140. // deviation from the spec to support null-propagation helper
  141. if (baseValue.IsNullOrUndefined()
  142. && _engine._referenceResolver.TryUnresolvableReference(_engine, referenceRecord, out var value))
  143. {
  144. return value;
  145. }
  146. var refEnv = (EnvironmentRecord) baseValue;
  147. thisValue = refEnv.WithBaseObject();
  148. }
  149. }
  150. else
  151. {
  152. thisValue = Undefined.Instance;
  153. }
  154. var argList = ArgumentListEvaluation();
  155. if (!func.IsObject() && !_engine._referenceResolver.TryGetCallable(_engine, reference, out func))
  156. {
  157. var message = referenceRecord == null
  158. ? reference + " is not a function"
  159. : $"Property '{referenceRecord.GetReferencedName()}' of object is not a function";
  160. ExceptionHelper.ThrowTypeError(_engine.Realm, message);
  161. }
  162. var callable = func as ICallable;
  163. if (callable is null)
  164. {
  165. var message = $"{referenceRecord?.GetReferencedName() ?? reference} is not a function";
  166. ExceptionHelper.ThrowTypeError(_engine.Realm, message);
  167. }
  168. if (tailPosition)
  169. {
  170. // TODO tail call
  171. // PrepareForTailCall();
  172. }
  173. var result = _engine.Call(callable, thisValue, argList, _calleeExpression);
  174. if (!_cached && argList.Length > 0)
  175. {
  176. _engine._jsValueArrayPool.ReturnArray(argList);
  177. }
  178. _engine._referencePool.Return(referenceRecord);
  179. return result;
  180. }
  181. /// <summary>
  182. /// https://tc39.es/ecma262/#sec-isintailposition
  183. /// </summary>
  184. private static bool IsInTailPosition(CallExpression call)
  185. {
  186. // TODO tail calls
  187. return false;
  188. }
  189. private JsValue[] ArgumentListEvaluation()
  190. {
  191. var cachedArguments = _cachedArguments;
  192. var arguments = System.Array.Empty<JsValue>();
  193. if (_cached)
  194. {
  195. arguments = cachedArguments.CachedArguments;
  196. }
  197. else
  198. {
  199. if (cachedArguments.JintArguments.Length > 0)
  200. {
  201. if (_hasSpreads)
  202. {
  203. arguments = BuildArgumentsWithSpreads(cachedArguments.JintArguments);
  204. }
  205. else
  206. {
  207. arguments = _engine._jsValueArrayPool.RentArray(cachedArguments.JintArguments.Length);
  208. BuildArguments(cachedArguments.JintArguments, arguments);
  209. }
  210. }
  211. }
  212. return arguments;
  213. }
  214. private class CachedArgumentsHolder
  215. {
  216. internal JintExpression[] JintArguments;
  217. internal JsValue[] CachedArguments;
  218. }
  219. }
  220. }