Files
servo/components/script/realms.rs
Sam d1ba789482 script: Obtain &mut JSContext at the start of the script and pass it down to msg handlers (#41564)
At the start of the script we create first (and only[^1]) safe
`JSContext`, from where we should pass it down to every code that needs
the cx. This PR brings it all the way down to message handlers. There is
clear separation between functions that use new model `&mut JSContext`
and the ones that use the old model (usually they take can_gc).

[^1]: Another place of construction are SM hooks (that pass us down raw
JSContext), but to reach that point we already needed to call another
function that takes `&mut JSContext`, so one can look at this case as
reentering/reborrowing (we derive new shorter `&mut JSContext` from the
old one).

Testing: This is just refactor, but should covered by WPT tests.
Part of #40600

Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
2025-12-30 18:19:41 +00:00

21 lines
744 B
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use js::context::JSContext;
use js::jsapi::JSAutoRealm;
use js::realm::AutoRealm;
pub(crate) use script_bindings::realms::{AlreadyInRealm, InRealm};
use script_bindings::reflector::DomObject;
pub(crate) fn enter_realm(object: &impl DomObject) -> JSAutoRealm {
script_bindings::realms::enter_realm::<crate::DomTypeHolder>(object)
}
pub(crate) fn enter_auto_realm<'cx>(
cx: &'cx mut JSContext,
object: &impl DomObject,
) -> AutoRealm<'cx> {
script_bindings::realms::enter_auto_realm::<crate::DomTypeHolder>(cx, object)
}