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

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

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

    21x

    21x

    1x

    1x

    1x

     // @flow
    import compile from "..";
    import {
      CLOSURE_SET,
      CLOSURE_GET,
      CLOSURE_FREE,
      CLOSURE_MALLOC,
    } from "../semantics/closure";
    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 malloc(size: i32): i32 {
        const ptr: i32 = heapPointer;
        heapPointer += size;
        return ptr;
      }

      export function free(ptr: i32) {
      }

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

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

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

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

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

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

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

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

    export const mapToImports = (plugin: WebAssemblyModuleType) => {
      const {
        malloc,
        free,
        geti32,
        getf32,
        geti64,
        getf64,
        seti32,
        setf32,
        seti64,
        setf64,
      } = plugin.instance.exports;

      return {
        [CLOSURE_MALLOC]: malloc,
        [CLOSURE_FREE]: free,
        [`${CLOSURE_GET}-i32`]: geti32,
        [`${CLOSURE_GET}-f32`]: getf32,
        [`${CLOSURE_GET}-i64`]: geti64,
        [`${CLOSURE_GET}-f64`]: getf64,
        [`${CLOSURE_SET}-i32`]: seti32,
        [`${CLOSURE_SET}-f32`]: setf32,
        [`${CLOSURE_SET}-i64`]: seti64,
        [`${CLOSURE_SET}-f64`]: setf64,
      };
    };

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