All files / src/emitter/section writer.js

100% Statements 13/13
100% Branches 5/5
100% Functions 2/2
100% Lines 13/13
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  21x 21x 21x   21x               252x 867x 867x 498x     369x 369x   369x 369x   369x        
// @flow
import { u8 } from 'wasm-types';
import { varuint32 } from '../numbers';
import OutputStream from '../../utils/output-stream';
 
const writer = ({
  type,
  label,
  emitter,
}: {
  type: number,
  label: string,
  emitter: any => OutputStream,
}) => (ast: any): ?OutputStream => {
  const field = ast[label];
  if (!field || (Array.isArray(field) && !field.length)) {
    return null;
  }
 
  const stream = new OutputStream().push(u8, type, label + ' section');
  const entries = emitter(field);
 
  stream.push(varuint32, entries.size, 'size');
  stream.write(entries);
 
  return stream;
};
 
export default writer;