All files / codegen/src Codegen.ts

42.85% Statements 30/70
33.33% Branches 5/15
48% Functions 12/25
45.31% Lines 29/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 3091x           1x 4x                                                                                                                                                     1x         1x           1x                 1x             2x                                                                                                                               1x                                                     1x 1x                           1x 1x 1x                     1x                               1x 1x                           2x 2x 2x               2x                         2x 2x 2x 4x     6x       2x                       1x 1x      
import {compileClosure} from './compile';
import type {JavaScriptLinked} from './types';
 
/**
 * Inline JavaScript statements that are executed in main function body.
 */
export class CodegenStepExecJs {
  constructor(public readonly js: string) {}
}
 
/**
 * A step can be `CodegenStepExecJs` or some application specific step, which
 * will later will need to be converted to `CodegenStepExecJs`.
 */
type JsonSerializerStep = CodegenStepExecJs | unknown;
 
/**
 * Configuration options for {@link Codegen} instances.
 */
export interface CodegenOptions<Linkable = Record<string, unknown>> {
  /**
   * Inline JavaScript string that represents the arguments that will be passed
   * to the main function body. Defaults to "r0", i.e. the first register.
   */
  args?: string[];
 
  /**
   * Name of the generated function.
   */
  name?: string;
 
  /**
   * Inline JavaScript statements, that execute at the beginning of the main
   * function body.
   */
  prologue?: string;
 
  /**
   * Inline JavaScript statements, that execute at the end of the main
   * function body.
   */
  epilogue?: string | (() => string);
 
  /**
   * Converts all steps to `CodegenStepExecJs`.
   */
  processSteps?: (steps: JsonSerializerStep[]) => CodegenStepExecJs[];
 
  /**
   * Predefined list of dependencies that can be linked on demand. Dependency is
   * linked with the name of the property and is linked only once.
   */
  linkable?: Linkable;
}
 
export type CodegenGenerateOptions = Pick<CodegenOptions, 'name' | 'args' | 'prologue' | 'epilogue'>;
 
/**
 * A helper class which helps with building JavaScript code for a single
 * function. It keeps track of external dependencies, internally generated
 * constants, and execution steps, which at the end are all converted to
 * to an executable JavaScript function.
 *
 * The final output is a JavaScript function enclosed in a closure:
 *
 * ```js
 * (function(d1, d2, d3) {
 *   var c1 = something;
 *   var c2 = something;
 *   var c3 = something;
 *   return function(r0) {
 *     var r1 = something;
 *     var r2 = something;
 *     var r3 = something;
 *     return something;
 *   }
 * })
 * ```
 *
 * Where `d*` are the external dependencies, `c*` are the internal constants,
 * and `r*` are the local immutable infinite registers.
 */
export class Codegen<
  Fn extends (...deps: any[]) => any = (...deps: unknown[]) => unknown,
  Linkable = Record<string, unknown>,
> {
  /** @ignore */
  protected steps: JsonSerializerStep[] = [];
 
  /** @ignore */
  public options: Required<CodegenOptions<Linkable>>;
 
  constructor(opts: CodegenOptions<Linkable>) {
    this.options = {
      args: ['r0'],
      name: '',
      prologue: '',
      epilogue: '',
      processSteps: (steps) => steps.filter((step) => step instanceof CodegenStepExecJs) as CodegenStepExecJs[],
      linkable: {} as Linkable,
      ...opts,
    };
    this.registerCounter = this.options.args.length;
  }
 
  /**
   * Add one or more JavaScript statements to the main function body.
   */
  public js(js: string): void {
    this.steps.push(new CodegenStepExecJs(js));
  }
 
  public var(expression?: string): string {
    const r = this.getRegister();
    if (expression) this.js('var ' + r + ' = ' + expression + ';');
    else this.js('var ' + r + ';');
    return r;
  }
 
  public if(condition: string, then: () => void, otherwise?: () => void): void {
    this.js('if (' + condition + ') {');
    then();
    Iif (otherwise) {
      this.js('} else {');
      otherwise();
    }
    this.js('}');
  }
 
  public while(condition: string, block: () => void): void {
    this.js('while (' + condition + ') {');
    block();
    this.js('}');
  }
 
  public doWhile(block: () => void, condition: string): void {
    this.js('do {');
    block();
    this.js('} while (' + condition + ');');
  }
 
  public switch(
    expression: string,
    cases: [match: string | number | boolean | null, block: () => void, noBreak?: boolean][],
    def?: () => void,
  ): void {
    this.js('switch (' + expression + ') {');
    for (const [match, block, noBreak] of cases) {
      this.js('case ' + match + ': {');
      block();
      Iif (!noBreak) this.js('break;');
      this.js('}');
    }
    Iif (def) {
      this.js('default: {');
      def();
      this.js('}');
    }
    this.js('}');
  }
 
  public return(expression: string): void {
    this.js('return ' + expression + ';');
  }
 
  /**
   * Add any application specific execution step. Steps of `unknown` type
   * later need to converted to `CodegenStepExecJs` steps in the `.processStep`
   * callback.
   *
   * @param step A step in function execution logic.
   */
  public step(step: unknown): void {
    this.steps.push(step);
  }
 
  protected registerCounter: number;
 
  /**
   * Codegen uses the idea of infinite registers. It starts with `0` and
   * increments it by one for each new register. Best practice is to use
   * a new register for each new variable and keep them immutable.
   *
   * Usage:
   *
   * ```js
   * const r = codegen.getRegister();
   * codegen.js(`const ${r} = 1;`);
   * ```
   *
   * @returns a unique identifier for a variable.
   */
  public getRegister(): string {
    return `r${this.registerCounter++}`;
  }
  public r(): string {
    return this.getRegister();
  }
 
  /** @ignore */
  protected dependencies: unknown[] = [];
  protected dependencyNames: string[] = [];
 
  /**
   * Allows to wire up dependencies to the generated code.
   *
   * @param dep Any JavaScript dependency, could be a function, an object,
   *        or anything else.
   * @param name Optional name of the dependency. If not provided, a unique
   *        name will be generated, which starts with `d` and a counter
   *        appended.
   * @returns Returns the dependency name, a code symbol which can be used as
   *          variable name.
   */
  public linkDependency(dep: unknown, name: string = 'd' + this.dependencies.length): string {
    this.dependencies.push(dep);
    this.dependencyNames.push(name);
    return name;
  }
 
  /**
   * Sames as {@link Codegen#linkDependency}, but allows to wire up multiple
   * dependencies at once.
   */
  public linkDependencies(deps: unknown[]): string[] {
    return deps.map((dep) => this.linkDependency(dep));
  }
 
  protected linked: {[key: string]: 1} = {};
 
  /**
   * Link a dependency from the pre-defined `options.linkable` object. This method
   * can be called many times with the same dependency name, the dependency will
   * be linked only once.
   *
   * @param name Linkable dependency name.
   */
  public link(name: keyof Linkable): void {
    Iif (this.linked[name as string]) return;
    this.linked[name as string] = 1;
    this.linkDependency(this.options.linkable[name], name as string);
  }
 
  /** @ignore */
  protected constants: string[] = [];
  protected constantNames: string[] = [];
 
  /**
   * Allows to encode any code or value in the closure of the generated
   * function.
   *
   * @param constant Any JavaScript value in string form.
   * @param name Optional name of the constant. If not provided, a unique
   *        name will be generated, which starts with `c` and a counter
   *        appended.
   * @returns Returns the constant name, a code symbol which can be used as
   *          variable name.
   */
  public addConstant(constant: string, name: string = 'c' + this.constants.length): string {
    this.constants.push(constant);
    this.constantNames.push(name);
    return name;
  }
 
  /**
   * Sames as {@link Codegen#addConstant}, but allows to create multiple
   * constants at once.
   */
  public addConstants(constants: string[]): string[] {
    return constants.map((constant) => this.addConstant(constant));
  }
 
  /**
   * Returns generated JavaScript code with the dependency list.
   *
   * ```js
   * const code = codegen.generate();
   * const fn = eval(code.js)(...code.deps);
   * const result = fn(...args);
   * ```
   */
  public generate(opts: CodegenGenerateOptions = {}): JavaScriptLinked<Fn> {
    const {name, args, prologue, epilogue} = {...this.options, ...opts};
    const steps = this.options.processSteps(this.steps);
    const js = `(function(${this.dependencyNames.join(', ')}) {
${this.constants.map((constant, index) => `var ${this.constantNames[index]} = (${constant});`).join('\n')}
return ${name ? `function ${name}` : 'function'}(${args.join(',')}){
${prologue}
${steps.map((step) => (step as CodegenStepExecJs).js).join('\n')}
${typeof epilogue === 'function' ? epilogue() : epilogue || ''}
}})`;
    // console.log(js);
    return {
      deps: this.dependencies,
      js: js as JavaScriptLinked<Fn>['js'],
    };
  }
 
  /**
   * Compiles the generated JavaScript code into a function.
   *
   * @returns JavaScript function ready for execution.
   */
  public compile(opts?: CodegenGenerateOptions): Fn {
    const closure = this.generate(opts);
    return compileClosure(closure);
  }
}