From e21ea2a135a8e85244b13b1ef06f23294d580663 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Mon, 25 Aug 2025 05:31:51 -0700 Subject: [PATCH] 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 --- components/script/dom/globalscope.rs | 50 ++++++++++++++++------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 7a6c78c1a7c..3d6d05f65c7 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -2253,16 +2253,16 @@ impl GlobalScope { #[allow(unsafe_code)] pub(crate) unsafe fn from_object(obj: *mut JSObject) -> DomRoot { assert!(!obj.is_null()); - let global = GetNonCCWObjectGlobal(obj); - global_scope_from_global_static(global) + let global = unsafe { GetNonCCWObjectGlobal(obj) }; + unsafe { global_scope_from_global_static(global) } } /// Returns the global scope for the given JSContext #[allow(unsafe_code)] pub(crate) unsafe fn from_context(cx: *mut JSContext, _realm: InRealm) -> DomRoot { - let global = CurrentGlobalOrNull(cx); + let global = unsafe { CurrentGlobalOrNull(cx) }; 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 @@ -2278,11 +2278,13 @@ impl GlobalScope { mut obj: *mut JSObject, cx: *mut JSContext, ) -> DomRoot { - if IsWrapper(obj) { - obj = UnwrapObjectDynamic(obj, cx, /* stopAtWindowProxy = */ false); - assert!(!obj.is_null()); + unsafe { + if IsWrapper(obj) { + 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) { @@ -3457,31 +3459,37 @@ unsafe fn global_scope_from_global( global: *mut JSObject, cx: *mut JSContext, ) -> DomRoot { - assert!(!global.is_null()); - let clasp = get_object_class(global); - assert_ne!( - ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), - 0 - ); - root_from_object(global, cx).unwrap() + unsafe { + assert!(!global.is_null()); + let clasp = get_object_class(global); + assert_ne!( + ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), + 0 + ); + root_from_object(global, cx).unwrap() + } } /// Returns the Rust global scope from a JS global object. #[allow(unsafe_code)] unsafe fn global_scope_from_global_static(global: *mut JSObject) -> DomRoot { assert!(!global.is_null()); - let clasp = get_object_class(global); - assert_ne!( - ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), - 0 - ); + let clasp = unsafe { get_object_class(global) }; + + unsafe { + assert_ne!( + ((*clasp).flags & (JSCLASS_IS_DOMJSCLASS | JSCLASS_IS_GLOBAL)), + 0 + ); + } + root_from_object_static(global).unwrap() } #[allow(unsafe_code)] impl GlobalScopeHelpers for GlobalScope { unsafe fn from_context(cx: *mut JSContext, realm: InRealm) -> DomRoot { - GlobalScope::from_context(cx, realm) + unsafe { GlobalScope::from_context(cx, realm) } } fn get_cx() -> SafeJSContext {