Skip to content
Permalink
Browse files
[ESNext][BigInt] Implement support for "<<" and ">>"
https://bugs.webkit.org/show_bug.cgi?id=186233

Reviewed by Yusuke Suzuki.

JSTests:

* stress/big-int-left-shift-general.js: Added.
* stress/big-int-left-shift-range-error.js: Added.
* stress/big-int-left-shift-type-error.js: Added.
* stress/big-int-left-shift-wrapped-value.js: Added.
* stress/big-int-right-shift-general.js: Added.
* stress/big-int-right-shift-type-error.js: Added.
* stress/big-int-right-shift-wrapped-value.js: Added.
* stress/left-shift-to-primitive-precedence.js: Added.
* stress/right-shift-to-primitive-precedence.js: Added.

Source/JavaScriptCore:

This patch is introducing the support for BigInt into lshift and
rshift into LLint and Baseline layers.

* runtime/CommonSlowPaths.cpp:
(JSC::SLOW_PATH_DECL):
* runtime/JSBigInt.cpp:
(JSC::JSBigInt::createWithLength):
(JSC::JSBigInt::leftShift):
(JSC::JSBigInt::signedRightShift):
(JSC::JSBigInt::leftShiftByAbsolute):
(JSC::JSBigInt::rightShiftByAbsolute):
(JSC::JSBigInt::rightShiftByMaximum):
(JSC::JSBigInt::toShiftAmount):
* runtime/JSBigInt.h:


Canonical link: https://commits.webkit.org/206934@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@238790 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
caiolima committed Dec 2, 2018
1 parent b1b0c29 commit 3f23465d98f76b75f42089b96fccf1036152ce53
Showing 15 changed files with 825 additions and 24 deletions.
@@ -1,3 +1,20 @@
2018-12-02 Caio Lima <ticaiolima@gmail.com>

[ESNext][BigInt] Implement support for "<<" and ">>"
https://bugs.webkit.org/show_bug.cgi?id=186233

Reviewed by Yusuke Suzuki.

* stress/big-int-left-shift-general.js: Added.
* stress/big-int-left-shift-range-error.js: Added.
* stress/big-int-left-shift-type-error.js: Added.
* stress/big-int-left-shift-wrapped-value.js: Added.
* stress/big-int-right-shift-general.js: Added.
* stress/big-int-right-shift-type-error.js: Added.
* stress/big-int-right-shift-wrapped-value.js: Added.
* stress/left-shift-to-primitive-precedence.js: Added.
* stress/right-shift-to-primitive-precedence.js: Added.

2018-11-30 Dean Jackson <dino@apple.com>

Add first-class support for .mjs files in jsc binary
@@ -0,0 +1,103 @@
//@ runBigIntEnabled

// Copyright (C) 2017 Josh Wolfe. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

function assert(a) {
if (!a)
throw new Error("Bad assertion");
}

assert.sameValue = function (input, expected, message) {
if (input !== expected)
throw new Error(message);
}

assert.sameValue(0n << 0n, 0n, "0n << 0n === 0n");
assert.sameValue(0b101n << 1n, 0b1010n, "0b101n << 1n === 0b1010n");
assert.sameValue(0b101n << 2n, 0b10100n, "0b101n << 2n === 0b10100n");
assert.sameValue(0b101n << 3n, 0b101000n, "0b101n << 3n === 0b101000n");
assert.sameValue(0b101n << -1n, 0b10n, "0b101n << -1n === 0b10n");
assert.sameValue(0b101n << -2n, 1n, "0b101n << -2n === 1n");
assert.sameValue(0b101n << -3n, 0n, "0b101n << -3n === 0n");
assert.sameValue(0n << 128n, 0n, "0n << 128n === 0n");
assert.sameValue(0n << -128n, 0n, "0n << -128n === 0n");
assert.sameValue(0x246n << 0n, 0x246n, "0x246n << 0n === 0x246n");
assert.sameValue(0x246n << 127n, 0x12300000000000000000000000000000000n, "0x246n << 127n === 0x12300000000000000000000000000000000n");
assert.sameValue(0x246n << 128n, 0x24600000000000000000000000000000000n, "0x246n << 128n === 0x24600000000000000000000000000000000n");
assert.sameValue(0x246n << 129n, 0x48c00000000000000000000000000000000n, "0x246n << 129n === 0x48c00000000000000000000000000000000n");
assert.sameValue(0x246n << -128n, 0n, "0x246n << -128n === 0n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << 64n, 0x123456789abcdef0fedcba98765432123456780000000000000000n,
"0x123456789abcdef0fedcba9876543212345678n << 64n === 0x123456789abcdef0fedcba98765432123456780000000000000000n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << 32n, 0x123456789abcdef0fedcba987654321234567800000000n,
"0x123456789abcdef0fedcba9876543212345678n << 32n === 0x123456789abcdef0fedcba987654321234567800000000n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << 16n, 0x123456789abcdef0fedcba98765432123456780000n,
"0x123456789abcdef0fedcba9876543212345678n << 16n === 0x123456789abcdef0fedcba98765432123456780000n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << 0n, 0x123456789abcdef0fedcba9876543212345678n,
"0x123456789abcdef0fedcba9876543212345678n << 0n === 0x123456789abcdef0fedcba9876543212345678n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -16n, 0x123456789abcdef0fedcba987654321234n,
"0x123456789abcdef0fedcba9876543212345678n << -16n === 0x123456789abcdef0fedcba987654321234n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -32n, 0x123456789abcdef0fedcba98765432n,
"0x123456789abcdef0fedcba9876543212345678n << -32n === 0x123456789abcdef0fedcba98765432n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -64n, 0x123456789abcdef0fedcban,
"0x123456789abcdef0fedcba9876543212345678n << -64n === 0x123456789abcdef0fedcban");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -127n, 0x2468acn,
"0x123456789abcdef0fedcba9876543212345678n << -127n === 0x2468acn");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -128n, 0x123456n,
"0x123456789abcdef0fedcba9876543212345678n << -128n === 0x123456n");
assert.sameValue(
0x123456789abcdef0fedcba9876543212345678n << -129n, 0x91a2bn,
"0x123456789abcdef0fedcba9876543212345678n << -129n === 0x91a2bn");
assert.sameValue(-5n << 1n, -0xan, "-5n << 1n === -0xan");
assert.sameValue(-5n << 2n, -0x14n, "-5n << 2n === -0x14n");
assert.sameValue(-5n << 3n, -0x28n, "-5n << 3n === -0x28n");
assert.sameValue(-5n << -1n, -3n, "-5n << -1n === -3n");
assert.sameValue(-5n << -2n, -2n, "-5n << -2n === -2n");
assert.sameValue(-5n << -3n, -1n, "-5n << -3n === -1n");
assert.sameValue(-1n << 128n, -0x100000000000000000000000000000000n, "-1n << 128n === -0x100000000000000000000000000000000n");
assert.sameValue(-1n << 0n, -1n, "-1n << 0n === -1n");
assert.sameValue(-1n << -128n, -1n, "-1n << -128n === -1n");
assert.sameValue(-0x246n << 0n, -0x246n, "-0x246n << 0n === -0x246n");
assert.sameValue(-0x246n << 127n, -0x12300000000000000000000000000000000n, "-0x246n << 127n === -0x12300000000000000000000000000000000n");
assert.sameValue(-0x246n << 128n, -0x24600000000000000000000000000000000n, "-0x246n << 128n === -0x24600000000000000000000000000000000n");
assert.sameValue(-0x246n << 129n, -0x48c00000000000000000000000000000000n, "-0x246n << 129n === -0x48c00000000000000000000000000000000n");
assert.sameValue(-0x246n << -128n, -1n, "-0x246n << -128n === -1n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << 64n, -0x123456789abcdef0fedcba98765432123456780000000000000000n,
"-0x123456789abcdef0fedcba9876543212345678n << 64n === -0x123456789abcdef0fedcba98765432123456780000000000000000n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << 32n, -0x123456789abcdef0fedcba987654321234567800000000n,
"-0x123456789abcdef0fedcba9876543212345678n << 32n === -0x123456789abcdef0fedcba987654321234567800000000n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << 16n, -0x123456789abcdef0fedcba98765432123456780000n,
"-0x123456789abcdef0fedcba9876543212345678n << 16n === -0x123456789abcdef0fedcba98765432123456780000n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << 0n, -0x123456789abcdef0fedcba9876543212345678n,
"-0x123456789abcdef0fedcba9876543212345678n << 0n === -0x123456789abcdef0fedcba9876543212345678n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -16n, -0x123456789abcdef0fedcba987654321235n,
"-0x123456789abcdef0fedcba9876543212345678n << -16n === -0x123456789abcdef0fedcba987654321235n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -32n, -0x123456789abcdef0fedcba98765433n,
"-0x123456789abcdef0fedcba9876543212345678n << -32n === -0x123456789abcdef0fedcba98765433n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -64n, -0x123456789abcdef0fedcbbn,
"-0x123456789abcdef0fedcba9876543212345678n << -64n === -0x123456789abcdef0fedcbbn");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -127n, -0x2468adn,
"-0x123456789abcdef0fedcba9876543212345678n << -127n === -0x2468adn");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -128n, -0x123457n,
"-0x123456789abcdef0fedcba9876543212345678n << -128n === -0x123457n");
assert.sameValue(
-0x123456789abcdef0fedcba9876543212345678n << -129n, -0x91a2cn,
"-0x123456789abcdef0fedcba9876543212345678n << -129n === -0x91a2cn");
@@ -0,0 +1,22 @@
//@ runBigIntEnabled

function assert(a, message) {
if (!a)
throw new Error(message);
}

function assertThrowRangeError(a, b, message) {
try {
let n = a << b;
assert(false, message + ": Should throw RangeError, but executed without exception");
} catch (e) {
assert(e instanceof RangeError, message + ": expected RangeError, got: " + e);
}
}

let a = 1n << 64n;
assertThrowRangeError(1n, a, "Left shift by " + a);

a = 1n << 30n;
assertThrowRangeError(1n, a, "Left shift by " + a);

@@ -0,0 +1,104 @@
//@ runBigIntEnabled

function assert(a, message) {
if (!a)
throw new Error(message);
}

function assertThrowTypeError(a, b, message) {
try {
let n = a << b;
assert(false, message + ": Should throw TypeError, but executed without exception");
} catch (e) {
assert(e instanceof TypeError, message + ": expected TypeError, got: " + e);
}
}

assertThrowTypeError(30n, "foo", "BigInt << String");
assertThrowTypeError("bar", 18757382984821n, "String << BigInt");
assertThrowTypeError(30n, Symbol("foo"), "BigInt << Symbol");
assertThrowTypeError(Symbol("bar"), 18757382984821n, "Symbol << BigInt");
assertThrowTypeError(30n, 3320, "BigInt << Int32");
assertThrowTypeError(33256, 18757382984821n, "Int32 << BigInt");
assertThrowTypeError(30n, 0.543, "BigInt << Double");
assertThrowTypeError(230.19293, 18757382984821n, "Double << BigInt");
assertThrowTypeError(30n, NaN, "BigInt << NaN");
assertThrowTypeError(NaN, 18757382984821n, "NaN << BigInt");
assertThrowTypeError(30n, +Infinity, "BigInt << +Infinity");
assertThrowTypeError(+Infinity, 18757382984821n, "+Infinity << BigInt");
assertThrowTypeError(30n, -Infinity, "BigInt << -Infinity");
assertThrowTypeError(-Infinity, 18757382984821n, "-Infinity << BigInt");
assertThrowTypeError(30n, null, "BigInt << null");
assertThrowTypeError(null, 18757382984821n, "null << BigInt");
assertThrowTypeError(30n, undefined, "BigInt << undefined");
assertThrowTypeError(undefined, 18757382984821n, "undefined << BigInt");
assertThrowTypeError(30n, true, "BigInt << true");
assertThrowTypeError(true, 18757382984821n, "true << BigInt");
assertThrowTypeError(30n, false, "BigInt << false");
assertThrowTypeError(false, 18757382984821n, "false << BigInt");

// Error when returning from object

let o = {
valueOf: function () { return Symbol("Foo"); }
};

assertThrowTypeError(30n, o, "BigInt << Object.valueOf returning Symbol");
assertThrowTypeError(o, 18757382984821n, "Object.valueOf returning Symbol << BigInt");

o = {
valueOf: function () { return 33256; }
};

assertThrowTypeError(30n, o, "BigInt << Object.valueOf returning Int32");
assertThrowTypeError(o, 18757382984821n, "Object.valueOf returning Int32 << BigInt");

o = {
valueOf: function () { return 0.453; }
};

assertThrowTypeError(30n, o, "BigInt << Object.valueOf returning Double");
assertThrowTypeError(o, 18757382984821n, "Object.valueOf returning Double << BigInt");

o = {
toString: function () { return Symbol("Foo"); }
};

assertThrowTypeError(30n, o, "BigInt << Object.toString returning Symbol");
assertThrowTypeError(o, 18757382984821n, "Object.toString returning Symbol << BigInt");

o = {
toString: function () { return 33256; }
};

assertThrowTypeError(30n, o, "BigInt << Object.toString returning Int32");
assertThrowTypeError(o, 18757382984821n, "Object.toString returning Int32 << BigInt");

o = {
toString: function () { return 0.453; }
};

assertThrowTypeError(30n, o, "BigInt << Object.toString returning Double");
assertThrowTypeError(o, 18757382984821n, "Object.toString returning Double << BigInt");

o = {
[Symbol.toPrimitive]: function () { return Symbol("Foo"); }
};

assertThrowTypeError(30n, o, "BigInt << Object.@@toPrimitive returning Symbol");
assertThrowTypeError(o, 18757382984821n, "Object.@@toPrimitive returning Symbol << BigInt");

o = {
[Symbol.toPrimitive]: function () { return 33256; }
};

assertThrowTypeError(30n, o, "BigInt << Object.@@toPrimitive returning Int32");
assertThrowTypeError(o, 18757382984821n, "Object.@@toPrimitive returning Int32 << BigInt");

o = {
[Symbol.toPrimitive]: function () { return 0.453; }
};

assertThrowTypeError(30n, o, "BigInt << Object.@@toPrimitive returning Double");
assertThrowTypeError(o, 18757382984821n, "Object.@@toPrimitive returning Double << BigInt");

@@ -0,0 +1,36 @@
//@ runBigIntEnabled

assert = {
sameValue: function (input, expected, message) {
if (input !== expected)
throw new Error(message);
}
};

function testLeftShift(x, y, z, message) {
assert.sameValue(x << y, z, message);
}

testLeftShift(Object(0b10n), 1n, 0b100n, "ToPrimitive: unbox object with internal slot");

let o = {
[Symbol.toPrimitive]: function() {
return 0b10n;
}
};
testLeftShift(o, 0b01n, 0b100n, "ToPrimitive: @@toPrimitive");

o = {
valueOf: function() {
return 0b10n;
}
};
testLeftShift(o, 0b01n, 0b100n, "ToPrimitive: valueOf");

o = {
toString: function() {
return 0b10n;
}
}
testLeftShift(o, 0b01n, 0b100n, "ToPrimitive: toString");

@@ -5,19 +5,8 @@ function assert(a, message) {
throw new Error(message);
}

function lshift(y) {
let out = 1n;
for (let i = 0; i < y; i++) {
out *= 340282366920938463463374607431768211456n;
}

return out;
}

let a = lshift(8064);
for (let i = 0; i < 256; i++) {
a *= 18446744073709551615n;
}
let a = (1n << 1048575n) - 1n;
a = (a << 1n) | 1n;

try {
let b = a + 1n;

0 comments on commit 3f23465

Please sign in to comment.