From 9df7bf79ccd5fb306817e45c15d34ce399fb7114 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 8 Aug 2023 07:05:26 +0200 Subject: [PATCH] LibJS: Add create_simple_execution_context() helper This makes it easy to set up a realm, global object and root execution context with a single call to LibJS. It will be useful to basically everyone except LibWeb. --- Userland/Libraries/LibJS/Runtime/VM.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Userland/Libraries/LibJS/Runtime/VM.h b/Userland/Libraries/LibJS/Runtime/VM.h index 6168f542cc..4dab5e2717 100644 --- a/Userland/Libraries/LibJS/Runtime/VM.h +++ b/Userland/Libraries/LibJS/Runtime/VM.h @@ -344,4 +344,16 @@ private: OwnPtr m_bytecode_interpreter; }; +template +[[nodiscard]] static NonnullOwnPtr create_simple_execution_context(VM& vm, Args&&... args) +{ + auto root_execution_context = MUST(Realm::initialize_host_defined_realm( + vm, + [&](Realm& realm_) -> GlobalObject* { + return vm.heap().allocate_without_realm(realm_, forward(args)...); + }, + nullptr)); + return root_execution_context; +} + }