StatementInterpreter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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.HasValue)
  71. {
  72. v = stmt.Value.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.HasValue)
  107. {
  108. v = stmt.Value.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.HasValue)
  154. {
  155. v = stmt.Value.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.Properties.Keys.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.Properties.ContainsKey(p))
  209. {
  210. continue;
  211. }
  212. var value = cursor.Properties[p];
  213. if (!value.Enumerable.HasValue || !value.Enumerable.Value.AsBoolean())
  214. {
  215. continue;
  216. }
  217. _engine.PutValue(varRef, p);
  218. var stmt = ExecuteStatement(forInStatement.Body);
  219. if (stmt.Value.HasValue)
  220. {
  221. v = stmt.Value.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. }
  292. finally
  293. {
  294. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  295. }
  296. return c;
  297. }
  298. /// <summary>
  299. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.11
  300. /// </summary>
  301. /// <param name="switchStatement"></param>
  302. /// <returns></returns>
  303. public Completion ExecuteSwitchStatement(SwitchStatement switchStatement)
  304. {
  305. var exprRef = _engine.EvaluateExpression(switchStatement.Discriminant);
  306. var r = ExecuteSwitchBlock(switchStatement.Cases, _engine.GetValue(exprRef));
  307. if (r.Type == Completion.Break && r.Identifier == switchStatement.LabelSet)
  308. {
  309. return new Completion(Completion.Normal, r.Value, null);
  310. }
  311. return r;
  312. }
  313. public Completion ExecuteSwitchBlock(IEnumerable<SwitchCase> switchBlock, JsValue input)
  314. {
  315. JsValue v = Undefined.Instance;
  316. SwitchCase defaultCase = null;
  317. bool hit = false;
  318. foreach (var clause in switchBlock)
  319. {
  320. if (clause.Test == null)
  321. {
  322. defaultCase = clause;
  323. }
  324. else
  325. {
  326. var clauseSelector = _engine.GetValue(_engine.EvaluateExpression(clause.Test));
  327. if (ExpressionInterpreter.StriclyEqual(clauseSelector, input))
  328. {
  329. hit = true;
  330. }
  331. }
  332. if (hit && clause.Consequent != null)
  333. {
  334. var r = ExecuteStatementList(clause.Consequent);
  335. if (r.Type != Completion.Normal)
  336. {
  337. return r;
  338. }
  339. v = r.Value.HasValue ? r.Value.Value : Undefined.Instance;
  340. }
  341. }
  342. // do we need to execute the default case ?
  343. if (hit == false && defaultCase != null)
  344. {
  345. var r = ExecuteStatementList(defaultCase.Consequent);
  346. if (r.Type != Completion.Normal)
  347. {
  348. return r;
  349. }
  350. v = r.Value.HasValue ? r.Value.Value : Undefined.Instance;
  351. }
  352. return new Completion(Completion.Normal, v, null);
  353. }
  354. public Completion ExecuteStatementList(IEnumerable<Statement> statementList)
  355. {
  356. var c = new Completion(Completion.Normal, null, null);
  357. Completion sl = c;
  358. try
  359. {
  360. foreach (var statement in statementList)
  361. {
  362. c = ExecuteStatement(statement);
  363. if (c.Type != Completion.Normal)
  364. {
  365. return new Completion(c.Type, c.Value.HasValue ? c.Value : sl.Value, c.Identifier);
  366. }
  367. sl = c;
  368. }
  369. }
  370. catch(JavaScriptException v)
  371. {
  372. return new Completion(Completion.Throw, v.Error, null);
  373. }
  374. return new Completion(c.Type, c.GetValueOrDefault(), c.Identifier);
  375. }
  376. /// <summary>
  377. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  378. /// </summary>
  379. /// <param name="throwStatement"></param>
  380. /// <returns></returns>
  381. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  382. {
  383. var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
  384. return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
  385. }
  386. /// <summary>
  387. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  388. /// </summary>
  389. /// <param name="tryStatement"></param>
  390. /// <returns></returns>
  391. public Completion ExecuteTryStatement(TryStatement tryStatement)
  392. {
  393. var b = ExecuteStatement(tryStatement.Block);
  394. if (b.Type == Completion.Throw)
  395. {
  396. // execute catch
  397. if (tryStatement.Handlers.Any())
  398. {
  399. foreach (var catchClause in tryStatement.Handlers)
  400. {
  401. var c = _engine.GetValue(b);
  402. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  403. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  404. catchEnv.Record.CreateMutableBinding(catchClause.Param.Name);
  405. catchEnv.Record.SetMutableBinding(catchClause.Param.Name, c, false);
  406. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  407. b = ExecuteStatement(catchClause.Body);
  408. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  409. }
  410. }
  411. }
  412. if (tryStatement.Finalizer != null)
  413. {
  414. var f = ExecuteStatement(tryStatement.Finalizer);
  415. if (f.Type == Completion.Normal)
  416. {
  417. return b;
  418. }
  419. return f;
  420. }
  421. return b;
  422. }
  423. public Completion ExecuteProgram(Program program)
  424. {
  425. return ExecuteStatementList(program.Body);
  426. }
  427. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  428. {
  429. foreach (var declaration in statement.Declarations)
  430. {
  431. if (declaration.Init != null)
  432. {
  433. var lhs = _engine.EvaluateExpression(declaration.Id) as Reference;
  434. if (lhs == null)
  435. {
  436. throw new ArgumentException();
  437. }
  438. if (lhs.IsStrict() && lhs.GetBase().TryCast<EnvironmentRecord>() != null &&
  439. (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
  440. {
  441. throw new JavaScriptException(_engine.SyntaxError);
  442. }
  443. lhs.GetReferencedName();
  444. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
  445. _engine.PutValue(lhs, value);
  446. }
  447. }
  448. return new Completion(Completion.Normal, Undefined.Instance, null);
  449. }
  450. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  451. {
  452. return ExecuteStatementList(blockStatement.Body);
  453. }
  454. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  455. {
  456. if (_engine.Options.IsDebuggerStatementAllowed())
  457. {
  458. if (!System.Diagnostics.Debugger.IsAttached)
  459. {
  460. System.Diagnostics.Debugger.Launch();
  461. }
  462. System.Diagnostics.Debugger.Break();
  463. }
  464. return new Completion(Completion.Normal, null, null);
  465. }
  466. }
  467. }