Skip to content

Commit

Permalink
[runtime] throw a realm type error when constructors are called
Browse files Browse the repository at this point in the history
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: tc39/ecma262#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}
  • Loading branch information
legendecas authored and V8 LUCI CQ committed Jul 29, 2021
1 parent 140cd81 commit 8db991a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/runtime/runtime-classes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


Expand Down
9 changes: 5 additions & 4 deletions test/mjsunit/es6/classes-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions test/test262/test262.status
Original file line number Diff line number Diff line change
Expand Up @@ -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],

Expand Down

0 comments on commit 8db991a

Please sign in to comment.