script: Wrap unsafe code in globalscope.rs in unsafe {} blocks (#38908)

This is a step toward fixing Rust warnings about unsafe code needing to
be wrapped in `unsafe {}` blocks.

Testing: This does not change behavior is thus covered by existing
tests.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-25 05:31:51 -07:00 committed by GitHub
parent fb1c0a4c48
commit e21ea2a135
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2253,16 +2253,16 @@ impl GlobalScope {
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub(crate) unsafe fn from_object(obj: *mut JSObject) -> DomRoot<Self> { pub(crate) unsafe fn from_object(obj: *mut JSObject) -> DomRoot<Self> {
assert!(!obj.is_null()); assert!(!obj.is_null());
let global = GetNonCCWObjectGlobal(obj); let global = unsafe { GetNonCCWObjectGlobal(obj) };
global_scope_from_global_static(global) unsafe { global_scope_from_global_static(global) }
} }
/// Returns the global scope for the given JSContext /// Returns the global scope for the given JSContext
#[allow(unsafe_code)] #[allow(unsafe_code)]
pub(crate) unsafe fn from_context(cx: *mut JSContext, _realm: InRealm) -> DomRoot<Self> { pub(crate) unsafe fn from_context(cx: *mut JSContext, _realm: InRealm) -> DomRoot<Self> {
let global = CurrentGlobalOrNull(cx); let global = unsafe { CurrentGlobalOrNull(cx) };
assert!(!global.is_null()); assert!(!global.is_null());
global_scope_from_global(global, cx) unsafe { global_scope_from_global(global, cx) }
} }
/// Returns the global scope for the given SafeJSContext /// Returns the global scope for the given SafeJSContext
@ -2278,11 +2278,13 @@ impl GlobalScope {
mut obj: *mut JSObject, mut obj: *mut JSObject,
cx: *mut JSContext, cx: *mut JSContext,
) -> DomRoot<Self> { ) -> DomRoot<Self> {
if IsWrapper(obj) { unsafe {
obj = UnwrapObjectDynamic(obj, cx, /* stopAtWindowProxy = */ false); if IsWrapper(obj) {
assert!(!obj.is_null()); obj = UnwrapObjectDynamic(obj, cx, /* stopAtWindowProxy = */ false);
assert!(!obj.is_null());
}
GlobalScope::from_object(obj)
} }
GlobalScope::from_object(obj)
} }
pub(crate) fn add_uncaught_rejection(&self, rejection: HandleObject) { pub(crate) fn add_uncaught_rejection(&self, rejection: HandleObject) {
@ -3457,31 +3459,37 @@ unsafe fn global_scope_from_global(
global: *mut JSObject, global: *mut JSObject,
cx: *mut JSContext, cx: *mut JSContext,
) -> DomRoot<GlobalScope> { ) -> DomRoot<GlobalScope> {
assert!(!global.is_null()); unsafe {
let clasp = get_object_class(global); assert!(!global.is_null());
assert_ne!( let clasp = get_object_class(global);
((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), assert_ne!(
0 ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)),
); 0
root_from_object(global, cx).unwrap() );
root_from_object(global, cx).unwrap()
}
} }
/// Returns the Rust global scope from a JS global object. /// Returns the Rust global scope from a JS global object.
#[allow(unsafe_code)] #[allow(unsafe_code)]
unsafe fn global_scope_from_global_static(global: *mut JSObject) -> DomRoot<GlobalScope> { unsafe fn global_scope_from_global_static(global: *mut JSObject) -> DomRoot<GlobalScope> {
assert!(!global.is_null()); assert!(!global.is_null());
let clasp = get_object_class(global); let clasp = unsafe { get_object_class(global) };
assert_ne!(
((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), unsafe {
0 assert_ne!(
); ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)),
0
);
}
root_from_object_static(global).unwrap() root_from_object_static(global).unwrap()
} }
#[allow(unsafe_code)] #[allow(unsafe_code)]
impl GlobalScopeHelpers<crate::DomTypeHolder> for GlobalScope { impl GlobalScopeHelpers<crate::DomTypeHolder> for GlobalScope {
unsafe fn from_context(cx: *mut JSContext, realm: InRealm) -> DomRoot<Self> { unsafe fn from_context(cx: *mut JSContext, realm: InRealm) -> DomRoot<Self> {
GlobalScope::from_context(cx, realm) unsafe { GlobalScope::from_context(cx, realm) }
} }
fn get_cx() -> SafeJSContext { fn get_cx() -> SafeJSContext {