StatementInterpreter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Jint.Native;
  5. using Jint.Native.Object;
  6. using Jint.Parser.Ast;
  7. using Jint.Runtime.Environments;
  8. using Jint.Runtime.References;
  9. namespace Jint.Runtime
  10. {
  11. public class StatementInterpreter
  12. {
  13. private readonly Engine _engine;
  14. public StatementInterpreter(Engine engine)
  15. {
  16. _engine = engine;
  17. }
  18. private Completion ExecuteStatement(Statement statement)
  19. {
  20. return _engine.ExecuteStatement(statement);
  21. }
  22. public Completion ExecuteEmptyStatement(EmptyStatement emptyStatement)
  23. {
  24. return new Completion(Completion.Normal, null, null);
  25. }
  26. public Completion ExecuteExpressionStatement(ExpressionStatement expressionStatement)
  27. {
  28. var exprRef = _engine.EvaluateExpression(expressionStatement.Expression);
  29. return new Completion(Completion.Normal, _engine.GetValue(exprRef), null);
  30. }
  31. public Completion ExecuteIfStatement(IfStatement ifStatement)
  32. {
  33. var exprRef = _engine.EvaluateExpression(ifStatement.Test);
  34. Completion result;
  35. if (TypeConverter.ToBoolean(_engine.GetValue(exprRef)))
  36. {
  37. result = ExecuteStatement(ifStatement.Consequent);
  38. }
  39. else if (ifStatement.Alternate != null)
  40. {
  41. result = ExecuteStatement(ifStatement.Alternate);
  42. }
  43. else
  44. {
  45. return new Completion(Completion.Normal, null, null);
  46. }
  47. return result;
  48. }
  49. public Completion ExecuteLabelledStatement(LabelledStatement labelledStatement)
  50. {
  51. labelledStatement.Body.LabelSet = labelledStatement.Label.Name;
  52. var result = ExecuteStatement(labelledStatement.Body);
  53. if (result.Type == Completion.Break && result.Identifier == labelledStatement.Label.Name)
  54. {
  55. return new Completion(Completion.Normal, result.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. object v = null;
  67. bool iterating;
  68. do
  69. {
  70. var stmt = ExecuteStatement(doWhileStatement.Body);
  71. if (stmt.Value != null)
  72. {
  73. v = stmt.Value;
  74. }
  75. if (stmt.Type != Completion.Continue || stmt.Identifier != doWhileStatement.LabelSet)
  76. {
  77. if (stmt.Type == Completion.Break && stmt.Identifier == doWhileStatement.LabelSet)
  78. {
  79. return new Completion(Completion.Normal, v, null);
  80. }
  81. if (stmt.Type != Completion.Normal)
  82. {
  83. return stmt;
  84. }
  85. }
  86. var exprRef = _engine.EvaluateExpression(doWhileStatement.Test);
  87. iterating = TypeConverter.ToBoolean(_engine.GetValue(exprRef));
  88. } while (iterating);
  89. return new Completion(Completion.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. object v = null;
  99. while (true)
  100. {
  101. var exprRef = _engine.EvaluateExpression(whileStatement.Test);
  102. if (!TypeConverter.ToBoolean(_engine.GetValue(exprRef)))
  103. {
  104. return new Completion(Completion.Normal, v, null);
  105. }
  106. var stmt = ExecuteStatement(whileStatement.Body);
  107. if (stmt.Value != null)
  108. {
  109. v = stmt.Value;
  110. }
  111. if (stmt.Type != Completion.Continue || stmt.Identifier != whileStatement.LabelSet)
  112. {
  113. if (stmt.Type == Completion.Break && stmt.Identifier == whileStatement.LabelSet)
  114. {
  115. return new Completion(Completion.Normal, v, null);
  116. }
  117. if (stmt.Type != Completion.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. if (forStatement.Init != null)
  132. {
  133. if (forStatement.Init.Type == SyntaxNodes.VariableDeclaration)
  134. {
  135. ExecuteStatement(forStatement.Init.As<Statement>());
  136. }
  137. else
  138. {
  139. _engine.GetValue(_engine.EvaluateExpression(forStatement.Init.As<Expression>()));
  140. }
  141. }
  142. object v = null;
  143. while (true)
  144. {
  145. if (forStatement.Test != null)
  146. {
  147. var testExprRef = _engine.EvaluateExpression(forStatement.Test);
  148. if (!TypeConverter.ToBoolean(_engine.GetValue(testExprRef)))
  149. {
  150. return new Completion(Completion.Normal, v, null);
  151. }
  152. }
  153. var stmt = ExecuteStatement(forStatement.Body);
  154. if (stmt.Value != null)
  155. {
  156. v = stmt.Value;
  157. }
  158. if (stmt.Type == Completion.Break && stmt.Identifier == forStatement.LabelSet)
  159. {
  160. return new Completion(Completion.Normal, v, null);
  161. }
  162. if (stmt.Type != Completion.Continue || ((stmt.Identifier != null) && stmt.Identifier != forStatement.LabelSet))
  163. {
  164. if (stmt.Type != Completion.Normal)
  165. {
  166. return stmt;
  167. }
  168. }
  169. if (forStatement.Update != null)
  170. {
  171. var incExprRef = _engine.EvaluateExpression(forStatement.Update);
  172. _engine.GetValue(incExprRef);
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
  178. /// </summary>
  179. /// <param name="forInStatement"></param>
  180. /// <returns></returns>
  181. public Completion ExecuteForInStatement(ForInStatement forInStatement)
  182. {
  183. Identifier identifier = forInStatement.Left.Type == SyntaxNodes.VariableDeclaration
  184. ? forInStatement.Left.As<VariableDeclaration>().Declarations.First().Id
  185. : forInStatement.Left.As<Identifier>();
  186. var varRef = _engine.EvaluateExpression(identifier) as Reference;
  187. var exprRef = _engine.EvaluateExpression(forInStatement.Right);
  188. var experValue = _engine.GetValue(exprRef);
  189. if (experValue == Undefined.Instance || experValue == Null.Instance)
  190. {
  191. return new Completion(Completion.Normal, null, null);
  192. }
  193. var obj = TypeConverter.ToObject(_engine, experValue);
  194. object v = null;
  195. // keys are constructed using the prototype chain
  196. var cursor = obj;
  197. var processedKeys = new HashSet<string>();
  198. while (cursor != null)
  199. {
  200. var keys = cursor.Properties.Keys.ToArray();
  201. foreach (var p in keys)
  202. {
  203. if (processedKeys.Contains(p))
  204. {
  205. continue;
  206. }
  207. processedKeys.Add(p);
  208. // collection might be modified by inner statement
  209. if (!cursor.Properties.ContainsKey(p))
  210. {
  211. continue;
  212. }
  213. var value = cursor.Properties[p];
  214. if (!value.EnumerableIsSet)
  215. {
  216. continue;
  217. }
  218. _engine.PutValue(varRef, p);
  219. var stmt = ExecuteStatement(forInStatement.Body);
  220. if (stmt.Value != null)
  221. {
  222. v = stmt.Value;
  223. }
  224. if (stmt.Type == Completion.Break /* todo: complete */)
  225. {
  226. return new Completion(Completion.Normal, v, null);
  227. }
  228. if (stmt.Type != Completion.Continue /* todo: complete */)
  229. {
  230. if (stmt.Type != Completion.Normal)
  231. {
  232. return stmt;
  233. }
  234. }
  235. }
  236. cursor = cursor.Prototype;
  237. }
  238. return new Completion(Completion.Normal, v, null);
  239. }
  240. /// <summary>
  241. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.7
  242. /// </summary>
  243. /// <param name="continueStatement"></param>
  244. /// <returns></returns>
  245. public Completion ExecuteContinueStatement(ContinueStatement continueStatement)
  246. {
  247. return new Completion(Completion.Continue, null, continueStatement.Label != null ? continueStatement.Label.Name : null);
  248. }
  249. /// <summary>
  250. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.8
  251. /// </summary>
  252. /// <param name="breakStatement"></param>
  253. /// <returns></returns>
  254. public Completion ExecuteBreakStatement(BreakStatement breakStatement)
  255. {
  256. return new Completion(Completion.Break, null, breakStatement.Label != null ? breakStatement.Label.Name : null);
  257. }
  258. /// <summary>
  259. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.9
  260. /// </summary>
  261. /// <param name="statement"></param>
  262. /// <returns></returns>
  263. public Completion ExecuteReturnStatement(ReturnStatement statement)
  264. {
  265. if (statement.Argument == null)
  266. {
  267. return new Completion(Completion.Return, Undefined.Instance, null);
  268. }
  269. var exprRef = _engine.EvaluateExpression(statement.Argument);
  270. return new Completion(Completion.Return, _engine.GetValue(exprRef), null);
  271. }
  272. /// <summary>
  273. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
  274. /// </summary>
  275. /// <param name="withStatement"></param>
  276. /// <returns></returns>
  277. public Completion ExecuteWithStatement(WithStatement withStatement)
  278. {
  279. var val = _engine.EvaluateExpression(withStatement.Object);
  280. var obj = TypeConverter.ToObject(_engine, _engine.GetValue(val));
  281. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  282. var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, true);
  283. _engine.ExecutionContext.LexicalEnvironment = newEnv;
  284. Completion c;
  285. try
  286. {
  287. c = ExecuteStatement(withStatement.Body);
  288. }
  289. catch (JavaScriptException e)
  290. {
  291. c = new Completion(Completion.Throw, e.Error, null);
  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, object input)
  315. {
  316. object v = null;
  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.StriclyEqual(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;
  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;
  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. try
  360. {
  361. foreach (var statement in statementList)
  362. {
  363. c = ExecuteStatement(statement);
  364. if (c.Type != Completion.Normal)
  365. {
  366. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  367. }
  368. sl = c;
  369. }
  370. }
  371. catch(JavaScriptException v)
  372. {
  373. return new Completion(Completion.Throw, v.Error, null);
  374. }
  375. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  376. }
  377. /// <summary>
  378. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  379. /// </summary>
  380. /// <param name="throwStatement"></param>
  381. /// <returns></returns>
  382. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  383. {
  384. var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
  385. return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
  386. }
  387. /// <summary>
  388. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  389. /// </summary>
  390. /// <param name="tryStatement"></param>
  391. /// <returns></returns>
  392. public Completion ExecuteTryStatement(TryStatement tryStatement)
  393. {
  394. var b = ExecuteStatement(tryStatement.Block);
  395. if (b.Type == Completion.Throw)
  396. {
  397. // execute catch
  398. if (tryStatement.Handlers.Any())
  399. {
  400. foreach (var catchClause in tryStatement.Handlers)
  401. {
  402. var c = _engine.GetValue(b);
  403. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  404. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  405. catchEnv.Record.CreateMutableBinding(catchClause.Param.Name);
  406. catchEnv.Record.SetMutableBinding(catchClause.Param.Name, c, false);
  407. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  408. b = ExecuteStatement(catchClause.Body);
  409. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  410. }
  411. }
  412. }
  413. if (tryStatement.Finalizer != null)
  414. {
  415. var f = ExecuteStatement(tryStatement.Finalizer);
  416. if (f.Type == Completion.Normal)
  417. {
  418. return b;
  419. }
  420. return f;
  421. }
  422. return b;
  423. }
  424. public Completion ExecuteProgram(Program program)
  425. {
  426. return ExecuteStatementList(program.Body);
  427. }
  428. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  429. {
  430. string lastIdentifier = null;
  431. foreach (var declaration in statement.Declarations)
  432. {
  433. if (declaration.Init != null)
  434. {
  435. var lhs = _engine.EvaluateExpression(declaration.Id) as Reference;
  436. if (lhs == null)
  437. {
  438. throw new ArgumentException();
  439. }
  440. if (lhs.IsStrict() && lhs.GetBase() is EnvironmentRecord &&
  441. (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
  442. {
  443. throw new JavaScriptException(_engine.SyntaxError);
  444. }
  445. lastIdentifier = lhs.GetReferencedName();
  446. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
  447. _engine.PutValue(lhs, value);
  448. }
  449. }
  450. return new Completion(Completion.Normal, lastIdentifier, null);
  451. }
  452. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  453. {
  454. return ExecuteStatementList(blockStatement.Body);
  455. }
  456. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  457. {
  458. if (!System.Diagnostics.Debugger.IsAttached)
  459. {
  460. System.Diagnostics.Debugger.Launch();
  461. }
  462. System.Diagnostics.Debugger.Break();
  463. return new Completion(Completion.Normal, null, null);
  464. }
  465. }
  466. }