{"record":{"id":"84285bd77a6ee099","repo":"karatelabs/karate","slug":"weakmap-prototype-set-is-not-callable","errorCode":null,"errorMessage":"WeakMap.prototype.set is not callable","messagePattern":"WeakMap\\.prototype\\.set is not callable","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsWeakMapConstructor.java","lineNumber":54,"sourceCode":"    JsWeakMapConstructor() {\n        this.name = \"WeakMap\";\n        defineOwn(\"prototype\", JsWeakMapPrototype.INSTANCE, PropertySlot.INTRINSIC);\n    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        CallInfo callInfo = context.getCallInfo();\n        boolean isNew = callInfo != null && callInfo.constructor;\n        if (!isNew) {\n            throw JsErrorException.typeError(\"Constructor WeakMap requires 'new'\");\n        }\n        JsWeakMap map = new JsWeakMap();\n        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {\n            return map;\n        }\n        Object setFn = map.getMember(\"set\");\n        if (!(setFn instanceof JsCallable adder)) {\n            throw JsErrorException.typeError(\"WeakMap.prototype.set is not callable\");\n        }\n        JsIterator iter = IterUtils.getIterator(args[0], context);\n        CoreContext cc = context instanceof CoreContext c ? c : null;\n        Object savedThis = cc != null ? cc.thisObject : null;\n        try {\n            while (iter.hasNext()) {\n                Object entry = iter.next();\n                if (!(entry instanceof List) && !(entry instanceof ObjectLike)) {\n                    throw JsErrorException.typeError(\"Iterator value \" + entry + \" is not an entry object\");\n                }\n                Object k;\n                Object v;\n                if (entry instanceof List<?> list) {\n                    k = list.isEmpty() ? Terms.UNDEFINED : list.get(0);\n                    v = list.size() < 2 ? Terms.UNDEFINED : list.get(1);\n                } else {\n                    ObjectLike ol = (ObjectLike) entry;\n                    k = ol.getMember(\"0\");","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsWeakMapConstructor.java#L36-L72","documentation":"When constructing a WeakMap from an iterable of entries, the constructor fetches the map's own 'set' method to use as the adder. The spec guarantees set is callable, but if getMember(\"set\") does not return a JsCallable (e.g. the prototype's set member was overwritten or is missing in this engine state), Karate throws this TypeError instead of proceeding.","triggerScenarios":"new WeakMap(iterable) where the JsWeakMap instance's 'set' member fails to resolve to a JsCallable — typically only if prototype methods have been tampered with or shadowed, since normal scripts can't usually cause it.","commonSituations":"Monkey-patching or deleting WeakMap.prototype.set earlier in the same JS evaluation; assigning a non-function to map.set before/while constructing from an iterable; exotic host-object interactions in embedded engines.","solutions":["Do not overwrite or delete WeakMap.prototype.set in your Karate JS scripts.","Restore the original: WeakMap.prototype.set = originalSet (capture it before patching).","Construct with explicit set calls instead of an iterable argument to bypass the adder path.","Audit any reflection/proxy code that could shadow the 'set' member."],"exampleFix":"// before\nWeakMap.prototype.set = 'oops';\nconst wm = new WeakMap([[k, v]]); // TypeError\n\n// after\nconst wm = new WeakMap();\nwm.set(k, v);","handlingStrategy":"validation","validationCode":"if (typeof WeakMap.prototype.set !== 'function') throw new Error('WeakMap.prototype.set was clobbered');","typeGuard":null,"tryCatchPattern":"try { var wm = new WeakMap(entries); } catch (e) { var wm2 = new WeakMap(); entries.forEach(en => wm2.set(en[0], en[1])); }","preventionTips":["Do not monkey-patch or delete WeakMap.prototype members","Save originals before any patching and restore them","Prefer explicit set() calls over iterable construction in embedded engines"],"tags":["javascript","weakmap","prototype","typeerror"],"backgroundTag":"unsupported-operation","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"}