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

100% Statements 18/18
100% Branches 2/2
100% Functions 4/4
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
    21x
    21x
    21x

    21x

    122x
    122x
    122x
    122x
    95x
    95x

    27x

    21x
    64x
    64x

    122x

    64x

     // @flow
    import { FUNC, getTypeString } from "../value_type";
    import { varuint32, varint7, varint1 } from "../numbers";
    import OutputStream from "../../utils/output-stream";

    const emitType = (stream, { params, result }, index) => {
      // as of wasm 1.0 spec types are only of from === func
      stream.push(varint7, FUNC, `func type (${index})`);
      stream.push(varuint32, params.length, "parameter count");
      params.forEach(type => stream.push(varint7, type, "param"));
      if (result) {
        stream.push(varint1, 1, "result count");
        stream.push(varint7, result, `result type ${getTypeString(result)}`);
      } else {
        stream.push(varint1, 0, "result count");
      }
    };

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

      types.forEach((type, index) => emitType(stream, type, index));

      return stream;
    };

    export default emit;