Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES2020 BigInt #1027

Merged
merged 23 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Jint.Repl/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static void Main(string[] args)
try
{
var result = engine.Evaluate(input, parserOptions);
if (!result.IsNull() && !result.IsUndefined())
if (!result.IsPrimitive() && result is not IPrimitiveInstance)
{
var serializer = new JsonSerializer(engine);
var str = serializer.Serialize(result, Undefined.Instance, " ");
Expand Down
15 changes: 15 additions & 0 deletions Jint.Tests.Test262/BuiltIns/BigIntTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Xunit;

namespace Jint.Tests.Test262.BuiltIns
{
public class BigIntTests : Test262Test
{
[Theory(DisplayName = "built-ins\\BigInt")]
[MemberData(nameof(SourceFiles), "built-ins\\BigInt", false)]
[MemberData(nameof(SourceFiles), "built-ins\\BigInt", true, Skip = "Skipped")]
protected void RunTest(SourceFile sourceFile)
{
RunTestInternal(sourceFile);
}
}
}
7 changes: 2 additions & 5 deletions Jint.Tests.Test262/Test262Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static Test262Test()
"detachArrayBuffer.js",
"byteConversionValues.js",
"hidden-constructors.js",
"testBigIntTypedArray.js"
};

Sources = new Dictionary<string, Script>(files.Length);
Expand Down Expand Up @@ -174,7 +175,7 @@ protected void RunTestCode(string fileName, string code, bool strict)

if (!negative && !string.IsNullOrWhiteSpace(lastError))
{
throw new XunitException(lastError);
throw new XunitException($"{Environment.NewLine}{fileName}{Environment.NewLine}{Environment.NewLine}{lastError}");
}
}

Expand Down Expand Up @@ -242,10 +243,6 @@ public static IEnumerable<object[]> SourceFiles(string pathPrefix, bool skipped)
skip = true;
reason = "tail-calls not implemented";
break;
case "BigInt":
skip = true;
reason = "BigInt not implemented";
break;
case "generators":
skip = true;
reason = "generators not implemented";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ info: |
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asIntN, 'function');

assert.throws(TypeError, function () {
BigInt.asIntN();
}, "ToBigInt: no argument => undefined => TypeError");
assert.throws(TypeError, function () {
BigInt.asIntN(0);
}, "ToBigInt: no argument => undefined => TypeError");

assert.throws(TypeError, function() {
BigInt.asIntN(0, undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ info: |
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/

assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asIntN, 'function');
function err() {
throw new Test262Error();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ info: |
1. Let bits be ? ToIndex(bits).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asIntN, 'function');

assert.throws(RangeError, function() {
BigInt.asIntN(-1, 0n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ info: |
1. Let bits be ? ToIndex(bits).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asIntN, 'function');

function err() {
throw new Test262Error();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt.asIntN does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, BigInt, arrow-function]
---*/
assert.sameValue(isConstructor(BigInt.asIntN), false, 'isConstructor(BigInt.asIntN) must return false');

assert.throws(TypeError, () => {
new BigInt.asIntN(64, 1n);
}, '`new BigInt.asIntN(64, 1n)` throws TypeError');
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ info: |
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asUintN, 'function');

assert.throws(TypeError, function () {
BigInt.asUintN();
}, "ToBigInt: no argument => undefined => TypeError");
assert.throws(TypeError, function () {
BigInt.asUintN(0);
}, "ToBigInt: no argument => undefined => TypeError");

assert.throws(TypeError, function() {
BigInt.asUintN(0, undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ info: |
2. Let bigint ? ToBigInt(bigint).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asUintN, 'function');

function err() {
throw new Test262Error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ info: |
1. Let bits be ? ToIndex(bits).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asUintN, 'function');

assert.throws(RangeError, function() {
BigInt.asUintN(-1, 0n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ info: |
1. Let bits be ? ToIndex(bits).
features: [BigInt, computed-property-names, Symbol, Symbol.toPrimitive]
---*/
assert.sameValue(typeof BigInt, 'function');
assert.sameValue(typeof BigInt.asUintN, 'function');

function err() {
throw new Test262Error();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt.asUintN does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, BigInt, arrow-function]
---*/
assert.sameValue(isConstructor(BigInt.asUintN), false, 'isConstructor(BigInt.asUintN) must return false');

assert.throws(TypeError, () => {
new BigInt.asUintN(64, 1n);
}, '`new BigInt.asUintN(64, 1n)` throws TypeError');
33 changes: 33 additions & 0 deletions Jint.Tests.Test262/test/built-ins/BigInt/is-a-constructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt is a constructor, and does implement [[Construct]], but is not new target
info: |
sec-bigint-constructor

- is not intended to be used with the new operator or to be subclassed. It may be used as the value of an extends clause of a class definition but a super call to the BigInt constructor will cause an exception.

sec-bigint-constructor-number-value

1. If NewTarget is not undefined, throw a TypeError exception.
includes: [isConstructor.js]
features: [BigInt, Reflect.construct, arrow-function]
---*/

assert.sameValue(
isConstructor(BigInt),
true,
'isConstructor(BigInt) must return true'
);

assert.throws(TypeError, () => {
new BigInt({
valueOf() {
new Test262Error();
}
});
}, '`new BigInt({ valueOf() {new Test262Error();}})` throws TypeError');

23 changes: 0 additions & 23 deletions Jint.Tests.Test262/test/built-ins/BigInt/new-target-throws.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt.prototype.toLocaleString does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, BigInt, arrow-function]
---*/
assert.sameValue(
isConstructor(BigInt.prototype.toLocaleString),
false,
'isConstructor(BigInt.prototype.toLocaleString) must return false'
);

assert.throws(TypeError, () => {
let n = 1n;
new n.toLocaleString();
}, '`let n = 1n; new n.toLocaleString()` throws TypeError');
21 changes: 21 additions & 0 deletions Jint.Tests.Test262/test/built-ins/BigInt/prototype/toString/a-z.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-bigint.prototype.tostring
description: >
Letters a-z are used for digits with values 10 through 35
info: |
6. Return the String representation of this Number value using
the radix specified by radixNumber. Letters a-z are used for
digits with values 10 through 35. The precise algorithm is
implementation-dependent, however the algorithm should be a
generalization of that specified in 6.1.6.2.23.
features: [BigInt]
---*/

for (let radix = 11; radix <= 36; radix++) {
for (let i = 10n; i < radix; i++) {
assert.sameValue(i.toString(radix), String.fromCharCode(Number(i + 87n)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt.prototype.toString does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, BigInt, arrow-function]
---*/
assert.sameValue(
isConstructor(BigInt.prototype.toString),
false,
'isConstructor(BigInt.prototype.toString) must return false'
);

assert.throws(TypeError, () => {
let n = 1n;
new n.toString();
}, '`let n = 1n; new n.toString()` throws TypeError');
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ info: |
[[BigIntData]] internal slot.
features: [BigInt]
---*/
assert.sameValue(typeof BigInt, 'function');

assert.throws(TypeError, function() {
BigInt.prototype.toString(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ features: [BigInt, Symbol, Symbol.toPrimitive]

var toString = BigInt.prototype.toString;

assert.sameValue(typeof toString, 'function');

assert.throws(TypeError, function() {
toString.call({
x: 1n
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (C) 2020 Rick Waldron. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-ecmascript-standard-built-in-objects
description: >
BigInt.prototype.valueOf does not implement [[Construct]], is not new-able
info: |
ECMAScript Function Objects

Built-in function objects that are not identified as constructors do not
implement the [[Construct]] internal method unless otherwise specified in
the description of a particular function.

sec-evaluatenew

...
7. If IsConstructor(constructor) is false, throw a TypeError exception.
...
includes: [isConstructor.js]
features: [Reflect.construct, BigInt, arrow-function]
---*/
assert.sameValue(
isConstructor(BigInt.prototype.valueOf),
false,
'isConstructor(BigInt.prototype.valueOf) must return false'
);

assert.throws(TypeError, () => {
let n = 1n;
new n.valueOf();
}, '`let n = 1n; new n.valueOf()` throws TypeError');
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ features: [BigInt]
---*/

var valueOf = BigInt.prototype.valueOf;
assert.sameValue(typeof valueOf, 'function');

assert.throws(TypeError, function() {
valueOf.call({});
Expand Down