All files / src/generator generate-data.js

100% Statements 12/12
100% Branches 0/0
100% Functions 2/2
100% Lines 12/12
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    21x 21x             76x   76x   76x   106x 106x 106x 106x           76x 76x   76x          
// @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.entries(statics).reduce(
    (acc, [key, encoded]: [string, any]) => {
      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,
  };
}