From 85cf80507f41b11167b95a8bb19083159543f6e5 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 7 Feb 2022 16:28:39 +0100 Subject: [PATCH] LibJS: Make ScriptOrModule use WeakPtr instead of raw pointers --- Userland/Libraries/LibJS/AST.cpp | 7 ++++--- Userland/Libraries/LibJS/CyclicModule.cpp | 4 ++-- Userland/Libraries/LibJS/Interpreter.cpp | 2 +- Userland/Libraries/LibJS/Module.h | 4 +++- Userland/Libraries/LibJS/Runtime/ExecutionContext.h | 2 +- Userland/Libraries/LibJS/Runtime/VM.cpp | 4 ++-- Userland/Libraries/LibJS/Script.h | 4 +++- Userland/Libraries/LibJS/SourceTextModule.cpp | 12 ++++++------ Userland/Libraries/LibJS/SyntheticModule.cpp | 2 +- 9 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 62ae748dc9..3bde7974b9 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -3175,9 +3175,10 @@ Completion MetaProperty::execute(Interpreter& interpreter, GlobalObject& global_ auto script_or_module = interpreter.vm().get_active_script_or_module(); // 2. Assert: module is a Source Text Module Record. - VERIFY(script_or_module.has()); - VERIFY(is(*script_or_module.get())); - auto& module = static_cast(*script_or_module.get()); + VERIFY(script_or_module.has>()); + VERIFY(script_or_module.get>()); + VERIFY(is(*script_or_module.get>())); + auto& module = static_cast(*script_or_module.get>()); // 3. Let importMeta be module.[[ImportMeta]]. auto* import_meta = module.import_meta(); diff --git a/Userland/Libraries/LibJS/CyclicModule.cpp b/Userland/Libraries/LibJS/CyclicModule.cpp index 9276d01be2..3414103e80 100644 --- a/Userland/Libraries/LibJS/CyclicModule.cpp +++ b/Userland/Libraries/LibJS/CyclicModule.cpp @@ -107,7 +107,7 @@ ThrowCompletionOr CyclicModule::inner_module_linking(VM& vm, Vectormake_weak_ptr(), required)); // b. Set index to ? InnerModuleLinking(requiredModule, stack, index). index = TRY(required_module->inner_module_linking(vm, stack, index)); @@ -310,7 +310,7 @@ ThrowCompletionOr CyclicModule::inner_module_evaluation(VM& vm, Vectormake_weak_ptr(), required)).ptr(); // b. NOTE: Link must be completed successfully prior to invoking this method, so every requested module is guaranteed to resolve successfully. // c. Set index to ? InnerModuleEvaluation(requiredModule, stack, index). diff --git a/Userland/Libraries/LibJS/Interpreter.cpp b/Userland/Libraries/LibJS/Interpreter.cpp index 8fda3bad4c..1e7bf35847 100644 --- a/Userland/Libraries/LibJS/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Interpreter.cpp @@ -63,7 +63,7 @@ ThrowCompletionOr Interpreter::run(Script& script_record) script_context.realm = &script_record.realm(); // 5. Set the ScriptOrModule of scriptContext to scriptRecord. - script_context.script_or_module = &script_record; + script_context.script_or_module = script_record.make_weak_ptr(); // 6. Set the VariableEnvironment of scriptContext to globalEnv. script_context.variable_environment = &global_environment; diff --git a/Userland/Libraries/LibJS/Module.h b/Userland/Libraries/LibJS/Module.h index 820f6af0e4..051f0cb7f7 100644 --- a/Userland/Libraries/LibJS/Module.h +++ b/Userland/Libraries/LibJS/Module.h @@ -53,7 +53,9 @@ struct ResolvedBinding { }; // 16.2.1.4 Abstract Module Records, https://tc39.es/ecma262/#sec-abstract-module-records -class Module : public RefCounted { +class Module + : public RefCounted + , public Weakable { public: virtual ~Module(); diff --git a/Userland/Libraries/LibJS/Runtime/ExecutionContext.h b/Userland/Libraries/LibJS/Runtime/ExecutionContext.h index 8f247666ca..a102982aaa 100644 --- a/Userland/Libraries/LibJS/Runtime/ExecutionContext.h +++ b/Userland/Libraries/LibJS/Runtime/ExecutionContext.h @@ -15,7 +15,7 @@ namespace JS { -using ScriptOrModule = Variant; +using ScriptOrModule = Variant, WeakPtr>; // 9.4 Execution Contexts, https://tc39.es/ecma262/#sec-execution-contexts struct ExecutionContext { diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp index f4e116c96e..1444a31974 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.cpp +++ b/Userland/Libraries/LibJS/Runtime/VM.cpp @@ -770,7 +770,7 @@ ThrowCompletionOr VM::link_and_eval_module(Module& module) dbgln("Warning: Using multiple modules as entry point can lead to unexpected results"); m_loaded_modules.empend( - &module, + module.make_weak_ptr(), module.filename(), String {}, // Null type module, @@ -836,7 +836,7 @@ ThrowCompletionOr> VM::resolve_imported_module(ScriptOrMod [&](Empty) { return "."sv; }, - [&](auto* script_or_module) { + [&](auto& script_or_module) { return script_or_module->filename(); }); diff --git a/Userland/Libraries/LibJS/Script.h b/Userland/Libraries/LibJS/Script.h index 782627e99f..6fa37e8dd3 100644 --- a/Userland/Libraries/LibJS/Script.h +++ b/Userland/Libraries/LibJS/Script.h @@ -16,7 +16,9 @@ namespace JS { // 16.1.4 Script Records, https://tc39.es/ecma262/#sec-script-records -class Script : public RefCounted