File size: 1,669 Bytes
5641073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * @author jdiaz5513
 */

import { INVARIANT_UNREACHABLE_CODE } from "../errors";
import { Int64, Uint64 } from "../types/index";

type DataViewSetter = (byteOffset: number, value: number, littleEndian?: boolean) => void;

function _makePrimitiveMaskFn(byteLength: number, setter: DataViewSetter): (x: number) => DataView {
  return (x: number): DataView => {
    const dv = new DataView(new ArrayBuffer(byteLength));
    setter.call(dv, 0, x, true);
    return dv;
  };
}

/* eslint-disable @typescript-eslint/unbound-method */
export const getFloat32Mask = _makePrimitiveMaskFn(4, DataView.prototype.setFloat32);
export const getFloat64Mask = _makePrimitiveMaskFn(8, DataView.prototype.setFloat64);
export const getInt16Mask = _makePrimitiveMaskFn(2, DataView.prototype.setInt16);
export const getInt32Mask = _makePrimitiveMaskFn(4, DataView.prototype.setInt32);
export const getInt8Mask = _makePrimitiveMaskFn(1, DataView.prototype.setInt8);
export const getUint16Mask = _makePrimitiveMaskFn(2, DataView.prototype.setUint16);
export const getUint32Mask = _makePrimitiveMaskFn(4, DataView.prototype.setUint32);
export const getUint8Mask = _makePrimitiveMaskFn(1, DataView.prototype.setUint8);
/* eslint-enable */

export function getBitMask(value: boolean, bitOffset: number): DataView {
  const dv = new DataView(new ArrayBuffer(1));

  if (!value) return dv;

  dv.setUint8(0, 1 << bitOffset % 8);
  return dv;
}

export function getInt64Mask(x: Int64): DataView {
  return x.toDataView();
}

export function getUint64Mask(x: Uint64): DataView {
  return x.toDataView();
}

export function getVoidMask(): void {
  throw new Error(INVARIANT_UNREACHABLE_CODE);
}