• Compiles the specified code as sandboxed function. The compiled function can be executed with a context object. Optionally the context object can be mutable, allowing the compiled code to modify the context object.

    Parameters

    • code: string
    • Optional options: {
          mode: "vm" | "sandbox";
      }
      • mode: "vm" | "sandbox"

    Returns ((context?, options?) => any)

      • (context?, options?): any
      • Parameters

        Returns any

    Usage

    const context = { counter: 0 };
    const code = 'return context.coounter++;';
    const compiledFn = compileFunction(code, { mode: 'sandbox' });
    expect(compiledFn(context, true)).toBe(1);
    expect(context.counter).toBe(1);

    @param code JS code @param options specifies how to compile the function @returns