StatementInterpreter.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. object v = null;
  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 == 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. object v = null;
  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 == 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. object v = null;
  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 == forStatement.LabelSet)
  158. {
  159. return new Completion(Completion.Normal, v, null);
  160. }
  161. if (stmt.Type != Completion.Continue || 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. object v = null;
  194. var keys = obj.Properties.Keys.ToArray();
  195. foreach (var p in keys)
  196. {
  197. // collection might be modified by inner statement
  198. if (!obj.Properties.ContainsKey(p))
  199. {
  200. continue;
  201. }
  202. var value = obj.Properties[p];
  203. if (!value.EnumerableIsSet)
  204. {
  205. continue;
  206. }
  207. _engine.PutValue(varRef, p);
  208. var stmt = ExecuteStatement(forInStatement.Body);
  209. if (stmt.Value != null)
  210. {
  211. v = stmt.Value;
  212. }
  213. if (stmt.Type == Completion.Break /* todo: complete */)
  214. {
  215. return new Completion(Completion.Normal, v, null);
  216. }
  217. if (stmt.Type != Completion.Continue /* todo: complete */)
  218. {
  219. if (stmt.Type != Completion.Normal)
  220. {
  221. return stmt;
  222. }
  223. }
  224. }
  225. return new Completion(Completion.Normal, v, null);
  226. }
  227. /// <summary>
  228. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.7
  229. /// </summary>
  230. /// <param name="continueStatement"></param>
  231. /// <returns></returns>
  232. public Completion ExecuteContinueStatement(ContinueStatement continueStatement)
  233. {
  234. return new Completion(Completion.Continue, null, continueStatement.Label != null ? continueStatement.Label.Name : null);
  235. }
  236. /// <summary>
  237. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.8
  238. /// </summary>
  239. /// <param name="breakStatement"></param>
  240. /// <returns></returns>
  241. public Completion ExecuteBreakStatement(BreakStatement breakStatement)
  242. {
  243. return new Completion(Completion.Break, null, breakStatement.Label != null ? breakStatement.Label.Name : null);
  244. }
  245. /// <summary>
  246. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.9
  247. /// </summary>
  248. /// <param name="statement"></param>
  249. /// <returns></returns>
  250. public Completion ExecuteReturnStatement(ReturnStatement statement)
  251. {
  252. if (statement.Argument == null)
  253. {
  254. return new Completion(Completion.Return, Undefined.Instance, null);
  255. }
  256. var exprRef = _engine.EvaluateExpression(statement.Argument);
  257. return new Completion(Completion.Return, _engine.GetValue(exprRef), null);
  258. }
  259. /// <summary>
  260. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
  261. /// </summary>
  262. /// <param name="withStatement"></param>
  263. /// <returns></returns>
  264. public Completion ExecuteWithStatement(WithStatement withStatement)
  265. {
  266. var val = _engine.EvaluateExpression(withStatement.Object);
  267. var obj = TypeConverter.ToObject(_engine, _engine.GetValue(val));
  268. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  269. var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, true);
  270. _engine.ExecutionContext.LexicalEnvironment = newEnv;
  271. Completion c;
  272. try
  273. {
  274. c = ExecuteStatement(withStatement.Body);
  275. }
  276. catch (JavaScriptException e)
  277. {
  278. c = new Completion(Completion.Throw, e.Error, null);
  279. }
  280. finally
  281. {
  282. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  283. }
  284. return c;
  285. }
  286. /// <summary>
  287. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.11
  288. /// </summary>
  289. /// <param name="switchStatement"></param>
  290. /// <returns></returns>
  291. public Completion ExecuteSwitchStatement(SwitchStatement switchStatement)
  292. {
  293. var exprRef = _engine.EvaluateExpression(switchStatement.Discriminant);
  294. var r = ExecuteSwitchBlock(switchStatement.Cases, _engine.GetValue(exprRef));
  295. if (r.Type == Completion.Break && r.Identifier == switchStatement.LabelSet)
  296. {
  297. return new Completion(Completion.Normal, r.Value, null);
  298. }
  299. return r;
  300. }
  301. public Completion ExecuteSwitchBlock(IEnumerable<SwitchCase> switchBlock, object input)
  302. {
  303. object v = null;
  304. SwitchCase defaultCase = null;
  305. foreach (var clause in switchBlock)
  306. {
  307. if (clause.Test == null)
  308. {
  309. defaultCase = clause;
  310. }
  311. else
  312. {
  313. var clauseSelector = _engine.GetValue(_engine.EvaluateExpression(clause.Test));
  314. if (ExpressionInterpreter.StriclyEqual(clauseSelector, input))
  315. {
  316. if (clause.Consequent != null)
  317. {
  318. var r = ExecuteStatementList(clause.Consequent);
  319. if (r.Type != Completion.Normal)
  320. {
  321. return r;
  322. }
  323. v = r.Value;
  324. }
  325. }
  326. }
  327. }
  328. if (defaultCase != null)
  329. {
  330. var r = ExecuteStatementList(defaultCase.Consequent);
  331. if (r.Type != Completion.Normal)
  332. {
  333. return r;
  334. }
  335. v = r.Value;
  336. }
  337. return new Completion(Completion.Normal, v, null);
  338. }
  339. public Completion ExecuteStatementList(IEnumerable<Statement> statementList)
  340. {
  341. var c = new Completion(Completion.Normal, null, null);
  342. Completion sl = c;
  343. try
  344. {
  345. foreach (var statement in statementList)
  346. {
  347. c = ExecuteStatement(statement);
  348. if (c.Type != Completion.Normal)
  349. {
  350. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  351. }
  352. sl = c;
  353. }
  354. }
  355. catch(JavaScriptException v)
  356. {
  357. return new Completion(Completion.Throw, v.Error, null);
  358. }
  359. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  360. }
  361. /// <summary>
  362. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  363. /// </summary>
  364. /// <param name="throwStatement"></param>
  365. /// <returns></returns>
  366. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  367. {
  368. var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
  369. return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
  370. }
  371. /// <summary>
  372. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  373. /// </summary>
  374. /// <param name="tryStatement"></param>
  375. /// <returns></returns>
  376. public Completion ExecuteTryStatement(TryStatement tryStatement)
  377. {
  378. var b = ExecuteStatement(tryStatement.Block);
  379. if (b.Type == Completion.Throw)
  380. {
  381. // execute catch
  382. if (tryStatement.Handlers.Any())
  383. {
  384. foreach (var catchClause in tryStatement.Handlers)
  385. {
  386. var c = _engine.GetValue(b);
  387. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  388. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  389. catchEnv.Record.CreateMutableBinding(catchClause.Param.Name);
  390. catchEnv.Record.SetMutableBinding(catchClause.Param.Name, c, false);
  391. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  392. b = ExecuteStatement(catchClause.Body);
  393. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  394. }
  395. }
  396. }
  397. if (tryStatement.Finalizer != null)
  398. {
  399. var f = ExecuteStatement(tryStatement.Finalizer);
  400. if (f.Type == Completion.Normal)
  401. {
  402. return b;
  403. }
  404. return f;
  405. }
  406. return b;
  407. }
  408. public Completion ExecuteProgram(Program program)
  409. {
  410. return ExecuteStatementList(program.Body);
  411. }
  412. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  413. {
  414. string lastIdentifier = null;
  415. foreach (var declaration in statement.Declarations)
  416. {
  417. if (declaration.Init != null)
  418. {
  419. var lhs = _engine.EvaluateExpression(declaration.Id) as Reference;
  420. if (lhs == null)
  421. {
  422. throw new ArgumentException();
  423. }
  424. if (lhs.IsStrict() && lhs.GetBase() is EnvironmentRecord &&
  425. (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
  426. {
  427. throw new JavaScriptException(_engine.SyntaxError);
  428. }
  429. lastIdentifier = lhs.GetReferencedName();
  430. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
  431. _engine.PutValue(lhs, value);
  432. }
  433. }
  434. return new Completion(Completion.Normal, lastIdentifier, null);
  435. }
  436. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  437. {
  438. return ExecuteStatementList(blockStatement.Body);
  439. }
  440. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  441. {
  442. if (!System.Diagnostics.Debugger.IsAttached)
  443. {
  444. System.Diagnostics.Debugger.Launch();
  445. }
  446. System.Diagnostics.Debugger.Break();
  447. return new Completion(Completion.Normal, null, null);
  448. }
  449. }
  450. }