{"record":{"id":"b70f24a3bdf2c680","repo":"karatelabs/karate","slug":"bigint-prototype-method-called-on-non-bigint","errorCode":null,"errorMessage":"BigInt.prototype method called on non-BigInt","messagePattern":"BigInt\\.prototype method called on non-BigInt","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsBigIntPrototype.java","lineNumber":82,"sourceCode":"            }\n            return n.toString(radix);\n        }\n        return n.toString();\n    }\n\n    private Object valueOf(Context context, Object[] args) {\n        return asBigInt(context);\n    }\n\n    private static BigInteger asBigInt(Context context) {\n        Object thisObj = context.getThisObject();\n        if (thisObj instanceof JsBigInt jb) {\n            return jb.value;\n        }\n        if (thisObj instanceof BigInteger bi) {\n            return bi;\n        }\n        throw JsErrorException.typeError(\"BigInt.prototype method called on non-BigInt\");\n    }\n\n}\n","sourceCodeStart":64,"sourceCodeEnd":86,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsBigIntPrototype.java#L64-L86","documentation":"Calling a BigInt.prototype method (toString, valueOf, etc.) with a `this` that is not a BigInt or BigInt wrapper throws this TypeError. Karate's JS engine names it more explicitly than V8's variants but enforces the same spec rule that the receiver must be a BigInt.","triggerScenarios":"BigInt.prototype.valueOf.call(42), extracting a method and calling it unbound (const f = x.toString; f()), using .call/.apply with a plain number or string as this, calling prototype methods on Object.create(BigInt.prototype).","commonSituations":"Destructuring/aliasing BigInt methods and losing the receiver; generic function-composition utilities that rebind `this`; porting Number method patterns (Number.prototype.toString.call is laxer) to BigInt.","solutions":["Always call methods on an actual BigInt: 5n.toString(2), not a detached reference","If rebinding, ensure `this` is BigInt: BigInt.prototype.toString.call(5n, 2)","Coerce before invoking prototype methods: const asBigInt = (v) => typeof v === 'bigint' ? v : BigInt(v)","Avoid borrowing BigInt methods across types; use typeof checks instead"],"exampleFix":"// before\nconst toStr = BigInt.prototype.toString;\ntoStr.call(5, 2); // TypeError: non-BigInt this\n// after\nconst toStr = (v, r) => BigInt.prototype.toString.call(typeof v === 'bigint' ? v : BigInt(v), r);","handlingStrategy":"type-guard","validationCode":"function callBigIntMethod(v, fn, ...args) {\n  if (typeof v !== 'bigint') v = BigInt(v);\n  return fn.apply(v, args);\n}","typeGuard":"const isBigInt = (v) => typeof v === 'bigint';","tryCatchPattern":"let out;\ntry { out = detachedFn.call(recv, ...args); } catch (e) {\n  if (e instanceof TypeError && /non-BigInt/.test(e.message)) out = BigInt(recv).toString(...args);\n  else throw e;\n}","preventionTips":["Never detach BigInt.prototype methods without re-coercing `this`","Check receiver type before .call/.apply on BigInt methods","Use wrappers like (v) => v.toString(2) instead of borrowed references","Audit generic function-composition utilities for receiver loss"],"tags":["javascript","bigint","type-error","this-binding"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}