StatementInterpreter.cs 18 KB

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