[errors] Improve error message for Promise constructor

Originally, 'Promise()' without 'new' will throw "undefined is not a
promise". Now it will throw "Promise constructor cannot be invoked
without 'new'".

Bug: v8:10817
Change-Id: Ic8b72a902ed395e44dbb32ccf96a2130a4a9422f
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3459924
Reviewed-by: Nikolaos Papaspyrou <nikolaos@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Commit-Queue: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79547}
This commit is contained in:
jameslahm 2022-03-21 17:15:27 +08:00 committed by V8 LUCI CQ
parent cce657cc55
commit bba8bc2bb1
4 changed files with 11 additions and 3 deletions

View File

@ -402,7 +402,7 @@ extern enum MessageTemplate {
kRegExpNonRegExp,
kRegExpNonObject,
kPromiseNonCallable,
kNotAPromise,
kPromiseNewTargetUndefined,
kResolverNotAFunction,
kTooManyElementsInPromiseCombinator,
kToRadixFormatRange,

View File

@ -50,7 +50,7 @@ PromiseConstructor(
newTarget: JSAny)(executor: JSAny): JSAny {
// 1. If NewTarget is undefined, throw a TypeError exception.
if (newTarget == Undefined) {
ThrowTypeError(MessageTemplate::kNotAPromise, newTarget);
ThrowTypeError(MessageTemplate::kPromiseNewTargetUndefined);
}
// 2. If IsCallable(executor) is false, throw a TypeError exception.

View File

@ -147,7 +147,8 @@ namespace internal {
T(NonStringImportAssertionValue, "Import assertion value must be a string") \
T(NoSetterInCallback, "Cannot set property % of % which has only a getter") \
T(NotAnIterator, "% is not an iterator") \
T(NotAPromise, "% is not a promise") \
T(PromiseNewTargetUndefined, \
"Promise constructor cannot be invoked without 'new'") \
T(NotConstructor, "% is not a constructor") \
T(NotDateObject, "this is not a Date object.") \
T(NotGeneric, "% requires that 'this' be a %") \

View File

@ -0,0 +1,7 @@
// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
assertThrows(() => {
Promise()
}, TypeError, "Promise constructor cannot be invoked without 'new'");