{"record":{"id":"5e1941bed75985f6","repo":"mozilla/pdf.js","slug":"invalid-function-in-afsimple-calculate","errorCode":null,"errorMessage":"Invalid function in AFSimple_Calculate","messagePattern":"Invalid function in AFSimple_Calculate","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"warning","filePath":"src/scripting_api/aform.js","lineNumber":376,"sourceCode":"        return Math.min(value1, value2);\n      case \"MAX\":\n        return Math.max(value1, value2);\n    }\n\n    throw new Error(\"Invalid cFunction in AFSimple\");\n  }\n\n  AFSimple_Calculate(cFunction, cFields) {\n    const actions = {\n      AVG: args => Math.sumPrecise(args) / args.length,\n      SUM: args => Math.sumPrecise(args),\n      PRD: args => args.reduce((acc, value) => acc * value, 1),\n      MIN: args => Math.min(...args),\n      MAX: args => Math.max(...args),\n    };\n\n    if (!(cFunction in actions)) {\n      throw new TypeError(\"Invalid function in AFSimple_Calculate\");\n    }\n\n    const event = globalThis.event;\n    const values = [];\n\n    cFields = this.AFMakeArrayFromList(cFields);\n    for (const cField of cFields) {\n      const field = this._document.getField(cField);\n      if (!field) {\n        continue;\n      }\n      for (const child of field.getArray()) {\n        const number = this.AFMakeNumber(child.value);\n        values.push(number ?? 0);\n      }\n    }\n\n    if (values.length === 0) {","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/aform.js#L358-L394","documentation":"Thrown as a TypeError by AFSimple_Calculate when cFunction is not a key of its internal actions map (AVG/SUM/PRD/MIN/MAX). Note this is a TypeError, not a plain Error, because the function guards the actions lookup before iterating fields. It runs in the sandboxed PDF-JS environment.","triggerScenarios":"A PDF form script calls AFSimple_Calculate(cFunction, cFields) with cFunction not present in the actions map.","commonSituations":"A malformed or non-standard calculate action in the PDF; an authoring tool emitting an unsupported function name.","solutions":["Repair or report the PDF form script.","Update PDF.js for broader function support.","Catch TypeError separately in the embedding app if you inspect scripting errors.","If authoring the PDF, use only AVG/SUM/PRD/MIN/MAX."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isSupportedAFormCalculateFn(fn) {\n  return ['AVG', 'SUM', 'PRD', 'MIN', 'MAX'].includes(fn);\n}","tryCatchPattern":"try {\n  await runFormScript(...);\n} catch (e) {\n  if (e instanceof TypeError && /AFSimple_Calculate/.test(e.message)) { /* skip */ }\n}","preventionTips":["Catch TypeError distinctly from Error when inspecting sandbox failures.","Repair PDFs that reference unsupported calculate functions.","Update PDF.js for broader function support.","Keep the viewer resilient to per-field scripting failures."],"tags":["aform","scripting","calculation","typeerror","pdf-content"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}