{"record":{"id":"a0e4af60b8d478c5","repo":"different-ai/openwork","slug":"math-name-is-not-available-in-codemode","errorCode":null,"errorMessage":"Math.${name} is not available in CodeMode.","messagePattern":"Math\\.(.+?) is not available in CodeMode\\.","errorType":"exception","errorClass":"InterpreterRuntimeError","httpStatus":null,"severity":"error","filePath":"packages/codemode/src/stdlib/math.ts","lineNumber":23,"sourceCode":"  \"min\",\n  \"abs\",\n  \"floor\",\n  \"ceil\",\n  \"round\",\n  \"trunc\",\n  \"sign\",\n  \"sqrt\",\n  \"cbrt\",\n  \"pow\",\n  \"hypot\",\n  \"log\",\n  \"log2\",\n  \"log10\",\n  \"exp\",\n])\n\nexport const invokeMathMethod = (name: string, args: Array<unknown>, node: AstNode): number => {\n  if (!mathMethods.has(name)) throw new InterpreterRuntimeError(`Math.${name} is not available in CodeMode.`, node)\n  const nums = args.map((arg) => {\n    if (typeof arg !== \"number\") throw new InterpreterRuntimeError(`Math.${name} expects number arguments.`, node)\n    return arg\n  })\n  const [a = Number.NaN, b = Number.NaN] = nums\n  switch (name) {\n    case \"max\":\n      return Math.max(...nums)\n    case \"min\":\n      return Math.min(...nums)\n    case \"abs\":\n      return Math.abs(a)\n    case \"floor\":\n      return Math.floor(a)\n    case \"ceil\":\n      return Math.ceil(a)\n    case \"round\":\n      return Math.round(a)","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/packages/codemode/src/stdlib/math.ts#L5-L41","documentation":"CodeMode's Math object exposes only a whitelisted set of static methods (stored in the mathMethods Set). If the invoked method name is not in that set, the library throws this error before evaluating any arguments. This pre-check guards the whitelist so unsupported Math APIs fail fast with a clear message.","triggerScenarios":"Calling Math.random(), Math.clamp(), Math.hypot(), Math.sign(), or any method missing from the whitelist (e.g. abs, ceil, floor, round, sqrt, pow, min, max, log, log2, log10, exp are supported); also dynamic dispatch Math[fn]() with an unexpected fn.","commonSituations":"Porting code that uses Math.random() — CodeMode forbids it for determinism; using newer Math methods like Math.clamp or Math.fsum from other engines; typos like Math.sqtr.","solutions":["Use only whitelisted Math methods; check the mathMethods whitelist in packages/codemode/src/stdlib/math.ts for the supported list.","Replace Math.random() with a seeded PRNG implemented in sandbox JS if randomness is needed deterministically.","Implement unsupported helpers (e.g. clamp) inline: const clamp = (x, lo, hi) => Math.min(hi, Math.max(lo, x))."],"exampleFix":"// before\nconst r = Math.random()\n// after\nconst r = (seed = 1) => (seed * 16807) % 2147483647 / 2147483647","handlingStrategy":"validation","validationCode":"const allowedMath = new Set([\"abs\",\"ceil\",\"floor\",\"round\",\"sqrt\",\"pow\",\"min\",\"max\",\"log\",\"log2\",\"log10\",\"exp\"])\nif (!allowedMath.has(fnName)) throw new Error(`Math.${fnName} is not whitelisted in CodeMode`)","typeGuard":"const isMathMethod = (n: string): n is \"abs\" | \"ceil\" | \"floor\" | \"round\" | \"sqrt\" | \"pow\" | \"min\" | \"max\" | \"log\" | \"log2\" | \"log10\" | \"exp\" => allowedMath.has(n)","tryCatchPattern":"try { return Math[fnName](x) } catch (e) { if (/Math\\..+ is not available in CodeMode/.test(String(e))) return inlineImplementation(fnName, x); throw e }","preventionTips":["Check the mathMethods whitelist before using a Math API in sandbox code","Never use Math.random() in CodeMode — implement a seeded PRNG instead","Avoid newer engine-specific Math helpers (clamp, fsum, hypot)","Verify dynamic dispatch targets against the whitelist"],"tags":["math","codemode","unsupported-api","whitelist"],"backgroundTag":"method-not-supported","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}