From 3dc5f467a8200b7c4043cf01d78b08745ddf8528 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 27 Nov 2023 16:45:45 +0100 Subject: [PATCH] LibJS: Always allocate ExecutionContext objects on the malloc heap Instead of allocating these in a mixture of ways, we now always put them on the malloc heap, and keep an intrusive linked list of them that we can iterate for GC marking purposes. --- .../Applications/Spreadsheet/Workbook.cpp | 16 +++--- Userland/Applications/Spreadsheet/Workbook.h | 2 +- Userland/Libraries/LibJS/AST.cpp | 2 +- .../Libraries/LibJS/Bytecode/Interpreter.cpp | 20 ++++---- Userland/Libraries/LibJS/Heap/Heap.cpp | 3 ++ Userland/Libraries/LibJS/Heap/Heap.h | 17 +++++++ .../Libraries/LibJS/JIT/NativeExecutable.cpp | 2 +- .../LibJS/Runtime/AbstractOperations.cpp | 20 ++++---- .../LibJS/Runtime/AbstractOperations.h | 4 +- .../Runtime/AsyncFunctionConstructor.cpp | 4 +- .../Runtime/AsyncFunctionDriverWrapper.cpp | 9 ++-- .../Runtime/AsyncFunctionDriverWrapper.h | 2 +- .../LibJS/Runtime/AsyncGenerator.cpp | 7 ++- .../Libraries/LibJS/Runtime/AsyncGenerator.h | 12 ++--- .../AsyncGeneratorFunctionConstructor.cpp | 4 +- .../Runtime/ECMAScriptFunctionObject.cpp | 44 ++++++++--------- .../LibJS/Runtime/ExecutionContext.cpp | 49 ++++++++++++------- .../LibJS/Runtime/ExecutionContext.h | 31 +++++++++--- .../LibJS/Runtime/FunctionConstructor.cpp | 4 +- .../LibJS/Runtime/FunctionPrototype.cpp | 3 +- .../Runtime/GeneratorFunctionConstructor.cpp | 4 +- .../LibJS/Runtime/GeneratorObject.cpp | 9 ++-- .../Libraries/LibJS/Runtime/GeneratorObject.h | 6 +-- .../LibJS/Runtime/NativeFunction.cpp | 44 ++++++++--------- Userland/Libraries/LibJS/Runtime/Realm.cpp | 2 +- .../Libraries/LibJS/Runtime/ShadowRealm.cpp | 16 +++--- .../Libraries/LibJS/Runtime/ShadowRealm.h | 10 ++-- .../LibJS/Runtime/ShadowRealmConstructor.cpp | 6 +-- Userland/Libraries/LibJS/Runtime/VM.cpp | 25 ---------- Userland/Libraries/LibJS/Runtime/VM.h | 3 +- Userland/Libraries/LibJS/Runtime/Value.h | 6 +++ .../LibJS/Runtime/WrappedFunction.cpp | 6 +-- Userland/Libraries/LibJS/SourceTextModule.cpp | 30 ++++++------ Userland/Libraries/LibJS/SourceTextModule.h | 14 +++--- Userland/Libraries/LibJS/SyntheticModule.cpp | 12 ++--- .../LibWeb/Bindings/MainThreadVM.cpp | 12 ++--- .../LibWeb/WebAssembly/WebAssembly.cpp | 2 +- .../LibWeb/WebIDL/AbstractOperations.cpp | 6 +-- 38 files changed, 251 insertions(+), 217 deletions(-) diff --git a/Userland/Applications/Spreadsheet/Workbook.cpp b/Userland/Applications/Spreadsheet/Workbook.cpp index ec53f15969..31bbce44da 100644 --- a/Userland/Applications/Spreadsheet/Workbook.cpp +++ b/Userland/Applications/Spreadsheet/Workbook.cpp @@ -23,7 +23,7 @@ Workbook::Workbook(Vector>&& sheets, GUI::Window& parent_wi : m_sheets(move(sheets)) , m_vm(JS::VM::create().release_value_but_fixme_should_propagate_errors()) , m_root_execution_context(JS::create_simple_execution_context(m_vm)) - , m_main_execution_context(m_vm->heap()) + , m_main_execution_context(JS::ExecutionContext::create(m_vm->heap())) , m_parent_window(parent_window) { auto& realm = *m_root_execution_context->realm; @@ -31,13 +31,13 @@ Workbook::Workbook(Vector>&& sheets, GUI::Window& parent_wi m_workbook_object = vm.heap().allocate(realm, realm, *this); realm.global_object().define_direct_property("workbook", workbook_object(), JS::default_attributes); - m_main_execution_context.this_value = &realm.global_object(); - m_main_execution_context.function_name = "(global execution context)"sv; - m_main_execution_context.lexical_environment = &realm.global_environment(); - m_main_execution_context.variable_environment = &realm.global_environment(); - m_main_execution_context.realm = &realm; - m_main_execution_context.is_strict_mode = true; - m_vm->push_execution_context(m_main_execution_context); + m_main_execution_context->this_value = &realm.global_object(); + m_main_execution_context->function_name = JS::PrimitiveString::create(vm, "(global execution context)"sv); + m_main_execution_context->lexical_environment = &realm.global_environment(); + m_main_execution_context->variable_environment = &realm.global_environment(); + m_main_execution_context->realm = &realm; + m_main_execution_context->is_strict_mode = true; + m_vm->push_execution_context(*m_main_execution_context); m_vm->enable_default_host_import_module_dynamically_hook(); } diff --git a/Userland/Applications/Spreadsheet/Workbook.h b/Userland/Applications/Spreadsheet/Workbook.h index b6bf8ac59e..864eba764a 100644 --- a/Userland/Applications/Spreadsheet/Workbook.h +++ b/Userland/Applications/Spreadsheet/Workbook.h @@ -47,7 +47,7 @@ private: NonnullOwnPtr m_root_execution_context; JS::GCPtr m_workbook_object; - JS::ExecutionContext m_main_execution_context; + NonnullOwnPtr m_main_execution_context; GUI::Window& m_parent_window; DeprecatedString m_current_filename; diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index 0f911e3322..b7ae469ccc 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -1666,7 +1666,7 @@ void ScopeNode::block_declaration_instantiation(VM& vm, Environment* environment // iii. Perform ! env.InitializeBinding(fn, fo). NOTE: This step is replaced in section B.3.2.6. if (function_declaration.name_identifier()->is_local()) { - vm.running_execution_context().local_variables[function_declaration.name_identifier()->local_variable_index()] = function; + vm.running_execution_context().local(function_declaration.name_identifier()->local_variable_index()) = function; } else { VERIFY(is(*environment)); static_cast(*environment).initialize_or_set_mutable_binding({}, vm, function_declaration.name(), function); diff --git a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp index 2b5b8a5b20..78d3f65b37 100644 --- a/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp +++ b/Userland/Libraries/LibJS/Bytecode/Interpreter.cpp @@ -65,36 +65,36 @@ ThrowCompletionOr Interpreter::run(Script& script_record, JS::GCPtrrealm = &script_record.realm(); // 5. Set the ScriptOrModule of scriptContext to scriptRecord. - script_context.script_or_module = NonnullGCPtr