Skip to content

Truly independent web browser

LibJS: Tell host objects when script makes their identity outlive it

Andreas Klingacf156f9504d
diff --git a/Libraries/LibJS/HostClassBuilder.h b/Libraries/LibJS/HostClassBuilder.h
index d2c84a43544..ceaaf1873d4 100644
--- a/Libraries/LibJS/HostClassBuilder.h
+++ b/Libraries/LibJS/HostClassBuilder.h
@@ -399,6 +399,11 @@ struct HostObjectHookThunks {
{
return normal_completion_to_abi(value_to_abi(Traits::get_indexed_value(host_object_from_abi(object), index)));
}
+
+ static void preserve_identity(JSObject* object)
+ {
+ Traits::preserve_identity(host_object_from_abi(object));
+ }
};
template<typename Traits>
@@ -481,6 +486,8 @@ consteval JSHostObjectHooks make_host_object_hooks()
hooks.finalize = &Thunks::finalize;
if constexpr (requires { &Traits::get_indexed_value; })
hooks.get_indexed_value = &Thunks::get_indexed_value;
+ if constexpr (requires { &Traits::preserve_identity; })
+ hooks.preserve_identity = &Thunks::preserve_identity;
return hooks;
}
diff --git a/Libraries/LibJS/HostObjectABI.h b/Libraries/LibJS/HostObjectABI.h
index 5468d47d503..7689bc8ff00 100644
--- a/Libraries/LibJS/HostObjectABI.h
+++ b/Libraries/LibJS/HostObjectABI.h
@@ -194,6 +194,10 @@ typedef struct JSHostObjectHooks {
// the own property there, if the hook can tell without running code; otherwise it returns the empty value, and
// [[Get]] goes on as it would without it.
JSCompletion (*get_indexed_value)(JSObject*, uint32_t index);
+ // Optional. Runs when script makes the identity of the object outlive the references to it: when the object is
+ // held weakly, by a WeakRef, a WeakMap, a WeakSet or a FinalizationRegistry, or gains a private element. An
+ // embedder that drops an object it can recreate must keep this one from then on. The hook must not run code.
+ void (*preserve_identity)(JSObject*);
} JSHostObjectHooks;
// The [[Call]] and [[Construct]] behavior of kind JS_HOST_CLASS_FUNCTION, which the engine runs in the function's own
diff --git a/Libraries/LibJS/Rust/generate/layout_header.rs b/Libraries/LibJS/Rust/generate/layout_header.rs
index 53af639ca56..d7a67befbb1 100644
--- a/Libraries/LibJS/Rust/generate/layout_header.rs
+++ b/Libraries/LibJS/Rust/generate/layout_header.rs
@@ -453,6 +453,7 @@ pub fn generate() -> LayoutHeader {
error_data,
finalize,
get_indexed_value,
+ preserve_identity,
});
check_host_object_abi_struct!(w, JSHostFunctionHooks { call, construct, finalize });
check_host_object_abi_struct!(w, JSHostArrayHooks { set, delete_property });
diff --git a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs b/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
index 9d21e1c083c..8f157744243 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/class_table.rs
@@ -57,6 +57,7 @@ static HOST_OBJECT_HOOKS_THAT_ARE_ALL_ORDINARY: JSHostObjectHooks = JSHostObject
error_data: None,
finalize: None,
get_indexed_value: None,
+ preserve_identity: None,
};
static HOST_ARRAY_HOOKS_THAT_ARE_ALL_ORDINARY: JSHostArrayHooks = JSHostArrayHooks {
diff --git a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
index 635596000fd..1d67f2f338a 100644
--- a/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
+++ b/Libraries/LibJS/Rust/src/embedding/host/host_object.rs
@@ -133,6 +133,9 @@ fn host_object_methods(table: &'static JSHostClass) -> ObjectMethods {
if hooks.own_property_keys.is_some() {
methods.internal_own_property_keys = own_property_keys_through_hook;
}
+ if hooks.preserve_identity.is_some() {
+ methods.preserve_identity = Some(preserve_identity_through_hook);
+ }
if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_PROPERTY_ABSENCE) {
methods.is_cacheable_for_property_absence = false;
} else if table.has_flag(JS_HOST_CLASS_NOT_CACHEABLE_FOR_INDEXED_PROPERTY_ABSENCE) {
@@ -413,6 +416,14 @@ fn is_cacheable_for_inherited_property_through_hook(object: &Object) -> bool {
unsafe { hook(lend_object_to_hook(object)) }
}
+fn preserve_identity_through_hook(object: &Object) {
+ let hook = hooks_of(object)
+ .preserve_identity
+ .expect("the class has a preserve_identity hook");
+ // SAFETY: The hook takes an object of its class.
+ unsafe { hook(lend_object_to_hook(object)) }
+}
+
fn error_data_through_hook(object: &Object) -> Option<&ErrorData> {
let hook = hooks_of(object).error_data.expect("the class has an error_data hook");
// SAFETY: The hook takes an object of its class, and returns null or error data that lives as long as the object,
diff --git a/Libraries/LibJS/Rust/src/layout/host_class.rs b/Libraries/LibJS/Rust/src/layout/host_class.rs
index 1a321d8ac05..3f216c9559b 100644
--- a/Libraries/LibJS/Rust/src/layout/host_class.rs
+++ b/Libraries/LibJS/Rust/src/layout/host_class.rs
@@ -153,6 +153,7 @@ pub struct JSHostObjectHooks {
pub error_data: Option<unsafe extern "C" fn(object: *mut JSObject) -> *mut c_void>,
pub finalize: Option<unsafe extern "C" fn(object: *mut JSObject)>,
pub get_indexed_value: Option<unsafe extern "C" fn(object: *mut JSObject, index: u32) -> JSCompletion>,
+ pub preserve_identity: Option<unsafe extern "C" fn(object: *mut JSObject)>,
}
#[repr(C)]
diff --git a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
index 5d5a8b2b6bd..2b2fc10cd2b 100644
--- a/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
+++ b/Libraries/LibJS/Rust/src/runtime/abstract_operations.rs
@@ -1019,6 +1019,14 @@ pub fn can_be_held_weakly(value: Value) -> bool {
false
}
+/// NB: Not an operation of the spec. Script is about to hold `value` weakly, which makes the identity of an object
+/// outlive the references to it, so the embedder must keep the object from then on (Object::preserve_identity()).
+pub fn preserve_identity_of_value_held_weakly(value: Value) {
+ if value.is_object() {
+ value.as_object().preserve_identity();
+ }
+}
+
// 13.3.7.2 GetSuperConstructor ( ), https://tc39.es/ecma262/#sec-getsuperconstructor
pub fn get_super_constructor(vm: &Vm) -> Option<Gc<Object>> {
// OPTIMIZATION: The running function is envRec.[[FunctionObject]] if its frame answers for the environment.
diff --git a/Libraries/LibJS/Rust/src/runtime/finalization_registry_prototype.rs b/Libraries/LibJS/Rust/src/runtime/finalization_registry_prototype.rs
index 0a098c96495..f4c677dae6f 100644
--- a/Libraries/LibJS/Rust/src/runtime/finalization_registry_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/finalization_registry_prototype.rs
@@ -11,7 +11,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
-use crate::runtime::abstract_operations::can_be_held_weakly;
+use crate::runtime::abstract_operations::{can_be_held_weakly, preserve_identity_of_value_held_weakly};
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
@@ -126,6 +126,8 @@ impl FinalizationRegistryPrototype {
// 6. Let cell be the Record { [[WeakRefTarget]]: target, [[HeldValue]]: heldValue, [[UnregisterToken]]: unregisterToken }.
// 7. Append cell to finalizationRegistry.[[Cells]].
+ preserve_identity_of_value_held_weakly(target);
+ preserve_identity_of_value_held_weakly(unregister_token);
finalization_registry.add_finalization_record(
vm,
target.as_cell(),
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 5b14cb73f23..a69c021fe15 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -257,6 +257,8 @@ pub struct ObjectMethods {
/// [[PrivateElements]], for a class whose fields do not tell: one whose fields only cache, or one with a slot that
/// holds nothing. None leaves it to the fields, which Object::has_internal_slots() reads.
pub has_internal_slots: Option<bool>,
+ /// What Object::preserve_identity() runs, for the objects of an embedder that drops objects it can recreate.
+ pub preserve_identity: Option<fn(&Object)>,
}
/// [[GetPrototypeOf]].
@@ -293,6 +295,7 @@ pub static ORDINARY_OBJECT_METHODS: ObjectMethods = ObjectMethods {
as_builtin_iterator_if_next_is_not_redefined: |_, _| None,
error_data: error_data_of_error,
has_internal_slots: None,
+ preserve_identity: None,
};
define_cell!(
@@ -1746,6 +1749,8 @@ impl Object {
&name.property_key(),
ValueAndAttributes::new(value, PropertyAttributes::new(Attribute::WRITABLE)),
);
+ // NB: The private element is state of the object that the embedder can't recreate.
+ self.preserve_identity();
// 5. Return unused.
Ok(())
@@ -1778,6 +1783,8 @@ impl Object {
&element.key.property_key(),
ValueAndAttributes::new(element.value, PropertyAttributes::default()),
);
+ // NB: The private element is state of the object that the embedder can't recreate.
+ self.preserve_identity();
// 6. Return unused.
Ok(())
@@ -2098,6 +2105,15 @@ impl Object {
(self.methods().error_data)(self)
}
+ /// Tells the object that script made its identity outlive the references to it: it is held weakly, or has a
+ /// private element. An embedder may drop an object of its own while script holds no reference to it and create an
+ /// equal one when script asks for it again, which is unobservable only until then.
+ pub fn preserve_identity(&self) {
+ if let Some(preserve_identity) = self.methods().preserve_identity {
+ preserve_identity(self);
+ }
+ }
+
/// Whether the object has an [[ErrorData]] internal slot.
pub fn has_error_data(&self) -> bool {
self.error_data().is_some()
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_map_prototype.rs b/Libraries/LibJS/Rust/src/runtime/weak_map_prototype.rs
index 595e51c7980..51da2601827 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_map_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_map_prototype.rs
@@ -11,7 +11,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
-use crate::runtime::abstract_operations::{call, can_be_held_weakly};
+use crate::runtime::abstract_operations::{call, can_be_held_weakly, preserve_identity_of_value_held_weakly};
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
@@ -147,6 +147,7 @@ impl WeakMapPrototype {
// 5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
// 6. Append p to M.[[WeakMapData]].
+ preserve_identity_of_value_held_weakly(key);
weak_map.weak_map_set(vm, key.as_cell(), value);
// 7. Return value.
@@ -189,6 +190,7 @@ impl WeakMapPrototype {
// ii. Return value.
// 9. Let p be the Record { [[Key]]: key, [[Value]]: value }.
// 10. Append p to M.[[WeakMapData]].
+ preserve_identity_of_value_held_weakly(key);
weak_map.weak_map_set(vm, key.as_cell(), value);
// 11. Return value.
@@ -238,6 +240,7 @@ impl WeakMapPrototype {
// ii. Return M.
// 5. Let p be the Record { [[Key]]: key, [[Value]]: value }.
// 6. Append p to M.[[WeakMapData]].
+ preserve_identity_of_value_held_weakly(key);
weak_map.weak_map_set(vm, key.as_cell(), value);
// 7. Return M.
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
index b6178500ea6..f3cca0a921b 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_ref.rs
@@ -15,6 +15,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
+use crate::runtime::abstract_operations::preserve_identity_of_value_held_weakly;
use crate::runtime::object::{MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, define_object_class};
use crate::runtime::realm::Realm;
@@ -52,6 +53,7 @@ impl WeakRef {
/// A WeakRef to `value`, an object or a symbol.
pub fn new(vm: &Vm, realm: Gc<Realm>, value: Value, prototype: Gc<Object>) -> WeakRef {
assert!(value.is_object() || value.is_symbol());
+ preserve_identity_of_value_held_weakly(value);
WeakRef {
base: Object::new_with_prototype(
vm,
diff --git a/Libraries/LibJS/Rust/src/runtime/weak_set_prototype.rs b/Libraries/LibJS/Rust/src/runtime/weak_set_prototype.rs
index 5ab9a66d379..8c1ff38cf9f 100644
--- a/Libraries/LibJS/Rust/src/runtime/weak_set_prototype.rs
+++ b/Libraries/LibJS/Rust/src/runtime/weak_set_prototype.rs
@@ -11,7 +11,7 @@ use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::layout::value::Value;
-use crate::runtime::abstract_operations::can_be_held_weakly;
+use crate::runtime::abstract_operations::{can_be_held_weakly, preserve_identity_of_value_held_weakly};
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
@@ -90,6 +90,7 @@ impl WeakSetPrototype {
// a. If e is not empty and SameValue(e, value) is true, then
// i. Return S.
// 5. Append value to S.[[WeakSetData]].
+ preserve_identity_of_value_held_weakly(value);
weak_set.weak_set_add(vm, value.as_cell());
// 6. Return S.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/gc_object.rs b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/gc_object.rs
index fe15aaf3ce1..c70fbc36133 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/web_assembly/gc_object.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/web_assembly/gc_object.rs
@@ -37,6 +37,7 @@ static EXPORTED_GC_OBJECT_HOOKS: JSHostObjectHooks = JSHostObjectHooks {
error_data: None,
finalize: None,
get_indexed_value: None,
+ preserve_identity: None,
};
static EXPORTED_GC_OBJECT_CLASS: JSHostClass = JSHostClass::new(