# All files / src/generator select.js

100% Statements 10/10
100% Branches 2/2
100% Functions 1/1
100% Lines 10/10

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

    22x
    12x
    12x
    12x

    12x
    5x

    7x

     // @flow
    import opcode from '../emitter/opcode';
    import mapSyntax from './map-syntax';
    import mergeBlock from './merge-block';
    import type { GeneratorType } from './flow/types';

    const generateSelect: GeneratorType = (node, parent) => {
      const [leftHandSide, rightHandSide] = node.params;
      const selectOpcode = { kind: opcode.Select, params: [] };
      const condition = [leftHandSide]
        .map(mapSyntax(parent))
        .reduce(mergeBlock, []);

      if (node.value === '&&') {
        return [
          ...[rightHandSide].map(mapSyntax(parent)).reduce(mergeBlock, []),
          { kind: opcode.i32Const, params: [0] },
          ...condition,
          selectOpcode,
        ];
      }

      return [
        ...condition,
        ...[rightHandSide].map(mapSyntax(parent)).reduce(mergeBlock, []),
        ...condition,
        selectOpcode,
      ];
    };

    export default generateSelect;