# All files / src/semantics/map-function map-identifier.js

100% Statements 24/24
100% Branches 16/16
100% Functions 2/2
100% Lines 24/24

    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

    610x
    610x

    610x
    3x

    607x
    11x

    596x
    2x

    594x
    442x
    442x
    442x

    442x

    152x
    87x

    65x
    7x
    6x

    7x

    58x

     // @flow
    import Syntax from "../../Syntax";
    import curry from "curry";
    import { FUNCTION_INDEX, TYPE_ARRAY } from "../metadata";

    const mapIdentifier = curry(
      ({ locals, globals, functions, table }, identifier) => {
        // Not a function call or pointer, look-up variables
        const local = locals[identifier.value];
        const global = globals[identifier.value];

        if (identifier.value === "false") {
          return {
            ...identifier,
            type: "i32",
            value: "0",
            Type: Syntax.Constant,
          };
        }

        if (identifier.value === "true") {
          return {
            ...identifier,
            type: "i32",
            value: "1",
            Type: Syntax.Constant,
          };
        }

        if (identifier.value === "__DATA_LENGTH__") {
          return {
            ...identifier,
            type: "i32",
            Type: Syntax.ArraySubscript,
            params: [
              { ...identifier, type: "i32", value: "0", Type: Syntax.Constant },
              { ...identifier, type: "i32", value: "0", Type: Syntax.Constant },
            ],
          };
        }

        if (local != null) {
          const type = (() => {
            const isArray = local.meta[TYPE_ARRAY];
            return isArray || local.type;
          })();
          return {
            ...identifier,
            type,
            meta: { ...local.meta },
          };
        } else if (global != null) {
          return {
            ...identifier,
            type: globals[identifier.value].type,
            meta: { ...global.meta },
          };
        } else if (functions[identifier.value] != null) {
          if (table[identifier.value] == null) {
            table[identifier.value] = functions[identifier.value];
          }
          return {
            ...identifier,
            type: "i32",
            meta: {
              [FUNCTION_INDEX]: Object.keys(functions).indexOf(identifier.value),
            },
            value: Object.keys(table).indexOf(identifier.value),
            Type: Syntax.FunctionPointer,
          };
        }

        return identifier;
      }
    );

    export default mapIdentifier;