[runtime] throw a realm type error when constructors are called

Each time a constructor is being called without new operator, a TypeError
is thrown. The TypeError should be the realm's one according to 10.2.1.5.b.

Refs: https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
Refs: https://github.com/tc39/ecma262/pull/2216
Bug: v8:11530
Change-Id: Iff10a78e96fb547fe2062c86b9f93a30d2a8be20
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3056830
Reviewed-by: Marja Hölttä <marja@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76002}
This commit is contained in:
legendecas 2021-07-29 21:56:25 +08:00 committed by V8 LUCI CQ
parent 140cd812a2
commit 8db991a042
3 changed files with 15 additions and 10 deletions

View File

@ -41,13 +41,20 @@ RUNTIME_FUNCTION(Runtime_ThrowConstructorNonCallableError) {
DCHECK_EQ(1, args.length());
CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, 0);
Handle<String> name(constructor->shared().Name(), isolate);
Handle<Context> context = handle(constructor->native_context(), isolate);
DCHECK(context->IsNativeContext());
Handle<JSFunction> realm_type_error_function(
JSFunction::cast(context->get(Context::TYPE_ERROR_FUNCTION_INDEX)),
isolate);
if (name->length() == 0) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate,
NewTypeError(MessageTemplate::kAnonymousConstructorNonCallable));
isolate, NewError(realm_type_error_function,
MessageTemplate::kAnonymousConstructorNonCallable));
}
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kConstructorNonCallable, name));
isolate, NewError(realm_type_error_function,
MessageTemplate::kConstructorNonCallable, name));
}

View File

@ -25,11 +25,12 @@
throw Error('Should not happen!');
}
// ES6 9.2.1[[Call]] throws a TypeError in the caller context/Realm when the
// called function is a classConstructor
// https://tc39.es/ecma262/#sec-ecmascript-function-objects-call-thisargument-argumentslist
// 10.2.1 [[Call]] throws a TypeError created in callee context with F's
// associated Realm Record when the called function is a classConstructor
assertThrows(function() { Realm.eval(realmIndex, "A()") }, otherTypeError);
assertThrows(function() { instance.constructor() }, TypeError);
assertThrows(function() { A() }, TypeError);
assertThrows(function() { instance.constructor() }, otherTypeError);
assertThrows(function() { A() }, otherTypeError);
// ES6 9.3.1 call() first activates the callee context before invoking the
// method. The TypeError from the constructor is thus thrown in the other

View File

@ -543,9 +543,6 @@
# http://crbug/v8/10905
'language/identifier-resolution/assign-to-global-undefined': [FAIL],
# http://crbug/v8/11530
'built-ins/Function/internals/Call/class-ctor-realm': [FAIL],
# http://crbug/v8/11531
'built-ins/RegExp/prototype/flags/get-order': [FAIL],