diff --git a/components/script/dom/debuggerglobalscope.rs b/components/script/dom/debuggerglobalscope.rs index afe693a38a8..3833b24889b 100644 --- a/components/script/dom/debuggerglobalscope.rs +++ b/components/script/dom/debuggerglobalscope.rs @@ -12,7 +12,6 @@ use embedder_traits::JavaScriptEvaluationError; use embedder_traits::resources::{self, Resource}; use ipc_channel::ipc::IpcSender; use js::jsval::UndefinedValue; -use js::rust::Runtime; use js::rust::wrappers::JS_DefineDebuggerObject; use net_traits::ResourceThreads; use profile_traits::{mem, time}; @@ -61,7 +60,6 @@ impl DebuggerGlobalScope { /// those threads can’t generate pipeline ids, and they only contain one debuggee from one pipeline #[allow(unsafe_code, clippy::too_many_arguments)] pub(crate) fn new( - runtime: &Runtime, debugger_pipeline_id: PipelineId, script_to_devtools_sender: Option>, devtools_to_script_sender: IpcSender, @@ -93,12 +91,8 @@ impl DebuggerGlobalScope { devtools_to_script_sender, get_possible_breakpoints_result_sender: RefCell::new(None), }); - let global = unsafe { - DebuggerGlobalScopeBinding::Wrap::( - JSContext::from_ptr(runtime.cx()), - global, - ) - }; + let global = + DebuggerGlobalScopeBinding::Wrap::(GlobalScope::get_cx(), global); let realm = enter_realm(&*global); define_all_exposed_interfaces(global.upcast(), InRealm::entered(&realm), can_gc); diff --git a/components/script/dom/dedicatedworkerglobalscope.rs b/components/script/dom/dedicatedworkerglobalscope.rs index 59aa4d17bf3..423d33d9f5c 100644 --- a/components/script/dom/dedicatedworkerglobalscope.rs +++ b/components/script/dom/dedicatedworkerglobalscope.rs @@ -325,7 +325,6 @@ impl DedicatedWorkerGlobalScope { control_receiver: Receiver, insecure_requests_policy: InsecureRequestsPolicy, ) -> DomRoot { - let cx = runtime.cx(); let scope = Box::new(DedicatedWorkerGlobalScope::new_inherited( init, worker_name, @@ -344,12 +343,10 @@ impl DedicatedWorkerGlobalScope { control_receiver, insecure_requests_policy, )); - unsafe { - DedicatedWorkerGlobalScopeBinding::Wrap::( - SafeJSContext::from_ptr(cx), - scope, - ) - } + DedicatedWorkerGlobalScopeBinding::Wrap::( + GlobalScope::get_cx(), + scope, + ) } /// @@ -430,7 +427,6 @@ impl DedicatedWorkerGlobalScope { Runtime::new_with_parent(Some(parent), Some(task_source)) }; let debugger_global = DebuggerGlobalScope::new( - &runtime, pipeline_id, init.to_devtools_sender.clone(), init.from_devtools_sender diff --git a/components/script/dom/dissimilaroriginwindow.rs b/components/script/dom/dissimilaroriginwindow.rs index 96554b605b6..23ebd2e90f6 100644 --- a/components/script/dom/dissimilaroriginwindow.rs +++ b/components/script/dom/dissimilaroriginwindow.rs @@ -73,7 +73,7 @@ impl DissimilarOriginWindow { window_proxy: Dom::from_ref(window_proxy), location: Default::default(), }); - unsafe { DissimilarOriginWindowBinding::Wrap::(cx, win) } + DissimilarOriginWindowBinding::Wrap::(cx, win) } pub(crate) fn window_proxy(&self) -> DomRoot { diff --git a/components/script/dom/paintworkletglobalscope.rs b/components/script/dom/paintworkletglobalscope.rs index 7beffb5c6bd..798d438409a 100644 --- a/components/script/dom/paintworkletglobalscope.rs +++ b/components/script/dom/paintworkletglobalscope.rs @@ -19,8 +19,8 @@ use js::jsapi::{ JS_IsExceptionPending, JSAutoRealm, JSObject, NewArrayObject, Value, }; use js::jsval::{JSVal, ObjectValue, UndefinedValue}; +use js::rust::HandleValue; use js::rust::wrappers::{Call, Construct1}; -use js::rust::{HandleValue, Runtime}; use net_traits::image_cache::ImageCache; use pixels::PixelFormat; use script_traits::{DrawAPaintImageResult, PaintWorkletError, Painter}; @@ -43,12 +43,13 @@ use crate::dom::bindings::reflector::DomObject; use crate::dom::bindings::root::{Dom, DomRoot}; use crate::dom::bindings::str::DOMString; use crate::dom::cssstylevalue::CSSStyleValue; +use crate::dom::globalscope::GlobalScope; use crate::dom::paintrenderingcontext2d::PaintRenderingContext2D; use crate::dom::paintsize::PaintSize; use crate::dom::stylepropertymapreadonly::StylePropertyMapReadOnly; use crate::dom::worklet::WorkletExecutor; use crate::dom::workletglobalscope::{WorkletGlobalScope, WorkletGlobalScopeInit, WorkletTask}; -use crate::script_runtime::{CanGc, JSContext}; +use crate::script_runtime::CanGc; /// #[dom_struct] @@ -86,7 +87,6 @@ pub(crate) struct PaintWorkletGlobalScope { impl PaintWorkletGlobalScope { #[allow(unsafe_code)] pub(crate) fn new( - runtime: &Runtime, pipeline_id: PipelineId, base_url: ServoUrl, executor: WorkletExecutor, @@ -119,12 +119,7 @@ impl PaintWorkletGlobalScope { missing_image_urls: Vec::new(), }), }); - unsafe { - PaintWorkletGlobalScopeBinding::Wrap::( - JSContext::from_ptr(runtime.cx()), - global, - ) - } + PaintWorkletGlobalScopeBinding::Wrap::(GlobalScope::get_cx(), global) } pub(crate) fn image_cache(&self) -> Arc { diff --git a/components/script/dom/serviceworkerglobalscope.rs b/components/script/dom/serviceworkerglobalscope.rs index 6f4f1bf01d6..51cf1d7a07c 100644 --- a/components/script/dom/serviceworkerglobalscope.rs +++ b/components/script/dom/serviceworkerglobalscope.rs @@ -265,7 +265,6 @@ impl ServiceWorkerGlobalScope { control_receiver: Receiver, closing: Arc, ) -> DomRoot { - let cx = runtime.cx(); let scope = Box::new(ServiceWorkerGlobalScope::new_inherited( init, worker_url, @@ -279,12 +278,7 @@ impl ServiceWorkerGlobalScope { control_receiver, closing, )); - unsafe { - ServiceWorkerGlobalScopeBinding::Wrap::( - SafeJSContext::from_ptr(cx), - scope, - ) - } + ServiceWorkerGlobalScopeBinding::Wrap::(GlobalScope::get_cx(), scope) } /// diff --git a/components/script/dom/testworkletglobalscope.rs b/components/script/dom/testworkletglobalscope.rs index ac73f68797f..c3764b3efb3 100644 --- a/components/script/dom/testworkletglobalscope.rs +++ b/components/script/dom/testworkletglobalscope.rs @@ -7,7 +7,6 @@ use std::collections::HashMap; use base::id::PipelineId; use crossbeam_channel::Sender; use dom_struct::dom_struct; -use js::rust::Runtime; use servo_url::ServoUrl; use crate::dom::bindings::cell::DomRefCell; @@ -15,9 +14,9 @@ use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding; use crate::dom::bindings::codegen::Bindings::TestWorkletGlobalScopeBinding::TestWorkletGlobalScopeMethods; use crate::dom::bindings::root::DomRoot; use crate::dom::bindings::str::DOMString; +use crate::dom::globalscope::GlobalScope; use crate::dom::worklet::WorkletExecutor; use crate::dom::workletglobalscope::{WorkletGlobalScope, WorkletGlobalScopeInit}; -use crate::script_runtime::JSContext; // check-tidy: no specs after this line @@ -32,7 +31,6 @@ pub(crate) struct TestWorkletGlobalScope { impl TestWorkletGlobalScope { #[allow(unsafe_code)] pub(crate) fn new( - runtime: &Runtime, pipeline_id: PipelineId, base_url: ServoUrl, executor: WorkletExecutor, @@ -51,12 +49,7 @@ impl TestWorkletGlobalScope { ), lookup_table: Default::default(), }); - unsafe { - TestWorkletGlobalScopeBinding::Wrap::( - JSContext::from_ptr(runtime.cx()), - global, - ) - } + TestWorkletGlobalScopeBinding::Wrap::(GlobalScope::get_cx(), global) } pub(crate) fn perform_a_worklet_task(&self, task: TestWorkletTask) { diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index baa67ead879..9adb5a022bc 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -3202,9 +3202,7 @@ impl Window { endpoints_list: Default::default(), }); - unsafe { - WindowBinding::Wrap::(JSContext::from_ptr(runtime.cx()), win) - } + WindowBinding::Wrap::(GlobalScope::get_cx(), win) } pub(crate) fn pipeline_id(&self) -> PipelineId { diff --git a/components/script/dom/worklet.rs b/components/script/dom/worklet.rs index 2058e05599d..650a101033c 100644 --- a/components/script/dom/worklet.rs +++ b/components/script/dom/worklet.rs @@ -629,7 +629,6 @@ impl WorkletThread { let executor = WorkletExecutor::new(worklet_id, self.primary_sender.clone()); let result = WorkletGlobalScope::new( global_type, - &self.runtime, pipeline_id, base_url, executor, diff --git a/components/script/dom/workletglobalscope.rs b/components/script/dom/workletglobalscope.rs index 5c185fae919..117fbf56302 100644 --- a/components/script/dom/workletglobalscope.rs +++ b/components/script/dom/workletglobalscope.rs @@ -13,7 +13,6 @@ use dom_struct::dom_struct; use embedder_traits::JavaScriptEvaluationError; use ipc_channel::ipc::IpcSender; use js::jsval::UndefinedValue; -use js::rust::Runtime; use net_traits::ResourceThreads; use net_traits::image_cache::ImageCache; use profile_traits::{mem, time}; @@ -56,7 +55,6 @@ impl WorkletGlobalScope { /// Create a new heap-allocated `WorkletGlobalScope`. pub(crate) fn new( scope_type: WorkletGlobalScopeType, - runtime: &Runtime, pipeline_id: PipelineId, base_url: ServoUrl, executor: WorkletExecutor, @@ -65,14 +63,12 @@ impl WorkletGlobalScope { let scope: DomRoot = match scope_type { #[cfg(feature = "testbinding")] WorkletGlobalScopeType::Test => DomRoot::upcast(TestWorkletGlobalScope::new( - runtime, pipeline_id, base_url, executor, init, )), WorkletGlobalScopeType::Paint => DomRoot::upcast(PaintWorkletGlobalScope::new( - runtime, pipeline_id, base_url, executor, diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index e03430f694c..5f8e22a42a8 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -939,7 +939,6 @@ impl ScriptThread { pipeline_id, }; let debugger_global = DebuggerGlobalScope::new( - &js_runtime.clone(), PipelineId::new(), senders.devtools_server_sender.clone(), senders.devtools_client_to_script_thread_sender.clone(), diff --git a/components/script_bindings/codegen/codegen.py b/components/script_bindings/codegen/codegen.py index 0a7f97db6ea..bccabb5ecba 100644 --- a/components/script_bindings/codegen/codegen.py +++ b/components/script_bindings/codegen/codegen.py @@ -3312,7 +3312,7 @@ class CGWrapGlobalMethod(CGAbstractMethod): Argument(f"Box<{descriptor.concreteType}>", 'object')] retval = f'DomRoot<{descriptor.concreteType}>' CGAbstractMethod.__init__(self, descriptor, 'Wrap', retval, args, - pub=True, unsafe=True, + pub=True, extra_decorators=['#[cfg_attr(crown, allow(crown::unrooted_must_root))]'], templateArgs=['D: DomTypes']) self.properties = properties @@ -3328,35 +3328,37 @@ class CGWrapGlobalMethod(CGAbstractMethod): membersStr = "\n".join(members) return CGGeneric(f""" -let raw = Root::new(MaybeUnreflectedDom::from_box(object)); -let origin = (*raw.as_ptr()).upcast::().origin(); +unsafe {{ + let raw = Root::new(MaybeUnreflectedDom::from_box(object)); + let origin = (*raw.as_ptr()).upcast::().origin(); -rooted!(in(*cx) let mut obj = ptr::null_mut::()); -create_global_object::( - cx, - &Class.get().base, - raw.as_ptr() as *const libc::c_void, - {TRACE_HOOK_NAME}::, - obj.handle_mut(), - origin, - {"true" if self.descriptor.useSystemCompartment else "false"}); -assert!(!obj.is_null()); + rooted!(in(*cx) let mut obj = ptr::null_mut::()); + create_global_object::( + cx, + &Class.get().base, + raw.as_ptr() as *const libc::c_void, + {TRACE_HOOK_NAME}::, + obj.handle_mut(), + origin, + {"true" if self.descriptor.useSystemCompartment else "false"}); + assert!(!obj.is_null()); -let root = raw.reflect_with(obj.get()); + let root = raw.reflect_with(obj.get()); -let _ac = JSAutoRealm::new(*cx, obj.get()); -rooted!(in(*cx) let mut canonical_proto = ptr::null_mut::()); -GetProtoObject::(cx, obj.handle(), canonical_proto.handle_mut()); -assert!(JS_SetPrototype(*cx, obj.handle(), canonical_proto.handle())); -let mut immutable = false; -assert!(JS_SetImmutablePrototype(*cx, obj.handle(), &mut immutable)); -assert!(immutable); + let _ac = JSAutoRealm::new(*cx, obj.get()); + rooted!(in(*cx) let mut canonical_proto = ptr::null_mut::()); + GetProtoObject::(cx, obj.handle(), canonical_proto.handle_mut()); + assert!(JS_SetPrototype(*cx, obj.handle(), canonical_proto.handle())); + let mut immutable = false; + assert!(JS_SetImmutablePrototype(*cx, obj.handle(), &mut immutable)); + assert!(immutable); -{membersStr} + {membersStr} -{CopyLegacyUnforgeablePropertiesToInstance(self.descriptor)} + {CopyLegacyUnforgeablePropertiesToInstance(self.descriptor)} -DomRoot::from_ref(&*root)\ + DomRoot::from_ref(&*root) +}} """)