# All files / src/core memory.js

100% Statements 14/14
100% Branches 6/6
100% Functions 5/5
100% Lines 14/14

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

    87x

    87x
    87x
    573x
    573x
    2x

    571x

    87x
    108x

    108x
    6x

    102x

     import Syntax from '../Syntax';
    import { GLOBAL_INDEX } from '../semantics/metadata';

    export default function memoryPlugin() {
      return {
        semantics() {
          return {
            Identifier: next => args => {
              const [identifier] = args;
              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,
                    },
                  ],
                };
              }

              return next(args);
            },
            ImmutableDeclaration: next => args => {
              const [decl, context] = args;

              // Short circuit since memory is a special type of declaration
              if (!context.locals && decl.type === 'Memory') {
                return {
                  ...decl,
                  meta: {
                    ...decl.meta,
                    [GLOBAL_INDEX]: -1,
                  },
                };
              }

              return next(args);
            },
          };
        },
      };
    }