# All files / src index.js

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

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

    22x
    22x

    22x
    22x

    22x
    22x
    22x

    22x

    22x

    66x
    64x

    64x

    64x

    63x

    63x

    22x

    1x

    1x

    1x
    1x
    1x

    1x

    58x
    56x

     // @flow
    import parser from './parser';
    import semantics from './semantics';
    import validate from './validation';
    import generator from './generator';
    import emitter from './emitter';

    import debug from './utils/debug';
    import prettyPrintNode from './utils/print-node';

    import closurePlugin, { mapToImports } from './closure-plugin';
    import { VERSION_1 } from './emitter/preamble';
    import type { WebAssemblyModuleType, ConfigType } from './flow/types';
    import { stringEncoder, stringDecoder } from './utils/string';
    import walkNode from './utils/walk-node';
    import mapNode from './utils/map-node';

    export {
      parser,
      semantics,
      validate,
      generator,
      emitter,
      prettyPrintNode,
      debug,
      closurePlugin,
      stringEncoder,
      stringDecoder,
      walkNode,
      mapNode,
    };
    export const VERSION = '0.9.1';

    // 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();
    }