Guest User

Untitled

a guest
Nov 12th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import JSBI from 'jsbi';
  2. const useNativeBigIntsIfAvailable = true;
  3. if (useNativeBigIntsIfAvailable) JSBI.useNativeBigIntsIfAvailable();
  4. const BigInt = JSBI.BigInt
  5.  
  6. DataView.prototype._setBigUint64 = DataView.prototype.setBigUint64;
  7. DataView.prototype.setBigUint64 = function(byteOffset, value, littleEndian) {
  8. if (typeof value === 'bigint' && typeof this._setBigUint64 !== 'undefined') {
  9. // the original native implementation for bigint
  10. this._setBigUint64(byteOffset, value, littleEndian);
  11. } else if (value.constructor === JSBI && typeof value.sign === 'bigint' && typeof this._setBigUint64 !== 'undefined') {
  12. // JSBI wrapping a native bigint
  13. this._setBigUint64(byteOffset, value.sign, littleEndian);
  14. } else if (value.constructor === JSBI) {
  15. // JSBI polyfill implementation
  16. let lowWord = value[0], highWord = 0;
  17. if (value.length >= 2) {
  18. highWord = value[1];
  19. }
  20. this.setUint32(littleEndian ? 0 : 4, lowWord, littleEndian);
  21. this.setUint32(littleEndian ? 4 : 0, highWord, littleEndian);
  22. } else {
  23. throw TypeError('Value needs to be BigInt ot JSBI');
  24. }
  25. }
  26.  
  27. DataView.prototype._getBigUint64 = DataView.prototype.getBigUint64;
  28. DataView.prototype.getBigUint64 = function(byteOffset, littleEndian) {
  29. if (typeof this._setBigUint64 !== 'undefined' && useNativeBigIntsIfAvailable) {
  30. return BigInt(this._getBigUint64(byteOffset, littleEndian));
  31. } else {
  32. let lowWord = 0, highWord = 0;
  33. lowWord = this.getUint32(littleEndian ? 0 : 4, littleEndian);
  34. highWord = this.getUint32(littleEndian ? 4 : 0, littleEndian);
  35. const result = new JSBI(2, false);
  36. result.__setDigit(0, lowWord);
  37. result.__setDigit(1, highWord);
  38. return result;
  39. }
  40. }
  41.  
  42. const number = BigInt(String(2n ** 63n - 1n)); // '9223372036854775807'
  43.  
  44. console.log(number, String(number));
  45.  
  46. function toUint8Array(bigint, littleEndian = false) {
  47. const arr = new ArrayBuffer(8);
  48. const view = new DataView(arr);
  49. view.setBigUint64(0, bigint, littleEndian);
  50. return new Uint8Array(arr);
  51. }
  52.  
  53. function fromUint8Array(uint8arr, littleEndian = false) {
  54. const view = new DataView(uint8arr.buffer);
  55. return view.getBigUint64(0, littleEndian);
  56. }
  57.  
  58. const arr = toUint8Array(number, false);
  59. console.log(arr);
  60. console.log(fromUint8Array(arr, false), String(fromUint8Array(arr, false)));
  61.  
  62. const arr2 = toUint8Array(number, true);
  63. console.log(arr2);
  64. console.log(fromUint8Array(arr2, true), String(fromUint8Array(arr2, true)));
  65.  
  66. /*
  67. Output for polyfilled implementation:
  68.  
  69. JSBI [ -1, 2147483647, sign: false ] '9223372036854775807'
  70. Uint8Array [ 127, 255, 255, 255, 255, 255, 255, 255 ]
  71. JSBI [ -1, 2147483647, sign: false ] '9223372036854775807'
  72. Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 127 ]
  73. JSBI [ -1, 2147483647, sign: false ] '9223372036854775807'
  74.  
  75. Output for native implementation:
  76.  
  77. JSBI [ sign: 9223372036854775807n ] '9223372036854775807'
  78. Uint8Array [ 127, 255, 255, 255, 255, 255, 255, 255 ]
  79. JSBI [ sign: 9223372036854775807n ] '9223372036854775807'
  80. Uint8Array [ 255, 255, 255, 255, 255, 255, 255, 127 ]
  81. JSBI [ sign: 9223372036854775807n ] '9223372036854775807'
  82. */
Add Comment
Please, Sign In to add comment