JintFunctionDefinition.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections.Generic;
  3. using Esprima.Ast;
  4. using Jint.Native;
  5. using Jint.Native.Function;
  6. using Jint.Runtime.Interpreter.Expressions;
  7. namespace Jint.Runtime.Interpreter
  8. {
  9. /// <summary>
  10. /// Works as memento for function execution. Optimization to cache things that don't change.
  11. /// </summary>
  12. internal sealed class JintFunctionDefinition
  13. {
  14. private readonly Engine _engine;
  15. private JintExpression _bodyExpression;
  16. private JintStatementList _bodyStatementList;
  17. public readonly string Name;
  18. public readonly bool Strict;
  19. public readonly IFunction Function;
  20. private State _state;
  21. public JintFunctionDefinition(
  22. Engine engine,
  23. IFunction function)
  24. {
  25. _engine = engine;
  26. Function = function;
  27. Name = !string.IsNullOrEmpty(function.Id?.Name) ? function.Id.Name : null;
  28. Strict = function.Strict;
  29. }
  30. public FunctionThisMode ThisMode => Strict || _engine._isStrict ? FunctionThisMode.Strict : FunctionThisMode.Global;
  31. internal Completion Execute(EvaluationContext context)
  32. {
  33. if (Function.Expression)
  34. {
  35. _bodyExpression ??= JintExpression.Build(_engine, (Expression) Function.Body);
  36. var jsValue = _bodyExpression?.GetValue(context).Value ?? Undefined.Instance;
  37. return new Completion(CompletionType.Return, jsValue, Function.Body.Location);
  38. }
  39. var blockStatement = (BlockStatement) Function.Body;
  40. _bodyStatementList ??= new JintStatementList(blockStatement, blockStatement.Body);
  41. return _bodyStatementList.Execute(context);
  42. }
  43. internal State Initialize(FunctionInstance functionInstance)
  44. {
  45. return _state ??= DoInitialize(functionInstance);
  46. }
  47. internal sealed class State
  48. {
  49. public bool HasRestParameter;
  50. public int Length;
  51. public Key[] ParameterNames;
  52. public bool HasDuplicates;
  53. public bool IsSimpleParameterList;
  54. public bool HasParameterExpressions;
  55. public bool ArgumentsObjectNeeded;
  56. public List<Key> VarNames;
  57. public LinkedList<JintFunctionDefinition> FunctionsToInitialize;
  58. public readonly HashSet<Key> FunctionNames = new HashSet<Key>();
  59. public LexicalVariableDeclaration[] LexicalDeclarations = Array.Empty<LexicalVariableDeclaration>();
  60. public HashSet<Key> ParameterBindings;
  61. public List<VariableValuePair> VarsToInitialize;
  62. internal struct VariableValuePair
  63. {
  64. public Key Name;
  65. public JsValue InitialValue;
  66. }
  67. internal struct LexicalVariableDeclaration
  68. {
  69. public VariableDeclarationKind Kind;
  70. public List<string> BoundNames;
  71. }
  72. }
  73. private State DoInitialize(FunctionInstance functionInstance)
  74. {
  75. var state = new State();
  76. ProcessParameters(Function, state, out var hasArguments);
  77. var hoistingScope = HoistingScope.GetFunctionLevelDeclarations(Function, collectVarNames: true, collectLexicalNames: true);
  78. var functionDeclarations = hoistingScope._functionDeclarations;
  79. var lexicalNames = hoistingScope._lexicalNames;
  80. state.VarNames = hoistingScope._varNames;
  81. LinkedList<JintFunctionDefinition> functionsToInitialize = null;
  82. if (functionDeclarations != null)
  83. {
  84. functionsToInitialize = new LinkedList<JintFunctionDefinition>();
  85. for (var i = functionDeclarations.Count - 1; i >= 0; i--)
  86. {
  87. var d = functionDeclarations[i];
  88. var fn = d.Id.Name;
  89. if (state.FunctionNames.Add(fn))
  90. {
  91. functionsToInitialize.AddFirst(new JintFunctionDefinition(_engine, d));
  92. }
  93. }
  94. }
  95. state.FunctionsToInitialize = functionsToInitialize;
  96. const string ParameterNameArguments = "arguments";
  97. state.ArgumentsObjectNeeded = true;
  98. if (functionInstance._thisMode == FunctionThisMode.Lexical)
  99. {
  100. state.ArgumentsObjectNeeded = false;
  101. }
  102. else if (hasArguments)
  103. {
  104. state.ArgumentsObjectNeeded = false;
  105. }
  106. else if (!state.HasParameterExpressions)
  107. {
  108. if (state.FunctionNames.Contains(ParameterNameArguments)
  109. || lexicalNames?.Contains(ParameterNameArguments) == true)
  110. {
  111. state.ArgumentsObjectNeeded = false;
  112. }
  113. }
  114. var parameterBindings = new HashSet<Key>(state.ParameterNames);
  115. if (state.ArgumentsObjectNeeded)
  116. {
  117. parameterBindings.Add(KnownKeys.Arguments);
  118. }
  119. state.ParameterBindings = parameterBindings;
  120. var varsToInitialize = new List<State.VariableValuePair>();
  121. if (!state.HasParameterExpressions)
  122. {
  123. var instantiatedVarNames = state.VarNames != null
  124. ? new HashSet<Key>(state.ParameterBindings)
  125. : new HashSet<Key>();
  126. for (var i = 0; i < state.VarNames?.Count; i++)
  127. {
  128. var n = state.VarNames[i];
  129. if (instantiatedVarNames.Add(n))
  130. {
  131. varsToInitialize.Add(new State.VariableValuePair
  132. {
  133. Name = n
  134. });
  135. }
  136. }
  137. }
  138. else
  139. {
  140. var instantiatedVarNames = state.VarNames != null
  141. ? new HashSet<Key>(state.ParameterBindings)
  142. : null;
  143. for (var i = 0; i < state.VarNames?.Count; i++)
  144. {
  145. var n = state.VarNames[i];
  146. if (instantiatedVarNames.Add(n))
  147. {
  148. JsValue initialValue = null;
  149. if (!state.ParameterBindings.Contains(n) || state.FunctionNames.Contains(n))
  150. {
  151. initialValue = JsValue.Undefined;
  152. }
  153. varsToInitialize.Add(new State.VariableValuePair
  154. {
  155. Name = n,
  156. InitialValue = initialValue
  157. });
  158. }
  159. }
  160. }
  161. state.VarsToInitialize = varsToInitialize;
  162. if (hoistingScope._lexicalDeclarations != null)
  163. {
  164. var _lexicalDeclarations = hoistingScope._lexicalDeclarations;
  165. var lexicalDeclarationsCount = _lexicalDeclarations.Count;
  166. var declarations = new State.LexicalVariableDeclaration[lexicalDeclarationsCount];
  167. for (var i = 0; i < lexicalDeclarationsCount; i++)
  168. {
  169. var d = _lexicalDeclarations[i];
  170. var boundNames = new List<string>();
  171. d.GetBoundNames(boundNames);
  172. declarations[i] = new State.LexicalVariableDeclaration
  173. {
  174. Kind = d.Kind,
  175. BoundNames = boundNames
  176. };
  177. }
  178. state.LexicalDeclarations = declarations;
  179. }
  180. return state;
  181. }
  182. private static void GetBoundNames(
  183. Expression parameter,
  184. List<Key> target,
  185. bool checkDuplicates,
  186. ref bool _hasRestParameter,
  187. ref bool _hasParameterExpressions,
  188. ref bool _hasDuplicates,
  189. ref bool hasArguments)
  190. {
  191. if (parameter is Identifier identifier)
  192. {
  193. _hasDuplicates |= checkDuplicates && target.Contains(identifier.Name);
  194. target.Add(identifier.Name);
  195. hasArguments |= identifier.Name == "arguments";
  196. return;
  197. }
  198. while (true)
  199. {
  200. if (parameter is RestElement restElement)
  201. {
  202. _hasRestParameter = true;
  203. _hasParameterExpressions = true;
  204. parameter = restElement.Argument;
  205. continue;
  206. }
  207. if (parameter is ArrayPattern arrayPattern)
  208. {
  209. _hasParameterExpressions = true;
  210. ref readonly var arrayPatternElements = ref arrayPattern.Elements;
  211. for (var i = 0; i < arrayPatternElements.Count; i++)
  212. {
  213. var expression = arrayPatternElements[i];
  214. GetBoundNames(
  215. expression,
  216. target,
  217. checkDuplicates,
  218. ref _hasRestParameter,
  219. ref _hasParameterExpressions,
  220. ref _hasDuplicates,
  221. ref hasArguments);
  222. }
  223. }
  224. else if (parameter is ObjectPattern objectPattern)
  225. {
  226. _hasParameterExpressions = true;
  227. ref readonly var objectPatternProperties = ref objectPattern.Properties;
  228. for (var i = 0; i < objectPatternProperties.Count; i++)
  229. {
  230. var property = objectPatternProperties[i];
  231. if (property is Property p)
  232. {
  233. GetBoundNames(
  234. p.Value,
  235. target,
  236. checkDuplicates,
  237. ref _hasRestParameter,
  238. ref _hasParameterExpressions,
  239. ref _hasDuplicates,
  240. ref hasArguments);
  241. }
  242. else
  243. {
  244. _hasRestParameter = true;
  245. _hasParameterExpressions = true;
  246. parameter = ((RestElement) property).Argument;
  247. continue;
  248. }
  249. }
  250. }
  251. else if (parameter is AssignmentPattern assignmentPattern)
  252. {
  253. _hasParameterExpressions = true;
  254. parameter = assignmentPattern.Left;
  255. continue;
  256. }
  257. break;
  258. }
  259. }
  260. private static void ProcessParameters(
  261. IFunction function,
  262. State state,
  263. out bool hasArguments)
  264. {
  265. hasArguments = false;
  266. state.IsSimpleParameterList = true;
  267. ref readonly var functionDeclarationParams = ref function.Params;
  268. var count = functionDeclarationParams.Count;
  269. var parameterNames = new List<Key>(count);
  270. for (var i = 0; i < count; i++)
  271. {
  272. var parameter = functionDeclarationParams[i];
  273. if (parameter is Identifier id)
  274. {
  275. state.HasDuplicates |= parameterNames.Contains(id.Name);
  276. hasArguments = id.Name == "arguments";
  277. parameterNames.Add(id.Name);
  278. if (state.IsSimpleParameterList)
  279. {
  280. state.Length++;
  281. }
  282. }
  283. else if (parameter.Type != Nodes.Literal)
  284. {
  285. state.IsSimpleParameterList = false;
  286. GetBoundNames(
  287. parameter,
  288. parameterNames,
  289. checkDuplicates: true,
  290. ref state.HasRestParameter,
  291. ref state.HasParameterExpressions,
  292. ref state.HasDuplicates,
  293. ref hasArguments);
  294. }
  295. }
  296. state.ParameterNames = parameterNames.ToArray();
  297. }
  298. }
  299. }