# All files / src/semantics map-import.js

100% Statements 18/18
100% Branches 6/6
100% Functions 2/2
100% Lines 18/18

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

    23x
    16x

    33x
    33x

    33x

    23x
    23x
    23x

    23x
    23x

    10x
    2x
    2x

    10x

     // @flow
    import curry from "curry";
    import Syntax from "../Syntax";
    import mapNode from "../utils/map-node";
    import {
      FUNCTION_INDEX,
      TYPE_INDEX,
      TYPE_CONST,
      GLOBAL_INDEX,
    } from "./metadata";

    export const mapImport = curry((options, node, _) =>
      mapNode({
        [Syntax.Pair]: (pairNode, __) => {
          const { types, functions, globals } = options;
          const [identifierNode, typeNode] = pairNode.params;

          if (types[typeNode.value] != null) {
            // crate a new type
            const functionIndex = Object.keys(functions).length;
            const typeIndex = Object.keys(types).indexOf(typeNode.value);
            const functionNode = {
              ...identifierNode,
              id: identifierNode.value,
              type: types[typeNode.value].type,
              meta: {
                [FUNCTION_INDEX]: functionIndex,
                [TYPE_INDEX]: typeIndex,
              },
            };
            functions[identifierNode.value] = functionNode;
            return {
              ...pairNode,
              params: [functionNode, types[typeNode.value]],
            };
          }

          if (typeNode.type !== "Table" && typeNode.type !== "Memory") {
            const index = Object.keys(globals).length;
            globals[identifierNode.value] = {
              ...identifierNode,
              meta: { [GLOBAL_INDEX]: index, [TYPE_CONST]: true },
              type: typeNode.type,
            };
          }

          return pairNode;
        },
      })(node)
    );