StatementInterpreter.cs 20 KB

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