JintWithStatement.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Esprima.Ast;
  2. using Jint.Runtime.Environments;
  3. using Jint.Runtime.Interpreter.Expressions;
  4. namespace Jint.Runtime.Interpreter.Statements
  5. {
  6. /// <summary>
  7. /// http://www.ecma-international.org/ecma-262/5.1/#sec-12.10
  8. /// </summary>
  9. internal sealed class JintWithStatement : JintStatement<WithStatement>
  10. {
  11. private readonly JintStatement _body;
  12. private readonly JintExpression _object;
  13. public JintWithStatement(Engine engine, WithStatement statement) : base(engine, statement)
  14. {
  15. _body = Build(engine, statement.Body);
  16. _object = JintExpression.Build(engine, _statement.Object);
  17. }
  18. protected override Completion ExecuteInternal()
  19. {
  20. var jsValue = _object.GetValue();
  21. var obj = TypeConverter.ToObject(_engine, jsValue);
  22. var oldEnv = _engine.ExecutionContext.LexicalEnvironment;
  23. var newEnv = LexicalEnvironment.NewObjectEnvironment(_engine, obj, oldEnv, provideThis: true, withEnvironment: true);
  24. _engine.UpdateLexicalEnvironment(newEnv);
  25. Completion c;
  26. try
  27. {
  28. c = _body.Execute();
  29. }
  30. catch (JavaScriptException e)
  31. {
  32. c = new Completion(CompletionType.Throw, e.Error, null, _statement.Location);
  33. }
  34. finally
  35. {
  36. _engine.UpdateLexicalEnvironment(oldEnv);
  37. }
  38. return c;
  39. }
  40. }
  41. }