Skip to content

Truly independent web browser

LibJS: Keep an object's shape in the first word of its cell

Shannon Booth475a5fcb7815
diff --git a/Libraries/LibJS/HostObjectABI.h b/Libraries/LibJS/HostObjectABI.h
index 0e6e0a0502e..a1a6843d98e 100644
--- a/Libraries/LibJS/HostObjectABI.h
+++ b/Libraries/LibJS/HostObjectABI.h
@@ -32,9 +32,9 @@ enum {
// Layout of every host object of kind JS_HOST_CLASS_OBJECT. Direct getter functions read the wrappable slot without
// checking what it holds, so it only ever holds the embedder's wrapped implementation object. Any other per-object state
-// belongs in the host data slot. The host object's own fields follow the engine's object, which is 64 bytes.
+// belongs in the host data slot. The host object's own fields follow the engine's object, which is 56 bytes.
enum {
- JS_HOST_OBJECT_HOST_CLASS_OFFSET = 64,
+ JS_HOST_OBJECT_HOST_CLASS_OFFSET = 56,
JS_HOST_OBJECT_WRAPPABLE_OFFSET = JS_HOST_OBJECT_HOST_CLASS_OFFSET + 8,
JS_HOST_OBJECT_HOST_DATA_OFFSET = JS_HOST_OBJECT_WRAPPABLE_OFFSET + 8,
JS_HOST_OBJECT_SIZE = JS_HOST_OBJECT_HOST_DATA_OFFSET + 8,
diff --git a/Libraries/LibJS/Rust/src/layout/function_object.rs b/Libraries/LibJS/Rust/src/layout/function_object.rs
index fee8e98822a..37a03224d56 100644
--- a/Libraries/LibJS/Rust/src/layout/function_object.rs
+++ b/Libraries/LibJS/Rust/src/layout/function_object.rs
@@ -31,7 +31,7 @@ pub struct FunctionObject {
pub ecmascript_shared_data: NullableMember<SharedFunctionInstanceData>,
}
-const _: () = assert!(size_of::<FunctionObject>() == 72);
+const _: () = assert!(size_of::<FunctionObject>() == 64);
#[repr(C)]
pub struct NativeFunction {
@@ -45,14 +45,14 @@ pub struct NativeFunction {
pub raw_native_function_index: Cell<u32>,
}
-const _: () = assert!(size_of::<NativeFunction>() == 96);
+const _: () = assert!(size_of::<NativeFunction>() == 88);
#[repr(C)]
pub struct RawNativeFunction {
pub base: NativeFunction,
}
-const _: () = assert!(size_of::<RawNativeFunction>() == 96);
+const _: () = assert!(size_of::<RawNativeFunction>() == 88);
/// 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
diff --git a/Libraries/LibJS/Rust/src/layout/object.rs b/Libraries/LibJS/Rust/src/layout/object.rs
index 464d7d24b94..7e5c88aebd6 100644
--- a/Libraries/LibJS/Rust/src/layout/object.rs
+++ b/Libraries/LibJS/Rust/src/layout/object.rs
@@ -6,7 +6,7 @@
use core::cell::Cell;
-use super::cell::CellHeader;
+use super::cell::{CellHeader, CellKind, CellState};
use super::member::Member;
use super::shape::Shape;
use super::value::Value;
@@ -48,16 +48,20 @@ pub const INDEXED_ELEMENTS_HEADER_SIZE: usize = 8;
pub const INLINE_NAMED_STORAGE_CAPACITY: usize = 2;
+/// An object starts with the shape and the array-like size in the first word of its cell header, which LibGC leaves
+/// to the cell, followed by the rest of the header.
#[repr(C)]
pub struct Object {
- pub header: CellHeader,
- pub flags: Cell<u16>,
+ pub shape: Member<Shape>,
+ pub indexed_array_like_size: Cell<u32>,
+ pub mark: Cell<bool>,
+ pub state: Cell<CellState>,
+ pub kind: CellKind,
pub indexed_storage_kind: Cell<IndexedStorageKind>,
+ pub flags: Cell<u16>,
/// How many named property values the inline storage holds. It ends the object, and only plain objects and
/// mapped arguments objects have cells with room for more than INLINE_NAMED_STORAGE_CAPACITY.
pub inline_named_capacity: Cell<u8>,
- pub indexed_array_like_size: Cell<u32>,
- pub shape: Member<Shape>,
/// Points at `inline_named_storage` until the object needs more room.
pub named_properties: Cell<*mut Value>,
pub indexed_elements: Cell<*mut Value>,
@@ -65,8 +69,14 @@ pub struct Object {
pub inline_named_storage: [Cell<Value>; INLINE_NAMED_STORAGE_CAPACITY],
}
-// NB: The inline capacity fits in the padding in front of the array-like size, and the inline storage ends the object.
-const _: () = assert!(size_of::<Object>() == 64);
+const _: () = {
+ use core::mem::offset_of;
+ assert!(offset_of!(Object, mark) == offset_of!(CellHeader, mark));
+ assert!(offset_of!(Object, state) == offset_of!(CellHeader, state));
+ assert!(offset_of!(Object, kind) == offset_of!(CellHeader, kind));
+};
+
+const _: () = assert!(size_of::<Object>() == 56);
const _: () = assert!(
core::mem::offset_of!(Object, inline_named_storage) + INLINE_NAMED_STORAGE_CAPACITY * size_of::<Value>()
== size_of::<Object>()
diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rs
index aef80beed13..d00a4195cf0 100644
--- a/Libraries/LibJS/Rust/src/runtime/array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array.rs
@@ -43,7 +43,7 @@ pub struct Array {
is_proxy_target: Cell<bool>,
}
-const _: () = assert!(size_of::<Array>() == 72);
+const _: () = assert!(size_of::<Array>() == 64);
/// The offsets of the flags JIT code checks before writing elements of an array.
pub const ARRAY_LENGTH_WRITABLE_OFFSET: usize = core::mem::offset_of!(Array, length_writable);
diff --git a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
index ea57e240faa..82d61db2ca9 100644
--- a/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/ecmascript_function_object.rs
@@ -76,7 +76,7 @@ pub struct EcmascriptFunctionObjectStorage {
}
// NB: Every ECMAScript function has these bytes, so keep what only some functions need in the rare data.
-const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 112);
+const _: () = assert!(size_of::<EcmascriptFunctionObject>() == 104);
// SAFETY: Visits the cells of the rare data, if there is any.
unsafe impl Trace for EcmascriptFunctionObjectStorage {
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index a26639ca566..a2c60d4856c 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -16,14 +16,14 @@ use libjs_runtime_macros::Trace;
use crate::bytecode::executable::PropertyLookupCache;
use crate::bytecode::property_access::{Strict, put_by_property_key};
use crate::embedding::abi_types::JSRealm;
-use crate::gc::class::{Class, Extends, ExternalMemorySize, GcCell, define_cell};
+use crate::gc::class::{Class, Extends, ExternalMemorySize, GcCell, class_at, define_cell};
use crate::gc::defer_gc::DeferGc;
use crate::gc::gc_ref_cell::GcRefCell;
use crate::gc::heap::{Heap, RuntimeClassAllocator, SizeClassAllocator};
use crate::gc::root::MarkedVec;
use crate::gc::visitor::{Trace, Visitor};
use crate::interpreter::vm::Vm;
-use crate::layout::cell::{CellHeader, Gc};
+use crate::layout::cell::{CellKind, CellState, Gc};
use crate::layout::execution_context::ExecutionContext;
use crate::layout::function_object::{EcmascriptFunctionObject, FunctionObject, SharedFunctionInstanceData};
use crate::layout::host_class::JSValue;
@@ -609,12 +609,14 @@ impl Object {
) -> Object {
assert!(class.is_subclass_of(Self::CLASS));
let object = Object {
- header: CellHeader::for_class(class),
- flags: Cell::new(object_flag::IS_EXTENSIBLE),
+ shape: Member::new(shape),
+ indexed_array_like_size: Cell::new(0),
+ mark: Cell::new(false),
+ state: Cell::new(CellState::Live),
+ kind: CellKind::Object,
indexed_storage_kind: Cell::new(IndexedStorageKind::None),
+ flags: Cell::new(object_flag::IS_EXTENSIBLE),
inline_named_capacity: Cell::new(INLINE_NAMED_STORAGE_CAPACITY as u8),
- indexed_array_like_size: Cell::new(0),
- shape: Member::new(shape),
named_properties: Cell::new(core::ptr::null_mut()),
indexed_elements: Cell::new(core::ptr::null_mut()),
private_elements: PrivateElements::default(),
@@ -774,7 +776,8 @@ impl Object {
}
pub fn class(&self) -> &'static Class {
- self.header.class()
+ // SAFETY: Objects live in the heap.
+ unsafe { class_at(core::ptr::from_ref(self).cast()) }
}
fn methods(&self) -> &'static ObjectMethods {
diff --git a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
index 50ba7bde571..2c635b34308 100644
--- a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
@@ -41,7 +41,7 @@ pub struct PromiseResolvingFunction {
kind: PromiseResolvingFunctionKind,
}
-const _: () = assert!(size_of::<PromiseResolvingFunction>() == 112);
+const _: () = assert!(size_of::<PromiseResolvingFunction>() == 104);
static PROMISE_RESOLVING_FUNCTION_VIRTUAL_METHODS: NativeFunctionMethods = NativeFunctionMethods {
call: PromiseResolvingFunction::call,
diff --git a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
index 4dea968005d..efea06bc7a9 100644
--- a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
@@ -85,7 +85,7 @@ pub struct ProxyObject {
is_revoked: Cell<bool>,
}
-const _: () = assert!(size_of::<ProxyObject>() == 88);
+const _: () = assert!(size_of::<ProxyObject>() == 80);
/// The internal methods of a Proxy exotic object. Every proxy has [[Call]] and [[Construct]]: IsCallable and
/// IsConstructor only reach them for a proxy whose target has them, since the proxy keeps the IsFunction flag only if
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs b/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
index 635159a34bb..09e2bf93c23 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
@@ -148,7 +148,7 @@ pub struct RegExpObject {
cached_regex: RefCell<Option<Rc<EcmaScriptRegex>>>,
}
-const _: () = assert!(size_of::<RegExpObject>() == 120);
+const _: () = assert!(size_of::<RegExpObject>() == 112);
static REGEXP_OBJECT_METHODS: ObjectMethods = ObjectMethods {
initialize: RegExpObject::initialize,