script: generalize worker common code for non-dedicated worker globals (#44244)

Testing: No new tests needed this is a refactor and no behavior changes
for any existing workers.
part of https://github.com/servo/servo/issues/7458

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi
2026-04-15 19:13:22 +02:00
committed by GitHub
parent 3cd71cf05a
commit 93a57147ce
3 changed files with 11 additions and 9 deletions

View File

@@ -530,6 +530,7 @@ impl DedicatedWorkerGlobalScope {
worker_url.url(),
fetch_client,
ModuleOwner::Worker(Trusted::new(scope)),
Destination::Worker,
referrer,
credentials,
);

View File

@@ -571,10 +571,6 @@ impl WorkerGlobalScope {
/// onComplete algorithm defined inside <https://html.spec.whatwg.org/multipage/#run-a-worker>
#[expect(unsafe_code)]
pub(crate) fn on_complete(&self, cx: &mut js::context::JSContext, script: Option<Script>) {
let dedicated_worker_scope = self
.downcast::<DedicatedWorkerGlobalScope>()
.expect("Only DedicatedWorkerGlobalScope is supported for now");
// Step 1. If script is null or if script's error to rethrow is non-null, then:
let script = match script {
Some(Script::Classic(script)) if script.record.is_ok() => Script::Classic(script),
@@ -586,7 +582,9 @@ impl WorkerGlobalScope {
_ => {
// Step 1.1 Queue a global task on the DOM manipulation task source given
// worker's relevant global object to fire an event named error at worker.
dedicated_worker_scope.forward_simple_error_at_worker();
if let Some(dedicated) = self.downcast::<DedicatedWorkerGlobalScope>() {
dedicated.forward_simple_error_at_worker();
}
// TODO Step 1.2. Run the environment discarding steps for inside settings.
// Step 1.3 Abort these steps.
@@ -606,7 +604,7 @@ impl WorkerGlobalScope {
{
let mut realm = enter_auto_realm(cx, self);
let cx = &mut realm.current_realm();
define_all_exposed_interfaces(cx, dedicated_worker_scope.upcast());
define_all_exposed_interfaces(cx, self.upcast());
self.execution_ready.store(true, Ordering::Relaxed);
match script {
Script::Classic(script) => {
@@ -619,7 +617,9 @@ impl WorkerGlobalScope {
},
_ => unreachable!(),
}
dedicated_worker_scope.fire_queued_messages(CanGc::from_cx(cx));
if let Some(dedicated) = self.downcast::<DedicatedWorkerGlobalScope>() {
dedicated.fire_queued_messages(CanGc::from_cx(cx));
}
}
}

View File

@@ -1224,6 +1224,7 @@ pub(crate) fn fetch_a_module_worker_script_graph(
url: ServoUrl,
fetch_client: ModuleFetchClient,
owner: ModuleOwner,
destination: Destination,
referrer: Referrer,
credentials_mode: CredentialsMode,
) {
@@ -1247,7 +1248,7 @@ pub(crate) fn fetch_a_module_worker_script_graph(
url,
fetch_client.clone(),
owner.clone(),
Destination::Worker,
destination,
options,
referrer,
None,
@@ -1265,7 +1266,7 @@ pub(crate) fn fetch_a_module_worker_script_graph(
cx,
module,
fetch_client,
Destination::Worker,
destination,
owner,
);
},