StatementInterpreter.cs 19 KB

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