Commit 1105d7ba authored by Marja Hölttä's avatar Marja Hölttä Committed by Commit Bot

[classes] Implement private brand checks

Notes: https://docs.google.com/document/d/1fEumNPCcOn4X0N5jGlAT7GQ5CEKKnw0YxLPXMoaSK5Q/edit?usp=sharing

Bug: v8:11374
Change-Id: I96720c0d69fe28e7229c4c22ed3d291587b73f59
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2667511
Commit-Queue: Marja Hölttä <marja@chromium.org>
Reviewed-by: 's avatarLeszek Swirski <leszeks@chromium.org>
Reviewed-by: 's avatarIgor Sheludko <ishell@chromium.org>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#72659}
parent 921a4a72
......@@ -9267,7 +9267,7 @@ void CodeStubAssembler::TryPrototypeChainLookup(
TNode<Object> receiver, TNode<Object> object_arg, TNode<Object> key,
const LookupPropertyInHolder& lookup_property_in_holder,
const LookupElementInHolder& lookup_element_in_holder, Label* if_end,
Label* if_bailout, Label* if_proxy) {
Label* if_bailout, Label* if_proxy, bool handle_private_names) {
// Ensure receiver is JSReceiver, otherwise bailout.
GotoIf(TaggedIsSmi(receiver), if_bailout);
TNode<HeapObject> object = CAST(object_arg);
......@@ -9322,6 +9322,11 @@ void CodeStubAssembler::TryPrototypeChainLookup(
BIND(&next_proto);
if (handle_private_names) {
// Private name lookup doesn't walk the prototype chain.
GotoIf(IsPrivateSymbol(CAST(key)), if_end);
}
TNode<HeapObject> proto = LoadMapPrototype(holder_map);
GotoIf(IsNull(proto), if_end);
......@@ -12392,9 +12397,10 @@ TNode<Oddball> CodeStubAssembler::HasProperty(TNode<Context> context,
&return_true, &return_false, next_holder, if_bailout);
};
const bool kHandlePrivateNames = mode == HasPropertyLookupMode::kHasProperty;
TryPrototypeChainLookup(object, object, key, lookup_property_in_holder,
lookup_element_in_holder, &return_false,
&call_runtime, &if_proxy);
&call_runtime, &if_proxy, kHandlePrivateNames);
TVARIABLE(Oddball, result);
......
......@@ -3128,7 +3128,7 @@ class V8_EXPORT_PRIVATE CodeStubAssembler
TNode<Object> receiver, TNode<Object> object, TNode<Object> key,
const LookupPropertyInHolder& lookup_property_in_holder,
const LookupElementInHolder& lookup_element_in_holder, Label* if_end,
Label* if_bailout, Label* if_proxy);
Label* if_bailout, Label* if_proxy, bool handle_private_names = false);
// Instanceof helpers.
// Returns true if {object} has {prototype} somewhere in it's prototype
......
......@@ -249,7 +249,8 @@ DEFINE_IMPLICATION(harmony_weak_refs_with_cleanup_some, harmony_weak_refs)
V(harmony_regexp_sequence, "RegExp Unicode sequence properties") \
V(harmony_weak_refs_with_cleanup_some, \
"harmony weak references with FinalizationRegistry.prototype.cleanupSome") \
V(harmony_import_assertions, "harmony import assertions")
V(harmony_import_assertions, "harmony import assertions") \
V(harmony_private_brand_checks, "harmony private brand checks")
#ifdef V8_INTL_SUPPORT
#define HARMONY_INPROGRESS(V) \
......
......@@ -432,7 +432,7 @@ MaybeHandle<Object> LoadIC::Load(Handle<Object> object, Handle<Name> name,
LookupForRead(&it, IsAnyHas());
if (name->IsPrivate()) {
if (name->IsPrivateName() && !it.IsFound()) {
if (!IsAnyHas() && name->IsPrivateName() && !it.IsFound()) {
Handle<String> name_string(
String::cast(Symbol::cast(*name).description()), isolate());
if (name->IsPrivateBrand()) {
......
......@@ -4320,6 +4320,7 @@ EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_regexp_sequence)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_top_level_await)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_logical_assignment)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_import_assertions)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_private_brand_checks)
#ifdef V8_INTL_SUPPORT
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_intl_displaynames_date_types)
......
......@@ -4792,6 +4792,65 @@ void BytecodeGenerator::BuildPrivateSetterAccess(Register object,
feedback_index(feedback_spec()->AddCallICSlot()));
}
void BytecodeGenerator::BuildPrivateMethodIn(Variable* private_name,
Expression* object_expression) {
DCHECK(IsPrivateMethodOrAccessorVariableMode(private_name->mode()));
ClassScope* scope = private_name->scope()->AsClassScope();
if (private_name->is_static()) {
// For static private methods, "#privatemethod in ..." only returns true for
// the class constructor.
if (scope->class_variable() == nullptr) {
// Can only happen via the debugger. See comment in
// BuildPrivateBrandCheck.
RegisterAllocationScope register_scope(this);
RegisterList args = register_allocator()->NewRegisterList(2);
builder()
->LoadLiteral(Smi::FromEnum(
MessageTemplate::
kInvalidUnusedPrivateStaticMethodAccessedByDebugger))
.StoreAccumulatorInRegister(args[0])
.LoadLiteral(private_name->raw_name())
.StoreAccumulatorInRegister(args[1])
.CallRuntime(Runtime::kNewError, args)
.Throw();
} else {
VisitForAccumulatorValue(object_expression);
Register object = register_allocator()->NewRegister();
builder()->StoreAccumulatorInRegister(object);
BytecodeLabel is_object;
builder()->JumpIfJSReceiver(&is_object);
RegisterList args = register_allocator()->NewRegisterList(3);
builder()
->StoreAccumulatorInRegister(args[2])
.LoadLiteral(Smi::FromEnum(MessageTemplate::kInvalidInOperatorUse))
.StoreAccumulatorInRegister(args[0])
.LoadLiteral(private_name->raw_name())
.StoreAccumulatorInRegister(args[1])
.CallRuntime(Runtime::kNewTypeError, args)
.Throw();
builder()->Bind(&is_object);
BuildVariableLoadForAccumulatorValue(scope->class_variable(),
HoleCheckMode::kElided);
builder()->CompareReference(object);
}
} else {
BuildVariableLoadForAccumulatorValue(scope->brand(),
HoleCheckMode::kElided);
Register brand = register_allocator()->NewRegister();
builder()->StoreAccumulatorInRegister(brand);
VisitForAccumulatorValue(object_expression);
builder()->SetExpressionPosition(object_expression);
FeedbackSlot slot = feedback_spec()->AddKeyedHasICSlot();
builder()->CompareOperation(Token::IN, brand, feedback_index(slot));
execution_result()->SetResultIsBoolean();
}
}
void BytecodeGenerator::BuildPrivateBrandCheck(Property* property,
Register object,
MessageTemplate tmpl) {
......@@ -5660,6 +5719,16 @@ void BytecodeGenerator::VisitCompareOperation(CompareOperation* expr) {
builder()->SetExpressionPosition(expr);
BuildLiteralCompareNil(expr->op(), BytecodeArrayBuilder::kNullValue);
} else {
if (expr->op() == Token::IN && expr->left()->IsPrivateName()) {
DCHECK(FLAG_harmony_private_brand_checks);
Variable* var = expr->left()->AsVariableProxy()->var();
if (IsPrivateMethodOrAccessorVariableMode(var->mode())) {
BuildPrivateMethodIn(var, expr->right());
return;
}
// For private fields, the code below does the right thing.
}
Register lhs = VisitForRegisterValue(expr->left());
VisitForAccumulatorValue(expr->right());
builder()->SetExpressionPosition(expr);
......
......@@ -312,6 +312,8 @@ class BytecodeGenerator final : public AstVisitor<BytecodeGenerator> {
void BuildInvalidPropertyAccess(MessageTemplate tmpl, Property* property);
void BuildPrivateBrandCheck(Property* property, Register object,
MessageTemplate tmpl);
void BuildPrivateMethodIn(Variable* private_name,
Expression* object_expression);
void BuildPrivateGetterAccess(Register obj, Register access_pair);
void BuildPrivateSetterAccess(Register obj, Register access_pair,
Register value);
......
......@@ -3144,7 +3144,20 @@ template <typename Impl>
typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseBinaryExpression(
int prec) {
DCHECK_GE(prec, 4);
ExpressionT x = ParseUnaryExpression();
ExpressionT x;
// "#foo in ShiftExpression" needs to be parsed separately, since private
// identifiers are not valid PrimaryExpressions.
if (V8_UNLIKELY(FLAG_harmony_private_brand_checks &&
peek() == Token::PRIVATE_NAME)) {
x = ParsePropertyOrPrivatePropertyName();
if (peek() != Token::IN) {
ReportUnexpectedToken(peek());
return impl()->FailureExpression();
}
} else {
x = ParseUnaryExpression();
}
int prec1 = Token::Precedence(peek(), accept_IN_);
if (prec1 >= prec) {
return ParseBinaryContinuation(x, prec, prec1);
......
This diff is collapsed.
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