# All files / src/core memory.js

100% Statements 29/29
100% Branches 15/15
100% Functions 9/9
100% Lines 29/29

    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

    23x
    23x

    23x
    5x
    5x

    81x

    81x
    81x
    641x
    641x
    2x

    639x

    81x
    87x
    87x

    87x

    6x

    6x

    81x

    81x
    240x
    240x
    240x

    240x

    414x

    240x
    240x

    235x

    5x

     /**
     * Handles access to memory and Memory type declaration
     *
     * @flow
     */
    import Syntax from 'walt-syntax';
    import { GLOBAL_INDEX } from '../semantics/metadata';
    import type { SemanticPlugin } from '../flow/types';

    const isMemoryIdentifier = (context, id) => {
      const memory = context.memories[0];
      return memory && memory.value === id.value;
    };

    export default function memoryPlugin(): SemanticPlugin {
      return {
        semantics() {
          return {
            [Syntax.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);
            },
            [Syntax.ImmutableDeclaration]: next => args => {
              const [decl, context] = args;
              const { scopes, memories } = context;

              // Short circuit since memory is a special type of declaration
              if (
                !scopes.length < 2 &&
                decl.type === 'Memory' &&
                !memories.length
              ) {
                memories.push({
                  ...decl,
                  meta: {
                    ...decl.meta,
                    [GLOBAL_INDEX]: -1,
                  },
                });
                return memories[0];
              }

              return next(args);
            },
            [Syntax.FunctionCall]: next => (args, transform) => {
              const [node, context] = args;
              const [subscript, ...rest] = node.params;
              const [id, field = {}] = subscript.params;

              const callMap = {
                dataSize: {
                  ...id,
                  type: 'i32',
                  Type: Syntax.ArraySubscript,
                  params: [
                    {
                      ...id,
                      type: 'i32',
                      value: '0',
                      Type: Syntax.Constant,
                    },
                    {
                      ...id,
                      type: 'i32',
                      value: '0',
                      Type: Syntax.Constant,
                    },
                  ],
                },
                grow: {
                  ...id,
                  value: 'grow_memory',
                  params: rest.map(p => transform([p, context])),
                  Type: Syntax.NativeMethod,
                },
                size: {
                  ...id,
                  value: 'current_memory',
                  params: [],
                  Type: Syntax.NativeMethod,
                },
              };

              const mapped = callMap[field.value];
              if (
                !(
                  subscript.Type === Syntax.ArraySubscript &&
                  isMemoryIdentifier(context, id) &&
                  mapped
                )
              ) {
                return next(args);
              }

              return mapped;
            },
          };
        },
      };
    }