ExecutionContext.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #nullable enable
  2. namespace Jint.Runtime.Environments
  3. {
  4. public readonly struct ExecutionContext
  5. {
  6. internal ExecutionContext(
  7. EnvironmentRecord lexicalEnvironment,
  8. EnvironmentRecord variableEnvironment)
  9. {
  10. LexicalEnvironment = lexicalEnvironment;
  11. VariableEnvironment = variableEnvironment;
  12. }
  13. public readonly EnvironmentRecord LexicalEnvironment;
  14. public readonly EnvironmentRecord VariableEnvironment;
  15. public ExecutionContext UpdateLexicalEnvironment(EnvironmentRecord lexicalEnvironment)
  16. {
  17. return new ExecutionContext(lexicalEnvironment, VariableEnvironment);
  18. }
  19. public ExecutionContext UpdateVariableEnvironment(EnvironmentRecord variableEnvironment)
  20. {
  21. return new ExecutionContext(LexicalEnvironment, variableEnvironment);
  22. }
  23. /// <summary>
  24. /// https://tc39.es/ecma262/#sec-getthisenvironment
  25. /// </summary>
  26. internal EnvironmentRecord GetThisEnvironment()
  27. {
  28. // The loop will always terminate because the list of environments always
  29. // ends with the global environment which has a this binding.
  30. var lex = LexicalEnvironment;
  31. while (true)
  32. {
  33. if (lex != null)
  34. {
  35. if (lex.HasThisBinding())
  36. {
  37. return lex;
  38. }
  39. lex = lex._outerEnv;
  40. }
  41. }
  42. }
  43. }
  44. }