# All files / src/closure-plugin index.js

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

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

    22x

    22x
    1x

    1x

     // @flow
    import compile from '..';
    import type { WebAssemblyModuleType } from '../flow/types';

    // Make this a .walt file or pre-parse into an ast.
    const source = `
      const memory: Memory = { initial: 1 };
      let heapPointer: i32 = 0;
      export function __closure_malloc(size: i32): i32 {
        const ptr: i32 = heapPointer;
        heapPointer += size;
        return ptr;
      }

      export function __closure_free(ptr: i32) {
      }

      // Getters
      export function __closure_get_i32(ptr: i32): i32 {
        const view: i32[] = ptr;
        return view[0];
      }

      export function __closure_get_f32(ptr: i32): f32 {
        const view: f32[] = ptr;
        return view[0];
      }

      export function __closure_get_i64(ptr: i32): i64 {
        const view: i64[] = ptr;
        return view[0];
      }

      export function __closure_get_f64(ptr: i32): f64 {
        const view: f64[] = ptr;
        return view[0];
      }

      // Setters
      export function __closure_set_i32(ptr: i32, value: i32) {
        const view: i32[] = ptr;
        view[0] = value;
      }

      export function __closure_set_f32(ptr: i32, value: f32) {
        const view: f32[] = ptr;
        view[0] = value;
      }

      export function __closure_set_i64(ptr: i32, value: i64) {
        const view: i64[] = ptr;
        view[0] = value;
      }

      export function __closure_set_f64(ptr: i32, value: f64) {
        const view: f64[] = ptr;
        view[0] = value;
      }
    `;

    export const mapToImports = (plugin: WebAssemblyModuleType) =>
      plugin.instance.exports;

    export default function closurePlugin() {
      return compile(source, {
        version: 0x1,
        encodeNames: false,
        filename: 'walt-closure-plugin',
        lines: source.split('\n'),
      });
    }