MCPcopy Create free account
hub / github.com/angular/angular / evaluateCode

Method evaluateCode

packages/compiler/src/output/output_jit.ts:60–87  ·  view source on GitHub ↗

* 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: Record<string, any>,
    createSourceMap: boolean,
  )

Source from the content-addressed store, hash-verified

58 * @returns The result of evaluating the code.
59 */
60 evaluateCode(
61 sourceUrl: string,
62 ctx: EmitterVisitorContext,
63 vars: Record<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 if (Object.hasOwn(vars, argName)) {
71 fnArgValues.push(vars[argName]);
72 fnArgNames.push(argName);
73 }
74 }
75 if (createSourceMap) {
76 // using `new Function(...)` generates a header, 1 line of no arguments, 2 lines otherwise
77 // E.g. ```
78 // function anonymous(a,b,c
79 // /**/) { ... }```
80 // We don't want to hard code this fact, so we auto detect it via an empty function first.
81 const emptyFn = newTrustedFunctionForJIT(...fnArgNames.concat('return null;')).toString();
82 const headerLines = emptyFn.slice(0, emptyFn.indexOf('return null;')).split('\n').length - 1;
83 fnBody += `\n${ctx.toSourceMapGenerator(sourceUrl, headerLines).toJsComment()}`;
84 }
85 const fn = newTrustedFunctionForJIT(...fnArgNames.concat(fnBody));
86 return this.executeFunction(fn, fnArgValues);
87 }
88
89 /**
90 * Execute a JIT generated function by calling it.

Callers 1

evaluateStatementsMethod · 0.95

Calls 9

executeFunctionMethod · 0.95
newTrustedFunctionForJITFunction · 0.90
toSourceMethod · 0.80
indexOfMethod · 0.80
toJsCommentMethod · 0.80
toSourceMapGeneratorMethod · 0.80
toStringMethod · 0.65
sliceMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected