All files / src/emitter/section table.js

100% Statements 14/14
100% Branches 4/4
100% Functions 3/3
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 31  21x 21x   21x                   21x 3x 3x 3x 3x 1x         3x 3x 3x   3x    
// @flow
import { varuint32, varint1, varint7 } from '../numbers';
import OutputStream from '../../utils/output-stream';
 
const typeBytecodes = {
  anyfunc: 0x70,
};
 
type TableEntryType = {
  initial: number,
  max?: number,
  type: string,
};
 
const emitEntry = (payload, entry: TableEntryType) => {
  payload.push(varint7, typeBytecodes[entry.type], entry.type);
  payload.push(varint1, entry.max ? 1 : 0, 'has max');
  payload.push(varuint32, entry.initial, 'initial table size');
  if (entry.max) {
    payload.push(varuint32, entry.max, 'max table size');
  }
};
 
export default function emitTables(tables: TableEntryType[]) {
  const stream = new OutputStream();
  stream.push(varuint32, tables.length, 'count');
  tables.forEach(entry => emitEntry(stream, entry));
 
  return stream;
}