# All files / src index.js

100% Statements 41/41
100% Branches 7/7
100% Functions 9/9
100% Lines 41/41

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

    27x
    27x

    27x
    27x

    27x
    27x
    27x

    27x

    27x

    64x
    62x

    62x

    62x

    61x

    61x

    27x

    1x

    1x

    1x
    1x
    1x

    1x

    27x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x

    1x

    1x

    1x

    56x
    54x

     // @flow
    import parser, { async as asyncParser } from "./parser";
    import semantics, { async as asyncSemantics } from "./semantics";
    import validate from "./validation";
    import generator, { async as asyncGenerator } from "./generator";
    import emitter, { async as asyncEmitter } 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,
      // Unstable, Asynchronous APIs
      asyncParser,
      asyncSemantics,
      asyncGenerator,
    };
    export const VERSION = "0.5.3";

    // 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,
      };
    };

    /**
     * Async compiler. UNSTABLE
     *
     * Uses WebAssembly internally/is self-hosted
     *
     * @param {String} source Source input
     */
    export const unstableAsyncCompile = (source: string): Promise<any> => {
      return Promise.all([asyncSemantics(), asyncGenerator(), asyncEmitter()]).then(
        ([sem, gen, emit]) => {
          const lines = source.split("\n");
          const filename = "??";
          const encodeNames = true;
          const version = 0x1;
          return asyncParser(source)
            .then(sem)
            .then(ast => {
              validate(ast, { lines, filename });
              return ast;
            })
            .then(ast => {
              const code = gen(ast, {
                version,
                encodeNames,
                lines,
                filename,
              });
              const wasm = emit(code, {
                version,
                encodeNames,
                filename,
                lines,
              });

              return wasm;
            });
        }
      );
    };

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