Truly independent web browser
LibJS: Read the realm and ScriptOrModule of a context inline
Andreas Klingcd75542c0942
diff --git a/Libraries/LibJS/Runtime/ExecutionContext.cpp b/Libraries/LibJS/Runtime/ExecutionContext.cppindex 28e9a463a0a..f34cf3f9c96 100644--- a/Libraries/LibJS/Runtime/ExecutionContext.cpp+++ b/Libraries/LibJS/Runtime/ExecutionContext.cpp@@ -4,8 +4,11 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <LibGC/CompressedPointer.h>#include <LibJS/EmbeddingABIConversions.h>#include <LibJS/Runtime/ExecutionContext.h>+#include <LibJS/Runtime/FunctionObject.h>+#include <LibJS/Runtime/Shape.h>namespace JS {@@ -47,8 +50,11 @@ SourceCode const* ExecutionContext::source_code() constreturn reinterpret_cast<SourceCode const*>(js_source_code_of_execution_context(execution_context_to_abi(*this)));}-GC::Ptr<Realm> ExecutionContext::realm() const+GC::Ptr<Realm> ExecutionContext::realm_of_function() const{+ // NB: An ECMAScript function runs in the realm of its shape.+ if (function->is_ecmascript_function_object())+ return &function->shape().realm();return cell_from_abi<Realm>(js_execution_context_realm(execution_context_to_abi(*this)));}@@ -59,9 +65,20 @@ void ExecutionContext::set_realm(GC::Ptr<Realm> realm)realm_without_function = realm;}-ScriptOrModule ExecutionContext::script_or_module() const+ScriptOrModule ExecutionContext::script_or_module_of_function() const{- return bit_cast<ScriptOrModule>(js_execution_context_script_or_module(execution_context_to_abi(*this)));+ // NB: Only ECMAScript functions have a [[ScriptOrModule]], which their shared data holds.+ if (!function->is_ecmascript_function_object())+ return Empty {};+ auto const* function_bytes = reinterpret_cast<u8 const*>(function.ptr());+ static_assert(JS_LAYOUT_ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA_SIZE == sizeof(u32));+ u32 compressed_shared_data;+ __builtin_memcpy(&compressed_shared_data, function_bytes + JS_LAYOUT_ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA_OFFSET, sizeof(compressed_shared_data));+ auto const* shared_data_bytes = reinterpret_cast<u8 const*>(GC::decompress_nonnull(compressed_shared_data));+ static_assert(JS_LAYOUT_SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE_SIZE == sizeof(ScriptOrModule));+ ScriptOrModule script_or_module;+ __builtin_memcpy(&script_or_module, shared_data_bytes + JS_LAYOUT_SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE_OFFSET, sizeof(script_or_module));+ return script_or_module;}void ExecutionContext::set_script_or_module(ScriptOrModule script_or_module)diff --git a/Libraries/LibJS/Runtime/ExecutionContext.h b/Libraries/LibJS/Runtime/ExecutionContext.hindex c55d98abd6e..f702c767673 100644--- a/Libraries/LibJS/Runtime/ExecutionContext.h+++ b/Libraries/LibJS/Runtime/ExecutionContext.h@@ -199,13 +199,27 @@ struct JS_API ExecutionContext {SourceCode const* source_code() const;// [[Realm]]- GC::Ptr<Realm> realm() const;+ GC::Ptr<Realm> realm() const+ {+ if (!function)+ return realm_without_function;+ return realm_of_function();+ }void set_realm(GC::Ptr<Realm>);// [[ScriptOrModule]]- ScriptOrModule script_or_module() const;+ ScriptOrModule script_or_module() const+ {+ if (!function)+ return script_or_module_without_function;+ return script_or_module_of_function();+ }void set_script_or_module(ScriptOrModule);Utf16FlyString function_name() const;++private:+ GC::Ptr<Realm> realm_of_function() const;+ ScriptOrModule script_or_module_of_function() const;};static_assert(IsTriviallyDestructible<ExecutionContext>);diff --git a/Libraries/LibJS/Rust/generate/layout_header.rs b/Libraries/LibJS/Rust/generate/layout_header.rsindex 0d8012275df..01462027784 100644--- a/Libraries/LibJS/Rust/generate/layout_header.rs+++ b/Libraries/LibJS/Rust/generate/layout_header.rs@@ -20,6 +20,7 @@ use crate::class_id::{ALL_CLASS_IDS, CLASS_COUNT};use crate::layout::cell::*;use crate::layout::class::*;use crate::layout::execution_context::*;+use crate::layout::function_object::*;use crate::layout::host_class::*;use crate::layout::host_object::*;use crate::layout::object::*;@@ -33,6 +34,7 @@ const STATIC_ASSERTIONS_PREAMBLE: &str = "\use crate::layout::cell::*;use crate::layout::class::*;use crate::layout::execution_context::*;+use crate::layout::function_object::*;use crate::layout::host_object::*;use crate::layout::object::*;use crate::layout::primitive_string::*;@@ -317,6 +319,17 @@ pub fn generate() -> LayoutHeader {offset!(w, "SHAPE_REALM", Shape, realm);offset!(w, "SHAPE_PROTOTYPE", Shape, prototype);+ w.section("ECMAScript functions, whose [[ScriptOrModule]] is in their shared data");+ w.define(+ "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA_OFFSET",+ ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA_OFFSET,+ );+ w.define(+ "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA_SIZE",+ size_of_field(|fields: &EcmascriptFunctionFields| &raw const fields.shared_data),+ );+ offset!(w, "SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE", SharedFunctionInstanceData, script_or_module);+w.section("Realm");offset!(w, "REALM_GLOBAL_OBJECT", Realm, global_object);offset!(w, "REALM_GLOBAL_ENVIRONMENT", Realm, global_environment);diff --git a/Libraries/LibJS/Rust/src/embedding/execution_context.rs b/Libraries/LibJS/Rust/src/embedding/execution_context.rsindex 29b5375616b..73d6b30dcb1 100644--- a/Libraries/LibJS/Rust/src/embedding/execution_context.rs+++ b/Libraries/LibJS/Rust/src/embedding/execution_context.rs@@ -9,8 +9,10 @@//! A JSExecutionContext is the runtime's ExecutionContext. LibJS/Embedding/Layout.h gives the offset and size of each//! of its fields as JS_LAYOUT_EXECUTION_CONTEXT_*, and its value slots, the arguments last, follow it directly. An//! embedder reads and writes the fields there, among them the this value and the skip-when-determining-incumbent-//! counter. It reads the realm and the ScriptOrModule with js_execution_context_realm() and-//! js_execution_context_script_or_module(), because those of a context with a function come from the function. It also finds the running context at+//! counter. The realm and the ScriptOrModule of a context with a function come from the function: it reads the realm+//! of an ECMAScript function from its shape and that of another function with js_execution_context_realm(), and the+//! ScriptOrModule of an ECMAScript function from its shared data at+//! JS_LAYOUT_SHARED_FUNCTION_INSTANCE_DATA_SCRIPT_OR_MODULE_OFFSET. It also finds the running context at//! JS_LAYOUT_VM_RUNNING_EXECUTION_CONTEXT_OFFSET in the VM, and with it the current realm and the arguments and this//! value of the running code, without a call.//!@@ -41,12 +43,6 @@ pub struct JSScriptOrModule(u64);const _: () = assert!(size_of::<JSScriptOrModule>() == size_of::<ScriptOrModuleSlot>());-impl From<ScriptOrModule> for JSScriptOrModule {- fn from(script_or_module: ScriptOrModule) -> Self {- Self(ScriptOrModuleSlot::new(script_or_module).word())- }-}-/// # Safety////// `script_or_module` must be empty or name a live Script or Module.@@ -235,22 +231,6 @@ pub unsafe extern "C" fn js_execution_context_realm(execution_context: *const JSoptional_cell_into_abi::<JSRealm>(execution_context.realm())}-/// ExecutionContext::script_or_module(): the [[ScriptOrModule]] of `execution_context`. That of a context with a-/// function comes from the function, and the field holds it only for a context without one.-///-/// # Safety-///-/// `execution_context` must be a live execution context. Must be called on the thread that runs the VM.-#[unsafe(no_mangle)]-pub unsafe extern "C" fn js_execution_context_script_or_module(- execution_context: *const JSExecutionContext,-) -> JSScriptOrModule {- assert!(!execution_context.is_null(), "the embedder passes an execution context");- // SAFETY: The caller passes a live execution context.- let execution_context = unsafe { &*execution_context.cast::<ExecutionContext>() };- execution_context.script_or_module().into()-}-/// ExecutionContext::function_name(): the name of the function whose bytecode the context runs, as an owned/// AK::Utf16String the caller adopts with AK::Utf16String::adopt_raw(). It is empty for a context that runs no/// bytecode, such as that of a native function, and for code that is not the body of a function.