All files / src/generator if-then-else.js

100% Statements 7/7
100% Branches 0/0
100% Functions 1/1
100% Lines 7/7
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 62x 62x 62x                                        
// @flow
import mapSyntax from './map-syntax';
import mergeBlock from './merge-block';
import opcode from '../emitter/opcode';
import type { GeneratorType } from './flow/types';
 
// probably should be called "generateBranch" and be more generic
// like handling ternary for example. A lot of shared logic here & ternary
const generateIf: GeneratorType = (node, parent) => {
  const mapper = mapSyntax(parent);
  const [condition, thenBlock, ...restParams] = node.params;
  return [
    ...[condition].map(mapper).reduce(mergeBlock, []),
    {
      kind: opcode.If,
      // if-then-else blocks have no return value and the Wasm spec requires us to
      // provide a literal byte '0x40' for "empty block" in these cases
      params: [0x40],
    },
 
    // after the expression is on the stack and opcode is following it we can write the
    // implicit 'then' block
    ...[thenBlock].map(mapper).reduce(mergeBlock, []),
 
    // followed by the optional 'else'
    ...restParams.map(mapper).reduce(mergeBlock, []),
    { kind: opcode.End, params: [] },
  ];
};
 
export default generateIf;