From 1d6af09cff84cd608f9359d0a220081b1b5342eb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 30 Nov 2026 17:59:40 -0800 Subject: [PATCH] Scripts: a stop from end when show() fails ends the run When show() failed (an answer that could not be read, a host call that threw, or a handler that threw), the dialog's failure function called the host's end and dropped its result, so an Abort from end did not stop the run and the script could catch the error and finish. The failure function now returns that stop, and the answer and call paths end the run with it before the error reaches the script. --- crates/js/src/interp.rs | 23 ++++++++++----- crates/js/src/realm.rs | 17 +++++++---- crates/js/src/scriptui.rs | 22 +++++++++++---- crates/js/src/scriptui_tests.rs | 50 +++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 19 deletions(-) diff --git a/crates/js/src/interp.rs b/crates/js/src/interp.rs index 390dd3f5c8..517caa9c06 100644 --- a/crates/js/src/interp.rs +++ b/crates/js/src/interp.rs @@ -254,8 +254,10 @@ pub(crate) type PausableFn = fn(&mut Interp, Value, &[Value]) -> JsResult /// resume function gives. pub(crate) type ResumeFn = fn(&mut Interp, &[Value], Value) -> JsResult; -/// What a [`Pause`] does with its kept values when its host call fails. -pub(crate) type FailFn = fn(&mut Interp, &[Value]); +/// What a [`Pause`] does with its kept values when its host call fails. An error it gives that is +/// not thrown (a stop, such as a host's Abort) ends the run in place of a thrown failure; a thrown +/// one gives way to the failure. +pub(crate) type FailFn = fn(&mut Interp, &[Value]) -> JsResult<()>; /// The host call a [`PausableFn`] makes: the host function `host` with `args`. Its answer goes to /// `resume` with `keep`, at once when the host answers at once. When the host waits @@ -280,14 +282,16 @@ pub(crate) struct Resumption { impl Resumption { /// The built-in does not get the host's answer: its failure function runs with its kept - /// values, which `thrown` is kept from collection with. - pub(crate) fn fail(self, interp: &mut Interp, thrown: &Value) { - let Some(failed) = self.failed else { return }; + /// values, which `thrown` is kept from collection with. → the stop the failure function + /// gave, which ends the run in place of `thrown`. + pub(crate) fn fail(self, interp: &mut Interp, thrown: &Value) -> JsResult<()> { + let Some(failed) = self.failed else { return Ok(()) }; let mark = interp.temp_roots.len(); interp.temp_roots.extend(self.keep.iter().cloned()); interp.temp_roots.push(thrown.clone()); - failed(interp, &self.keep); + let r = failed(interp, &self.keep); interp.temp_roots.truncate(mark); + r } } @@ -3585,6 +3589,7 @@ impl Interp { Some(Func::Data { f, name, read }) => self.call_data(&f, &name, &Value::Undefined, &args, &read), _ => self.call(&host, &Value::Undefined, &args), }; + let mut stop = Ok(()); if let Err(e) = &answer && let Some(failed) = failed { @@ -3592,9 +3597,13 @@ impl Interp { if let Abrupt::Throw(v) = e { self.temp_roots.push(v.clone()); } - failed(self, &keep); + stop = failed(self, &keep); } self.temp_roots.truncate(mark); + // A stop from `failed` is not masked by a thrown error the script could catch. + if matches!(answer, Err(Abrupt::Throw(_))) { + stop?; + } let answer = answer?; let r = Resumption { resume, keep, failed }; if self.waiting && !was_waiting { diff --git a/crates/js/src/realm.rs b/crates/js/src/realm.rs index 4a810927ab..d673c92d26 100644 --- a/crates/js/src/realm.rs +++ b/crates/js/src/realm.rs @@ -591,14 +591,19 @@ impl Realm { let r = match written { // An answer that cannot be read as a value (too deep, too many entries, a stale host // reference) is thrown at the call, where the script can catch it, and a built-in - // that waited with the call does not get it: it fails as when its host call throws. + // that waited with the call does not get it: it fails as when its host call throws. A + // stop its failure gives (an Abort from the host) ends the run before the error is + // thrown. Err(Abrupt::Throw(thrown)) => { interp.stack.pop(); - if let Some(r) = interp.resuming.take() { - r.fail(interp, &thrown); - } - interp.answer_throws = true; - interp.push(thrown) + let stop = match interp.resuming.take() { + Some(r) => r.fail(interp, &thrown), + None => Ok(()), + }; + stop.and_then(|()| { + interp.answer_throws = true; + interp.push(thrown) + }) } written => written.and_then(|v| { interp.stack.pop(); diff --git a/crates/js/src/scriptui.rs b/crates/js/src/scriptui.rs index 47d8aeaffb..a5f74e7f54 100644 --- a/crates/js/src/scriptui.rs +++ b/crates/js/src/scriptui.rs @@ -1543,16 +1543,21 @@ fn show(i: &mut Interp, this: Value, _: &[Value]) -> JsResult { }) } -/// A host call of `show()` failed, or a handler threw: the dialog is no longer showing. -fn failed(i: &mut Interp, keep: &[Value]) { - let Some(win) = arg(keep, 0).as_object() else { return }; +/// A host call of `show()` failed, or a handler threw: the dialog is no longer showing. → a stop +/// (an Abort) the host's `end` gave; an error `end` throws gives way to the failure. +fn failed(i: &mut Interp, keep: &[Value]) -> JsResult<()> { + let Some(win) = arg(keep, 0).as_object() else { return Ok(()) }; if classes(i).ok().and_then(|c| c.showing) != Some(win) { - return; + return Ok(()); } set_showing(i, None); if let Ok(end) = host_fn(i, "end") { - let _ = i.call(&end, &Value::Undefined, &[Value::Number(0.0), Value::Bool(false)]); + match i.call(&end, &Value::Undefined, &[Value::Number(0.0), Value::Bool(false)]) { + Ok(_) | Err(Abrupt::Throw(_)) => {} + Err(stop) => return Err(stop), + } } + Ok(()) } /// `show()` ends with `result`. @@ -1611,10 +1616,15 @@ fn guarded(i: &mut Interp, keep: &[Value], r: impl FnOnce(&mut Interp) -> JsResu Abrupt::Throw(t) => vec![t.clone()], _ => Vec::new(), }; + let thrown_error = !thrown.is_empty(); let mark = i.temp_roots.len(); i.temp_roots.extend(thrown); - failed(i, keep); + let stop = failed(i, keep); i.temp_roots.truncate(mark); + // A stop from `end` is not masked by a thrown error the script could catch. + if thrown_error { + stop?; + } } v } diff --git a/crates/js/src/scriptui_tests.rs b/crates/js/src/scriptui_tests.rs index d8c7014ea0..dafe4dabc8 100644 --- a/crates/js/src/scriptui_tests.rs +++ b/crates/js/src/scriptui_tests.rs @@ -255,6 +255,56 @@ fn an_event_that_cannot_be_read_ends_show_and_the_dialog_can_show_again() { assert_eq!(seen.borrow().ends, vec![(0.0, false), (0.0, true)]); } +/// A realm whose `end` stops the run (an Abort) when `show()` ends without its result, whose +/// `open` throws when `open_fails`, and whose `wait` gives `events` in order, then waits. +fn stopping_realm(open_fails: bool, events: Vec) -> Realm { + let mut r = Realm::new(); + r.set_limits(Limits { fuel: 10_000_000, ..Limits::default() }); + let queue = RefCell::new(VecDeque::from(events)); + let open: HostFunction = Rc::new(move |_| if open_fails { Err(HostError::Error("no window".into())) } else { Ok(Data::Undefined) }); + let wait: HostFunction = Rc::new(move |_| queue.borrow_mut().pop_front().ok_or(HostError::Wait)); + let end: HostFunction = Rc::new(|args| match args.get(1) { + Some(Data::Bool(true)) => Ok(Data::Undefined), + _ => Err(HostError::Abort("stopped by the host".into())), + }); + let items = vec![("open".into(), HostItem::Function(open)), ("wait".into(), HostItem::Function(wait)), ("end".into(), HostItem::Function(end))]; + r.define_script_ui(items).unwrap(); + r +} + +fn is_host_stop(e: &RunError) -> bool { + e.kind == RunErrorKind::Limit(LimitKind::Host) && e.message == "stopped by the host" +} + +/// A stop the host's `end` gives when `show()` fails ends the run: the script cannot catch the +/// failure and go on, whether the event could not be read, the host call threw or a handler threw. +#[test] +fn a_stop_from_end_when_show_fails_ends_the_run() { + let caught = "var w = new Window('dialog'); var b = w.add('button', undefined, 'Go'); \ + b.onClick = function () { throw new Error('handler'); }; \ + try { w.show(); } catch (e) {} 'went on'"; + + let mut r = stopping_realm(false, Vec::new()); + r.set_data_limits(DataLimits { depth: 8, bytes: 16 << 20, entries: 1_000 }); + let mut nested = Data::Null; + for _ in 0..20 { + nested = Data::Array(vec![nested]); + } + r.start(caught).unwrap(); + assert_eq!(r.advance(100_000).unwrap(), Progress::Paused); + assert!(r.waits()); + let answered = r.answer(nested); + assert!(answered.as_ref().is_err_and(is_host_stop), "unreadable event: {answered:?}"); + + let mut r = stopping_realm(true, Vec::new()); + let ran = r.run(caught); + assert!(ran.as_ref().is_err_and(is_host_stop), "open threw: {ran:?}"); + + let mut r = stopping_realm(false, vec![click(1)]); + let ran = r.run(caught); + assert!(ran.as_ref().is_err_and(is_host_stop), "handler threw: {ran:?}"); +} + #[test] fn types_that_are_not_available_yet_throw_errors_that_say_so() { for (source, want) in [