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

100% Statements 20/20
70.59% Branches 12/17
100% Functions 2/2
100% Lines 20/20

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

    4x

    4x

    23x
    18x
    18x
    18x

    18x
    18x

    18x
    10x
    10x
    10x

    8x

    8x

     // @flow
    import invariant from "invariant";
    import curry from "curry";
    import Syntax from "../../Syntax";
    import { OBJECT_SIZE } from "../../semantics/metadata";

    const variableSize = (type: string): string => {
      switch (type) {
        case "i64":
        case "f64":
          return "8";
        case "i32":
        case "f32":
        default:
          return "4";
      }
    };

    const mapSizeof = curry(({ locals, globals, functions, userTypes }, sizeof) => {
      const [target] = sizeof.params;
      const local = locals[target.value];
      const global = globals[target.value];
      const userType =
        userTypes[target.value] || (local ? userTypes[local.type] : null);
      const func = functions[target.value];

      if (userType != null) {
        const metaSize = userType.meta[OBJECT_SIZE];
        invariant(metaSize, "Object size information is missing");
        return {
          ...sizeof,
          value: metaSize,
          params: [],
          type: "i32",
          Type: Syntax.Constant,
        };
      }

      const node = local || global || userType || func;

      return {
        ...sizeof,
        value: variableSize(node ? node.type : target.value),
        type: "i32",
        params: [],
        Type: Syntax.Constant,
      };
    });

    export default mapSizeof;