# All files / src/emitter/section element.js

100% Statements 17/17
100% Branches 0/0
100% Functions 3/3
100% Lines 16/16

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

    21x

    6x
    6x
    6x
    6x
    6x
    6x

    21x
    3x
    3x

    3x

    3x

     // @flow
    import { u8 } from "wasm-types";
    import { varuint32 } from "../numbers";
    import opcode from "../opcode";
    import OutputStream from "../../utils/output-stream";

    type Element = {
      functionIndex: number,
    };

    const emitElement = (stream: OutputStream) => (
      { functionIndex }: Element,
      index: number
    ) => {
      stream.push(varuint32, 0, "table index");
      stream.push(u8, opcode.i32Const.code, "offset");
      stream.push(varuint32, index, index.toString());
      stream.push(u8, opcode.End.code, "end");
      stream.push(varuint32, 1, "number of elements");
      stream.push(varuint32, functionIndex, "function index");
    };

    const emit = (elements: Element[]) => {
      const stream = new OutputStream();
      stream.push(varuint32, elements.length, "count");

      elements.forEach(emitElement(stream));

      return stream;
    };

    export default emit;