fix(js): an XFA click past its 32 questions gets the safe default in every session

Review on #6984: past the 32nd question, a click split across script
sessions gave the later message boxes their buttons' safe default (the
click-wide limiter answers nothing), but a single session failed with
"too many interactive prompts" (interaction::reserve), so a form could
take its error path in one case and go on in the other. XfaHost's
message box now asks only while interaction::can_ask() and otherwise
gives the safe default, as the PR describes; the 32-question bound is
unchanged. FormCalc's $host.messageBox goes through the same path.
AcroForm app.alert keeps its own limit.

Test: an_xfa_click_asks_at_most_32_questions now asserts what the
script receives: 32 asked and answered Yes, the other 28 get No without
being asked, and the script completes (seen failing without the fix:
"too many interactive prompts").

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
alehelandClaude Opus 5.5 committed 2026-11-23 18:56:44 +01:00
1 parent 3d5e81a852
commit 504595dfa8
3 files changed
+17 -8

No files matched your search

+7 -5
View File
@@ -10238,15 +10238,16 @@ fn an_xfa_click_is_dropped_when_the_document_changes_while_it_asks() {
assert_eq!(qty(&s, id), ["7"]);
}
/// A click's prompts are limited as an AcroForm script's are: past 32 the script fails, and
/// keeps what it did before, as a failing script does.
/// A click asks at most 32 questions; every later message box gets its buttons' safe default (No
/// for Yes/No) without asking, and the script goes on, as it does when the click's questions are
/// spent across its script sessions (review on #6984).
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn an_xfa_click_asks_at_most_32_questions() {
use crate::js::{InteractiveEvent, Prompt};
let tpl = pdfcraft_xfa::fixtures::scripted_template().replace(
r#"xfa.host.messageBox("Hello " + qty.rawValue);"#,
r#"for (var i = 0; i &lt; 1000; i++) { if (xfa.host.messageBox("Again?", "", 2, 2) == 4) qty.rawValue = i + 1; }"#,
r#"var yes = 0; var no = 0; for (var i = 0; i &lt; 60; i++) { var a = xfa.host.messageBox("Again?", "", 2, 2); if (a == 4) yes++; else if (a == 3) no++; } qty.rawValue = yes * 10000 + no;"#,
);
let mut s = Session::new().with_clock(|| 1_700_000_000);
let id = s.open("many.pdf", None, Arc::new(pdfcraft_xfa::fixtures::shell(&tpl)), None).expect("opens");
@@ -10264,8 +10265,9 @@ fn an_xfa_click_asks_at_most_32_questions() {
};
assert_eq!(asked, 32);
let o = s.finish_javascript(p, out).unwrap();
assert!(o.error.as_deref().is_some_and(|e| e.contains("too many interactive prompts")), "{o:?}");
assert_eq!(qty(&s, id), ["32"]);
assert!(o.error.is_none(), "the script goes on: {o:?}");
// 32 answered Yes; the other 28 got No without being asked.
assert_eq!(qty(&s, id), ["320028"]);
}
/// The 32-question limit holds for the whole click, not for each script session: a script that
+5
View File
@@ -33,6 +33,11 @@ pub(crate) fn has_host() -> bool {
pub(crate) fn set_host(host: Option<Sender<InteractiveEvent>>) {
HOST.with(|h| *h.borrow_mut() = host.map(|h| (h, 0)));
}
/// Whether the invocation may still ask: it has a prompt channel and has asked fewer than
/// [`MAX_PROMPTS`] questions.
pub(crate) fn can_ask() -> bool {
HOST.with(|h| h.borrow().as_ref().is_some_and(|(_, n)| *n < MAX_PROMPTS))
}
/// Charge one prompt to the invocation's budget: the channel to send it on, if the worker has one.
fn reserve() -> JsResult<Option<Sender<InteractiveEvent>>> {
HOST.with(|h| {
+5 -3
View File
@@ -165,8 +165,10 @@ impl XHost {
/// Show message box `m` and return the button picked: the next of the event's answers
/// (headless runs); past them, the user's answer when the viewer can ask (an app's button
/// click: the script waits on its thread, as an AcroForm script's `app.alert` does), or
/// else the buttons' safe default. `Err`: an answer `m`'s buttons can't give, or the
/// event's prompts are spent.
/// else the buttons' safe default. Past [`crate::interaction::MAX_PROMPTS`] questions nobody
/// is asked and the safe default is given, as it is when the click's questions are spent
/// across its script sessions: a form follows the same path either way. `Err`: an answer
/// `m`'s buttons can't give.
pub(crate) fn message_box(&mut self, m: Message) -> Result<u8, String> {
// Clipped before the viewer is asked, as the effect is: the dialog shows no more than is kept.
let m = Message { text: clip(m.text), title: m.title.map(clip), ..m };
@@ -178,7 +180,7 @@ impl XHost {
};
let reply = match self.answers.get(at) {
Some(answer) => m.buttons.reply(answer.as_deref()).ok_or_else(|| invalid(answer.as_deref()))?,
None if crate::interaction::has_host() => {
None if crate::interaction::can_ask() => {
let answer = crate::interaction::ask(crate::interaction::Prompt::Alert(m.clone())).map_err(|e| e.to_string())?;
m.buttons.reply(answer.as_deref()).ok_or_else(|| invalid(answer.as_deref()))?
}