All files / src/generator export.js

100% Statements 10/10
100% Branches 6/6
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 33 34 35 36 37 38  21x 21x               21x           128x 128x   128x 5x 5x     5x             123x            
// @flow
import { GLOBAL_INDEX, FUNCTION_INDEX } from '../semantics/metadata';
import {
  EXTERN_GLOBAL,
  EXTERN_MEMORY,
  EXTERN_TABLE,
  EXTERN_FUNCTION,
} from '../emitter/external_kind';
import type { NodeType, IntermediateExportType } from './flow/types';
 
const externaKindMap = {
  Memory: EXTERN_MEMORY,
  Table: EXTERN_TABLE,
};
 
export default function generateExport(node: NodeType): IntermediateExportType {
  const functionIndexMeta = node.meta[FUNCTION_INDEX];
  const globalIndexMeta = node.meta[GLOBAL_INDEX];
 
  if (globalIndexMeta != null) {
    const kind = externaKindMap[String(node.type)] || EXTERN_GLOBAL;
    const index = [EXTERN_MEMORY, EXTERN_TABLE].includes(kind)
      ? 0
      : globalIndexMeta;
    return {
      index,
      kind,
      field: node.value,
    };
  }
 
  return {
    index: functionIndexMeta,
    kind: EXTERN_FUNCTION,
    field: node.value,
  };
}