Commit a829781c authored by Joyee Cheung's avatar Joyee Cheung Committed by Commit Bot

[class] improve error message of private brand checks

Previously we use the error message for normal invalid private member
access, so for a failed brand check for class C, the error is

TypeError: Cannot read private member C from an object whose class did not declare it

This updates the message to

TypeError: Object must be an instance of class C

Bug: v8:8330
Change-Id: Ida98f46b8387631194a9b7a48bd1f419045ac6e7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2100688
Commit-Queue: Joyee Cheung <joyee@igalia.com>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#66923}
parent d82b77e4
...@@ -415,6 +415,7 @@ namespace internal { ...@@ -415,6 +415,7 @@ namespace internal {
"Invalid left-hand side expression in prefix operation") \ "Invalid left-hand side expression in prefix operation") \
T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \ T(InvalidRegExpFlags, "Invalid flags supplied to RegExp constructor '%'") \
T(InvalidOrUnexpectedToken, "Invalid or unexpected token") \ T(InvalidOrUnexpectedToken, "Invalid or unexpected token") \
T(InvalidPrivateBrand, "Object must be an instance of class %") \
T(InvalidPrivateFieldResolution, \ T(InvalidPrivateFieldResolution, \
"Private field '%' must be declared in an enclosing class") \ "Private field '%' must be declared in an enclosing class") \
T(InvalidPrivateMemberRead, \ T(InvalidPrivateMemberRead, \
......
...@@ -416,6 +416,14 @@ MaybeHandle<Object> LoadIC::Load(Handle<Object> object, Handle<Name> name, ...@@ -416,6 +416,14 @@ MaybeHandle<Object> LoadIC::Load(Handle<Object> object, Handle<Name> name,
if (name->IsPrivateName() && !it.IsFound()) { if (name->IsPrivateName() && !it.IsFound()) {
Handle<String> name_string( Handle<String> name_string(
String::cast(Symbol::cast(*name).description()), isolate()); String::cast(Symbol::cast(*name).description()), isolate());
if (name->IsPrivateBrand()) {
Handle<String> class_name =
(name_string->length() == 0)
? isolate()->factory()->anonymous_string()
: name_string;
return TypeError(MessageTemplate::kInvalidPrivateBrand, object,
class_name);
}
return TypeError(MessageTemplate::kInvalidPrivateMemberRead, object, return TypeError(MessageTemplate::kInvalidPrivateMemberRead, object,
name_string); name_string);
} }
......
...@@ -41,8 +41,19 @@ MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate, ...@@ -41,8 +41,19 @@ MaybeHandle<Object> Runtime::GetObjectProperty(Isolate* isolate,
if (!it.IsFound() && key->IsSymbol() && if (!it.IsFound() && key->IsSymbol() &&
Symbol::cast(*key).is_private_name()) { Symbol::cast(*key).is_private_name()) {
Handle<Object> name_string(Symbol::cast(*key).description(), isolate); Handle<Symbol> sym = Handle<Symbol>::cast(key);
DCHECK(name_string->IsString()); Handle<Object> name(sym->description(), isolate);
DCHECK(name->IsString());
Handle<String> name_string = Handle<String>::cast(name);
if (sym->IsPrivateBrand()) {
Handle<String> class_name = (name_string->length() == 0)
? isolate->factory()->anonymous_string()
: name_string;
THROW_NEW_ERROR(isolate,
NewTypeError(MessageTemplate::kInvalidPrivateBrand,
class_name, object),
Object);
}
THROW_NEW_ERROR(isolate, THROW_NEW_ERROR(isolate,
NewTypeError(MessageTemplate::kInvalidPrivateMemberRead, NewTypeError(MessageTemplate::kInvalidPrivateMemberRead,
name_string, object), name_string, object),
......
...@@ -84,7 +84,7 @@ bytecodes: [ ...@@ -84,7 +84,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 48 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 48 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 53 S> */ B(Wide), B(LdaSmi), I16(265), /* 53 S> */ B(Wide), B(LdaSmi), I16(266),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
...@@ -115,7 +115,7 @@ bytecodes: [ ...@@ -115,7 +115,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 41 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 41 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 46 S> */ B(Wide), B(LdaSmi), I16(264), /* 46 S> */ B(Wide), B(LdaSmi), I16(265),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
...@@ -146,7 +146,7 @@ bytecodes: [ ...@@ -146,7 +146,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 48 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 48 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 53 S> */ B(Wide), B(LdaSmi), I16(265), /* 53 S> */ B(Wide), B(LdaSmi), I16(266),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
...@@ -177,7 +177,7 @@ bytecodes: [ ...@@ -177,7 +177,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 41 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 41 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 46 S> */ B(Wide), B(LdaSmi), I16(264), /* 46 S> */ B(Wide), B(LdaSmi), I16(265),
B(Star), R(4), B(Star), R(4),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(5), B(Star), R(5),
......
...@@ -57,7 +57,7 @@ bytecodes: [ ...@@ -57,7 +57,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 44 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 44 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 49 S> */ B(Wide), B(LdaSmi), I16(263), /* 49 S> */ B(Wide), B(LdaSmi), I16(264),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
...@@ -89,7 +89,7 @@ bytecodes: [ ...@@ -89,7 +89,7 @@ bytecodes: [
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(Mov), R(context), R(2), B(Mov), R(context), R(2),
/* 44 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3), /* 44 E> */ B(CallRuntime), U16(Runtime::kAddPrivateBrand), R(0), U8(3),
/* 49 S> */ B(Wide), B(LdaSmi), I16(263), /* 49 S> */ B(Wide), B(LdaSmi), I16(264),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
......
...@@ -25,7 +25,7 @@ bytecodes: [ ...@@ -25,7 +25,7 @@ bytecodes: [
B(TestReferenceEqual), R(this), B(TestReferenceEqual), R(this),
B(Mov), R(this), R(1), B(Mov), R(this), R(1),
B(JumpIfTrue), U8(18), B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(261), B(Wide), B(LdaSmi), I16(262),
B(Star), R(2), B(Star), R(2),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(3), B(Star), R(3),
...@@ -56,7 +56,7 @@ frame size: 2 ...@@ -56,7 +56,7 @@ frame size: 2
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 56 S> */ B(Wide), B(LdaSmi), I16(263), /* 56 S> */ B(Wide), B(LdaSmi), I16(264),
B(Star), R(0), B(Star), R(0),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(1), B(Star), R(1),
...@@ -83,7 +83,7 @@ frame size: 2 ...@@ -83,7 +83,7 @@ frame size: 2
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 56 S> */ B(Wide), B(LdaSmi), I16(263), /* 56 S> */ B(Wide), B(LdaSmi), I16(264),
B(Star), R(0), B(Star), R(0),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(1), B(Star), R(1),
...@@ -122,7 +122,7 @@ bytecodes: [ ...@@ -122,7 +122,7 @@ bytecodes: [
/* 94 E> */ B(TestReferenceEqual), R(this), /* 94 E> */ B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(JumpIfTrue), U8(18), B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(261), B(Wide), B(LdaSmi), I16(262),
B(Star), R(2), B(Star), R(2),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(3), B(Star), R(3),
...@@ -144,7 +144,7 @@ bytecodes: [ ...@@ -144,7 +144,7 @@ bytecodes: [
/* 109 E> */ B(TestReferenceEqual), R(this), /* 109 E> */ B(TestReferenceEqual), R(this),
B(Mov), R(this), R(1), B(Mov), R(this), R(1),
B(JumpIfTrue), U8(18), B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(262), B(Wide), B(LdaSmi), I16(263),
B(Star), R(3), B(Star), R(3),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(4), B(Star), R(4),
...@@ -159,7 +159,7 @@ bytecodes: [ ...@@ -159,7 +159,7 @@ bytecodes: [
/* 133 E> */ B(TestReferenceEqual), R(this), /* 133 E> */ B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0), B(Mov), R(this), R(0),
B(JumpIfTrue), U8(18), B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(261), B(Wide), B(LdaSmi), I16(262),
B(Star), R(2), B(Star), R(2),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(3), B(Star), R(3),
...@@ -189,7 +189,7 @@ frame size: 2 ...@@ -189,7 +189,7 @@ frame size: 2
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 60 S> */ B(Wide), B(LdaSmi), I16(265), /* 60 S> */ B(Wide), B(LdaSmi), I16(266),
B(Star), R(0), B(Star), R(0),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(1), B(Star), R(1),
...@@ -215,7 +215,7 @@ frame size: 2 ...@@ -215,7 +215,7 @@ frame size: 2
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 53 S> */ B(Wide), B(LdaSmi), I16(264), /* 53 S> */ B(Wide), B(LdaSmi), I16(265),
B(Star), R(0), B(Star), R(0),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(1), B(Star), R(1),
...@@ -241,7 +241,7 @@ frame size: 2 ...@@ -241,7 +241,7 @@ frame size: 2
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 60 S> */ B(Wide), B(LdaSmi), I16(265), /* 60 S> */ B(Wide), B(LdaSmi), I16(266),
B(Star), R(0), B(Star), R(0),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(1), B(Star), R(1),
...@@ -267,7 +267,7 @@ frame size: 3 ...@@ -267,7 +267,7 @@ frame size: 3
parameter count: 1 parameter count: 1
bytecode array length: 16 bytecode array length: 16
bytecodes: [ bytecodes: [
/* 46 S> */ B(Wide), B(LdaSmi), I16(264), /* 46 S> */ B(Wide), B(LdaSmi), I16(265),
B(Star), R(1), B(Star), R(1),
B(LdaConstant), U8(0), B(LdaConstant), U8(0),
B(Star), R(2), B(Star), R(2),
......
# Copyright 2019 the V8 project authors. All rights reserved. # Copyright 2019 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
*%(basename)s:9: TypeError: Cannot read private member C from an object whose class did not declare it *%(basename)s:9: TypeError: Object must be an instance of class C
setA(obj, val) { obj.#a = val; } setA(obj, val) { obj.#a = val; }
^ ^
TypeError: Cannot read private member C from an object whose class did not declare it TypeError: Object must be an instance of class C
at C.setA (*%(basename)s:9:24) at C.setA (*%(basename)s:9:24)
at new C (*%(basename)s:15:10) at new C (*%(basename)s:15:10)
at *%(basename)s:19:1 at *%(basename)s:19:1
\ No newline at end of file
# Copyright 2019 the V8 project authors. All rights reserved. # Copyright 2019 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be # Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file. # found in the LICENSE file.
*%(basename)s:9: TypeError: Cannot read private member C from an object whose class did not declare it *%(basename)s:9: TypeError: Object must be an instance of class C
getA(obj) { return obj.#a; } getA(obj) { return obj.#a; }
^ ^
TypeError: Cannot read private member C from an object whose class did not declare it TypeError: Object must be an instance of class C
at C.getA (*%(basename)s:9:26) at C.getA (*%(basename)s:9:26)
at new C (*%(basename)s:15:10) at new C (*%(basename)s:15:10)
at *%(basename)s:19:1 at *%(basename)s:19:1
// Copyright 2020 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.
// Flags: --harmony-private-methods
const C = class {
#a() {}
test(obj) { obj.#a(); }
};
(new C).test({});
# Copyright 2020 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.
*%(basename)s:9: TypeError: Object must be an instance of class anonymous
test(obj) { obj.#a(); }
^
TypeError: Object must be an instance of class anonymous
at C.test (*%(basename)s:9:19)
at *%(basename)s:11:9
// Copyright 2020 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.
// Flags: --harmony-private-methods
class C {
#a() {}
test(obj) { obj.#a(); }
}
(new C).test({});
# Copyright 2020 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.
*%(basename)s:9: TypeError: Object must be an instance of class C
test(obj) { obj.#a(); }
^
TypeError: Object must be an instance of class C
at C.test (*%(basename)s:9:19)
at *%(basename)s:11:9
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment