diff --git a/Libraries/LibJS/Bytecode/Interpreter.cpp b/Libraries/LibJS/Bytecode/Interpreter.cpp index 21d87eb0a73..13096972e54 100644 --- a/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024, Andreas Kling + * Copyright (c) 2021-2025, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -1132,10 +1133,10 @@ inline ThrowCompletionOr get_global(Interpreter& interpreter, IdentifierT auto& identifier = interpreter.current_executable().get_identifier(identifier_index); - if (vm.running_execution_context().script_or_module.has>()) { + if (auto* module = vm.running_execution_context().script_or_module.get_pointer>()) { // NOTE: GetGlobal is used to access variables stored in the module environment and global environment. // The module environment is checked first since it precedes the global environment in the environment chain. - auto& module_environment = *vm.running_execution_context().script_or_module.get>()->environment(); + auto& module_environment = *(*module)->environment(); if (TRY(module_environment.has_binding(identifier))) { // TODO: Cache offset of binding value return TRY(module_environment.get_binding_value(vm, identifier, vm.in_strict_mode())); diff --git a/Libraries/LibJS/Forward.h b/Libraries/LibJS/Forward.h index 15fffcdb700..b5f4a53f737 100644 --- a/Libraries/LibJS/Forward.h +++ b/Libraries/LibJS/Forward.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, Andreas Kling + * Copyright (c) 2020-2025, Andreas Kling * Copyright (c) 2020-2022, Linus Groh * * SPDX-License-Identifier: BSD-2-Clause @@ -189,6 +189,7 @@ class Intrinsics; class IteratorRecord; class MemberExpression; class MetaProperty; +class ModuleEnvironment; class Module; struct ModuleRequest; class NativeFunction; diff --git a/Libraries/LibJS/Module.cpp b/Libraries/LibJS/Module.cpp index 9c54ba5273e..3738c490339 100644 --- a/Libraries/LibJS/Module.cpp +++ b/Libraries/LibJS/Module.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2025, Andreas Kling * Copyright (c) 2022, David Tuin * Copyright (c) 2023, networkException * @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/Libraries/LibJS/Module.h b/Libraries/LibJS/Module.h index 1704e9e7e8b..8aa3da256bf 100644 --- a/Libraries/LibJS/Module.h +++ b/Libraries/LibJS/Module.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021-2025, Andreas Kling * Copyright (c) 2022, David Tuin * * SPDX-License-Identifier: BSD-2-Clause @@ -100,7 +100,7 @@ public: StringView filename() const { return m_filename; } - Environment* environment() { return m_environment; } + GC::Ptr environment() { return m_environment; } Script::HostDefined* host_defined() const { return m_host_defined; } @@ -122,7 +122,7 @@ protected: virtual void visit_edges(Cell::Visitor&) override; - void set_environment(Environment* environment) + void set_environment(GC::Ref environment) { m_environment = environment; } @@ -136,7 +136,7 @@ private: // stores modules with a RefPtr we cannot just store the VM as that leads to // cycles. GC::Ptr m_realm; // [[Realm]] - GC::Ptr m_environment; // [[Environment]] + GC::Ptr m_environment; // [[Environment]] GC::Ptr m_namespace; // [[Namespace]] Script::HostDefined* m_host_defined { nullptr }; // [[HostDefined]] diff --git a/Libraries/LibJS/Runtime/ModuleEnvironment.cpp b/Libraries/LibJS/Runtime/ModuleEnvironment.cpp index d663da1e1fa..a3e69ff51c4 100644 --- a/Libraries/LibJS/Runtime/ModuleEnvironment.cpp +++ b/Libraries/LibJS/Runtime/ModuleEnvironment.cpp @@ -34,7 +34,7 @@ ThrowCompletionOr ModuleEnvironment::get_binding_value(VM& vm, Deprecated // a. Let M and N2 be the indirection values provided when this binding for N was created. // b. Let targetEnv be M.[[Environment]]. - auto* target_env = indirect_binding->module->environment(); + auto target_env = indirect_binding->module->environment(); // c. If targetEnv is empty, throw a ReferenceError exception. if (!target_env) @@ -97,11 +97,10 @@ Optional ModuleEnvironment::find_binding_and { auto* indirect_binding = get_indirect_binding(name); if (indirect_binding != nullptr) { - auto* target_env = indirect_binding->module->environment(); + auto target_env = indirect_binding->module->environment(); if (!target_env) return {}; - VERIFY(is(target_env)); auto& target_module_environment = static_cast(*target_env); auto result = target_module_environment.find_binding_and_index(indirect_binding->binding_name); if (!result.has_value()) diff --git a/Libraries/LibJS/Runtime/ModuleNamespaceObject.cpp b/Libraries/LibJS/Runtime/ModuleNamespaceObject.cpp index e36c953741b..3aec92d5741 100644 --- a/Libraries/LibJS/Runtime/ModuleNamespaceObject.cpp +++ b/Libraries/LibJS/Runtime/ModuleNamespaceObject.cpp @@ -6,6 +6,7 @@ #include #include +#include #include namespace JS { @@ -173,7 +174,7 @@ ThrowCompletionOr ModuleNamespaceObject::internal_get(PropertyKey const& } // 10. Let targetEnv be targetModule.[[Environment]]. - auto* target_environment = target_module->environment(); + auto target_environment = target_module->environment(); // 11. If targetEnv is empty, throw a ReferenceError exception. if (!target_environment)