ModuleEnvironment.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/TypeCasts.h>
  7. #include <LibJS/Runtime/GlobalEnvironment.h>
  8. #include <LibJS/Runtime/ModuleEnvironment.h>
  9. #include <LibJS/Runtime/VM.h>
  10. namespace JS {
  11. JS_DEFINE_ALLOCATOR(ModuleEnvironment);
  12. // 9.1.2.6 NewModuleEnvironment ( E ), https://tc39.es/ecma262/#sec-newmoduleenvironment
  13. ModuleEnvironment::ModuleEnvironment(Environment* outer_environment)
  14. : DeclarativeEnvironment(outer_environment)
  15. {
  16. }
  17. // 9.1.1.5.1 GetBindingValue ( N, S ), https://tc39.es/ecma262/#sec-module-environment-records-getbindingvalue-n-s
  18. ThrowCompletionOr<Value> ModuleEnvironment::get_binding_value(VM& vm, DeprecatedFlyString const& name, bool strict)
  19. {
  20. // 1. Assert: S is true.
  21. VERIFY(strict);
  22. // 2. Assert: envRec has a binding for N.
  23. auto* indirect_binding = get_indirect_binding(name);
  24. VERIFY(indirect_binding || !DeclarativeEnvironment::has_binding(name).is_error());
  25. // 3. If the binding for N is an indirect binding, then
  26. if (indirect_binding) {
  27. // a. Let M and N2 be the indirection values provided when this binding for N was created.
  28. // b. Let targetEnv be M.[[Environment]].
  29. auto* target_env = indirect_binding->module->environment();
  30. // c. If targetEnv is empty, throw a ReferenceError exception.
  31. if (!target_env)
  32. return vm.throw_completion<ReferenceError>(ErrorType::ModuleNoEnvironment);
  33. // d. Return ? targetEnv.GetBindingValue(N2, true).
  34. return target_env->get_binding_value(vm, indirect_binding->binding_name, true);
  35. }
  36. // 4. If the binding for N in envRec is an uninitialized binding, throw a ReferenceError exception.
  37. // 5. Return the value currently bound to N in envRec.
  38. // Note: Step 4 & 5 are the steps performed by declarative environment GetBindingValue
  39. return DeclarativeEnvironment::get_binding_value(vm, name, strict);
  40. }
  41. // 9.1.1.5.2 DeleteBinding ( N ), https://tc39.es/ecma262/#sec-module-environment-records-deletebinding-n
  42. ThrowCompletionOr<bool> ModuleEnvironment::delete_binding(VM&, DeprecatedFlyString const&)
  43. {
  44. // The DeleteBinding concrete method of a module Environment Record is never used within this specification.
  45. VERIFY_NOT_REACHED();
  46. }
  47. // 9.1.1.5.4 GetThisBinding ( ), https://tc39.es/ecma262/#sec-module-environment-records-getthisbinding
  48. ThrowCompletionOr<Value> ModuleEnvironment::get_this_binding(VM&) const
  49. {
  50. // 1. Return undefined.
  51. return js_undefined();
  52. }
  53. // 9.1.1.5.5 CreateImportBinding ( N, M, N2 ), https://tc39.es/ecma262/#sec-createimportbinding
  54. ThrowCompletionOr<void> ModuleEnvironment::create_import_binding(DeprecatedFlyString name, Module* module, DeprecatedFlyString binding_name)
  55. {
  56. // 1. Assert: envRec does not already have a binding for N.
  57. VERIFY(!get_indirect_binding(name));
  58. // 2. Assert: When M.[[Environment]] is instantiated it will have a direct binding for N2.
  59. // FIXME: I don't know what this means or how to check it.
  60. // 3. Create an immutable indirect binding in envRec for N that references M and N2 as its target binding and record that the binding is initialized.
  61. // Note: We use the fact that the binding is in this list as it being initialized.
  62. m_indirect_bindings.append({ move(name),
  63. module,
  64. move(binding_name) });
  65. // 4. Return unused.
  66. return {};
  67. }
  68. ModuleEnvironment::IndirectBinding const* ModuleEnvironment::get_indirect_binding(DeprecatedFlyString const& name) const
  69. {
  70. auto binding_or_end = m_indirect_bindings.find_if([&](IndirectBinding const& binding) {
  71. return binding.name == name;
  72. });
  73. if (binding_or_end.is_end())
  74. return nullptr;
  75. return &(*binding_or_end);
  76. }
  77. Optional<ModuleEnvironment::BindingAndIndex> ModuleEnvironment::find_binding_and_index(DeprecatedFlyString const& name) const
  78. {
  79. auto* indirect_binding = get_indirect_binding(name);
  80. if (indirect_binding != nullptr) {
  81. auto* target_env = indirect_binding->module->environment();
  82. if (!target_env)
  83. return {};
  84. VERIFY(is<ModuleEnvironment>(target_env));
  85. auto& target_module_environment = static_cast<ModuleEnvironment&>(*target_env);
  86. auto result = target_module_environment.find_binding_and_index(indirect_binding->binding_name);
  87. if (!result.has_value())
  88. return {};
  89. // NOTE: We must pretend this binding is actually from this environment
  90. // so as specified by
  91. // 9.1.1.5.5 CreateImportBinding ( N, M, N2 ), https://tc39.es/ecma262/#sec-createimportbinding
  92. // It creates a new initialized immutable indirect binding for the
  93. // name N. A binding must not already exist in this Environment
  94. // Record for N. N2 is the name of a binding that exists in M's
  95. // Module Environment Record. Accesses to the value of the new
  96. // binding will indirectly access the bound value of the target
  97. // binding.
  98. // We don't alter the name of the binding as the name is only used
  99. // for lookup.
  100. Binding copy_binding = result->binding();
  101. copy_binding.mutable_ = false;
  102. copy_binding.can_be_deleted = false;
  103. copy_binding.initialized = true;
  104. return BindingAndIndex { copy_binding };
  105. }
  106. return DeclarativeEnvironment::find_binding_and_index(name);
  107. }
  108. void ModuleEnvironment::visit_edges(Visitor& visitor)
  109. {
  110. Base::visit_edges(visitor);
  111. for (auto& indirect_binding : m_indirect_bindings)
  112. visitor.visit(indirect_binding.module);
  113. }
  114. }