StatementInterpreter.cs 19 KB

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