StatementInterpreter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 != 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. 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. if (clause.Consequent != null)
  330. {
  331. var r = ExecuteStatementList(clause.Consequent);
  332. if (r.Type != Completion.Normal)
  333. {
  334. return r;
  335. }
  336. v = r.Value;
  337. }
  338. }
  339. }
  340. }
  341. if (defaultCase != null)
  342. {
  343. var r = ExecuteStatementList(defaultCase.Consequent);
  344. if (r.Type != Completion.Normal)
  345. {
  346. return r;
  347. }
  348. v = r.Value;
  349. }
  350. return new Completion(Completion.Normal, v, null);
  351. }
  352. public Completion ExecuteStatementList(IEnumerable<Statement> statementList)
  353. {
  354. var c = new Completion(Completion.Normal, null, null);
  355. Completion sl = c;
  356. try
  357. {
  358. foreach (var statement in statementList)
  359. {
  360. c = ExecuteStatement(statement);
  361. if (c.Type != Completion.Normal)
  362. {
  363. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  364. }
  365. sl = c;
  366. }
  367. }
  368. catch(JavaScriptException v)
  369. {
  370. return new Completion(Completion.Throw, v.Error, null);
  371. }
  372. return new Completion(c.Type, c.Value ?? sl.Value, c.Identifier);
  373. }
  374. /// <summary>
  375. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.13
  376. /// </summary>
  377. /// <param name="throwStatement"></param>
  378. /// <returns></returns>
  379. public Completion ExecuteThrowStatement(ThrowStatement throwStatement)
  380. {
  381. var exprRef = _engine.EvaluateExpression(throwStatement.Argument);
  382. return new Completion(Completion.Throw, _engine.GetValue(exprRef), null);
  383. }
  384. /// <summary>
  385. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.14
  386. /// </summary>
  387. /// <param name="tryStatement"></param>
  388. /// <returns></returns>
  389. public Completion ExecuteTryStatement(TryStatement tryStatement)
  390. {
  391. var b = ExecuteStatement(tryStatement.Block);
  392. if (b.Type == Completion.Throw)
  393. {
  394. // execute catch
  395. if (tryStatement.Handlers.Any())
  396. {
  397. foreach (var catchClause in tryStatement.Handlers)
  398. {
  399. var c = _engine.GetValue(b);
  400. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  401. var catchEnv = LexicalEnvironment.NewDeclarativeEnvironment(_engine, oldEnv);
  402. catchEnv.Record.CreateMutableBinding(catchClause.Param.Name);
  403. catchEnv.Record.SetMutableBinding(catchClause.Param.Name, c, false);
  404. _engine.ExecutionContext.LexicalEnvironment = catchEnv;
  405. b = ExecuteStatement(catchClause.Body);
  406. _engine.ExecutionContext.LexicalEnvironment = oldEnv;
  407. }
  408. }
  409. }
  410. if (tryStatement.Finalizer != null)
  411. {
  412. var f = ExecuteStatement(tryStatement.Finalizer);
  413. if (f.Type == Completion.Normal)
  414. {
  415. return b;
  416. }
  417. return f;
  418. }
  419. return b;
  420. }
  421. public Completion ExecuteProgram(Program program)
  422. {
  423. return ExecuteStatementList(program.Body);
  424. }
  425. public Completion ExecuteVariableDeclaration(VariableDeclaration statement)
  426. {
  427. string lastIdentifier = null;
  428. foreach (var declaration in statement.Declarations)
  429. {
  430. if (declaration.Init != null)
  431. {
  432. var lhs = _engine.EvaluateExpression(declaration.Id) as Reference;
  433. if (lhs == null)
  434. {
  435. throw new ArgumentException();
  436. }
  437. if (lhs.IsStrict() && lhs.GetBase() is EnvironmentRecord &&
  438. (lhs.GetReferencedName() == "eval" || lhs.GetReferencedName() == "arguments"))
  439. {
  440. throw new JavaScriptException(_engine.SyntaxError);
  441. }
  442. lastIdentifier = lhs.GetReferencedName();
  443. var value = _engine.GetValue(_engine.EvaluateExpression(declaration.Init));
  444. _engine.PutValue(lhs, value);
  445. }
  446. }
  447. return new Completion(Completion.Normal, lastIdentifier, null);
  448. }
  449. public Completion ExecuteBlockStatement(BlockStatement blockStatement)
  450. {
  451. return ExecuteStatementList(blockStatement.Body);
  452. }
  453. public Completion ExecuteDebuggerStatement(DebuggerStatement debuggerStatement)
  454. {
  455. if (!System.Diagnostics.Debugger.IsAttached)
  456. {
  457. System.Diagnostics.Debugger.Launch();
  458. }
  459. System.Diagnostics.Debugger.Break();
  460. return new Completion(Completion.Normal, null, null);
  461. }
  462. }
  463. }