From ca428aadae9d6a33f2f206b7e239577b60b35908 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 7 Mar 2026 12:37:30 +0100 Subject: [PATCH] Use `Math.sumPrecise` in the scripting implementation This adds a *very basic* non-MOZCENTRAL polyfill for now, which we should be able to remove once the next QuickJS version is released; note the pending changelog at https://github.com/bellard/quickjs/blob/f1139494d18a2053630c5ed3384a42bb70db3c53/Changelog#L8 --- src/scripting_api/aform.js | 4 ++-- src/scripting_api/app_utils.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/scripting_api/aform.js b/src/scripting_api/aform.js index 17f3f1aea..81639bdd2 100644 --- a/src/scripting_api/aform.js +++ b/src/scripting_api/aform.js @@ -368,8 +368,8 @@ class AForm { AFSimple_Calculate(cFunction, cFields) { const actions = { - AVG: args => args.reduce((acc, value) => acc + value, 0) / args.length, - SUM: args => args.reduce((acc, value) => acc + value, 0), + AVG: args => Math.sumPrecise(args) / args.length, + SUM: args => Math.sumPrecise(args), PRD: args => args.reduce((acc, value) => acc * value, 1), MIN: args => Math.min(...args), MAX: args => Math.max(...args), diff --git a/src/scripting_api/app_utils.js b/src/scripting_api/app_utils.js index b46f8ea26..a35bf9de5 100644 --- a/src/scripting_api/app_utils.js +++ b/src/scripting_api/app_utils.js @@ -26,6 +26,18 @@ function serializeError(error) { return { command: "error", value }; } +if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { + // TODO: Remove this once `Math.sumPrecise` is supported in QuickJS. + // + // Note that this isn't a "proper" polyfill, but since we're only using it to + // replace `Array.prototype.reduce()` invocations it should be fine. + if (typeof Math.sumPrecise !== "function") { + Math.sumPrecise = function (numbers) { + return numbers.reduce((a, b) => a + b, 0); + }; + } +} + export { FORMS_VERSION, serializeError,