Truly independent web browser
LibJS: Add an empty realm to the VM
Shannon Booth43c5eba4fed7
diff --git a/Libraries/LibJS/Runtime/Realm.h b/Libraries/LibJS/Runtime/Realm.hindex 659ddd0478b..01929bdbadc 100644--- a/Libraries/LibJS/Runtime/Realm.h+++ b/Libraries/LibJS/Runtime/Realm.h@@ -49,8 +49,18 @@ public:static ThrowCompletionOr<NonnullOwnPtr<ExecutionContext>> initialize_host_defined_realm(VM&, Function<GC::Ref<Object>(Realm&)> create_global_object, Function<GC::Ref<Object>(Realm&)> create_global_this_value);- [[nodiscard]] Object& global_object() const { return *field_at<Object*>(JS_LAYOUT_REALM_GLOBAL_OBJECT_OFFSET); }- [[nodiscard]] GlobalEnvironment& global_environment() const { return *field_at<GlobalEnvironment*>(JS_LAYOUT_REALM_GLOBAL_ENVIRONMENT_OFFSET); }+ [[nodiscard]] Object& global_object() const+ {+ auto* global_object = field_at<Object*>(JS_LAYOUT_REALM_GLOBAL_OBJECT_OFFSET);+ VERIFY(global_object);+ return *global_object;+ }+ [[nodiscard]] GlobalEnvironment& global_environment() const+ {+ auto* global_environment = field_at<GlobalEnvironment*>(JS_LAYOUT_REALM_GLOBAL_ENVIRONMENT_OFFSET);+ VERIFY(global_environment);+ return *global_environment;+ }[[nodiscard]] Intrinsics const& intrinsics() const { return *reinterpret_cast<Intrinsics const*>(this); }[[nodiscard]] Intrinsics& intrinsics() { return *reinterpret_cast<Intrinsics*>(this); }diff --git a/Libraries/LibJS/Runtime/VM.cpp b/Libraries/LibJS/Runtime/VM.cppindex c6a4d1d1f61..3973b3d08e8 100644--- a/Libraries/LibJS/Runtime/VM.cpp+++ b/Libraries/LibJS/Runtime/VM.cpp@@ -505,6 +505,11 @@ PrimitiveString& VM::empty_string()return cell_ref_from_abi<PrimitiveString>(js_string_create_from_utf16_view(vm_abi(*this), utf16_view_to_abi(Utf16View {})));}+Realm& VM::empty_realm()+{+ return cell_ref_from_abi<Realm>(js_vm_empty_realm(vm_abi(*this)));+}+Utf16String const& VM::error_message(ErrorMessage type) const{VERIFY(type < ErrorMessage::__Count);diff --git a/Libraries/LibJS/Runtime/VM.h b/Libraries/LibJS/Runtime/VM.hindex 1887f82a72c..ca6cc85bea7 100644--- a/Libraries/LibJS/Runtime/VM.h+++ b/Libraries/LibJS/Runtime/VM.h@@ -180,6 +180,10 @@ public:PrimitiveString& empty_string();+ // A realm with no intrinsics, global object, global environment or host-defined data, which nothing can give it. An+ // object that belongs to no realm lives in it.+ Realm& empty_realm();+// This represents the list of errors from ErrorTypes.h whose messages are used in contexts which// must not fail to allocate when they are used. For example, we cannot allocate when we raise an// out-of-memory error, thus we pre-allocate that error string at VM creation time.diff --git a/Libraries/LibJS/Rust/src/embedding/realm.rs b/Libraries/LibJS/Rust/src/embedding/realm.rsindex 85773a88eca..00957fc4b49 100644--- a/Libraries/LibJS/Rust/src/embedding/realm.rs+++ b/Libraries/LibJS/Rust/src/embedding/realm.rs@@ -157,10 +157,23 @@ pub unsafe extern "C" fn js_realm_initialize_host_defined_realm(pub unsafe extern "C" fn js_realm_set_host_defined(realm: *mut JSRealm, host_defined: *mut c_void) {// SAFETY: The caller passes a live realm.let realm = unsafe { cell_from_abi(realm) };+ realm.assert_not_empty();// SAFETY: The caller passes null or a live cell, which the realm keeps alive from now on.unsafe { realm.host_defined.set(NonNull::new(host_defined)) };}+/// The VM's empty realm, which has no intrinsics, global object, global environment or host-defined data, and which+/// nothing can give them. An object that belongs to no realm lives in it. The VM keeps it alive. Main thread only.+///+/// # Safety+///+/// `vm` must be the live VM.+#[unsafe(no_mangle)]+pub unsafe extern "C" fn js_vm_empty_realm(vm: *mut JSVM) -> *mut JSRealm {+ // SAFETY: The caller passes the live VM.+ cell_into_abi(unsafe { vm_from_abi(vm) }.empty_realm())+}+/// The intrinsic `intrinsic` names of the realm, which this creates if the realm has not needed it yet, or null for a/// JSIntrinsic this runtime does not know. Main thread only.///diff --git a/Libraries/LibJS/Rust/src/interpreter/vm.rs b/Libraries/LibJS/Rust/src/interpreter/vm.rsindex fbd286382c9..0855944efe1 100644--- a/Libraries/LibJS/Rust/src/interpreter/vm.rs+++ b/Libraries/LibJS/Rust/src/interpreter/vm.rs@@ -559,6 +559,7 @@ pub struct Vm {json_parse_cache: JsonParseCache,json_stringify_cache: JsonStringifyCache,empty_string: OnceCell<Gc<PrimitiveString>>,+ empty_realm: OnceCell<Gc<Realm>>,cached_strings: OnceCell<CachedStrings>,single_ascii_character_strings: OnceCell<[Gc<PrimitiveString>; SINGLE_ASCII_CHARACTER_STRING_COUNT]>,well_known_symbols: OnceCell<WellKnownSymbols>,@@ -727,6 +728,7 @@ impl Vm {json_parse_cache: JsonParseCache::default(),json_stringify_cache: JsonStringifyCache::new(),empty_string: OnceCell::new(),+ empty_realm: OnceCell::new(),cached_strings: OnceCell::new(),single_ascii_character_strings: OnceCell::new(),well_known_symbols: OnceCell::new(),@@ -1204,6 +1206,7 @@ impl Vm {}self.roots.trace(visitor);self.empty_string.trace(visitor);+ self.empty_realm.trace(visitor);self.single_ascii_character_strings.trace(visitor);self.numeric_string_cache.trace(visitor);self.large_numeric_string_cache.trace(visitor);@@ -1923,6 +1926,12 @@ impl Vm {*self.empty_string.get().expect("the VM allocates the empty string")}+ /// A realm with no intrinsics, global object, global environment or host-defined data, which nothing can give it.+ /// An object that belongs to no realm lives in it, and whatever asks that realm for one of those panics.+ pub fn empty_realm(&self) -> Gc<Realm> {+ *self.empty_realm.get_or_init(|| Realm::create_empty(self))+ }+pub fn single_ascii_character_string(&self, character: u8) -> Gc<PrimitiveString> {assert!(character < 0x80);self.single_ascii_character_stringsdiff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex d1aca835540..166e52a02b7 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -48,6 +48,8 @@ pub struct RealmStorage {/// Whether ToPrimitive of a String object that inherits from %String.prototype% gives its [[StringData]] (see/// `runtime::realm_watchpoints`).pub(crate) string_objects_convert_to_their_string_data: WatchedCondition,+ /// Whether this is the VM's empty realm, which nothing may fill (see `Vm::empty_realm()`).+ empty: bool,}define_cell!(Realm, Other);@@ -137,6 +139,20 @@ impl Realm {/// A realm without intrinsics, a global object or a global environment, which/// InitializeHostDefinedRealm goes on to create.pub fn create(vm: &Vm) -> Gc<Realm> {+ Self::allocate(vm, RealmStorage::default())+ }++ pub(crate) fn create_empty(vm: &Vm) -> Gc<Realm> {+ Self::allocate(+ vm,+ RealmStorage {+ empty: true,+ ..RealmStorage::default()+ },+ )+ }++ fn allocate(vm: &Vm, storage: RealmStorage) -> Gc<Realm> {vm.heap().allocate(|_| Realm {header: CellHeader::for_class(Self::CLASS),global_object: Cell::new(None),@@ -146,7 +162,7 @@ impl Realm {host_defined: ForeignCellSlot::empty(),array_iterator_next_watchpoint_set: Cell::new(None),array_iterator_return_watchpoint_set: Cell::new(None),- storage: RealmStorage::default(),+ storage,})}@@ -257,6 +273,7 @@ impl Realm {}pub fn set_intrinsics(&self, intrinsics: Gc<Intrinsics>) {+ self.assert_not_empty();assert!(self.intrinsics.get().is_none());self.intrinsics.set(Some(intrinsics));}@@ -266,6 +283,7 @@ impl Realm {}pub fn set_global_object(&self, global: Gc<Object>) {+ self.assert_not_empty();self.global_object.set(Some(global));}@@ -276,6 +294,7 @@ impl Realm {}pub fn set_global_environment(&self, environment: Gc<GlobalEnvironment>) {+ self.assert_not_empty();self.global_environment.set(Some(environment));self.global_declarative_environment.set(Some(environment.declarative_record()));@@ -297,10 +316,15 @@ impl Realm {/// Sets the data of the realm, a cell that the realm keeps alive from then on. A realm holds the data of one/// embedder.pub fn set_embedder_data<T: EmbedderValue>(&self, data: Gc<EmbedderCell<T>>) {+ self.assert_not_empty();// SAFETY: The cell is a live cell of the heap that the realm is in.unsafe { self.storage.embedder_data.set(Some(data.as_non_null().cast())) };}+ pub(crate) fn assert_not_empty(&self) {+ assert!(!self.storage.empty, "nothing may fill the empty realm");+ }+pub fn global_declarative_environment(&self) -> Gc<DeclarativeEnvironment> {self.global_declarative_environment.get()