All files main.js

94.15% Statements 403/428
94.11% Branches 48/51
100% Functions 9/9
94.15% Lines 403/428

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 4294x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 79x 79x 79x 79x 1x 1x 78x 79x 7x 2x 2x 5x 5x 5x 79x 60x 60x 41x 60x 19x 19x 71x 11x 11x 65x 65x 79x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 24x 24x 8x 8x 24x 3x 3x 13x 24x 1x 1x 1x 1x 12x 12x 12x 12x 12x 24x 24x 12x 24x 10x 10x 10x 10x 10x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 32x 32x 20x 20x 32x 10x 10x 2x 2x 2x 2x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1087x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1087x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1066x 1066x 1066x 1066x 1066x 4x 1066x 1050x 1062x 12x 12x 1054x 1054x 1054x                       4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 9x 9x 9x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1060x 1060x 1060x 1060x 1060x 1060x                     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x         1x 1x 4x 1x 1x 2x 4x 4x 4x 4x 4x 4x 4x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
/* eslint-disable max-len, no-restricted-syntax, no-invalid-this */
 
'use strict';
 
// MODULES //
 
var hasBigIntSupport = require( '@stdlib/assert/has-bigint-support' );
var hasToPrimitiveSymbolSupport = require( '@stdlib/assert/has-to-primitive-symbol-support' ); // eslint-disable-line id-length
var isUint32Array = require( '@stdlib/assert/is-uint32array' );
var isInteger = require( '@stdlib/assert/is-integer' ).isPrimitive;
var isBigInt = require( '@stdlib/assert/is-bigint' ).isPrimitive;
var isCollection = require( '@stdlib/assert/is-collection' );
var isBetween = require( '@stdlib/assert/is-between' );
var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
var BigInt = require( '@stdlib/bigint/ctor' );
var Number = require( '@stdlib/number/ctor' );
var Uint32Array = require( '@stdlib/array/uint32' );
var join = require( '@stdlib/array/base/join' );
var UINT32_MAX = require( '@stdlib/constants/uint32/max' );
var MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
var MIN_SAFE_INTEGER = require( '@stdlib/constants/float64/min-safe-integer' );
var format = require( '@stdlib/string/format' );
var ToPrimitiveSymbol = require( '@stdlib/symbol/to-primitive' );
var indices = require( './indices.js' );
var int2str = require( './to_string.js' );
 
 
// VARIABLES //
 
var HAS_BIGINT = hasBigIntSupport();
var TWO_32 = 0x100000000; // 2^32
 
 
// MAIN //
 
/**
* 64-bit signed integer constructor.
*
* @constructor
* @param {(number|bigint)} value - integer value
* @throws {TypeError} must invoke using the `new` keyword
* @throws {TypeError} must provide a valid number
* @returns {Int64} 64-bit signed integer
*
* @example
* var v = new Int64( 5 );
* // returns <Int64>
*/
function Int64( value ) {
	var buffer;
	var v;
	if ( !( this instanceof Int64 ) ) {
		throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' );
	}
	buffer = new Uint32Array( 2 );
	if ( isBigInt( value ) ) {
		if ( BigInt.asIntN( 64, value ) !== value ) {
			throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [-(2^63), 2^63-1]. Value: `%s`.', value ) );
		}
		v = BigInt.asUintN( 64, value ); // Convert to unsigned to make it fixed-width
		buffer[ indices.HIGH ] = Number( v >> BigInt( 32 ) );
		buffer[ indices.LOW ] = Number( v & BigInt( UINT32_MAX ) );
	} else if ( isInteger( value ) && isBetween( value, MIN_SAFE_INTEGER, MAX_SAFE_INTEGER ) ) {
		buffer[ indices.LOW ] = value >>> 0; // Truncate to 32-bit unsigned integer
		if ( value >= 0 ) {
			buffer[ indices.HIGH ] = ( value / TWO_32 ) >>> 0;
		} else {
			buffer[ indices.HIGH ] = ( ~( ( -value ) / TWO_32 ) + !buffer[ indices.LOW ] ) >>> 0; // 2's complement with carry from low word
		}
	} else {
		throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [-(2^53-1), 2^53-1]. Value: `%s`.', value ) );
	}
	setReadOnly( this, '_buffer', buffer );
	return this;
}
 
/**
* Constructor name.
*
* @name name
* @memberof Int64
* @readonly
* @type {string}
* @default 'Int64'
*
* @example
* var name = Int64.name;
* // returns 'Int64'
*/
setReadOnly( Int64, 'name', 'Int64' );
 
/**
* Creates a new 64-bit signed integer from an array-like object containing a high and low word.
*
* @name from
* @memberof Int64
* @type {Function}
* @param {Collection} words - high and low words as 32-bit unsigned integers
* @throws {TypeError} must provide an array-like object containing two elements
* @returns {Int64} 64-bit signed integer
*
* @example
* var x = Int64.from( [ 1234, 5678 ] );
* // returns <Int64>
*/
setReadOnly( Int64, 'from', function fromWords( words ) {
	var v;
	if ( !isCollection( words ) ) {
		throw new TypeError( format( 'invalid argument. Must provide an array. Value: `%s`.', words ) );
	}
	if ( words.length !== 2 ) {
		throw new TypeError( format( 'invalid argument. Must provide an array of length 2. Value: `[%s]`.', join( words, ',' ) ) );
	}
	v = new Int64( 0 );
	if ( isUint32Array( words ) ) {
		v._buffer[ indices.HIGH ] = words[ 0 ]; // eslint-disable-line no-underscore-dangle
		v._buffer[ indices.LOW ] = words[ 1 ]; // eslint-disable-line no-underscore-dangle
		return v;
	}
 
	// TODO: consider supporting any iterable object
 
	if (
		isInteger( words[ 0 ] ) &&
		isInteger( words[ 1 ] ) &&
		isBetween( words[ 0 ], 0, UINT32_MAX ) &&
		isBetween( words[ 1 ], 0, UINT32_MAX )
	) {
		// TODO: consider whether we should support accessor arrays here
		v._buffer[ indices.HIGH ] = words[ 0 ]; // eslint-disable-line no-underscore-dangle
		v._buffer[ indices.LOW ] = words[ 1 ]; // eslint-disable-line no-underscore-dangle
		return v;
	}
	throw new TypeError( format( 'invalid argument. Must provide an array of integer values on the interval [%u, %u]. Value: `[%s]`.', 0, UINT32_MAX, join( words, ', ' ) ) );
});
 
/**
* Creates a new 64-bit signed integer from a high and low word.
*
* @name of
* @memberof Int64
* @type {Function}
* @param {uinteger} high - high word (32-bit unsigned integer)
* @param {uinteger} low - low word (32-bit unsigned integer)
* @throws {TypeError} first argument must be a nonnegative integer
* @throws {TypeError} second argument must be a nonnegative integer
* @returns {Int64} 64-bit signed integer
*
* @example
* var x = Int64.of( 1234, 5678 );
* // returns <Int64>
*/
setReadOnly( Int64, 'of', function of( high, low ) {
	var v;
	if ( !isInteger( high ) || !isBetween( high, 0, UINT32_MAX ) ) {
		throw new TypeError( format( 'invalid argument. First argument must be a nonnegative integer on the interval [%u, %u]. Value: `%s`.', 0, UINT32_MAX, high ) );
	}
	if ( !isInteger( low ) || !isBetween( low, 0, UINT32_MAX ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer on the interval [%u, %u]. Value: `%s`.', 0, UINT32_MAX, low ) );
	}
	v = new Int64( 0 );
	v._buffer[ indices.HIGH ] = high; // eslint-disable-line no-underscore-dangle
	v._buffer[ indices.LOW ] = low; // eslint-disable-line no-underscore-dangle
	return v;
});
 
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Int64
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var nbytes = Int64.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Int64, 'BYTES_PER_ELEMENT', 8 );
 
/**
* Size (in bytes) of the underlying value.
*
* @name BYTES_PER_ELEMENT
* @memberof Int64.prototype
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var x = new Int64( 5 );
*
* var nbytes = x.BYTES_PER_ELEMENT;
* // returns 8
*/
setReadOnly( Int64.prototype, 'BYTES_PER_ELEMENT', 8 );
 
/**
* Size (in bytes) of the underlying value.
*
* @name byteLength
* @memberof Int64.prototype
* @type {integer}
* @returns {integer} size in bytes
*
* @example
* var x = new Int64( 5 );
*
* var nbytes = x.byteLength;
* // returns 8
*/
setReadOnly( Int64.prototype, 'byteLength', 8 );
 
/**
* Returns the high 32-bit word of a 64-bit signed integer.
*
* @name hi
* @memberof Int64.prototype
* @type {uinteger}
* @returns {uinteger} high word (32-bit unsigned integer)
*
* @example
* var x = Int64.from( [ 1234, 5678 ] );
*
* var w = x.hi;
* // returns 1234
*/
setReadOnlyAccessor( Int64.prototype, 'hi', function getHighWord() {
	return this._buffer[ indices.HIGH ];
});
 
/**
* Returns the low 32-bit word of a 64-bit signed integer.
*
* @name lo
* @memberof Int64.prototype
* @type {uinteger}
* @returns {uinteger} low word (32-bit unsigned integer)
*
* @example
* var x = Int64.from( [ 1234, 5678 ] );
*
* var w = x.lo;
* // returns 5678
*/
setReadOnlyAccessor( Int64.prototype, 'lo', function getLowWord() {
	return this._buffer[ indices.LOW ];
});
 
/**
* Serializes a 64-bit signed integer as a string.
*
* @name toString
* @memberof Int64.prototype
* @type {Function}
* @param {PositiveInteger} [radix=10] - radix (base) to use for string conversion (2-36)
* @throws {TypeError} must provide an integer on the interval [2,36]
* @returns {string} serialized 64-bit signed integer
*
* @example
* var str = new Int64( 5 ).toString();
* // returns '5'
*
* str = Int64.from( [ 1, 0 ] ).toString();
* // returns '4294967296'
*
* str = new Int64( 100000000001 ).toString();
* // returns '100000000001'
*
*/
setReadOnly( Int64.prototype, 'toString', function toString( radix ) {
	var sign;
	var rad;
	var hi;
	var lo;
	if ( arguments.length < 1 ) {
		rad = 10;
	} else if ( isInteger( radix ) && isBetween( radix, 2, 36 ) ) {
		rad = radix;
	} else {
		throw new TypeError( format( 'invalid argument. Must provide an integer on the interval [%d, %d]. Value: `%s`.', 2, 36, radix ) );
	}
	if ( HAS_BIGINT ) {
		return BigInt( this.valueOf() ).toString( rad );
	}

	sign = '';
	hi = this.hi;
	lo = this.lo;
	if ( hi >>> 31 ) {
		sign = '-';
		lo = ( ~lo + 1 ) >>> 0;
		hi = ( ~hi + !lo ) >>> 0;
	}
	// TODO: Refactor to use `uint64/base/words2string` later
	return sign + int2str( [ hi, lo ], rad );
});
 
/**
* Serializes a 64-bit signed integer as a JSON object.
*
* ## Notes
*
* -   `JSON.stringify()` implicitly calls this method when stringifying a `Int64` instance.
*
* @name toJSON
* @memberof Int64.prototype
* @type {Function}
* @returns {Object} serialized 64-bit signed integer
*
* @example
* var x = new Int64( 5 );
*
* var y = x.toJSON();
* // returns { 'type': 'Int64', 'words': [ 0, 5 ] }
*/
setReadOnly( Int64.prototype, 'toJSON', function toJSON() {
	return {
		'type': 'Int64',
		'words': [ this.hi, this.lo ]
	};
});
 
/**
* Converts a 64-bit signed integer to a primitive value.
*
* @name valueOf
* @memberof Int64.prototype
* @type {Function}
* @returns {(number|bigint)} primitive value
*
* @example
* var x = new Int64( 5 );
*
* var v = x.valueOf();
* // e.g., returns <bigint>
*/
setReadOnly( Int64.prototype, 'valueOf', function valueOf() {
	var sign;
	var hi;
	var lo;
	if ( HAS_BIGINT ) {
		return BigInt.asIntN( 64, ( BigInt( this.hi ) << BigInt( 32 ) ) | BigInt( this.lo ) );
	}

	sign = 1;
	hi = this.hi;
	lo = this.lo;
	if ( hi >>> 31 ) {
		sign = -1;
		lo = ( ~lo + 1 ) >>> 0;
		hi = ( ~hi + !lo ) >>> 0;
	}
	return sign * ( ( hi * TWO_32 ) + lo );
});
 
/**
* Returns the primitive value of a 64-bit signed integer.
*
* @name toPrimitive
* @memberof Int64.prototype
* @type {Function}
* @param {string} hint - conversion hint
* @returns {(string|number|bigint)} primitive value
*
* @example
* var hasSymbol = require( '@stdlib/assert/has-to-primitive-symbol-support' );
* var ToPrimitiveSymbol = require( '@stdlib/symbol/to-primitive' );
*
* var x = new Int64( 5 );
*
* var v;
* if ( hasSymbol() ) {
*     v = x[ ToPrimitiveSymbol ]( 'number' );
*     // returns 5
*
*     v = x[ ToPrimitiveSymbol ]( 'string' );
*     // returns '5'
*
*     v = x[ ToPrimitiveSymbol ]();
*     // e.g., returns <bigint>
* }
*/
if ( hasToPrimitiveSymbolSupport() ) {
	setReadOnly( Int64.prototype, ToPrimitiveSymbol, function toPrimitive( hint ) {
		var sign;
		var hi;
		var lo;
		if ( hint === 'number' ) {
			sign = 1;
			hi = this.hi;
			lo = this.lo;
			if ( hi >>> 31 ) {
				sign = -1;
				lo = ( ~lo + 1 ) >>> 0;
				hi = ( ~hi + !lo ) >>> 0;
			}
			return sign * ( ( hi * TWO_32 ) + lo );
		}
		if ( hint === 'string' ) {
			return this.toString();
		}
		return this.valueOf();
	});
}
 
 
// EXPORTS //
 
module.exports = Int64;