{"record":{"id":"16ea3a7a9cdbe7ea","repo":"karatelabs/karate","slug":"reflect-construct-bad-context","errorCode":null,"errorMessage":"Reflect.construct: bad context","messagePattern":"Reflect\\.construct: bad context","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsReflect.java","lineNumber":93,"sourceCode":"    // isConstructor harness only cares whether the call throws, so we use\n    // newTarget purely as the constructable check when supplied.\n    private Object construct(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof JsCallable target)) {\n            throw JsErrorException.typeError(\"Reflect.construct: target is not a constructor\");\n        }\n        Object[] cArgs = listToArray(args.length >= 2 ? args[1] : null);\n        JsCallable check = target;\n        if (args.length >= 3) {\n            if (!(args[2] instanceof JsCallable nt)) {\n                throw JsErrorException.typeError(\"Reflect.construct: newTarget is not a constructor\");\n            }\n            check = nt;\n        }\n        if (!check.isConstructable()) {\n            throw JsErrorException.typeError(\"Reflect.construct: target is not a constructor\");\n        }\n        if (!(context instanceof CoreContext cc)) {\n            throw JsErrorException.typeError(\"Reflect.construct: bad context\");\n        }\n        return Interpreter.constructFromHost(target, cArgs, cc);\n    }\n\n    // Reflect.apply(target, thisArgument, argumentsList) — spec §28.1.1.\n    private Object apply(Context context, Object[] args) {\n        if (args.length < 1 || !(args[0] instanceof JsCallable target)) {\n            throw JsErrorException.typeError(\"Reflect.apply: target is not callable\");\n        }\n        Object thisArg = args.length >= 2 ? args[1] : Terms.UNDEFINED;\n        Object[] cArgs = listToArray(args.length >= 3 ? args[2] : null);\n        if (context instanceof CoreContext cc) {\n            cc.thisObject = thisArg;\n        }\n        return target.call(context, cArgs);\n    }\n\n    private static Object[] listToArray(Object value) {","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsReflect.java#L75-L111","documentation":"After validating the target/newTarget are constructable, JsReflect.construct requires the active interpreter Context to be an instance of CoreContext so it can dispatch Interpreter.constructFromHost. If the surrounding Context is some other Context implementation, a TypeError 'Reflect.construct: bad context' is thrown. This is effectively an internal invariant violation — normal Karate JS evaluation always provides a CoreContext.","triggerScenarios":"Calling Reflect.construct from a context where `context instanceof CoreContext` is false — e.g. embedding the Karate JS engine and invoking builtins with a custom/minimal Context implementation rather than the engine's CoreContext.","commonSituations":"Embedding karate-js in a host application with a hand-rolled Context; calling engine internals reflectively or from a custom host-object callback that supplies its own Context; version drift where a custom Context was written against an older engine that did not require CoreContext here.","solutions":["Run the script through the standard Karate JS engine entry point so a CoreContext is active","If embedding, ensure the Context passed to JsCallable.call is the engine's CoreContext, not a custom subclass/wrapper","Upgrade or re-align custom Context implementations after engine version changes"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Host-side check before invoking the engine builtin:\nif (!(context instanceof io.karatelabs.js.CoreContext)) {\n    throw new IllegalStateException('Reflect.construct requires a CoreContext');\n}","typeGuard":"if (!(context instanceof io.karatelabs.js.CoreContext cc)) {\n    throw new IllegalStateException('bad context: ' + context.getClass().getName());\n}","tryCatchPattern":"try {\n    return Reflect_construct(ctx, args);\n} catch (JsErrorException e) {\n    if (e.getMessage().contains(\"bad context\")) {\n        // re-run through the standard engine entry point\n        return engine.eval(scriptWithContextFixed);\n    }\n    throw e;\n}","preventionTips":["Always evaluate scripts through the standard Karate JS engine entry points","Do not pass custom Context implementations into JsCallable.call for builtins","Re-validate custom embedding code after upgrading karate-js","Keep host-object callbacks receiving the engine-provided Context unchanged"],"tags":["javascript","reflect","internal","context"],"backgroundTag":"internal-invariant-violation","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"}