JintEnvironment.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Diagnostics.CodeAnalysis;
  2. using Jint.Native;
  3. using Jint.Native.Function;
  4. using Jint.Native.Object;
  5. namespace Jint.Runtime.Environments
  6. {
  7. internal static class JintEnvironment
  8. {
  9. internal static bool TryGetIdentifierEnvironmentWithBinding(
  10. Environment env,
  11. in Environment.BindingName name,
  12. [NotNullWhen(true)] out Environment? record)
  13. {
  14. record = env;
  15. if (env._outerEnv is null)
  16. {
  17. return env.HasBinding(name);
  18. }
  19. while (!ReferenceEquals(record, null))
  20. {
  21. if (record.HasBinding(name))
  22. {
  23. return true;
  24. }
  25. record = record._outerEnv;
  26. }
  27. return false;
  28. }
  29. internal static bool TryGetIdentifierEnvironmentWithBindingValue(
  30. Environment env,
  31. in Environment.BindingName name,
  32. bool strict,
  33. [NotNullWhen(true)] out Environment? record,
  34. [NotNullWhen(true)] out JsValue? value)
  35. {
  36. record = env;
  37. value = default;
  38. if (env._outerEnv is null)
  39. {
  40. return ((GlobalEnvironment) env).TryGetBinding(name, strict, out _, out value);
  41. }
  42. while (!ReferenceEquals(record, null))
  43. {
  44. if (record.TryGetBinding(
  45. name,
  46. strict,
  47. out _,
  48. out value))
  49. {
  50. return true;
  51. }
  52. record = record._outerEnv;
  53. }
  54. return false;
  55. }
  56. /// <summary>
  57. /// https://tc39.es/ecma262/#sec-newdeclarativeenvironment
  58. /// </summary>
  59. internal static DeclarativeEnvironment NewDeclarativeEnvironment(Engine engine, Environment? outer, bool catchEnvironment = false)
  60. {
  61. return new DeclarativeEnvironment(engine, catchEnvironment)
  62. {
  63. _outerEnv = outer
  64. };
  65. }
  66. /// <summary>
  67. /// https://tc39.es/ecma262/#sec-newfunctionenvironment
  68. /// </summary>
  69. internal static FunctionEnvironment NewFunctionEnvironment(Engine engine, Function f, JsValue newTarget)
  70. {
  71. return new FunctionEnvironment(engine, f, newTarget)
  72. {
  73. _outerEnv = f._environment
  74. };
  75. }
  76. /// <summary>
  77. /// https://tc39.es/ecma262/#sec-newglobalenvironment
  78. /// </summary>
  79. internal static GlobalEnvironment NewGlobalEnvironment(Engine engine, ObjectInstance objectInstance, JsValue thisValue)
  80. {
  81. return new GlobalEnvironment(engine, objectInstance)
  82. {
  83. _outerEnv = null
  84. };
  85. }
  86. /// <summary>
  87. /// https://tc39.es/ecma262/#sec-newobjectenvironment
  88. /// </summary>
  89. internal static ObjectEnvironment NewObjectEnvironment(Engine engine, ObjectInstance objectInstance, Environment outer, bool provideThis, bool withEnvironment = false)
  90. {
  91. return new ObjectEnvironment(engine, objectInstance, provideThis, withEnvironment)
  92. {
  93. _outerEnv = outer
  94. };
  95. }
  96. /// <summary>
  97. /// https://tc39.es/ecma262/#sec-newprivateenvironment
  98. /// </summary>
  99. internal static PrivateEnvironment NewPrivateEnvironment(Engine engine, PrivateEnvironment? outerPriv)
  100. {
  101. return new PrivateEnvironment(outerPriv);
  102. }
  103. /// <summary>
  104. /// https://tc39.es/ecma262/#sec-newmoduleenvironment
  105. /// </summary>
  106. internal static ModuleEnvironment NewModuleEnvironment(Engine engine, Environment outer)
  107. {
  108. return new ModuleEnvironment(engine)
  109. {
  110. _outerEnv = outer
  111. };
  112. }
  113. }
  114. }