StatementInterpreter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. /// <summary>
  49. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.1
  50. /// </summary>
  51. /// <param name="doWhileStatement"></param>
  52. /// <returns></returns>
  53. public Completion ExecuteDoWhileStatement(DoWhileStatement doWhileStatement)
  54. {
  55. object v = null;
  56. bool iterating;
  57. do
  58. {
  59. var stmt = ExecuteStatement(doWhileStatement.Body);
  60. if (stmt.Value != null)
  61. {
  62. v = stmt.Value;
  63. }
  64. if (stmt.Type != Completion.Continue /* todo: || stmt.Target*/)
  65. {
  66. if (stmt.Type == Completion.Break /* todo: complete */)
  67. {
  68. return new Completion(Completion.Normal, v, null);
  69. }
  70. if (stmt.Type != Completion.Normal)
  71. {
  72. return stmt;
  73. }
  74. }
  75. var exprRef = _engine.EvaluateExpression(doWhileStatement.Test);
  76. iterating = TypeConverter.ToBoolean(_engine.GetValue(exprRef));
  77. } while (iterating);
  78. return new Completion(Completion.Normal, v, null);
  79. }
  80. /// <summary>
  81. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.2
  82. /// </summary>
  83. /// <param name="whileStatement"></param>
  84. /// <returns></returns>
  85. public Completion ExecuteWhileStatement(WhileStatement whileStatement)
  86. {
  87. object v = null;
  88. while (true)
  89. {
  90. var exprRef = _engine.EvaluateExpression(whileStatement.Test);
  91. if (!TypeConverter.ToBoolean(_engine.GetValue(exprRef)))
  92. {
  93. return new Completion(Completion.Normal, v, null);
  94. }
  95. var stmt = ExecuteStatement(whileStatement.Body);
  96. if (stmt.Value != null)
  97. {
  98. v = stmt.Value;
  99. }
  100. if (stmt.Type != Completion.Continue /* todo: complete */)
  101. {
  102. if (stmt.Type == Completion.Break /* todo: complete */)
  103. {
  104. return new Completion(Completion.Normal, v, null);
  105. }
  106. if (stmt.Type != Completion.Normal)
  107. {
  108. return stmt;
  109. }
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.3
  115. /// </summary>
  116. /// <param name="forStatement"></param>
  117. /// <returns></returns>
  118. public Completion ExecuteForStatement(ForStatement forStatement)
  119. {
  120. if (forStatement.Init != null)
  121. {
  122. if (forStatement.Init.Type == SyntaxNodes.VariableDeclaration)
  123. {
  124. ExecuteStatement(forStatement.Init.As<Statement>());
  125. }
  126. else
  127. {
  128. _engine.GetValue(_engine.EvaluateExpression(forStatement.Init.As<Expression>()));
  129. }
  130. }
  131. object v = null;
  132. while (true)
  133. {
  134. if (forStatement.Test != null)
  135. {
  136. var testExprRef = _engine.EvaluateExpression(forStatement.Test);
  137. if (!TypeConverter.ToBoolean(_engine.GetValue(testExprRef)))
  138. {
  139. return new Completion(Completion.Normal, v, null);
  140. }
  141. }
  142. var stmt = ExecuteStatement(forStatement.Body);
  143. if (stmt.Value != null)
  144. {
  145. v = stmt.Value;
  146. }
  147. if (stmt.Type == Completion.Break /* todo: complete */)
  148. {
  149. return new Completion(Completion.Normal, v, null);
  150. }
  151. if (stmt.Type != Completion.Continue /* todo: complete */)
  152. {
  153. if (stmt.Type != Completion.Normal)
  154. {
  155. return stmt;
  156. }
  157. }
  158. if (forStatement.Update != null)
  159. {
  160. var incExprRef = _engine.EvaluateExpression(forStatement.Update);
  161. _engine.GetValue(incExprRef);
  162. }
  163. }
  164. }
  165. /// <summary>
  166. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
  167. /// </summary>
  168. /// <param name="forInStatement"></param>
  169. /// <returns></returns>
  170. public Completion ExecuteForInStatement(ForInStatement forInStatement)
  171. {
  172. Identifier identifier = forInStatement.Left.Type == SyntaxNodes.VariableDeclaration
  173. ? forInStatement.Left.As<VariableDeclaration>().Declarations.First().Id
  174. : forInStatement.Left.As<Identifier>();
  175. var varRef = _engine.EvaluateExpression(identifier) as Reference;
  176. var exprRef = _engine.EvaluateExpression(forInStatement.Right);
  177. var experValue = _engine.GetValue(exprRef);
  178. if (experValue == Undefined.Instance || experValue == Null.Instance)
  179. {
  180. return new Completion(Completion.Normal, null, null);
  181. }
  182. var obj = TypeConverter.ToObject(_engine, experValue);
  183. object v = null;
  184. var keys = obj.Properties.Keys.ToArray();
  185. foreach (var p in keys)
  186. {
  187. var value = obj.Properties[p];
  188. if (!value.EnumerableIsSet)
  189. {
  190. continue;
  191. }
  192. _engine.PutValue(varRef, p);
  193. var stmt = ExecuteStatement(forInStatement.Body);
  194. if (stmt.Value != null)
  195. {
  196. v = stmt.Value;
  197. }
  198. if (stmt.Type == Completion.Break /* todo: complete */)
  199. {
  200. return new Completion(Completion.Normal, v, null);
  201. }
  202. if (stmt.Type != Completion.Continue /* todo: complete */)
  203. {
  204. if (stmt.Type != Completion.Normal)
  205. {
  206. return stmt;
  207. }
  208. }
  209. }
  210. return new Completion(Completion.Normal, v, null);
  211. }
  212. /// <summary>
  213. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.7
  214. /// </summary>
  215. /// <param name="continueStatement"></param>
  216. /// <returns></returns>
  217. public Completion ExecuteContinueStatement(ContinueStatement continueStatement)
  218. {
  219. return new Completion(Completion.Continue, null, continueStatement.Label != null ? continueStatement.Label.Name : null);
  220. }
  221. /// <summary>
  222. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.8
  223. /// </summary>
  224. /// <param name="breakStatement"></param>
  225. /// <returns></returns>
  226. public Completion ExecuteBreakStatement(BreakStatement breakStatement)
  227. {
  228. return new Completion(Completion.Break, null, breakStatement.Label != null ? breakStatement.Label.Name : null);
  229. }
  230. /// <summary>
  231. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.9
  232. /// </summary>
  233. /// <param name="statement"></param>
  234. /// <returns></returns>
  235. public Completion ExecuteReturnStatement(ReturnStatement statement)
  236. {
  237. if (statement.Argument == null)
  238. {
  239. return new Completion(Completion.Return, Undefined.Instance, null);
  240. }
  241. var exprRef = _engine.EvaluateExpression(statement.Argument);
  242. return new Completion(Completion.Return, _engine.GetValue(exprRef), null);
  243. }
  244. /// <summary>
  245. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
  246. /// </summary>
  247. /// <param name="withStatement"></param>
  248. /// <returns></returns>
  249. public Completion ExecuteWithStatement(WithStatement withStatement)
  250. {
  251. var val = _engine.EvaluateExpression(withStatement.Object);
  252. var obj = TypeConverter.ToObject(_engine, _engine.GetValue(val));
  253. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  254. var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, true);
  255. _engine.ExecutionContext.LexicalEnvironment = newEnv;
  256. Completion c;
  257. try
  258. {
  259. c = ExecuteStatement(withStatement.Body);
  260. }
  261. catch (JavaScriptException e)
  262. {
  263. c = new Completion(Completion.Throw, e.Error, null);
  264. }
  265. finally
  266. {
  267. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  268. }
  269. return c;
  270. }
  271. /// <summary>
  272. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.11
  273. /// </summary>
  274. /// <param name="switchStatement"></param>
  275. /// <returns></returns>
  276. public Completion ExecuteSwitchStatement(SwitchStatement switchStatement)
  277. {
  278. var exprRef = _engine.EvaluateExpression(switchStatement.Discriminant);
  279. var r = ExecuteSwitchBlock(switchStatement.Cases, _engine.GetValue(exprRef));
  280. if (r.Type == Completion.Break /* too: complete */)
  281. {
  282. return new Completion(Completion.Normal, r.Value, null);
  283. }
  284. return r;
  285. }
  286. public Completion ExecuteSwitchBlock(IEnumerable<SwitchCase> switchBlock, object input)
  287. {
  288. object v = null;
  289. SwitchCase defaultCase = null;
  290. foreach (var clause in switchBlock)
  291. {
  292. if (clause.Test == null)
  293. {
  294. defaultCase = clause;
  295. }
  296. else
  297. {
  298. var clauseSelector = _engine.GetValue(_engine.EvaluateExpression(clause.Test));
  299. if (ExpressionInterpreter.StriclyEqual(clauseSelector, input))
  300. {
  301. if (clause.Consequent != null)
  302. {
  303. var r = ExecuteStatementList(clause.Consequent);
  304. if (r.Type != Completion.Normal)
  305. {
  306. return r;
  307. }
  308. v = r.Value;
  309. }
  310. }
  311. }
  312. }
  313. if (defaultCase != null)
  314. {
  315. var r = ExecuteStatementList(defaultCase.Consequent);
  316. if (r.Type != Completion.Normal)
  317. {
  318. return r;
  319. }
  320. v = r.Value;
  321. }
  322. return new Completion(Completion.Normal, v, null);
  323. }
  324. public Completion ExecuteStatementList(IEnumerable<Statement> statementList)
  325. {
  326. var c = new Completion(Completion.Normal, Undefined.Instance, null);
  327. Completion sl = c;
  328. try
  329. {
  330. foreach (var statement in statementList)
  331. {
  332. c = ExecuteStatement(statement);
  333. if (c.Type != Completion.Normal)
  334. {
  335. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  336. }
  337. sl = c;
  338. }
  339. }
  340. catch(JavaScriptException v)
  341. {
  342. return new Completion(Completion.Throw, v.Error, null);
  343. }
  344. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  345. }
  346. /// <summary>
  347. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  348. /// </summary>
  349. /// <param name="throwStatement"></param>
  350. /// <returns></returns>
  351. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  352. {
  353. var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
  354. return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
  355. }
  356. /// <summary>
  357. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  358. /// </summary>
  359. /// <param name="tryStatement"></param>
  360. /// <returns></returns>
  361. public Completion ExecuteTryStatement(TryStatement tryStatement)
  362. {
  363. var b = ExecuteStatement(tryStatement.Block);
  364. if (b.Type == Completion.Throw)
  365. {
  366. // execute catch
  367. if (tryStatement.Handlers.Any())
  368. {
  369. foreach (var catchClause in tryStatement.Handlers)
  370. {
  371. var c = _engine.GetValue(b);
  372. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  373. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  374. catchEnv.Record.CreateMutableBinding(catchClause.Param.Name);
  375. catchEnv.Record.SetMutableBinding(catchClause.Param.Name, c, false);
  376. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  377. b = ExecuteStatement(catchClause.Body);
  378. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  379. }
  380. }
  381. }
  382. if (tryStatement.Finalizer != null)
  383. {
  384. var f = ExecuteStatement(tryStatement.Finalizer);
  385. if (f.Type == Completion.Normal)
  386. {
  387. return b;
  388. }
  389. return f;
  390. }
  391. return b;
  392. }
  393. public Completion ExecuteProgram(Program program)
  394. {
  395. if (program.Strict)
  396. {
  397. _engine.Options.Strict();
  398. }
  399. _engine.FunctionDeclarationBindings(program, _engine.ExecutionContext.LexicalEnvironment, true, program.Strict);
  400. _engine.VariableDeclarationBinding(program.VariableDeclarations, _engine.ExecutionContext.LexicalEnvironment.Record, true, program.Strict);
  401. return ExecuteStatementList(program.Body);
  402. }
  403. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  404. {
  405. string lastIdentifier = null;
  406. foreach (var declaration in statement.Declarations)
  407. {
  408. if (declaration.Init != null)
  409. {
  410. var lhs = _engine.EvaluateExpression(declaration.Id) as Reference;
  411. if (lhs == null)
  412. {
  413. throw new ArgumentException();
  414. }
  415. lastIdentifier = lhs.GetReferencedName();
  416. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
  417. _engine.PutValue(lhs, value);
  418. }
  419. }
  420. return new Completion(Completion.Normal, lastIdentifier, null);
  421. }
  422. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  423. {
  424. return ExecuteStatementList(blockStatement.Body);
  425. }
  426. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  427. {
  428. return new Completion(Completion.Normal, null, null);
  429. }
  430. }
  431. }