Skip to content

Commit be7fb42

Browse files
authored
Auto merge of #28536 - yvt:fix-one-compartment, r=jdm
Create only one compartment for each script thread (agent) Documents in the same [agent][1] can share and exchange JS and DOM objects freely, so putting them in separate compartments would require almost every instance of `Dom` to be capable of handling cross-compartment references. [1]: https://html.spec.whatwg.org/multipage/webappapis.html#integration-with-the-javascript-agent-formalism --- - [x] `./mach build -d` does not report any errors - [ ] `./mach test-tidy` does not report any errors - [ ] These changes fix #___ (GitHub issue number if applicable) --- - [ ] There are tests for these changes OR - [x] These changes do not require tests because I think there's already a wide test coverage for same-origin-domain JS object passing, albeit this requires Servo to be built with `--debug-mozjs` for the errors to be (reliably) observable
2 parents 42d7892 + d597264 commit be7fb42

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

components/script/dom/bindings/interface.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ use js::glue::UncheckedUnwrapObject;
1616
use js::jsapi::GetWellKnownSymbol;
1717
use js::jsapi::HandleObject as RawHandleObject;
1818
use js::jsapi::{jsid, JSClass, JSClassOps};
19+
use js::jsapi::{
20+
Compartment, CompartmentSpecifier, IsSharableCompartment, IsSystemCompartment,
21+
JS_IterateCompartments, JS::CompartmentIterResult,
22+
};
1923
use js::jsapi::{JSAutoRealm, JSContext, JSFunctionSpec, JSObject, JSFUN_CONSTRUCTOR};
2024
use js::jsapi::{JSPropertySpec, JSString, JSTracer, JS_AtomizeAndPinString};
2125
use js::jsapi::{JS_GetFunctionObject, JS_NewFunction, JS_NewGlobalObject};
@@ -139,6 +143,7 @@ pub unsafe fn create_global_object(
139143
options.creationOptions_.traceGlobal_ = Some(trace);
140144
options.creationOptions_.sharedMemoryAndAtomics_ = false;
141145
options.creationOptions_.streams_ = true;
146+
select_compartment(cx, &mut options);
142147

143148
rval.set(JS_NewGlobalObject(
144149
*cx,
@@ -162,6 +167,43 @@ pub unsafe fn create_global_object(
162167
JS_FireOnNewGlobalObject(*cx, rval.handle());
163168
}
164169

170+
/// Choose the compartment to create a new global object in.
171+
fn select_compartment(cx: SafeJSContext, options: &mut RealmOptions) {
172+
type Data = *mut Compartment;
173+
unsafe extern "C" fn callback(
174+
_cx: *mut JSContext,
175+
data: *mut libc::c_void,
176+
compartment: *mut Compartment,
177+
) -> CompartmentIterResult {
178+
let data = data as *mut Data;
179+
180+
if !IsSharableCompartment(compartment) || IsSystemCompartment(compartment) {
181+
return CompartmentIterResult::KeepGoing;
182+
}
183+
184+
// Choose any sharable, non-system compartment in this context to allow
185+
// same-agent documents to share JS and DOM objects.
186+
*data = compartment;
187+
CompartmentIterResult::Stop
188+
}
189+
190+
let mut compartment: Data = ptr::null_mut();
191+
unsafe {
192+
JS_IterateCompartments(
193+
*cx,
194+
(&mut compartment) as *mut Data as *mut libc::c_void,
195+
Some(callback),
196+
);
197+
}
198+
199+
if compartment.is_null() {
200+
options.creationOptions_.compSpec_ = CompartmentSpecifier::NewCompartmentAndZone;
201+
} else {
202+
options.creationOptions_.compSpec_ = CompartmentSpecifier::ExistingCompartment;
203+
options.creationOptions_.__bindgen_anon_1.comp_ = compartment;
204+
}
205+
}
206+
165207
/// Create and define the interface object of a callback interface.
166208
pub fn create_callback_interface_object(
167209
cx: SafeJSContext,

components/script/dom/windowproxy.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,13 +637,11 @@ impl WindowProxy {
637637
// The old window proxy no longer owns this browsing context.
638638
SetProxyReservedSlot(old_js_proxy.get(), 0, &PrivateValue(ptr::null_mut()));
639639

640-
// Brain transpant the window proxy.
641-
// We need to do this, because the Window and WindowProxy
642-
// objects need to be in the same realm.
643-
// JS_TransplantObject does this by copying the contents
644-
// of the old window proxy to the new window proxy, then
645-
// making the old window proxy a cross-realm wrapper
646-
// pointing to the new window proxy.
640+
// Brain transpant the window proxy. Brain transplantation is
641+
// usually done to move a window proxy between compartments, but
642+
// that's not what we are doing here. We need to do this just
643+
// because we want to replace the wrapper's `ProxyTraps`, but we
644+
// don't want to update its identity.
647645
rooted!(in(*cx) let new_js_proxy = NewWindowProxy(*cx, window_jsobject, handler));
648646
debug!(
649647
"Transplanting proxy from {:p} to {:p}.",

0 commit comments

Comments
 (0)