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

100% Statements 18/18
100% Branches 4/4
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
    52
    53
    54
    55
    27x
    27x
    27x
    27x

    27x
    38x

    70x
    70x

    70x

    50x
    50x
    50x

    50x
    50x

    20x
    4x

    4x

    20x

     // @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,
                FUNCTION_METADATA: types[typeNode.value].meta.FUNCTION_METADATA,
                DEFAULT_ARGUMENTS: types[typeNode.value].meta.DEFAULT_ARGUMENTS,
              },
            };
            functions[identifierNode.value] = functionNode;
            return {
              ...pairNode,
              params: [functionNode, types[typeNode.value]],
            };
          }

          if (!["Table", "Memory"].includes(typeNode.type)) {
            const index = Object.keys(globals).length;

            globals[identifierNode.value] = {
              ...identifierNode,
              meta: { [GLOBAL_INDEX]: index, [TYPE_CONST]: true },
              type: typeNode.type,
            };
          }

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