Skip to content

Truly independent web browser

LibJS: Add Object::set_realm_and_prototype()

Jelle Raaijmakersd2724bf28975
diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp
index 517066b6a08..d7138127035 100644
--- a/Libraries/LibJS/Runtime/Object.cpp
+++ b/Libraries/LibJS/Runtime/Object.cpp
@@ -412,6 +412,11 @@ void Object::set_prototype(GC::Ptr<Object> prototype)
js_object_set_prototype(vm_of(*this), object_to_abi(*this), optional_object_to_abi(prototype.ptr()));
}
+void Object::set_realm_and_prototype(Realm& realm, GC::Ptr<Object> prototype)
+{
+ js_object_set_realm_and_prototype(vm_of(*this), object_to_abi(*this), cell_to_abi<JSRealm>(realm), optional_object_to_abi(prototype.ptr()));
+}
+
StringView Object::class_name() const
{
size_t length = 0;
diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h
index 4633f9a05ea..6b1575b0b6c 100644
--- a/Libraries/LibJS/Runtime/Object.h
+++ b/Libraries/LibJS/Runtime/Object.h
@@ -186,6 +186,7 @@ public:
void invalidate_property_lookup_caches();
void set_prototype(GC::Ptr<Object>);
+ void set_realm_and_prototype(Realm&, GC::Ptr<Object>);
[[nodiscard]] bool has_magical_length_property() const { return has_engine_object_flag(JS_LAYOUT_OBJECT_FLAG_HAS_MAGICAL_LENGTH_PROPERTY); }
[[nodiscard]] bool is_typed_array() const { return has_engine_object_flag(JS_LAYOUT_OBJECT_FLAG_IS_TYPED_ARRAY); }
diff --git a/Libraries/LibJS/Rust/src/embedding/object.rs b/Libraries/LibJS/Rust/src/embedding/object.rs
index 30157d64626..19d12afa341 100644
--- a/Libraries/LibJS/Rust/src/embedding/object.rs
+++ b/Libraries/LibJS/Rust/src/embedding/object.rs
@@ -735,6 +735,26 @@ pub unsafe extern "C" fn js_object_set_prototype(vm: *mut JSVM, object: *mut JSO
object.set_prototype(vm, prototype);
}
+/// Like js_object_set_prototype(), and moves the object to the realm as far as its shape goes. Main thread only.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn js_object_set_realm_and_prototype(
+ vm: *mut JSVM,
+ object: *mut JSObject,
+ realm: *mut JSRealm,
+ prototype: *mut JSObject,
+) {
+ // SAFETY: See the module documentation.
+ let (vm, object, realm, prototype) = unsafe {
+ (
+ vm_from_abi(vm),
+ cell_from_abi::<JSObject>(object),
+ cell_from_abi::<JSRealm>(realm),
+ optional_cell_from_abi::<JSObject>(prototype),
+ )
+ };
+ object.set_realm_and_prototype(vm, realm, prototype);
+}
+
/// Whether the object's own storage has a property of the key, without running any internal method. Borrows the key.
/// Main thread only.
#[unsafe(no_mangle)]
diff --git a/Libraries/LibJS/Rust/src/runtime/object.rs b/Libraries/LibJS/Rust/src/runtime/object.rs
index 651f38c5a86..81c7328ebc4 100644
--- a/Libraries/LibJS/Rust/src/runtime/object.rs
+++ b/Libraries/LibJS/Rust/src/runtime/object.rs
@@ -2938,6 +2938,17 @@ impl Object {
.set(self.shape().create_prototype_transition(vm, new_prototype));
}
+ pub fn set_realm_and_prototype(&self, vm: &Vm, realm: Gc<Realm>, new_prototype: Option<Gc<Object>>) {
+ if self.shape().realm() == realm {
+ self.set_prototype(vm, new_prototype);
+ return;
+ }
+ self.shape.set(
+ self.shape()
+ .create_prototype_transition_into_realm(vm, realm, new_prototype),
+ );
+ }
+
// Non-standard methods
pub fn for_each_own_property_with_enumerability(
diff --git a/Libraries/LibJS/Rust/src/runtime/shape.rs b/Libraries/LibJS/Rust/src/runtime/shape.rs
index 855a3a72368..8318dc0b82f 100644
--- a/Libraries/LibJS/Rust/src/runtime/shape.rs
+++ b/Libraries/LibJS/Rust/src/runtime/shape.rs
@@ -320,8 +320,12 @@ impl Shape {
shape
}
- fn new_from_previous_shape_with_prototype(previous_shape: &Shape, new_prototype: Option<Gc<Object>>) -> Self {
- let shape = Self::new(previous_shape.realm());
+ fn new_from_previous_shape_with_prototype(
+ previous_shape: &Shape,
+ realm: Gc<Realm>,
+ new_prototype: Option<Gc<Object>>,
+ ) -> Self {
+ let shape = Self::new(realm);
shape
.flags
.set(previous_shape.flags.get() & shape_flag::HAS_PARAMETER_MAP);
@@ -614,12 +618,36 @@ impl Shape {
}
return existing_shape;
}
+ let new_shape = self.build_prototype_transition(vm, self.realm(), new_prototype);
+ if !self.is_prototype_shape() {
+ let target = GcWeak::new(vm.heap(), new_shape);
+ self.with_rare_data(|rare_data| rare_data.prototype_transitions.insert(prototype, target));
+ }
+ if let Some(cache) = cache {
+ cache.set(self.as_gc(), prototype, new_shape);
+ }
+ new_shape
+ }
+
+ /// Like create_prototype_transition(), for an object that moves to `realm`. The transition caches are keyed without
+ /// the realm, so the result is not cached.
+ pub fn create_prototype_transition_into_realm(
+ &self,
+ vm: &Vm,
+ realm: Gc<Realm>,
+ new_prototype: Option<Gc<Object>>,
+ ) -> Gc<Shape> {
+ self.mark_unstable(vm);
+ self.build_prototype_transition(vm, realm, new_prototype)
+ }
+
+ fn build_prototype_transition(&self, vm: &Vm, realm: Gc<Realm>, new_prototype: Option<Gc<Object>>) -> Gc<Shape> {
if let Some(new_prototype) = new_prototype {
new_prototype.convert_to_prototype_if_needed(vm);
}
let new_shape = vm
.heap()
- .allocate(Self::new_from_previous_shape_with_prototype(self, new_prototype));
+ .allocate(Self::new_from_previous_shape_with_prototype(self, realm, new_prototype));
if self.is_dictionary() && self.property_count() > MAX_DESCRIPTOR_COUNT {
new_shape.become_dictionary_shape();
self.copy_properties_to_dictionary_shape(&new_shape);
@@ -629,13 +657,6 @@ impl Shape {
new_shape.set_descriptors(self.descriptors());
}
self.invalidate_prototype_if_needed_for_new_prototype(vm, new_shape);
- if !self.is_prototype_shape() {
- let target = GcWeak::new(vm.heap(), new_shape);
- self.with_rare_data(|rare_data| rare_data.prototype_transitions.insert(prototype, target));
- }
- if let Some(cache) = cache {
- cache.set(self.as_gc(), prototype, new_shape);
- }
new_shape
}