{"record":{"id":"00810a7028f3791e","repo":"karatelabs/karate","slug":"map-prototype-set-is-not-callable","errorCode":null,"errorMessage":"Map.prototype.set is not callable","messagePattern":"Map\\.prototype\\.set is not callable","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsMapConstructor.java","lineNumber":72,"sourceCode":"                GroupByImpl.run(items, callback, /* propertyMode= */ false, context));\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 Map requires 'new'\");\n        }\n        JsMap map = new JsMap();\n        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {\n            return map;\n        }\n        // Look up `set` on the freshly-constructed map's prototype chain — honors\n        // user-overridden Map.prototype.set per spec 24.1.1.1 step 9.\n        Object setFn = map.getMember(\"set\");\n        if (!(setFn instanceof JsCallable adder)) {\n            throw JsErrorException.typeError(\"Map.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":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsMapConstructor.java#L54-L90","documentation":"Karate's JS engine throws this while constructing a Map from an iterable, per spec 24.1.1.1 step 9: it looks up `set` on the new map's prototype chain and requires it to be a callable function. If `Map.prototype.set` was deleted, overwritten with a non-function, or the map instance's prototype was mangled, construction aborts with this TypeError.","triggerScenarios":"Passing an iterable to `new Map(iterable)` after user code has done `Map.prototype.set = null` / `delete Map.prototype.set`, or reassigned a Map instance's prototype to a plain object lacking a callable `set`.","commonSituations":"Monkey-patching Map.prototype in test setup or polyfills that accidentally clobber built-ins; transpilers or DSL code overriding Map methods; running JS that mutates global built-ins before constructing Maps.","solutions":["Restore the built-in: remove any `Map.prototype.set = ...` override or delete it before constructing Maps","Ensure any Map polyfill/shim defines `set` as a function","Check that code does not reassign an object's prototype (`Object.setPrototypeOf`) away from Map.prototype before passing it through `new Map()`","Isolate the mutating script — run the Map construction in a fresh Karate JS context"],"exampleFix":"// before\nMap.prototype.set = null;\nconst m = new Map([[1, 'a']]); // TypeError\n// after\nMap.prototype.set = origSet; // restore or don't override\nconst m = new Map([[1, 'a']]);","handlingStrategy":"type-guard","validationCode":"// before constructing\nif (typeof Map.prototype.set !== 'function') {\n  throw new Error('Map.prototype.set was clobbered; restore it before new Map(iterable)');\n}","typeGuard":"function mapSetIsCallable() { return typeof Map.prototype.set === 'function'; }","tryCatchPattern":"try {\n  const m = new Map(iterable);\n} catch (e) {\n  if (String(e.message).includes('Map.prototype.set is not callable')) {\n    // restore built-in / run in clean context\n  } else throw e;\n}","preventionTips":["Don't monkey-patch Map.prototype in test setup","Snapshot built-ins and restore in teardown","Run untrusted scripts in isolated contexts"],"tags":["javascript","map","prototype","typeerror"],"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"}