Skip to content

Truly independent web browser

LibJS: Compress the pointers of promise, proxy and RegExp cells

Andreas Kling394cce6584b9
diff --git a/Libraries/LibJS/Rust/src/runtime/job_callback.rs b/Libraries/LibJS/Rust/src/runtime/job_callback.rs
index 4ab1dbb8507..3d2f9b281ff 100644
--- a/Libraries/LibJS/Rust/src/runtime/job_callback.rs
+++ b/Libraries/LibJS/Rust/src/runtime/job_callback.rs
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use core::cell::Cell;
use core::ffi::c_void;
use core::ptr::NonNull;
@@ -14,6 +13,7 @@ use crate::gc::class::{GcCell, define_cell};
use crate::gc::foreign::ForeignCellSlot;
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
+use crate::layout::member::Member;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::call_function_object;
use crate::runtime::completion::ThrowCompletionOr;
@@ -24,17 +24,19 @@ use crate::runtime::function_object::FunctionObject;
#[derive(Trace)]
pub struct JobCallback {
header: CellHeader,
- callback: Cell<Gc<FunctionObject>>, // [[Callback]]
- custom_data: ForeignCellSlot, // [[HostDefined]]
+ callback: Member<FunctionObject>, // [[Callback]]
+ custom_data: ForeignCellSlot, // [[HostDefined]]
}
+const _: () = assert!(size_of::<JobCallback>() == 24);
+
define_cell!(JobCallback, Other);
impl JobCallback {
pub fn create(vm: &Vm, callback: Gc<FunctionObject>, custom_data: ForeignCellSlot) -> Gc<JobCallback> {
vm.heap().allocate(|_| JobCallback {
header: CellHeader::for_class(Self::CLASS),
- callback: Cell::new(callback),
+ callback: Member::new(callback),
custom_data,
})
}
diff --git a/Libraries/LibJS/Rust/src/runtime/promise_capability.rs b/Libraries/LibJS/Rust/src/runtime/promise_capability.rs
index a19a956542a..582d023afda 100644
--- a/Libraries/LibJS/Rust/src/runtime/promise_capability.rs
+++ b/Libraries/LibJS/Rust/src/runtime/promise_capability.rs
@@ -11,6 +11,7 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{GcCell, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
+use crate::layout::member::Member;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{construct, ordinary_create_from_constructor_of};
@@ -26,11 +27,13 @@ use crate::runtime::promise::Promise;
#[derive(Trace)]
pub struct PromiseCapability {
header: CellHeader,
- promise: Cell<Gc<Object>>,
- resolve: Cell<Gc<FunctionObject>>,
- reject: Cell<Gc<FunctionObject>>,
+ promise: Member<Object>,
+ resolve: Member<FunctionObject>,
+ reject: Member<FunctionObject>,
}
+const _: () = assert!(size_of::<PromiseCapability>() == 24);
+
define_cell!(PromiseCapability, Other);
impl PromiseCapability {
@@ -42,9 +45,9 @@ impl PromiseCapability {
) -> Gc<PromiseCapability> {
vm.heap().allocate(|_| PromiseCapability {
header: CellHeader::for_class(Self::CLASS),
- promise: Cell::new(promise),
- resolve: Cell::new(resolve),
- reject: Cell::new(reject),
+ promise: Member::new(promise),
+ resolve: Member::new(resolve),
+ reject: Member::new(reject),
})
}
diff --git a/Libraries/LibJS/Rust/src/runtime/promise_reaction.rs b/Libraries/LibJS/Rust/src/runtime/promise_reaction.rs
index fd106c1b4ed..a7fea677b26 100644
--- a/Libraries/LibJS/Rust/src/runtime/promise_reaction.rs
+++ b/Libraries/LibJS/Rust/src/runtime/promise_reaction.rs
@@ -4,13 +4,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use core::cell::Cell;
-
use libjs_runtime_macros::Trace;
use crate::gc::class::{GcCell, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::{CellHeader, Gc};
+use crate::layout::member::NullableMember;
use crate::runtime::job_callback::JobCallback;
use crate::runtime::promise_capability::PromiseCapability;
@@ -21,16 +20,19 @@ pub enum PromiseReactionType {
}
// 27.2.1.2 PromiseReaction Records, https://tc39.es/ecma262/#sec-promisereaction-records
-#[repr(C)]
+// NB: The fields only take 20 bytes, and cells take at least 24.
+#[repr(C, align(8))]
#[derive(Trace)]
pub struct PromiseReaction {
header: CellHeader,
#[gc(untraced)]
reaction_type: PromiseReactionType,
- capability: Cell<Option<Gc<PromiseCapability>>>,
- handler: Cell<Option<Gc<JobCallback>>>,
+ capability: NullableMember<PromiseCapability>,
+ handler: NullableMember<JobCallback>,
}
+const _: () = assert!(size_of::<PromiseReaction>() == 24);
+
define_cell!(PromiseReaction, Other);
impl PromiseReaction {
@@ -43,8 +45,8 @@ impl PromiseReaction {
vm.heap().allocate(|_| PromiseReaction {
header: CellHeader::for_class(Self::CLASS),
reaction_type,
- capability: Cell::new(capability),
- handler: Cell::new(handler),
+ capability: NullableMember::new(capability),
+ handler: NullableMember::new(handler),
})
}
diff --git a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
index 4f9fa3319a9..ce513b22762 100644
--- a/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
+++ b/Libraries/LibJS/Rust/src/runtime/promise_resolving_function.rs
@@ -12,6 +12,7 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{GcCell, cast_ref, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
+use crate::layout::member::{Member, NullableMember};
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::completion::ThrowCompletionOr;
@@ -33,13 +34,15 @@ pub enum PromiseResolvingFunctionKind {
#[derive(Trace)]
pub struct PromiseResolvingFunction {
base: NativeFunction,
- promise: Cell<Gc<Promise>>,
- resolve_function: Cell<Option<Gc<PromiseResolvingFunction>>>,
+ promise: Member<Promise>,
+ resolve_function: NullableMember<PromiseResolvingFunction>,
already_resolved: Cell<bool>,
#[gc(untraced)]
kind: PromiseResolvingFunctionKind,
}
+const _: () = assert!(size_of::<PromiseResolvingFunction>() == 120);
+
static PROMISE_RESOLVING_FUNCTION_VIRTUAL_METHODS: NativeFunctionMethods = NativeFunctionMethods {
call: PromiseResolvingFunction::call,
..NATIVE_FUNCTION_VIRTUAL_METHODS
@@ -105,8 +108,8 @@ impl PromiseResolvingFunction {
) -> PromiseResolvingFunction {
PromiseResolvingFunction {
base: NativeFunction::new_with_prototype(vm, Self::CLASS, prototype),
- promise: Cell::new(promise),
- resolve_function: Cell::new(resolve_function),
+ promise: Member::new(promise),
+ resolve_function: NullableMember::new(resolve_function),
already_resolved: Cell::new(false),
kind,
}
diff --git a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
index d38c15f722b..9b0486ccaf3 100644
--- a/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/proxy_object.rs
@@ -16,6 +16,7 @@ use crate::gc::root::MarkedVec;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::execution_context::ExecutionContext;
+use crate::layout::member::Member;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::{
@@ -79,11 +80,13 @@ fn fully_populated<T>(field: Option<T>) -> T {
#[derive(Trace)]
pub struct ProxyObject {
base: FunctionObject,
- target: Gc<Object>, // [[ProxyTarget]]
- handler: Gc<Object>, // [[ProxyHandler]]
+ target: Member<Object>, // [[ProxyTarget]]
+ handler: Member<Object>, // [[ProxyHandler]]
is_revoked: Cell<bool>,
}
+const _: () = assert!(size_of::<ProxyObject>() == 96);
+
/// 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
/// its target is callable and has_constructor() asks the target.
@@ -197,8 +200,8 @@ impl ProxyObject {
prototype,
MayInterfereWithIndexedPropertyAccess::Yes,
),
- target,
- handler,
+ target: Member::new(target),
+ handler: Member::new(handler),
is_revoked: Cell::new(false),
};
@@ -215,11 +218,11 @@ impl ProxyObject {
}
pub fn target(&self) -> Gc<Object> {
- self.target
+ self.target.get()
}
pub fn handler(&self) -> Gc<Object> {
- self.handler
+ self.handler.get()
}
pub fn is_revoked(&self) -> bool {
@@ -243,8 +246,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "getPrototypeOf").
let trap = Value::from_object(handler).get_method(vm, &vm.names.getPrototypeOf)?;
@@ -295,8 +298,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "setPrototypeOf").
let trap = Value::from_object(handler).get_method(vm, &vm.names.setPrototypeOf)?;
@@ -354,8 +357,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "isExtensible").
let trap = Value::from_object(handler).get_method(vm, &vm.names.isExtensible)?;
@@ -393,8 +396,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "preventExtensions").
let trap = Value::from_object(handler).get_method(vm, &vm.names.preventExtensions)?;
@@ -439,8 +442,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "getOwnPropertyDescriptor").
let trap = Value::from_object(handler).get_method(vm, &vm.names.getOwnPropertyDescriptor)?;
@@ -568,8 +571,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "defineProperty").
let trap = Value::from_object(handler).get_method(vm, &vm.names.defineProperty)?;
@@ -677,8 +680,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// NOTE: We need to protect ourselves from a Proxy with the handler's prototype set to the
// Proxy itself, which would by default bounce between these functions indefinitely and lead to
@@ -760,8 +763,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// NOTE: We need to protect ourselves from a Proxy with its (or handler's) prototype set to the
// Proxy itself, which would by default bounce between these functions indefinitely and lead to
@@ -846,8 +849,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// NOTE: We need to protect ourselves from a Proxy with its prototype set to the
// Proxy itself, which would by default bounce between these functions indefinitely and lead to
@@ -934,8 +937,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "deleteProperty").
let trap = Value::from_object(handler).get_method(vm, &vm.names.deleteProperty)?;
@@ -996,8 +999,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// 5. Let trap be ? GetMethod(handler, "ownKeys").
let trap = Value::from_object(handler).get_method(vm, &vm.names.ownKeys)?;
@@ -1170,8 +1173,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// NOTE: A Proxy exotic object only has a [[Call]] internal method if the initial value of its [[ProxyTarget]] internal slot is an object that has a [[Call]] internal method.
assert!(proxy.is_function());
@@ -1211,7 +1214,7 @@ impl ProxyObject {
return false;
}
- self.target.has_constructor()
+ self.target.get().has_constructor()
}
// 10.5.13 [[Construct]] ( argumentsList, newTarget ), https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget
@@ -1232,8 +1235,8 @@ impl ProxyObject {
// 2. Let target be O.[[ProxyTarget]].
// 3. Let handler be O.[[ProxyHandler]].
// 4. Assert: handler is an Object.
- let target = proxy.target;
- let handler = proxy.handler;
+ let target = proxy.target.get();
+ let handler = proxy.handler.get();
// NOTE: A Proxy exotic object only has a [[Construct]] internal method if the initial value of its [[ProxyTarget]] internal slot is an object that has a [[Construct]] internal method.
assert!(proxy.is_function());
@@ -1291,7 +1294,7 @@ impl ProxyObject {
}
fn get_stack_frame_info(object: &Object, vm: &Vm, stack_frame_info: &mut StackFrameInfo) {
- let target = as_proxy_object(object).target;
+ let target = as_proxy_object(object).target.get();
assert!(target.is_function());
target.get_stack_frame_info(vm, stack_frame_info);
}
diff --git a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs b/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
index a1085bc50de..9101d91bed9 100644
--- a/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/regexp_object.rs
@@ -14,6 +14,7 @@ use libjs_runtime_macros::Trace;
use crate::gc::class::{Finalize, GcCell, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
+use crate::layout::member::NullableMember;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::abstract_operations::ordinary_create_from_constructor_of;
@@ -140,13 +141,15 @@ pub struct RegExpObject {
flag_bits: Cell<RegExpFlags>,
#[gc(untraced)]
legacy_features_enabled: Cell<bool>, // [[LegacyFeaturesEnabled]]
+ // Note: This is initialized in RegExpAlloc, but will be non-null afterwards
+ realm: NullableMember<Realm>, // [[Realm]]
/// The compiled pattern, which RegExp objects with the same pattern and flags share through the regex cache.
#[gc(untraced)]
cached_regex: RefCell<Option<Rc<EcmaScriptRegex>>>,
- // Note: This is initialized in RegExpAlloc, but will be non-null afterwards
- realm: Cell<Option<Gc<Realm>>>, // [[Realm]]
}
+const _: () = assert!(size_of::<RegExpObject>() == 128);
+
static REGEXP_OBJECT_METHODS: ObjectMethods = ObjectMethods {
initialize: RegExpObject::initialize,
..ORDINARY_OBJECT_METHODS
@@ -200,7 +203,7 @@ impl RegExpObject {
flag_bits: Cell::new(flag_bits),
legacy_features_enabled: Cell::new(false),
cached_regex: RefCell::new(None),
- realm: Cell::new(None),
+ realm: NullableMember::null(),
});
regexp_object.initialize_direct(NEW_REGEXP_OBJECT_LAST_INDEX_OFFSET, Value::from_i32(0));
regexp_object
@@ -215,7 +218,7 @@ impl RegExpObject {
flag_bits: Cell::new(flag_bits),
legacy_features_enabled: Cell::new(false),
cached_regex: RefCell::new(None),
- realm: Cell::new(None),
+ realm: NullableMember::null(),
}
}