Skip to content

Truly independent web browser

LibJS: Let direct getters return an implementation's member fields

Andreas Kling85df777bef40
diff --git a/Libraries/LibGC/Member.h b/Libraries/LibGC/Member.h
index c9f46b0b546..f2eb6c19dc9 100644
--- a/Libraries/LibGC/Member.h
+++ b/Libraries/LibGC/Member.h
@@ -300,6 +300,24 @@ private:
u32 m_compressed;
};
+// The byte offset of a member in a cell, for code that loads the field directly and so has to know that it holds a
+// compressed pointer.
+struct MemberOffset {
+ size_t value { 0 };
+};
+
+template<typename FieldType>
+constexpr auto field_offset(size_t offset)
+{
+ if constexpr (Detail::IsMember<FieldType>)
+ return MemberOffset { offset };
+ else
+ return offset;
+}
+
+// The offset of a field of a cell, as a MemberOffset if the field is a member, and as a size_t otherwise.
+#define GC_FIELD_OFFSET(Type, field) ::GC::field_offset<RemoveCV<decltype(Type::field)>>(offsetof(Type, field))
+
namespace Detail {
// Whether a member of T that holds `compressed` points to `other`, as a comparison of T* with U* would tell.
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
index 01f8516f973..e07a751f513 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/aarch64.S
@@ -14075,8 +14075,18 @@ asm_handler_GetById_cold:
ldr x5, [x5, x4, lsl #3]
cmp x5, x3
b.ne .Lasm_GetById.ssa_block_3
- ldr w1, [x1, #100]
- ldr x0, [x0, x1, lsl #3]
+ ldr w3, [x1, #100]
+ ldr w1, [x1, #112]
+ adds x0, x0, x3
+ cbz w1, .Lasm_GetById.ssa_block_56
+ ldr w0, [x0]
+ add x9, x24, w0, uxtw #3
+ cmp w0, #0
+ csel x0, x9, xzr, ne
+ b .Lasm_GetById.ssa_block_57
+.Lasm_GetById.ssa_block_56:
+ ldr x0, [x0]
+.Lasm_GetById.ssa_block_57:
cbnz x0, .Lasm_GetById.ssa_block_50
movz x0, #0x7fff, lsl #48
b .Lasm_GetById.ssa_block_51
diff --git a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
index 117c71f3d62..a4736c48876 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
+++ b/Libraries/LibJS/Flap/tests/interpreter-assembly/x86_64.S
@@ -13726,8 +13726,19 @@ asm_handler_GetById_cold:
mov r8, QWORD PTR [r8 + rdi * 8]
cmp r8, rsi
jne .Lasm_GetById.ssa_block_3
- mov ecx, DWORD PTR [rcx + 100]
- mov rax, QWORD PTR [rax + rcx * 8]
+ mov esi, DWORD PTR [rcx + 100]
+ mov ecx, DWORD PTR [rcx + 112]
+ add rax, rsi
+ test ecx, ecx
+ jz .Lasm_GetById.ssa_block_56
+ mov eax, DWORD PTR [rax]
+ lea r11, [r15 + rax * 8]
+ test eax, eax
+ cmovnz rax, r11
+ jmp .Lasm_GetById.ssa_block_57
+.Lasm_GetById.ssa_block_56:
+ mov rax, QWORD PTR [rax]
+.Lasm_GetById.ssa_block_57:
test rax, rax
jnz .Lasm_GetById.ssa_block_50
movabs rax, 9223090561878065152
diff --git a/Libraries/LibJS/Flap/tests/interpreter-layout.conf b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
index 621aeaaa043..bd862c6262e 100644
--- a/Libraries/LibJS/Flap/tests/interpreter-layout.conf
+++ b/Libraries/LibJS/Flap/tests/interpreter-layout.conf
@@ -360,12 +360,14 @@ field NativeFunctionTableEntry.type u32 NATIVE_FUNCTION_TABLE_ENTRY_TYPE nullabl
# DirectGetterFunction layout
const DIRECT_GETTER_FUNCTION_WRAPPER_IMPLEMENTATION_WORD_OFFSET = 96
field DirectGetterFunction.wrapper_implementation_word_offset u32 DIRECT_GETTER_FUNCTION_WRAPPER_IMPLEMENTATION_WORD_OFFSET nullable scalar
-const DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_WORD_OFFSET = 100
-field DirectGetterFunction.implementation_value_word_offset u32 DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_WORD_OFFSET nullable scalar
+const DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_OFFSET = 100
+field DirectGetterFunction.implementation_value_offset u32 DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_OFFSET nullable scalar
const DIRECT_GETTER_FUNCTION_MAIN_WORLD_WRAPPER_WORD_OFFSET = 104
field DirectGetterFunction.main_world_wrapper_word_offset u32 DIRECT_GETTER_FUNCTION_MAIN_WORLD_WRAPPER_WORD_OFFSET nullable scalar
const DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET = 108
field DirectGetterFunction.weak_impl_value_word_offset u32 DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET nullable scalar
+const DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_IS_COMPRESSED = 112
+field DirectGetterFunction.implementation_value_is_compressed u32 DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_IS_COMPRESSED nullable scalar
# ECMAScriptFunctionObject layout
const ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA = 68
diff --git a/Libraries/LibJS/Interpreter/interpreter.flap b/Libraries/LibJS/Interpreter/interpreter.flap
index d984c9b6904..8b432f4ecb2 100644
--- a/Libraries/LibJS/Interpreter/interpreter.flap
+++ b/Libraries/LibJS/Interpreter/interpreter.flap
@@ -51,6 +51,19 @@ inline fn box_object(object: Object) -> Value<Object> {
value
}
+# The field a direct getter returns is a compressed cell pointer for a GC::Member, and a full one otherwise.
+inline fn load_direct_getter_implementation_value(address: u64, is_compressed: u32) -> u64 {
+ if is_compressed != 0 {
+ let mut value: u64;
+ load_compressed_cell_ptr(value, [address]);
+ value
+ } else {
+ let mut value: u64;
+ load64(value, [address]);
+ value
+ }
+}
+
inline fn load_direct_getter_value(
function: Object,
receiver_object: Object,
@@ -73,7 +86,11 @@ inline fn load_direct_getter_value(
let receiver_pointer: u64 = alias(receiver_object);
guard cached_receiver == receiver_pointer else failure;
- let result_implementation = implementation_words[direct_getter.implementation_value_word_offset];
+ let result_implementation_address: u64 = implementation + alias(direct_getter.implementation_value_offset);
+ let result_implementation = load_direct_getter_implementation_value(
+ result_implementation_address,
+ direct_getter.implementation_value_is_compressed
+ );
if result_implementation == 0 {
let mut null_value: Value;
mov(null_value, NULL_VALUE);
diff --git a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
index ab070ab02f8..9256947f657 100644
--- a/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
+++ b/Libraries/LibJS/JIT/Rust/src/builder/tests.rs
@@ -1065,6 +1065,7 @@ pub(crate) mod property_access {
direct_getter: validated.then_some(DirectGetterFields {
wrapper_implementation: 8,
implementation_value: 16,
+ implementation_value_is_compressed: false,
main_world_wrapper: 24,
weak_impl_value: 32,
}),
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
index a67370ee528..fc4cc96c52d 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/call.rs
@@ -1314,14 +1314,14 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
self.masm.load64(temp, &field(temp, fields.weak_impl_value));
self.masm.branch64(Condition::NotEqual, temp, receiver, call);
- let not_null = self.masm.new_label();
- self.masm
- .load64(implementation, &field(implementation, fields.implementation_value));
- self.masm.branch64_imm(Condition::NotEqual, implementation, 0, not_null);
- self.masm.move_imm64(M::RETURN_GPRS[0], value::NULL);
- self.masm.jump(done);
-
- self.masm.bind(not_null);
+ let null = self.masm.new_label();
+ let value = field(implementation, fields.implementation_value);
+ if fields.implementation_value_is_compressed {
+ self.emit_load_compressed_cell_or_branch(implementation, &value, null);
+ } else {
+ self.masm.load64(implementation, &value);
+ self.masm.branch64_imm(Condition::Equal, implementation, 0, null);
+ }
self.masm
.load64(temp, &field(implementation, fields.main_world_wrapper));
self.masm.branch64_imm(Condition::Equal, temp, 0, call);
@@ -1336,6 +1336,10 @@ impl<M: PortableMacroAssembler> Codegen<'_, M> {
);
self.box_cell_with_tag(M::RETURN_GPRS[0], temp, value::OBJECT_TAG);
self.masm.jump(done);
+
+ self.masm.bind(null);
+ self.masm.move_imm64(M::RETURN_GPRS[0], value::NULL);
+ self.masm.jump(done);
self.masm.bind(call);
}
diff --git a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
index 5ec038b18e9..7a3748273b6 100644
--- a/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
+++ b/Libraries/LibJS/JIT/Rust/src/codegen/tests/lowerings.rs
@@ -3046,14 +3046,24 @@ extern "C" fn fake_native_getter(_vm: u64) -> FakeNativeResult {
#[test]
fn get_by_id_reads_direct_getter_wrappers_inline() {
+ get_by_id_reads_direct_getter_wrappers_inline_from(false);
+}
+
+#[test]
+fn get_by_id_reads_direct_getter_wrappers_of_compressed_fields_inline() {
+ get_by_id_reads_direct_getter_wrappers_inline_from(true);
+}
+
+fn get_by_id_reads_direct_getter_wrappers_inline_from(compressed_field: bool) {
use crate::builder::tests::property_access::{cache, own};
use crate::snapshot::{
AccessorFunctionSnapshot, CellId, DirectGetterFields, NativeCallTarget, PropertyCacheEntryType,
};
// A wrapper whose implementation is in its word 10, with an accessor
- // property 1 whose getter reads the implementation's word 2, and the
- // main world wrapper of that through the implementation's word 1 and
- // the weak reference's word 1.
+ // property 1 whose getter reads the implementation's word 2 (or, as a
+ // compressed pointer, the high half of it), and the main world wrapper
+ // of that through the implementation's word 1 and the weak reference's
+ // word 1.
let function = low_cell(&[0; 4]);
let mut accessor = vec![0u64; 3];
accessor[1] = compressed(function);
@@ -3061,9 +3071,14 @@ fn get_by_id_reads_direct_getter_wrappers_inline() {
let mut fake = FakeCachedObject::new(&[value::int32(10), accessor_value]);
let mut result = vec![0u64; 2];
let mut result_weak = vec![0, result.as_ptr() as u64];
- let result_implementation = [0, result_weak.as_ptr() as u64, 0];
+ let result_implementation = low_cell(&[0, result_weak.as_ptr() as u64, 0]);
+ let result_implementation_field = if compressed_field {
+ compressed(result_implementation) << 32
+ } else {
+ result_implementation.as_ptr() as u64
+ };
let mut weak = vec![0, fake.object.as_ptr() as u64];
- let mut implementation = vec![0, weak.as_ptr() as u64, result_implementation.as_ptr() as u64];
+ let mut implementation = vec![0, weak.as_ptr() as u64, result_implementation_field];
fake.object[1] = u64::from(FLAG_IS_PLATFORM_OBJECT);
fake.object[10] = implementation.as_ptr() as u64;
let target = NativeCallTarget {
@@ -3072,7 +3087,8 @@ fn get_by_id_reads_direct_getter_wrappers_inline() {
entry: fake_native_getter as *const () as u64,
direct_getter: Some(DirectGetterFields {
wrapper_implementation: 80,
- implementation_value: 16,
+ implementation_value: if compressed_field { 20 } else { 16 },
+ implementation_value_is_compressed: compressed_field,
main_world_wrapper: 8,
weak_impl_value: 8,
}),
@@ -3113,7 +3129,7 @@ fn get_by_id_reads_direct_getter_wrappers_inline() {
assert_eq!(get(&fake), object_value(&result));
implementation[2] = 0;
assert_eq!(get(&fake), value::NULL);
- implementation[2] = result_implementation.as_ptr() as u64;
+ implementation[2] = result_implementation_field;
// The getter runs for a global object (which it returns as its window
// proxy), an implementation without a main world wrapper, a receiver
diff --git a/Libraries/LibJS/JIT/Rust/src/snapshot.rs b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
index dd43d57f9c8..7d8792016c3 100644
--- a/Libraries/LibJS/JIT/Rust/src/snapshot.rs
+++ b/Libraries/LibJS/JIT/Rust/src/snapshot.rs
@@ -615,11 +615,14 @@ pub struct NativeCallTarget {
/// wrapper, the field of the implementation the attribute returns, the weak
/// reference of an implementation to its main world wrapper, and the cell of
/// that weak reference. The getter returns the main world wrapper of the
-/// field's implementation, or null for a null field.
+/// field's implementation, or null for a null field. The field holds a
+/// compressed cell pointer if `implementation_value_is_compressed`, and a
+/// full one otherwise.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DirectGetterFields {
pub wrapper_implementation: u32,
pub implementation_value: u32,
+ pub implementation_value_is_compressed: bool,
pub main_world_wrapper: u32,
pub weak_impl_value: u32,
}
diff --git a/Libraries/LibJS/Runtime/NativeFunction.cpp b/Libraries/LibJS/Runtime/NativeFunction.cpp
index 2e13b26bd66..593f222b4ab 100644
--- a/Libraries/LibJS/Runtime/NativeFunction.cpp
+++ b/Libraries/LibJS/Runtime/NativeFunction.cpp
@@ -14,12 +14,6 @@ namespace JS {
using namespace EmbeddingABI;
-static_assert(sizeof(DirectGetterConfiguration) == sizeof(JSDirectGetterConfiguration));
-static_assert(offsetof(DirectGetterConfiguration, wrapper_implementation_offset) == offsetof(JSDirectGetterConfiguration, wrapper_implementation_offset));
-static_assert(offsetof(DirectGetterConfiguration, implementation_value_offset) == offsetof(JSDirectGetterConfiguration, implementation_value_offset));
-static_assert(offsetof(DirectGetterConfiguration, main_world_wrapper_offset) == offsetof(JSDirectGetterConfiguration, main_world_wrapper_offset));
-static_assert(offsetof(DirectGetterConfiguration, weak_impl_value_offset) == offsetof(JSDirectGetterConfiguration, weak_impl_value_offset));
-
// A closure's behaviour lives in a cell of the embedder's heap, which the function keeps alive, and whose captures the
// garbage collector scans conservatively.
using NativeFunctionBehaviour = GC::Function<ThrowCompletionOr<Value>(VM&)>;
@@ -70,9 +64,10 @@ GC::Ref<DirectGetterFunction> DirectGetterFunction::create(Realm& realm, NativeF
{
JSDirectGetterConfiguration abi_configuration {
.wrapper_implementation_offset = configuration.wrapper_implementation_offset,
- .implementation_value_offset = configuration.implementation_value_offset,
+ .implementation_value_offset = configuration.implementation_value_offset.offset,
.main_world_wrapper_offset = configuration.main_world_wrapper_offset,
.weak_impl_value_offset = configuration.weak_impl_value_offset,
+ .implementation_value_is_compressed = configuration.implementation_value_offset.is_compressed,
};
PrefixForABI abi_prefix { prefix };
auto* function = js_function_create_direct_getter(vm_to_abi(realm.vm()), cell_to_abi<JSRealm>(realm), native_function_to_abi(behaviour), length, property_key_to_abi(name), &abi_configuration, abi_prefix.characters, abi_prefix.length);
diff --git a/Libraries/LibJS/Runtime/NativeFunction.h b/Libraries/LibJS/Runtime/NativeFunction.h
index 5f98a9e09a1..8e2f8a7df39 100644
--- a/Libraries/LibJS/Runtime/NativeFunction.h
+++ b/Libraries/LibJS/Runtime/NativeFunction.h
@@ -10,6 +10,7 @@
#include <AK/Badge.h>
#include <AK/Function.h>
#include <AK/Optional.h>
+#include <LibGC/Member.h>
#include <LibJS/Export.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/FunctionObject.h>
@@ -33,14 +34,31 @@ public:
static bool is_engine_class_of(Object const& object) { return object.has_engine_object_flag(JS_LAYOUT_OBJECT_FLAG_IS_RAW_NATIVE_FUNCTION); }
};
+// The byte offset of the field of an implementation that a direct getter returns, which holds either a full-width
+// pointer or a member (GC::Member, a compressed pointer).
+struct DirectGetterValueOffset {
+ constexpr DirectGetterValueOffset(size_t offset)
+ : offset(offset)
+ {
+ }
+
+ constexpr DirectGetterValueOffset(GC::MemberOffset member_offset)
+ : offset(member_offset.value)
+ , is_compressed(true)
+ {
+ }
+
+ size_t offset { 0 };
+ bool is_compressed { false };
+};
+
struct DirectGetterConfiguration {
- // These are byte offsets to pointer-sized fields read directly by the interpreter. The bindings
- // generator obtains them from HostObject::wrappable_offset(), the implementation
- // field's generated offset helper, Wrappable::main_world_wrapper_offset(), and
- // GC::WeakImpl::value_offset(), respectively. DirectGetterFunction validates their alignment and
- // converts them to word offsets.
+ // These are byte offsets to fields read directly by the interpreter. The bindings generator obtains them from
+ // HostObject::wrappable_offset(), the implementation field's generated offset helper,
+ // Wrappable::main_world_wrapper_offset(), and GC::WeakImpl::value_offset(), respectively. The implementation value is
+ // a pointer-sized field or a member, the others are pointer-sized. DirectGetterFunction validates their alignment.
size_t wrapper_implementation_offset { 0 };
- size_t implementation_value_offset { 0 };
+ DirectGetterValueOffset implementation_value_offset { 0 };
size_t main_world_wrapper_offset { 0 };
size_t weak_impl_value_offset { 0 };
};
diff --git a/Libraries/LibJS/Rust/generate/layout_table.rs b/Libraries/LibJS/Rust/generate/layout_table.rs
index 873bdbd0daa..bcf472ddfba 100644
--- a/Libraries/LibJS/Rust/generate/layout_table.rs
+++ b/Libraries/LibJS/Rust/generate/layout_table.rs
@@ -485,9 +485,10 @@ pub fn generate(configuration: &LayoutConfiguration) -> LayoutWriter {
w.section("DirectGetterFunction layout");
field!(w, "DIRECT_GETTER_FUNCTION_WRAPPER_IMPLEMENTATION_WORD_OFFSET", "DirectGetterFunction.wrapper_implementation_word_offset", "u32", DirectGetterFunction, wrapper_implementation_word_offset, 4, "nullable", "scalar");
- field!(w, "DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_WORD_OFFSET", "DirectGetterFunction.implementation_value_word_offset", "u32", DirectGetterFunction, implementation_value_word_offset, 4, "nullable", "scalar");
+ field!(w, "DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_OFFSET", "DirectGetterFunction.implementation_value_offset", "u32", DirectGetterFunction, implementation_value_offset, 4, "nullable", "scalar");
field!(w, "DIRECT_GETTER_FUNCTION_MAIN_WORLD_WRAPPER_WORD_OFFSET", "DirectGetterFunction.main_world_wrapper_word_offset", "u32", DirectGetterFunction, main_world_wrapper_word_offset, 4, "nullable", "scalar");
field!(w, "DIRECT_GETTER_FUNCTION_WEAK_IMPL_VALUE_WORD_OFFSET", "DirectGetterFunction.weak_impl_value_word_offset", "u32", DirectGetterFunction, weak_impl_value_word_offset, 4, "nullable", "scalar");
+ field!(w, "DIRECT_GETTER_FUNCTION_IMPLEMENTATION_VALUE_IS_COMPRESSED", "DirectGetterFunction.implementation_value_is_compressed", "u32", DirectGetterFunction, implementation_value_is_compressed, 4, "nullable", "scalar");
w.section("ECMAScriptFunctionObject layout");
field!(w, "ECMASCRIPT_FUNCTION_OBJECT_SHARED_DATA", "ECMAScriptFunctionObject.shared_data", "SharedFunctionInstanceData", FunctionObject, ecmascript_shared_data, 4, "nonnull", "compressed");
diff --git a/Libraries/LibJS/Rust/src/embedding/function.rs b/Libraries/LibJS/Rust/src/embedding/function.rs
index 323b2b470ce..bdb684f864d 100644
--- a/Libraries/LibJS/Rust/src/embedding/function.rs
+++ b/Libraries/LibJS/Rust/src/embedding/function.rs
@@ -220,7 +220,8 @@ pub unsafe extern "C" fn js_function_create_closure_with_name(
/// Where a direct getter finds the value it returns, as byte offsets of pointer-sized fields: the wrapped
/// implementation in the wrapper (JS_HOST_OBJECT_WRAPPABLE_OFFSET for a host object), the field of the implementation
/// that holds the value's GC::Weak implementation, the main world wrapper in that value, and the cell in a
-/// GC::WeakImpl. This is LibJS's JS::DirectGetterConfiguration.
+/// GC::WeakImpl. The field of the implementation is a compressed cell pointer (a GC::Member) instead if
+/// implementation_value_is_compressed is set. This is what LibJS's JS::DirectGetterConfiguration describes.
#[derive(Clone, Copy)]
#[repr(C)]
pub struct JSDirectGetterConfiguration {
@@ -228,10 +229,12 @@ pub struct JSDirectGetterConfiguration {
pub implementation_value_offset: usize,
pub main_world_wrapper_offset: usize,
pub weak_impl_value_offset: usize,
+ pub implementation_value_is_compressed: bool,
}
/// js_function_create_native() for an attribute getter that the interpreter can answer without calling the behaviour,
-/// by following the configuration's offsets from the this value. Every offset must be a multiple of the pointer size.
+/// by following the configuration's offsets from the this value. Every offset must be a multiple of the pointer size,
+/// but that of a compressed implementation value, which must be a multiple of 4.
/// The realm must not be null. Borrows the key and the configuration. Returns an unrooted function. Main thread only.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn js_function_create_direct_getter(
@@ -265,6 +268,7 @@ pub unsafe extern "C" fn js_function_create_direct_getter(
implementation_value_offset: configuration.implementation_value_offset,
main_world_wrapper_offset: configuration.main_world_wrapper_offset,
weak_impl_value_offset: configuration.weak_impl_value_offset,
+ implementation_value_is_compressed: configuration.implementation_value_is_compressed,
},
prefix,
))
diff --git a/Libraries/LibJS/Rust/src/jit/snapshot.rs b/Libraries/LibJS/Rust/src/jit/snapshot.rs
index a739656cc6e..46c04582326 100644
--- a/Libraries/LibJS/Rust/src/jit/snapshot.rs
+++ b/Libraries/LibJS/Rust/src/jit/snapshot.rs
@@ -961,7 +961,8 @@ impl SnapshotBuilder<'_> {
let bytes = |word_offset: u32| word_offset * size_of::<u64>() as u32;
DirectGetterFields {
wrapper_implementation: bytes(getter.wrapper_implementation_word_offset.get()),
- implementation_value: bytes(getter.implementation_value_word_offset.get()),
+ implementation_value: getter.implementation_value_offset.get(),
+ implementation_value_is_compressed: getter.implementation_value_is_compressed.get() != 0,
main_world_wrapper: bytes(getter.main_world_wrapper_word_offset.get()),
weak_impl_value: bytes(getter.weak_impl_value_word_offset.get()),
}
diff --git a/Libraries/LibJS/Rust/src/layout/function_object.rs b/Libraries/LibJS/Rust/src/layout/function_object.rs
index 1cda7e2692f..fee8e98822a 100644
--- a/Libraries/LibJS/Rust/src/layout/function_object.rs
+++ b/Libraries/LibJS/Rust/src/layout/function_object.rs
@@ -54,15 +54,17 @@ pub struct RawNativeFunction {
const _: () = assert!(size_of::<RawNativeFunction>() == 96);
-/// A raw native getter that reads a wrapped value straight out of a platform object, at word offsets the embedder
-/// provides.
+/// A raw native getter that reads a wrapped value straight out of a platform object, at offsets the embedder provides:
+/// word offsets of pointer-sized fields, and the byte offset of the implementation value, which is a compressed cell
+/// pointer if `implementation_value_is_compressed` is 1, and pointer-sized if it is 0.
#[repr(C)]
pub struct DirectGetterFunction {
pub base: RawNativeFunction,
pub wrapper_implementation_word_offset: Cell<u32>,
- pub implementation_value_word_offset: Cell<u32>,
+ pub implementation_value_offset: Cell<u32>,
pub main_world_wrapper_word_offset: Cell<u32>,
pub weak_impl_value_word_offset: Cell<u32>,
+ pub implementation_value_is_compressed: Cell<u32>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
diff --git a/Libraries/LibJS/Rust/src/runtime/native_function.rs b/Libraries/LibJS/Rust/src/runtime/native_function.rs
index d0c7b85fc34..abf60911916 100644
--- a/Libraries/LibJS/Rust/src/runtime/native_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/native_function.rs
@@ -792,15 +792,16 @@ impl RawNativeFunction {
/// Where a DirectGetterFunction finds the value it returns in a platform object.
#[derive(Clone, Copy, Debug, Default)]
pub struct DirectGetterConfiguration {
- // These are byte offsets to pointer-sized fields read directly by the interpreter. The bindings
- // generator obtains them from PlatformObject::wrapped_implementation_offset(), the implementation
- // field's generated offset helper, Wrappable::main_world_wrapper_offset(), and
- // GC::WeakImpl::value_offset(), respectively. DirectGetterFunction validates their alignment and
- // converts them to word offsets.
+ // These are byte offsets to fields read directly by the interpreter. The bindings generator obtains them from
+ // PlatformObject::wrapped_implementation_offset(), the implementation field's generated offset helper,
+ // Wrappable::main_world_wrapper_offset(), and GC::WeakImpl::value_offset(), respectively. They are pointer-sized,
+ // but for the implementation value, which is a compressed cell pointer if implementation_value_is_compressed is set.
+ // DirectGetterFunction validates their alignment and converts all but the implementation value to word offsets.
pub wrapper_implementation_offset: usize,
pub implementation_value_offset: usize,
pub main_world_wrapper_offset: usize,
pub weak_impl_value_offset: usize,
+ pub implementation_value_is_compressed: bool,
}
define_cell!(
@@ -853,8 +854,13 @@ impl DirectGetterFunction {
) -> DirectGetterFunction {
let base = RawNativeFunction::new_with_realm(vm, Self::CLASS, native_function, Some(prototype), realm, None);
let word_size = size_of::<usize>();
+ let value_size = if configuration.implementation_value_is_compressed {
+ size_of::<u32>()
+ } else {
+ word_size
+ };
assert!(configuration.wrapper_implementation_offset.is_multiple_of(word_size));
- assert!(configuration.implementation_value_offset.is_multiple_of(word_size));
+ assert!(configuration.implementation_value_offset.is_multiple_of(value_size));
assert!(configuration.main_world_wrapper_offset.is_multiple_of(word_size));
assert!(configuration.weak_impl_value_offset.is_multiple_of(word_size));
let word_offset =
@@ -862,9 +868,12 @@ impl DirectGetterFunction {
let function = DirectGetterFunction {
base,
wrapper_implementation_word_offset: Cell::new(word_offset(configuration.wrapper_implementation_offset)),
- implementation_value_word_offset: Cell::new(word_offset(configuration.implementation_value_offset)),
+ implementation_value_offset: Cell::new(
+ u32::try_from(configuration.implementation_value_offset).expect("the offset of a field fits in u32"),
+ ),
main_world_wrapper_word_offset: Cell::new(word_offset(configuration.main_world_wrapper_offset)),
weak_impl_value_word_offset: Cell::new(word_offset(configuration.weak_impl_value_offset)),
+ implementation_value_is_compressed: Cell::new(u32::from(configuration.implementation_value_is_compressed)),
};
function.set_is_direct_getter_function();
function