Skip to content

Commit 022393f

Browse files
STREAMS-1895: Add old shell compatibility of NumberLong for JS Engine
1 parent ceb23b8 commit 022393f

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

snippets/mongocompat/mongotypes.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,53 @@ if (!NumberLong.prototype) {
378378
NumberLong.prototype = {};
379379
}
380380

381+
NumberLong.prototype.nativeToString = NumberLong.prototype.toString;
382+
NumberLong.prototype.toString = function () {
383+
return `NumberLong(${this.nativeToString()})`;
384+
};
385+
381386
NumberLong.prototype.tojson = function() {
382387
return this.toString();
383388
};
384389

390+
Object.defineProperty(NumberLong.prototype, 'floatApprox', {
391+
enumerable: false,
392+
configurable: true,
393+
get: function() {
394+
return Number(this.nativeToString());
395+
}
396+
});
397+
398+
Object.defineProperty(NumberLong.prototype, 'top', {
399+
enumerable: false,
400+
configurable: true,
401+
get: function() {
402+
const str = this.nativeToString();
403+
const bigIntValue = BigInt(str);
404+
const unsigned64 = bigIntValue < 0n
405+
? bigIntValue + (1n << 64n)
406+
: bigIntValue;
407+
return Number((unsigned64 >> 32n) & 0xFFFFFFFFn);
408+
}
409+
});
410+
411+
Object.defineProperty(NumberLong.prototype, 'bottom', {
412+
enumerable: false,
413+
configurable: true,
414+
get: function() {
415+
const str = this.nativeToString();
416+
const bigIntValue = BigInt(str);
417+
const unsigned64 = bigIntValue < 0n
418+
? bigIntValue + (1n << 64n)
419+
: bigIntValue;
420+
return Number(unsigned64 & 0xFFFFFFFFn);
421+
}
422+
});
423+
424+
NumberLong.prototype.exactValueString = function() {
425+
return this.nativeToString();
426+
};
427+
385428
// NumberInt
386429
if (!NumberInt.prototype) {
387430
NumberInt.prototype = {};

snippets/mongocompat/test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
load(__dirname + '/index.js');
22

33
assert.strictEqual(ObjectId('0123456789abcdef01234567').tojson(), 'ObjectId("0123456789abcdef01234567")');
4+
45
assert.strictEqual(BinData(4, 'abcdefgh').toString(), 'BinData(4,"abcdefgh")');
6+
7+
assert.strictEqual(NumberLong(1234567890).toString(), 'NumberLong(1234567890)');
8+
const nl1 = NumberLong(9.223372036854776e+18, 2147483647, 4294967295);
9+
assert.strictEqual(nl1.floatApprox, 9223372036854776000);
10+
assert.strictEqual(nl1.top, 2147483647);
11+
assert.strictEqual(nl1.bottom, 4294967295);
12+
assert.strictEqual(nl1.exactValueString(), "9223372036854775807");
13+
const nl2 = NumberLong("200");
14+
assert.strictEqual(nl1.compare(nl2), 1);

0 commit comments

Comments
 (0)