# All files / src/core struct.js

100% Statements 74/74
91.3% Branches 21/23
100% Functions 18/18
100% Lines 73/73

    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
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    21223x
    23x
    23x
    23x

    23x
    9x
    9x
    9x
    9x

    31x
    31x
    31x

    31x
    31x
    31x

    2x
    2x

    29x

    9x

    23x
    66x
    66x
    66x

    87x

    87x
    87x
    9x

    9x

    9x
    9x

    87x
    661x
    661x
    661x

    660x
    580x

    80x

    87x
    103x
    103x
    206x
    103x
    103x
    103x
    4x

    99x
    99x
    66x
    66x
    66x

    33x

    87x
    82x
    82x

    82x
    73x

    9x
    9x

    9x

    3x

    18x
    18x

    1x
    1x
    1x
    1x
    1x

    1x
    3x

    3x

    9x

    9x

    23x

     import Syntax from '../Syntax';
    import invariant from 'invariant';
    import walkNode from '../utils/walk-node';
    import { ALIAS, TYPE_OBJECT, OBJECT_KEY_TYPES } from '../semantics/metadata';

    export const getByteOffsetsAndSize = objectLiteralNode => {
      const offsetsByKey = {};
      const keyTypeMap = {};
      let size = 0;
      walkNode({
        [Syntax.Pair]: keyTypePair => {
          const { value: key } = keyTypePair.params[0];
          const { value: typeString } = keyTypePair.params[1];
          invariant(
            offsetsByKey[key] == null,
            `Duplicate key ${key} not allowed in object type`
          );

          keyTypeMap[key] = typeString;
          offsetsByKey[key] = size;
          switch (typeString) {
            case 'i64':
            case 'f64':
              size += 8;
              break;
            case 'i32':
            case 'f32':
            default:
              size += 4;
          }
        },
      })(objectLiteralNode);

      return [offsetsByKey, size, keyTypeMap];
    };

    const patchStringSubscript = (byteOffsetsByKey, params) => {
      const field = params[1];
      const absoluteByteOffset = byteOffsetsByKey[field.value];
      return [
        params[0],
        {
          ...field,
          meta: { [ALIAS]: field.value },
          value: absoluteByteOffset,
          type: 'i32',
          Type: Syntax.Constant,
        },
      ];
    };

    export default function Struct() {
      return {
        semantics() {
          return {
            Struct: _ => ([node, { userTypes }]) => {
              const [offsetsByKey, totalSize, keyTypeMap] = getByteOffsetsAndSize(
                node.params[0]
              );

              const struct = {
                ...node,
                meta: {
                  ...node.meta,
                  [TYPE_OBJECT]: offsetsByKey,
                  OBJECT_SIZE: totalSize,
                  [OBJECT_KEY_TYPES]: keyTypeMap,
                },
              };

              userTypes[struct.value] = struct;
              return struct;
            },
            Identifier: next => args => {
              const [node, context] = args;
              const { userTypes, locals, globals } = context;
              const local = locals[node.value] || globals[node.value];
              // Ignore anything not typed as a struct
              if (!(local && userTypes[local.type])) {
                return next(args);
              }

              // Convert all struct uses to i32 types
              return {
                ...node,
                meta: { ...node.meta, ...local.meta, ALIAS: local.type },
                type: 'i32',
              };
            },
            ArraySubscript: next => (args, transform) => {
              const [node, context] = args;
              const { userTypes, locals } = context;
              const params = node.params.map(p => transform([p, context]));
              const [identifier, field] = params;
              const local = locals[identifier.value];
              if (!local) {
                return next(args);
              }
              const userType = userTypes[local.type];
              if (userType != null) {
                const metaObject = userType.meta[TYPE_OBJECT];
                const objectKeyTypeMap = userType.meta[OBJECT_KEY_TYPES];
                return {
                  ...node,
                  type: objectKeyTypeMap ? objectKeyTypeMap[field.value] : 'i32',
                  params: patchStringSubscript(metaObject, params),
                };
              }

              return next(args);
            },
            Assignment: next => (args, transform) => {
              const [node, context] = args;
              const [lhs, rhs] = node.params;

              if (!(rhs && rhs.Type === Syntax.ObjectLiteral)) {
                return next(args);
              }
              const individualKeys = {};
              const spreadKeys = {};
              // We have to walk the nodes twice, once for regular prop keys and then again
              // for ...(spread)
              walkNode({
                // Top level Identifiers _inside_ an object literal === shorthand
                // Notice that we ignore chld mappers in both Pairs and Spread(s) so the
                // only way this is hit is if the identifier is TOP LEVEL
                [Syntax.Identifier]: (identifier, _) => {
                  individualKeys[identifier.value] = {
                    ...lhs,
                    Type: Syntax.MemoryAssignment,
                    params: [
                      {
                        ...lhs,
                        Type: Syntax.ArraySubscript,
                        params: [lhs, identifier],
                      },
                      identifier,
                    ],
                  };
                },
                [Syntax.Pair]: (pair, _) => {
                  const [property, value] = pair.params;
                  individualKeys[property.value] = {
                    ...lhs,
                    Type: Syntax.MemoryAssignment,
                    params: [
                      {
                        ...lhs,
                        Type: Syntax.ArraySubscript,
                        params: [lhs, property],
                      },
                      value,
                    ],
                  };
                },
                [Syntax.Spread]: (spread, _) => {
                  // find userType
                  const { locals, userTypes } = context;
                  const [target] = spread.params;
                  const userType = userTypes[locals[target.value].type];
                  const keyOffsetMap = userType.meta[TYPE_OBJECT];
                  Eif (keyOffsetMap != null) {
                    // map over the keys
                    Object.keys(keyOffsetMap).forEach(key => {
                      const offsetNode = {
                        ...target,
                        Type: Syntax.Identifier,
                        value: key,
                        params: [],
                      };
                      // profit
                      spreadKeys[key] = {
                        ...lhs,
                        Type: Syntax.MemoryAssignment,
                        params: [
                          {
                            ...lhs,
                            Type: Syntax.ArraySubscript,
                            params: [lhs, { ...offsetNode }],
                          },
                          {
                            ...target,
                            Type: Syntax.ArraySubscript,
                            params: [
                              target,
                              {
                                ...offsetNode,
                              },
                            ],
                          },
                        ],
                      };
                    });
                  }
                },
              })(rhs);

              const params = Object.values({ ...spreadKeys, ...individualKeys });

              return {
                ...lhs,
                Type: Syntax.Block,
                // We just created a bunch of MemoryAssignment nodes, map over them so that
                // the correct metadata is applied to everything
                params: params.map(p => transform([p, context])),
              };
            },
          };
        },
      };
    }