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 f1139494d1/Changelog (L8)
This commit is contained in:
Jonas Jenwald 2026-03-07 12:37:30 +01:00
parent d34a15e03f
commit ca428aadae
2 changed files with 14 additions and 2 deletions

View File

@ -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),

View File

@ -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,