All files / src instance.ts

57.81% Statements 37/64
25% Branches 4/16
41.67% Functions 5/12
59.68% Lines 37/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1206x     6x 6x 6x   6x 6x               6x       6x         6x   213x 213x           213x 213x 213x     6x 6x                       6x                                     6x       6x       6x 6x 218x 212x   218x 218x 218x     6x                         6x 6x 6x     6x 6x 6x     6x 224x     6x 203x    
import { loadWasmInstance } from './load';
import ZBar from './ZBar';
 
let inst: ZBar | null = null;
let HEAPU8 = new Uint8Array();
let HEAP32 = new Int32Array();
 
const UTF8Decoder = new TextDecoder('utf8');
const UTF8ArrayToString = (u8Array: Uint8Array, idx: number): string => {
  var endPtr = idx;
  while (u8Array[endPtr]) {
    ++endPtr;
  }
  return UTF8Decoder.decode(u8Array.subarray(idx, endPtr));
};
 
export const UTF8ToString = (ptr: number): string => {
  return ptr ? UTF8ArrayToString(HEAPU8, ptr) : '';
};
 
const proc_exit = (x: number) => {
  console.error('Proc_exit', x);
  throw x;
};
 
const clock_gettime = (clk_id: number, tp: number): number => {
  let now;
  Eif (clk_id === 0) {
    now = Date.now();
  } else {
    /* may need to set errorno, but we just skip the implementation here */
    // ___setErrNo(22);
    return -1;
  }
  HEAP32[tp >> 2] = (now / 1e3) | 0;
  HEAP32[(tp + 4) >> 2] = ((now % 1e3) * 1e3 * 1e3) | 0;
  return 0;
};
 
const buffers = [null, [], []];
const printCharWithBuffer = (stream: number, curr: number) => {
  const buffer: Array<number> = buffers[stream] || [];
  if (curr === 0 || curr === 10) {
    (stream === 1 ? console.log : console.error)(
      UTF8ArrayToString(Uint8Array.from(buffer), 0)
    );
    buffer.length = 0;
  } else {
    buffer.push(curr);
  }
};
 
const ___wasi_fd_write = (
  fd: number,
  iov: number,
  iovcnt: number,
  pnum: number
): number => {
  var num = 0;
  for (var i = 0; i < iovcnt; i++) {
    var ptr = HEAP32[(iov + i * 8) >> 2];
    var len = HEAP32[(iov + (i * 8 + 4)) >> 2];
    for (var j = 0; j < len; j++) {
      printCharWithBuffer(fd, HEAPU8[ptr + j]);
    }
    num += len;
  }
  HEAP32[pnum >> 2] = num;
  return 0;
};
 
const ___wasi_fd_close = () => {
  return 0;
};
 
const ___wasi_fd_seek = () => {
  return 0;
};
 
let lastGrowTimestamp = 0;
const emscripten_notify_memory_growth = (idx: number) => {
  if (lastGrowTimestamp) {
    console.info('zbar.wasm: Memory Grow: ', inst!.memory.buffer.byteLength);
  }
  lastGrowTimestamp = Date.now();
  HEAPU8 = new Uint8Array(inst!.memory.buffer);
  HEAP32 = new Int32Array(inst!.memory.buffer);
};
 
const importObj = {
  env: {
    clock_gettime,
    emscripten_notify_memory_growth
  },
  wasi_snapshot_preview1: {
    fd_write: ___wasi_fd_write,
    fd_close: ___wasi_fd_close,
    fd_seek: ___wasi_fd_seek,
    proc_exit
  }
};
 
let instPromise = (async () => {
  const res = await loadWasmInstance(importObj);
  Iif (!res) {
    throw Error('WASM was not loaded');
  }
  inst = res.exports as ZBar;
  emscripten_notify_memory_growth(0);
  return inst;
})();
 
export const getInstance = async (): Promise<ZBar> => {
  return await instPromise;
};
 
export const getMemoryGrowTimestamp = (): number => {
  return lastGrowTimestamp;
};