# All files / src/generator generate-data.js

100% Statements 14/14
100% Branches 0/0
100% Functions 2/2
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
    27x
    27x
    27x

    74x

    74x
    74x
    58x
    58x
    58x
    58x
    58x

    74x
    74x

    74x

     // @flow
    import { stringEncoder } from "../utils/string";
    import { u32 } from "wasm-types";
    import OutputStream from "../utils/output-stream";

    export default function generateData(
      statics: any,
      DATA_SECTION_HEADER_SIZE: number
    ) {
      // Reserve N bytes for data size header
      let offsetAccumulator = DATA_SECTION_HEADER_SIZE;

      const map: { [string]: number } = {};
      const data = Object.keys(statics).reduce((acc, key) => {
        const encoded = stringEncoder(key);
        acc.push({ offset: Number(offsetAccumulator), data: encoded });
        map[key] = offsetAccumulator;
        offsetAccumulator += encoded.size;
        return acc;
      }, []);

      // reserved stream for the size header
      const lengthStream = new OutputStream();
      lengthStream.push(u32, offsetAccumulator, String(offsetAccumulator));

      return {
        data: [{ offset: 0, data: lengthStream }, ...data],
        map,
      };
    }