# All files / src/generator indirect-function-call.js

100% Statements 12/12
100% Branches 0/0
100% Functions 1/1
100% Lines 12/12

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

    22x
    4x
    4x
    4x
    4x

    4x

    4x

     // @flow
    import invariant from "invariant";
    import mapSyntax from "./map-syntax";
    import mergeBlock from "./merge-block";
    import opcode from "../emitter/opcode";
    import { LOCAL_INDEX, TYPE_INDEX } from "../semantics/metadata";
    import type { GeneratorType } from "./flow/types";

    const generateIndirectFunctionCall: GeneratorType = (node, parent) => {
      const block = node.params.map(mapSyntax(parent)).reduce(mergeBlock, []);
      const localIndex = node.meta[LOCAL_INDEX];
      const typeIndexMeta = node.meta[TYPE_INDEX];
      invariant(
        localIndex != null,
        "Undefined local index, not a valid function pointer"
      );
      invariant(
        typeIndexMeta != null,
        "Variable is not of a valid function pointer type"
      );

      return [
        ...block,
        {
          kind: opcode.CallIndirect,
          params: [typeIndexMeta, 0],
        },
      ];
    };

    export default generateIndirectFunctionCall;