Skip to content

Commit ca428aa

Browse files
committed
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
1 parent d34a15e commit ca428aa

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/scripting_api/aform.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ class AForm {
368368

369369
AFSimple_Calculate(cFunction, cFields) {
370370
const actions = {
371-
AVG: args => args.reduce((acc, value) => acc + value, 0) / args.length,
372-
SUM: args => args.reduce((acc, value) => acc + value, 0),
371+
AVG: args => Math.sumPrecise(args) / args.length,
372+
SUM: args => Math.sumPrecise(args),
373373
PRD: args => args.reduce((acc, value) => acc * value, 1),
374374
MIN: args => Math.min(...args),
375375
MAX: args => Math.max(...args),

src/scripting_api/app_utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ function serializeError(error) {
2626
return { command: "error", value };
2727
}
2828

29+
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
30+
// TODO: Remove this once `Math.sumPrecise` is supported in QuickJS.
31+
//
32+
// Note that this isn't a "proper" polyfill, but since we're only using it to
33+
// replace `Array.prototype.reduce()` invocations it should be fine.
34+
if (typeof Math.sumPrecise !== "function") {
35+
Math.sumPrecise = function (numbers) {
36+
return numbers.reduce((a, b) => a + b, 0);
37+
};
38+
}
39+
}
40+
2941
export {
3042
FORMS_VERSION,
3143
serializeError,

0 commit comments

Comments
 (0)