{"record":{"id":"ec82336d3f8f9193","repo":"karatelabs/karate","slug":"constructor-weakmap-requires-new","errorCode":null,"errorMessage":"Constructor WeakMap requires 'new'","messagePattern":"Constructor WeakMap requires 'new'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsWeakMapConstructor.java","lineNumber":46,"sourceCode":"/**\n * Global {@code WeakMap} constructor — mirrors {@link JsMapConstructor}: no\n * {@code new} is a TypeError, and construction from an iterable of\n * {@code [key, value]} pairs invokes {@code this.set(k, v)} through the\n * prototype chain so a user-overridden {@code WeakMap.prototype.set} is honored.\n */\nclass JsWeakMapConstructor extends JsFunction {\n\n    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                }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsWeakMapConstructor.java#L28-L64","documentation":"WeakMap is a constructor, not a plain callable function. Per the ECMAScript spec (and V8 behavior), calling WeakMap() without new throws 'Constructor WeakMap requires new'. Karate's JsWeakMapConstructor.call checks CallInfo.constructor and throws this TypeError when the flag is absent.","triggerScenarios":"Evaluating `const wm = WeakMap()` (no new) in Karate JS; dynamic invocation like `const M = WeakMap; M()`; destructured or re-assigned constructor references losing new-call syntax.","commonSituations":"Porting code written for APIs that allowed both call styles; refactoring that dropped `new`; template-generated code where the new keyword was lost; confusion after using classes where calling without new is impossible.","solutions":["Always instantiate with new: const wm = new WeakMap().","If the constructor may be called either way, wrap it: function safeWeakMap(){ return new WeakMap(); }.","Search scripts for `WeakMap(` occurrences and add the missing `new` keyword.","Use new.target-aware wrappers if you must support both styles in shared code."],"exampleFix":"// before\nconst wm = WeakMap();\n\n// after\nconst wm = new WeakMap();","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var wm = WeakMap(); } catch (e) { if (String(e).indexOf('requires') !== -1) wm = new WeakMap(); }","preventionTips":["Always write `new WeakMap()`","Never alias constructors and call them bare","Lint for `WeakMap(` without preceding `new`"],"tags":["javascript","weakmap","constructor","typeerror"],"backgroundTag":"invalid-argument-value","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"}