# All files / src index.js

100% Statements 31/31
100% Branches 7/7
100% Functions 5/5
100% Lines 31/31

    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
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x
    23x

    23x
    23x
    23x
    23x
    23x
    23x

    23x

    65x
    63x

    63x

    63x

    62x

    62x

    23x

    1x

    1x

    1x
    1x
    1x

    1x

    57x
    55x

     // @flow
    import parser from "./parser";
    import emit from "./emitter";
    import codeGenerator from "./generator";
    import semanticAnalyzer from "./semantics";
    import astValidator from "./validation";
    import _debug from "./utils/debug";
    import printNode from "./utils/print-node";
    import closurePlugin, { mapToImports } from "./closure-plugin";
    import { VERSION_1 } from "./emitter/preamble";
    import type { WebAssemblyModuleType, ConfigType } from "./flow/types";

    export const debug = _debug;
    export const prettyPrintNode = printNode;
    export const semantics = semanticAnalyzer;
    export const generator = codeGenerator;
    export const validate = astValidator;
    export const emitter = emit;
    export { parser, printNode, closurePlugin };

    // Used for deugging purposes
    export const getIR = (
      source: string,
      {
        version = VERSION_1,
        encodeNames = false,
        lines = source ? source.split("\n") : [],
        filename = "unknown",
      }: ConfigType = {}
    ) => {
      const ast = parser(source);
      const semanticAST = semantics(ast);

      validate(semanticAST, {
        lines,
        filename,
      });
      const intermediateCode = generator(semanticAST, {
        version,
        encodeNames,
        lines,
        filename,
      });
      const wasm = emitter(intermediateCode, {
        version,
        encodeNames,
        filename,
        lines,
      });
      return wasm;
    };

    export const withPlugins = (
      plugins: { [string]: WebAssemblyModuleType },
      importsObj?: { [string]: any }
    ) => {
      const pluginMappers = {
        closure: (closure, imports) => {
          imports["walt-plugin-closure"] = mapToImports(closure);
        },
      };
      const resultImports = Object.entries(plugins).reduce((acc, [key, value]) => {
        pluginMappers[key](value, acc);
        return acc;
      }, {});

      return {
        ...resultImports,
        ...importsObj,
      };
    };

    // Compiles a raw binary wasm buffer
    export default function compileWalt(source: string, config: ConfigType) {
      const wasm = getIR(source, config);
      return wasm.buffer();
    }