script: Refactor channels in ScriptThread into receivers and senders (#34776)

Create two new data structures in the `script` crate to hold senders and
receiver:

- `ScriptThreadSenders`: holds all outgoing channels from the
  `ScriptThread` including a channel to the `ScriptThread` itself. The
  ultimate goal with this is to reduce duplication by giving a boxed
  version of this this to `Window`s.
- `ScriptThradReceivers`: holds all incoming channels to the
  `ScriptThread`. This isn't cloenable like the senders. This is used to
  abstract away `recv()` and `try_recv()` methods used to make the
  `ScriptThread` event loop easier to read.

In addition:

- The many duplicated `ScriptThread` self-senders for the `TaskManager`
  have been removed and, in general, a lot of boilerplate is removed as
  well.
- Visibilty of all methods affected by this change is changed to
  `pub(crate)` in order to take advantage of dead code detection. Some
  dead code produced from macros is removed.
- Some conversion code is refactord into implementations of the `From`
  trait.
- The names of channels uses a standard "sender" and "receiver" naming
  as well as trying to be descriptive of where they go in `ScriptThread`
  as well as `InitialScriptState`

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2024-12-26 04:34:54 +01:00 committed by GitHub
parent 1e95712772
commit 5f927a2c28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 732 additions and 902 deletions

View file

@ -35,7 +35,7 @@ impl ScriptChan for SendableWorkerScriptChan {
self.sender.send(msg).map_err(|_| ())
}
fn clone(&self) -> Box<dyn ScriptChan + Send> {
fn as_boxed(&self) -> Box<dyn ScriptChan + Send> {
Box::new(SendableWorkerScriptChan {
sender: self.sender.clone(),
worker: self.worker.clone(),
@ -62,7 +62,7 @@ impl ScriptChan for WorkerThreadWorkerChan {
self.sender.send(msg).map_err(|_| ())
}
fn clone(&self) -> Box<dyn ScriptChan + Send> {
fn as_boxed(&self) -> Box<dyn ScriptChan + Send> {
Box::new(WorkerThreadWorkerChan {
sender: self.sender.clone(),
worker: self.worker.clone(),

View file

@ -418,7 +418,7 @@ impl DedicatedWorkerGlobalScope {
worker_url,
devtools_mpsc_port,
runtime,
parent_sender.clone(),
parent_sender.as_boxed(),
own_sender,
receiver,
closing,

View file

@ -185,10 +185,11 @@ use crate::dom::windowproxy::WindowProxy;
use crate::dom::xpathevaluator::XPathEvaluator;
use crate::fetch::FetchCanceller;
use crate::iframe_collection::IFrameCollection;
use crate::messaging::MainThreadScriptMsg;
use crate::network_listener::{NetworkListener, PreInvoke};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
use crate::script_runtime::{CanGc, CommonScriptMsg, ScriptThreadEventCategory};
use crate::script_thread::{with_script_thread, MainThreadScriptMsg, ScriptThread};
use crate::script_thread::{with_script_thread, ScriptThread};
use crate::stylesheet_set::StylesheetSetRef;
use crate::task::TaskBox;
use crate::task_source::{TaskSource, TaskSourceName};

View file

@ -127,6 +127,7 @@ use crate::dom::webgpu::identityhub::IdentityHub;
use crate::dom::window::Window;
use crate::dom::workerglobalscope::WorkerGlobalScope;
use crate::dom::workletglobalscope::WorkletGlobalScope;
use crate::messaging::MainThreadScriptChan;
use crate::microtask::{Microtask, MicrotaskQueue, UserMicrotask};
use crate::network_listener::{NetworkListener, PreInvoke};
use crate::realms::{enter_realm, AlreadyInRealm, InRealm};
@ -134,7 +135,7 @@ use crate::script_module::{DynamicModuleList, ModuleScript, ModuleTree, ScriptFe
use crate::script_runtime::{
CanGc, CommonScriptMsg, JSContext as SafeJSContext, ScriptChan, ScriptPort, ThreadSafeJSContext,
};
use crate::script_thread::{with_script_thread, MainThreadScriptChan, ScriptThread};
use crate::script_thread::{with_script_thread, ScriptThread};
use crate::security_manager::CSPViolationReporter;
use crate::task::TaskCanceller;
use crate::task_source::dom_manipulation::DOMManipulationTaskSource;
@ -2581,7 +2582,9 @@ impl GlobalScope {
/// `ScriptChan` to send messages to the event loop of this global scope.
pub fn script_chan(&self) -> Box<dyn ScriptChan + Send> {
if let Some(window) = self.downcast::<Window>() {
return MainThreadScriptChan(window.main_thread_script_chan().clone()).clone();
return Box::new(
MainThreadScriptChan(window.main_thread_script_chan().clone()).clone(),
);
}
if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
return worker.script_chan();

View file

@ -86,7 +86,7 @@ pub struct PaintWorkletGlobalScope {
impl PaintWorkletGlobalScope {
#[allow(unsafe_code)]
pub fn new(
pub(crate) fn new(
runtime: &Runtime,
pipeline_id: PipelineId,
base_url: ServoUrl,

View file

@ -143,7 +143,7 @@ impl ScriptChan for ServiceWorkerChan {
.map_err(|_| ())
}
fn clone(&self) -> Box<dyn ScriptChan + Send> {
fn as_boxed(&self) -> Box<dyn ScriptChan + Send> {
Box::new(ServiceWorkerChan {
sender: self.sender.clone(),
})

View file

@ -31,7 +31,7 @@ pub struct TestWorkletGlobalScope {
impl TestWorkletGlobalScope {
#[allow(unsafe_code)]
pub fn new(
pub(crate) fn new(
runtime: &Runtime,
pipeline_id: PipelineId,
base_url: ServoUrl,

View file

@ -145,15 +145,15 @@ use crate::dom::windowproxy::{WindowProxy, WindowProxyHandler};
use crate::dom::worklet::Worklet;
use crate::dom::workletglobalscope::WorkletGlobalScopeType;
use crate::layout_image::fetch_image_for_layout;
use crate::messaging::{
ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg, SendableMainThreadScriptChan,
};
use crate::microtask::MicrotaskQueue;
use crate::realms::{enter_realm, InRealm};
use crate::script_runtime::{
CanGc, CommonScriptMsg, JSContext, Runtime, ScriptChan, ScriptPort, ScriptThreadEventCategory,
};
use crate::script_thread::{
ImageCacheMsg, MainThreadScriptChan, MainThreadScriptMsg, ScriptThread,
SendableMainThreadScriptChan,
};
use crate::script_thread::ScriptThread;
use crate::task_manager::TaskManager;
use crate::task_source::{TaskSource, TaskSourceName};
use crate::timers::{IsInterval, TimerCallback};
@ -453,7 +453,7 @@ impl Window {
self.js_runtime.borrow()
}
pub fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
pub(crate) fn main_thread_script_chan(&self) -> &Sender<MainThreadScriptMsg> {
&self.script_chan.0
}
@ -2683,7 +2683,7 @@ impl Window {
impl Window {
#[allow(unsafe_code)]
#[allow(clippy::too_many_arguments)]
pub fn new(
pub(crate) fn new(
runtime: Rc<Runtime>,
script_chan: MainThreadScriptChan,
task_manager: TaskManager,

View file

@ -49,9 +49,10 @@ use crate::dom::workletglobalscope::{
WorkletGlobalScope, WorkletGlobalScopeInit, WorkletGlobalScopeType, WorkletTask,
};
use crate::fetch::load_whole_resource;
use crate::messaging::MainThreadScriptMsg;
use crate::realms::InRealm;
use crate::script_runtime::{CanGc, CommonScriptMsg, Runtime, ScriptThreadEventCategory};
use crate::script_thread::{MainThreadScriptMsg, ScriptThread};
use crate::script_thread::ScriptThread;
use crate::task::TaskBox;
use crate::task_source::TaskSourceName;
@ -277,7 +278,7 @@ impl Drop for WorkletThreadPool {
impl WorkletThreadPool {
/// Create a new thread pool and spawn the threads.
/// When the thread pool is dropped, the threads will be asked to quit.
pub fn spawn(global_init: WorkletGlobalScopeInit) -> WorkletThreadPool {
pub(crate) fn spawn(global_init: WorkletGlobalScopeInit) -> WorkletThreadPool {
let primary_role = WorkletThreadRole::new(false, false);
let hot_backup_role = WorkletThreadRole::new(true, false);
let cold_backup_role = WorkletThreadRole::new(false, true);

View file

@ -27,9 +27,9 @@ use crate::dom::testworkletglobalscope::{TestWorkletGlobalScope, TestWorkletTask
#[cfg(feature = "webgpu")]
use crate::dom::webgpu::identityhub::IdentityHub;
use crate::dom::worklet::WorkletExecutor;
use crate::messaging::MainThreadScriptMsg;
use crate::script_module::ScriptFetchOptions;
use crate::script_runtime::{CanGc, JSContext};
use crate::script_thread::MainThreadScriptMsg;
#[dom_struct]
/// <https://drafts.css-houdini.org/worklets/#workletglobalscope>
@ -49,7 +49,7 @@ pub struct WorkletGlobalScope {
impl WorkletGlobalScope {
/// Create a new heap-allocated `WorkletGlobalScope`.
pub fn new(
pub(crate) fn new(
scope_type: WorkletGlobalScopeType,
runtime: &Runtime,
pipeline_id: PipelineId,
@ -76,7 +76,7 @@ impl WorkletGlobalScope {
}
/// Create a new stack-allocated `WorkletGlobalScope`.
pub fn new_inherited(
pub(crate) fn new_inherited(
pipeline_id: PipelineId,
base_url: ServoUrl,
executor: WorkletExecutor,
@ -172,7 +172,7 @@ impl WorkletGlobalScope {
/// Resources required by workletglobalscopes
#[derive(Clone)]
pub struct WorkletGlobalScopeInit {
pub(crate) struct WorkletGlobalScopeInit {
/// Channel to the main script thread
pub to_script_thread_sender: Sender<MainThreadScriptMsg>,
/// Channel to a resource thread