* Evaluate a piece of JIT generated code. * @param sourceUrl The URL of this generated code. * @param ctx A context object that contains an AST of the code to be evaluated. * @param vars A map containing the names and values of variables that the evaluated code might * reference. * @p
(
sourceUrl: string,
ctx: EmitterVisitorContext,
vars: {[key: string]: any},
createSourceMap: boolean,
)
| 58 | * @returns The result of evaluating the code. |
| 59 | */ |
| 60 | evaluateCode( |
| 61 | sourceUrl: string, |
| 62 | ctx: EmitterVisitorContext, |
| 63 | vars: {[key: string]: any}, |
| 64 | createSourceMap: boolean, |
| 65 | ): any { |
| 66 | let fnBody = `"use strict";${ctx.toSource()}\n//# sourceURL=${sourceUrl}`; |
| 67 | const fnArgNames: string[] = []; |
| 68 | const fnArgValues: any[] = []; |
| 69 | for (const argName in vars) { |
| 70 | fnArgValues.push(vars[argName]); |
| 71 | fnArgNames.push(argName); |
| 72 | } |
| 73 | if (createSourceMap) { |
| 74 | // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise |
| 75 | // E.g. ``` |
| 76 | // function anonymous(a,b,c |
| 77 | // /**/) { ... }``` |
| 78 | // We don't want to hard code this fact, so we auto detect it via an empty function first. |
| 79 | const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString(); |
| 80 | const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1; |
| 81 | fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`; |
| 82 | } |
| 83 | const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody)); |
| 84 | return this.executeFunction(fn, fnArgValues); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Execute a JIT generated function by calling it. |
no test coverage detected