Skip to content
Permalink
Browse files
[JSC] BigInt constructor should be constructible while it always thro…
…ws an error

https://bugs.webkit.org/show_bug.cgi?id=217575

Reviewed by Darin Adler.

JSTests:

* stress/is-constructor.js:
* stress/non-constructable-constructors.js: Added.
(shouldThrow):

Source/JavaScriptCore:

In terms of the spec, BigInt constructor should be a constructor. So we should put constructBigIntConstructor function instead of nullptr.
But it should always throw a TypeError. Error message looks a bit awkward ("TypeError: function is not a constructor..."), but this looks
most intuitive to users. Note that V8 and SpiderMonkey throw similar messages ("is not a constructor").

* runtime/BigIntConstructor.cpp:
(JSC::BigIntConstructor::BigIntConstructor):
(JSC::JSC_DEFINE_HOST_FUNCTION):


Canonical link: https://commits.webkit.org/230345@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@268322 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Constellation committed Oct 11, 2020
1 parent d2b1564 commit 1f1ba0765ea3d3c1643dc9c348efb13a5f431e60
Showing 5 changed files with 55 additions and 1 deletion.
@@ -1,3 +1,14 @@
2020-10-11 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] BigInt constructor should be constructible while it always throws an error
https://bugs.webkit.org/show_bug.cgi?id=217575

Reviewed by Darin Adler.

* stress/is-constructor.js:
* stress/non-constructable-constructors.js: Added.
(shouldThrow):

2020-10-11 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] LowerCase when LanguageTag checks duplicate variants
@@ -26,6 +26,7 @@ assert(isConstructor(function(){}.bind(null), {}));
// Builtin constructors.
assert(isConstructor(Array));
assert(isConstructor(ArrayBuffer));
assert(isConstructor(BigInt));
assert(isConstructor(Boolean));
assert(isConstructor(Date));
assert(isConstructor(Error));
@@ -0,0 +1,19 @@
function shouldThrow(func, errorMessage) {
var errorThrown = false;
var error = null;
try {
func();
} catch (e) {
errorThrown = true;
error = e;
}
if (!errorThrown)
throw new Error('not thrown');
if (String(error) !== errorMessage)
throw new Error(`bad error: ${String(error)}`);
}

shouldThrow(() => new Symbol(), `TypeError: function is not a constructor (evaluating 'new Symbol()')`);
shouldThrow(() => new Symbol(Symbol("Hey")), `TypeError: function is not a constructor (evaluating 'new Symbol(Symbol("Hey"))')`);
shouldThrow(() => new BigInt(), `TypeError: function is not a constructor (evaluating 'new BigInt()')`);
shouldThrow(() => new BigInt(0), `TypeError: function is not a constructor (evaluating 'new BigInt(0)')`);
@@ -1,3 +1,18 @@
2020-10-11 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] BigInt constructor should be constructible while it always throws an error
https://bugs.webkit.org/show_bug.cgi?id=217575

Reviewed by Darin Adler.

In terms of the spec, BigInt constructor should be a constructor. So we should put constructBigIntConstructor function instead of nullptr.
But it should always throw a TypeError. Error message looks a bit awkward ("TypeError: function is not a constructor..."), but this looks
most intuitive to users. Note that V8 and SpiderMonkey throw similar messages ("is not a constructor").

* runtime/BigIntConstructor.cpp:
(JSC::BigIntConstructor::BigIntConstructor):
(JSC::JSC_DEFINE_HOST_FUNCTION):

2020-10-11 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] LowerCase when LanguageTag checks duplicate variants
@@ -55,9 +55,10 @@ const ClassInfo BigIntConstructor::s_info = { "Function", &Base::s_info, &bigInt
*/

static JSC_DECLARE_HOST_FUNCTION(callBigIntConstructor);
static JSC_DECLARE_HOST_FUNCTION(constructBigIntConstructor);

BigIntConstructor::BigIntConstructor(VM& vm, Structure* structure)
: InternalFunction(vm, structure, callBigIntConstructor, nullptr)
: InternalFunction(vm, structure, callBigIntConstructor, constructBigIntConstructor)
{
}

@@ -129,6 +130,13 @@ JSC_DEFINE_HOST_FUNCTION(callBigIntConstructor, (JSGlobalObject* globalObject, C
RELEASE_AND_RETURN(scope, JSValue::encode(toBigInt(globalObject, primitive)));
}

JSC_DEFINE_HOST_FUNCTION(constructBigIntConstructor, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
return throwVMError(globalObject, scope, createNotAConstructorError(globalObject, callFrame->jsCallee()));
}

JSC_DEFINE_HOST_FUNCTION(bigIntConstructorFuncAsUintN, (JSGlobalObject* globalObject, CallFrame* callFrame))
{
VM& vm = globalObject->vm();

0 comments on commit 1f1ba07

Please sign in to comment.