script: Return a Result from GlobalScope::evaluate_script_on_global_with_result (#38549)

Make GlobalScope::evaluate_script_on_global_with_result return a Result
instead of a boolean. This is the first step to resolve issue #37810.

Testing: Should not break or fix any existing tests

---------

Signed-off-by: Rodion Borovyk <rodion.borovyk@gmail.com>
This commit is contained in:
Rodion Borovyk 2025-08-10 18:51:46 +02:00 committed by GitHub
parent 86c37a380b
commit 4f8731d562
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 45 additions and 28 deletions

View file

@ -6,6 +6,7 @@ use base::id::PipelineId;
use constellation_traits::ScriptToConstellationChan;
use devtools_traits::{ScriptToDevtoolsControlMsg, WorkerId};
use dom_struct::dom_struct;
use embedder_traits::JavaScriptEvaluationError;
use embedder_traits::resources::{self, Resource};
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
@ -104,7 +105,7 @@ impl DebuggerGlobalScope {
GlobalScope::get_cx()
}
fn evaluate_js(&self, script: &str, can_gc: CanGc) -> bool {
fn evaluate_js(&self, script: &str, can_gc: CanGc) -> Result<(), JavaScriptEvaluationError> {
rooted!(in (*Self::get_cx()) let mut rval = UndefinedValue());
self.global_scope.evaluate_js_on_global_with_result(
script,
@ -117,7 +118,10 @@ impl DebuggerGlobalScope {
}
pub(crate) fn execute(&self, can_gc: CanGc) {
if !self.evaluate_js(&resources::read_string(Resource::DebuggerJS), can_gc) {
if self
.evaluate_js(&resources::read_string(Resource::DebuggerJS), can_gc)
.is_err()
{
let ar = enter_realm(self);
report_pending_exception(Self::get_cx(), true, InRealm::Entered(&ar), can_gc);
}

View file

@ -26,7 +26,7 @@ use content_security_policy::CspList;
use crossbeam_channel::Sender;
use devtools_traits::{PageError, ScriptToDevtoolsControlMsg};
use dom_struct::dom_struct;
use embedder_traits::EmbedderMsg;
use embedder_traits::{EmbedderMsg, JavaScriptEvaluationError};
use ipc_channel::ipc::{self, IpcSender};
use ipc_channel::router::ROUTER;
use js::glue::{IsWrapper, UnwrapObjectDynamic};
@ -2762,7 +2762,7 @@ impl GlobalScope {
script_base_url: ServoUrl,
can_gc: CanGc,
introduction_type: Option<&'static CStr>,
) -> bool {
) -> Result<(), JavaScriptEvaluationError> {
let source_code = SourceCode::Text(Rc::new(DOMString::from_string((*code).to_string())));
self.evaluate_script_on_global_with_result(
&source_code,
@ -2789,7 +2789,7 @@ impl GlobalScope {
script_base_url: ServoUrl,
can_gc: CanGc,
introduction_type: Option<&'static CStr>,
) -> bool {
) -> Result<(), JavaScriptEvaluationError> {
let cx = GlobalScope::get_cx();
let ar = enter_realm(self);
@ -2815,7 +2815,7 @@ impl GlobalScope {
if compiled_script.is_null() {
debug!("error compiling Dom string");
report_pending_exception(cx, true, InRealm::Entered(&ar), can_gc);
return false;
return Err(JavaScriptEvaluationError::CompilationFailure);
}
},
SourceCode::Compiled(pre_compiled_script) => {
@ -2865,10 +2865,11 @@ impl GlobalScope {
if !result {
debug!("error evaluating Dom string");
report_pending_exception(cx, true, InRealm::Entered(&ar), can_gc);
return Err(JavaScriptEvaluationError::EvaluationFailure);
}
maybe_resume_unwind();
result
Ok(())
}
}

View file

@ -1160,7 +1160,7 @@ impl HTMLScriptElement {
self.line_number as u32
};
rooted!(in(*GlobalScope::get_cx()) let mut rval = UndefinedValue());
window
_ = window
.as_global_scope()
.evaluate_script_on_global_with_result(
&script.code,

View file

@ -31,7 +31,7 @@ pub(crate) fn load_script(head: &HTMLHeadElement) {
Rc::new(DOMString::from_string(user_script.script))
);
let global_scope = win.as_global_scope();
global_scope.evaluate_script_on_global_with_result(
_ = global_scope.evaluate_script_on_global_with_result(
&script_text,
&user_script.source_file.map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
rval.handle_mut(),

View file

@ -691,7 +691,7 @@ impl WorkletThread {
// to the main script thread.
// https://github.com/w3c/css-houdini-drafts/issues/407
let ok = script
.map(|script| global_scope.evaluate_js(&script, can_gc))
.map(|s| global_scope.evaluate_js(&s, can_gc).is_ok())
.unwrap_or(false);
if !ok {

View file

@ -9,6 +9,7 @@ use constellation_traits::{ScriptToConstellationChan, ScriptToConstellationMessa
use crossbeam_channel::Sender;
use devtools_traits::ScriptToDevtoolsControlMsg;
use dom_struct::dom_struct;
use embedder_traits::JavaScriptEvaluationError;
use ipc_channel::ipc::IpcSender;
use js::jsval::UndefinedValue;
use js::rust::Runtime;
@ -124,7 +125,11 @@ impl WorkletGlobalScope {
}
/// Evaluate a JS script in this global.
pub(crate) fn evaluate_js(&self, script: &str, can_gc: CanGc) -> bool {
pub(crate) fn evaluate_js(
&self,
script: &str,
can_gc: CanGc,
) -> Result<(), JavaScriptEvaluationError> {
debug!("Evaluating Dom in a worklet.");
rooted!(in (*GlobalScope::get_cx()) let mut rval = UndefinedValue());
self.globalscope.evaluate_js_on_global_with_result(