{"record":{"id":"89aa99fc743bf695","repo":"karatelabs/karate","slug":"function-constructor-unavailable-no-engine","errorCode":null,"errorMessage":"Function constructor unavailable: no engine","messagePattern":"Function constructor unavailable: no engine","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java","lineNumber":65,"sourceCode":"    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        StringBuilder src = new StringBuilder(\"(function anonymous(\");\n        if (args.length == 0) {\n            src.append(\") {\\n})\");\n        } else {\n            for (int i = 0; i < args.length - 1; i++) {\n                if (i > 0) src.append(',');\n                src.append(argToString(args[i]));\n            }\n            src.append(\"\\n) {\\n\");\n            src.append(argToString(args[args.length - 1]));\n            src.append(\"\\n})\");\n        }\n        Engine engine = context.getEngine();\n        if (engine == null) {\n            throw JsErrorException.typeError(\"Function constructor unavailable: no engine\");\n        }\n        try {\n            return engine.evalRaw(src.toString());\n        } catch (io.karatelabs.parser.ParserException e) {\n            // CreateDynamicFunction (§20.2.1.1): an unparsable body is a JS\n            // SyntaxError, not a host parse exception\n            throw JsErrorException.syntaxError(e.getMessage());\n        }\n    }\n\n    private static String argToString(Object arg) {\n        if (arg == null || arg == Terms.UNDEFINED) return \"\";\n        return arg.toString();\n    }\n\n}\n","sourceCodeStart":47,"sourceCodeEnd":82,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsFunctionConstructor.java#L47-L82","documentation":"new Function(...) compiles the body via the engine's evalRaw. When the running Context has no Engine attached (a non-evaluating or restricted context), Karate cannot compile the dynamic function and throws this TypeError. It is a host-environment limitation, not a spec-mandated JS error.","triggerScenarios":"Calling new Function('return 1') (or Function('...')) inside a Karate JS context configured without an engine — e.g. embedded/host-embedded contexts, static analysis of expressions, or contexts created purely for introspection.","commonSituations":"Libraries that lazily compile functions from strings (formatters, expression evaluators) running inside engine-less Karate contexts; sandboxed configurations that strip the compiler.","solutions":["Run the code in a context with an Engine available (use the standard Karate JS engine context).","Replace dynamic new Function compilation with a pre-defined function or static expression.","Feature-detect: if engine compilation is unavailable, fall back to an interpreted path or disable the dynamic-function feature."],"exampleFix":"// before\nconst f = new Function('x', 'return x * 2'); // TypeError: no engine\n// after\nconst f = (x) => x * 2; // no dynamic compilation","handlingStrategy":"fallback","validationCode":"if (typeof engine === 'undefined' || engine === null) throw new Error('dynamic Function compilation unavailable in this context');","typeGuard":"function canCompileDynamic(ctx) { return ctx && typeof ctx.getEngine === 'function' && ctx.getEngine() != null; }","tryCatchPattern":"try { return new Function(args, body); } catch (e) { if (e instanceof TypeError && /no engine/.test(e.message)) return staticFallback; throw e; }","preventionTips":["Avoid new Function in library code that may run in restricted contexts.","Prefer named, pre-defined functions or static expressions.","Feature-detect dynamic compilation before enabling string-based features."],"tags":["javascript","function-constructor","engine","unsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}