StatementInterpreter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Esprima.Ast;
  5. using Jint.Native;
  6. using Jint.Runtime.Descriptors;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.References;
  9. namespace Jint.Runtime
  10. {
  11. public class StatementInterpreter
  12. {
  13. private readonly Engine _engine;
  14. public StatementInterpreter(Engine engine)
  15. {
  16. _engine = engine;
  17. }
  18. private Completion ExecuteStatement(Statement statement)
  19. {
  20. return _engine.ExecuteStatement(statement);
  21. }
  22. public Completion ExecuteEmptyStatement(EmptyStatement emptyStatement)
  23. {
  24. return Completion.Empty;
  25. }
  26. public Completion ExecuteExpressionStatement(ExpressionStatement expressionStatement)
  27. {
  28. var exprRef = _engine.EvaluateExpression(expressionStatement.Expression);
  29. return _engine.CompletionPool.Rent(Completion.Normal, _engine.GetValue(exprRef, true), null);
  30. }
  31. public Completion ExecuteIfStatement(IfStatement ifStatement)
  32. {
  33. Completion result;
  34. if (TypeConverter.ToBoolean(_engine.GetValue(_engine.EvaluateExpression(ifStatement.Test), true)))
  35. {
  36. result = ExecuteStatement(ifStatement.Consequent);
  37. }
  38. else if (ifStatement.Alternate != null)
  39. {
  40. result = ExecuteStatement(ifStatement.Alternate);
  41. }
  42. else
  43. {
  44. return Completion.Empty;
  45. }
  46. return result;
  47. }
  48. public Completion ExecuteLabeledStatement(LabeledStatement labeledStatement)
  49. {
  50. // TODO: Esprima added Statement.Label, maybe not necessary as this line is finding the
  51. // containing label and could keep a table per program with all the labels
  52. // labeledStatement.Body.LabelSet = labeledStatement.Label;
  53. var result = ExecuteStatement(labeledStatement.Body);
  54. if (result.Type == Completion.Break && result.Identifier == labeledStatement.Label.Name)
  55. {
  56. var value = result.Value;
  57. _engine.CompletionPool.Return(result);
  58. return _engine.CompletionPool.Rent(Completion.Normal, value, null);
  59. }
  60. return result;
  61. }
  62. /// <summary>
  63. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.1
  64. /// </summary>
  65. /// <param name="doWhileStatement"></param>
  66. /// <returns></returns>
  67. public Completion ExecuteDoWhileStatement(DoWhileStatement doWhileStatement)
  68. {
  69. JsValue v = Undefined.Instance;
  70. bool iterating;
  71. do
  72. {
  73. var stmt = ExecuteStatement(doWhileStatement.Body);
  74. if (stmt.Value != null)
  75. {
  76. v = stmt.Value;
  77. }
  78. if (stmt.Type != Completion.Continue || stmt.Identifier != doWhileStatement?.LabelSet?.Name)
  79. {
  80. if (stmt.Type == Completion.Break && (stmt.Identifier == null || stmt.Identifier == doWhileStatement?.LabelSet?.Name))
  81. {
  82. _engine.CompletionPool.Return(stmt);
  83. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  84. }
  85. if (stmt.Type != Completion.Normal)
  86. {
  87. return stmt;
  88. }
  89. }
  90. _engine.CompletionPool.Return(stmt);
  91. var exprRef = _engine.EvaluateExpression(doWhileStatement.Test);
  92. iterating = TypeConverter.ToBoolean(_engine.GetValue(exprRef, true));
  93. } while (iterating);
  94. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  95. }
  96. /// <summary>
  97. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.2
  98. /// </summary>
  99. /// <param name="whileStatement"></param>
  100. /// <returns></returns>
  101. public Completion ExecuteWhileStatement(WhileStatement whileStatement)
  102. {
  103. JsValue v = Undefined.Instance;
  104. while (true)
  105. {
  106. var jsValue = _engine.GetValue(_engine.EvaluateExpression(whileStatement.Test), true);
  107. if (!TypeConverter.ToBoolean(jsValue))
  108. {
  109. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  110. }
  111. var stmt = ExecuteStatement(whileStatement.Body);
  112. if (stmt.Value != null)
  113. {
  114. v = stmt.Value;
  115. }
  116. if (stmt.Type != Completion.Continue || stmt.Identifier != whileStatement?.LabelSet?.Name)
  117. {
  118. if (stmt.Type == Completion.Break && (stmt.Identifier == null || stmt.Identifier == whileStatement?.LabelSet?.Name))
  119. {
  120. _engine.CompletionPool.Return(stmt);
  121. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  122. }
  123. if (stmt.Type != Completion.Normal)
  124. {
  125. return stmt;
  126. }
  127. }
  128. _engine.CompletionPool.Return(stmt);
  129. }
  130. }
  131. /// <summary>
  132. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.3
  133. /// </summary>
  134. /// <param name="forStatement"></param>
  135. /// <returns></returns>
  136. public Completion ExecuteForStatement(ForStatement forStatement)
  137. {
  138. if (forStatement.Init != null)
  139. {
  140. if (forStatement.Init.Type == Nodes.VariableDeclaration)
  141. {
  142. var c = ExecuteStatement(forStatement.Init.As<Statement>());
  143. _engine.CompletionPool.Return(c);
  144. }
  145. else
  146. {
  147. _engine.GetValue(_engine.EvaluateExpression(forStatement.Init.As<Expression>()), true);
  148. }
  149. }
  150. JsValue v = Undefined.Instance;
  151. while (true)
  152. {
  153. if (forStatement.Test != null)
  154. {
  155. var testExprRef = _engine.EvaluateExpression(forStatement.Test);
  156. if (!TypeConverter.ToBoolean(_engine.GetValue(testExprRef, true)))
  157. {
  158. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  159. }
  160. }
  161. var stmt = ExecuteStatement(forStatement.Body);
  162. if (stmt.Value != null)
  163. {
  164. v = stmt.Value;
  165. }
  166. if (stmt.Type == Completion.Break && (stmt.Identifier == null || stmt.Identifier == forStatement?.LabelSet?.Name))
  167. {
  168. _engine.CompletionPool.Return(stmt);
  169. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  170. }
  171. if (stmt.Type != Completion.Continue || ((stmt.Identifier != null) && stmt.Identifier != forStatement?.LabelSet?.Name))
  172. {
  173. if (stmt.Type != Completion.Normal)
  174. {
  175. return stmt;
  176. }
  177. }
  178. if (forStatement.Update != null)
  179. {
  180. _engine.GetValue(_engine.EvaluateExpression(forStatement.Update), true);
  181. }
  182. _engine.CompletionPool.Return(stmt);
  183. }
  184. }
  185. /// <summary>
  186. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
  187. /// </summary>
  188. /// <param name="forInStatement"></param>
  189. /// <returns></returns>
  190. public Completion ExecuteForInStatement(ForInStatement forInStatement)
  191. {
  192. Identifier identifier = forInStatement.Left.Type == Nodes.VariableDeclaration
  193. ? forInStatement.Left.As<VariableDeclaration>().Declarations.First().Id.As<Identifier>()
  194. : forInStatement.Left.As<Identifier>();
  195. var varRef = _engine.EvaluateExpression(identifier) as Reference;
  196. var experValue = _engine.GetValue(_engine.EvaluateExpression(forInStatement.Right), true);
  197. if (ReferenceEquals(experValue, Undefined.Instance) || ReferenceEquals(experValue, Null.Instance))
  198. {
  199. return Completion.Empty;
  200. }
  201. var obj = TypeConverter.ToObject(_engine, experValue);
  202. JsValue v = Null.Instance;
  203. // keys are constructed using the prototype chain
  204. var cursor = obj;
  205. var processedKeys = new HashSet<string>();
  206. while (cursor != null)
  207. {
  208. var keys = _engine.Object.GetOwnPropertyNames(Undefined.Instance, Arguments.From(cursor)).AsArray();
  209. for (var i = 0; i < keys.GetLength(); i++)
  210. {
  211. var p = keys.GetOwnProperty(TypeConverter.ToString(i)).Value.AsString();
  212. if (processedKeys.Contains(p))
  213. {
  214. continue;
  215. }
  216. processedKeys.Add(p);
  217. // collection might be modified by inner statement
  218. if (cursor.GetOwnProperty(p) == PropertyDescriptor.Undefined)
  219. {
  220. continue;
  221. }
  222. var value = cursor.GetOwnProperty(p);
  223. if (!value.Enumerable.HasValue || !value.Enumerable.Value)
  224. {
  225. continue;
  226. }
  227. _engine.PutValue(varRef, p);
  228. var stmt = ExecuteStatement(forInStatement.Body);
  229. if (stmt.Value != null)
  230. {
  231. v = stmt.Value;
  232. }
  233. if (stmt.Type == Completion.Break)
  234. {
  235. _engine.CompletionPool.Return(stmt);
  236. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  237. }
  238. if (stmt.Type != Completion.Continue)
  239. {
  240. if (stmt.Type != Completion.Normal)
  241. {
  242. return stmt;
  243. }
  244. }
  245. _engine.CompletionPool.Return(stmt);
  246. }
  247. cursor = cursor.Prototype;
  248. }
  249. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  250. }
  251. /// <summary>
  252. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.7
  253. /// </summary>
  254. /// <param name="continueStatement"></param>
  255. /// <returns></returns>
  256. public Completion ExecuteContinueStatement(ContinueStatement continueStatement)
  257. {
  258. return _engine.CompletionPool.Rent(
  259. Completion.Continue,
  260. null,
  261. continueStatement.Label != null ? continueStatement.Label.Name : null);
  262. }
  263. /// <summary>
  264. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.8
  265. /// </summary>
  266. /// <param name="breakStatement"></param>
  267. /// <returns></returns>
  268. public Completion ExecuteBreakStatement(BreakStatement breakStatement)
  269. {
  270. return _engine.CompletionPool.Rent(
  271. Completion.Break,
  272. null,
  273. breakStatement.Label != null ? breakStatement.Label.Name : null);
  274. }
  275. /// <summary>
  276. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.9
  277. /// </summary>
  278. /// <param name="statement"></param>
  279. /// <returns></returns>
  280. public Completion ExecuteReturnStatement(ReturnStatement statement)
  281. {
  282. if (statement.Argument == null)
  283. {
  284. return _engine.CompletionPool.Rent(Completion.Return, Undefined.Instance, null);
  285. }
  286. var jsValue = _engine.GetValue(_engine.EvaluateExpression(statement.Argument), true);
  287. return _engine.CompletionPool.Rent(Completion.Return, jsValue, null);
  288. }
  289. /// <summary>
  290. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
  291. /// </summary>
  292. /// <param name="withStatement"></param>
  293. /// <returns></returns>
  294. public Completion ExecuteWithStatement(WithStatement withStatement)
  295. {
  296. var jsValue = _engine.GetValue(_engine.EvaluateExpression(withStatement.Object), true);
  297. var obj = TypeConverter.ToObject(_engine, jsValue);
  298. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  299. var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, true);
  300. _engine.ExecutionContext.LexicalEnvironment = newEnv;
  301. Completion c;
  302. try
  303. {
  304. c = ExecuteStatement(withStatement.Body);
  305. }
  306. catch (JavaScriptException e)
  307. {
  308. c = _engine.CompletionPool.Rent(Completion.Throw, e.Error, null, withStatement.Location);
  309. }
  310. finally
  311. {
  312. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  313. }
  314. return c;
  315. }
  316. /// <summary>
  317. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.11
  318. /// </summary>
  319. /// <param name="switchStatement"></param>
  320. /// <returns></returns>
  321. public Completion ExecuteSwitchStatement(SwitchStatement switchStatement)
  322. {
  323. var jsValue = _engine.GetValue(_engine.EvaluateExpression(switchStatement.Discriminant), true);
  324. var r = ExecuteSwitchBlock(switchStatement.Cases, jsValue);
  325. if (r.Type == Completion.Break && r.Identifier == switchStatement.LabelSet?.Name)
  326. {
  327. return _engine.CompletionPool.Rent(Completion.Normal, r.Value, null);
  328. }
  329. return r;
  330. }
  331. public Completion ExecuteSwitchBlock(List<SwitchCase> switchBlock, JsValue input)
  332. {
  333. JsValue v = Undefined.Instance;
  334. SwitchCase defaultCase = null;
  335. bool hit = false;
  336. var switchBlockCount = switchBlock.Count;
  337. for (var i = 0; i < switchBlockCount; i++)
  338. {
  339. var clause = switchBlock[i];
  340. if (clause.Test == null)
  341. {
  342. defaultCase = clause;
  343. }
  344. else
  345. {
  346. var clauseSelector = _engine.GetValue(_engine.EvaluateExpression(clause.Test), true);
  347. if (ExpressionInterpreter.StrictlyEqual(clauseSelector, input))
  348. {
  349. hit = true;
  350. }
  351. }
  352. if (hit && clause.Consequent != null)
  353. {
  354. var r = ExecuteStatementList(clause.Consequent);
  355. if (r.Type != Completion.Normal)
  356. {
  357. return r;
  358. }
  359. v = r.Value != null ? r.Value : Undefined.Instance;
  360. }
  361. }
  362. // do we need to execute the default case ?
  363. if (hit == false && defaultCase != null)
  364. {
  365. var r = ExecuteStatementList(defaultCase.Consequent);
  366. if (r.Type != Completion.Normal)
  367. {
  368. return r;
  369. }
  370. v = r.Value != null ? r.Value : Undefined.Instance;
  371. }
  372. return _engine.CompletionPool.Rent(Completion.Normal, v, null);
  373. }
  374. public Completion ExecuteStatementList(List<StatementListItem> statementList)
  375. {
  376. var c = Completion.Empty;
  377. Completion sl = c;
  378. Statement s = null;
  379. try
  380. {
  381. var statementListCount = statementList.Count;
  382. for (var i = 0; i < statementListCount; i++)
  383. {
  384. var statement = statementList[i];
  385. s = statement.As<Statement>();
  386. c = ExecuteStatement(s);
  387. if (c.Type != Completion.Normal)
  388. {
  389. var executeStatementList = _engine.CompletionPool.Rent(
  390. c.Type,
  391. c.Value != null ? c.Value : sl.Value,
  392. c.Identifier,
  393. c.Location);
  394. _engine.CompletionPool.Return(sl);
  395. _engine.CompletionPool.Return(c);
  396. return executeStatementList;
  397. }
  398. if (sl != c)
  399. {
  400. _engine.CompletionPool.Return(sl);
  401. }
  402. sl = c;
  403. }
  404. }
  405. catch (JavaScriptException v)
  406. {
  407. var completion = _engine.CompletionPool.Rent(Completion.Throw, v.Error, null, v.Location ?? s.Location);
  408. return completion;
  409. }
  410. var rent = _engine.CompletionPool.Rent(c.Type, c.GetValueOrDefault(), c.Identifier);
  411. _engine.CompletionPool.Return(c);
  412. return rent;
  413. }
  414. /// <summary>
  415. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  416. /// </summary>
  417. /// <param name="throwStatement"></param>
  418. /// <returns></returns>
  419. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  420. {
  421. var jsValue = _engine.GetValue(_engine.EvaluateExpression(throwStatement.Argument), true);
  422. return _engine.CompletionPool.Rent(Completion.Throw, jsValue, null, throwStatement.Location);
  423. }
  424. /// <summary>
  425. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  426. /// </summary>
  427. /// <param name="tryStatement"></param>
  428. /// <returns></returns>
  429. public Completion ExecuteTryStatement(TryStatement tryStatement)
  430. {
  431. var b = ExecuteStatement(tryStatement.Block);
  432. if (b.Type == Completion.Throw)
  433. {
  434. // execute catch
  435. var catchClause = tryStatement.Handler;
  436. if (catchClause != null)
  437. {
  438. var c = _engine.GetValue(b);
  439. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  440. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  441. catchEnv.Record.CreateMutableBinding(catchClause.Param.As<Identifier>().Name, c);
  442. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  443. b = ExecuteStatement(catchClause.Body);
  444. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  445. }
  446. }
  447. if (tryStatement.Finalizer != null)
  448. {
  449. var f = ExecuteStatement(tryStatement.Finalizer);
  450. if (f.Type == Completion.Normal)
  451. {
  452. return b;
  453. }
  454. return f;
  455. }
  456. return b;
  457. }
  458. public Completion ExecuteProgram(Program program)
  459. {
  460. return ExecuteStatementList(program.Body);
  461. }
  462. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  463. {
  464. var declarationsCount = statement.Declarations.Count;
  465. for (var i = 0; i < declarationsCount; i++)
  466. {
  467. var declaration = statement.Declarations[i];
  468. if (declaration.Init != null)
  469. {
  470. if (!(_engine.EvaluateExpression(declaration.Id.As<Identifier>()) is Reference lhs))
  471. {
  472. throw new ArgumentException();
  473. }
  474. if (lhs.IsStrict() && lhs.GetBase().TryCast<EnvironmentRecord>() != null &&
  475. (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
  476. {
  477. throw new JavaScriptException(_engine.SyntaxError);
  478. }
  479. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init), true);
  480. _engine.PutValue(lhs, value);
  481. _engine.ReferencePool.Return(lhs);
  482. }
  483. }
  484. return Completion.EmptyUndefined;
  485. }
  486. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  487. {
  488. return ExecuteStatementList(blockStatement.Body);
  489. }
  490. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  491. {
  492. if (_engine.Options._IsDebuggerStatementAllowed)
  493. {
  494. if (!System.Diagnostics.Debugger.IsAttached)
  495. {
  496. System.Diagnostics.Debugger.Launch();
  497. }
  498. System.Diagnostics.Debugger.Break();
  499. }
  500. return Completion.Empty;
  501. }
  502. }
  503. }