| 3 | // Copyright (c) 2016 Samuel Groß |
| 4 | |
| 5 | function Int64(low, high) { |
| 6 | var bytes = new Uint8Array(8); |
| 7 | |
| 8 | if (arguments.length > 2 || arguments.length == 0) |
| 9 | throw TypeError("Incorrect number of arguments to constructor"); |
| 10 | if (arguments.length == 2) { |
| 11 | if (typeof low != 'number' || typeof high != 'number') { |
| 12 | throw TypeError("Both arguments must be numbers"); |
| 13 | } |
| 14 | if (low > 0xffffffff || high > 0xffffffff || low < 0 || high < 0) { |
| 15 | throw RangeError("Both arguments must fit inside a uint32"); |
| 16 | } |
| 17 | low = low.toString(16); |
| 18 | for (let i = 0; i < 8 - low.length; i++) { |
| 19 | low = "0" + low; |
| 20 | } |
| 21 | low = "0x" + high.toString(16) + low; |
| 22 | } |
| 23 | |
| 24 | switch (typeof low) { |
| 25 | case 'number': |
| 26 | low = '0x' + Math.floor(low).toString(16); |
| 27 | case 'string': |
| 28 | if (low.substr(0, 2) === "0x") |
| 29 | low = low.substr(2); |
| 30 | if (low.length % 2 == 1) |
| 31 | low = '0' + low; |
| 32 | var bigEndian = unhexlify(low, 8); |
| 33 | var arr = []; |
| 34 | for (var i = 0; i < bigEndian.length; i++) { |
| 35 | arr[i] = bigEndian[i]; |
| 36 | } |
| 37 | bytes.set(arr.reverse()); |
| 38 | break; |
| 39 | case 'object': |
| 40 | if (low instanceof Int64) { |
| 41 | bytes.set(low.bytes()); |
| 42 | } else { |
| 43 | if (low.length != 8) |
| 44 | throw TypeError("Array must have excactly 8 elements."); |
| 45 | bytes.set(low); |
| 46 | } |
| 47 | break; |
| 48 | case 'undefined': |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | // Return a double whith the same underlying bit representation. |
| 53 | this.asDouble = function () { |
| 54 | // Check for NaN |
| 55 | if (bytes[7] == 0xff && (bytes[6] == 0xff || bytes[6] == 0xfe)) |
| 56 | throw new RangeError("Can not be represented by a double"); |
| 57 | |
| 58 | return Struct.unpack(Struct.float64, bytes); |
| 59 | }; |
| 60 | |
| 61 | this.asInteger = function () { |
| 62 | if (bytes[7] != 0 || bytes[6] > 0x20) { |