Index: src/compiler/ast-graph-builder.cc |
diff --git a/src/compiler/ast-graph-builder.cc b/src/compiler/ast-graph-builder.cc |
index 4cf6dde9c1b474aa6539eb9a5d00ba9b4094f6ca..b94ac542eb4e681e532ee3771a3103c39ad99d60 100644 |
--- a/src/compiler/ast-graph-builder.cc |
+++ b/src/compiler/ast-graph-builder.cc |
@@ -1591,6 +1591,21 @@ void AstGraphBuilder::VisitClassLiteral(ClassLiteral* expr) { |
OutputFrameStateCombine::Push()); |
environment()->Push(literal); |
+ // The below proxy exists to allow static field initializers to have the |
+ // correct home object and receiver. It's only necessary if the initializers |
+ // are called as a part of class definition instead of immediately after it, |
+ // in which case they could simply refer to the class just constructed. The |
+ // latter is actually what's currently specified, and this will need to be |
+ // changed if that behavior is settled upon. See also |
+ // https://github.com/tc39/proposal-class-public-fields/issues/50 |
+ VariableProxy* static_initializer_proxy = expr->static_initializer_proxy(); |
+ if (static_initializer_proxy != nullptr) { |
+ Variable* variable = static_initializer_proxy->var(); |
+ BuildVariableAssignment(variable, literal, Token::INIT, |
+ CreateVectorSlotPair(FeedbackVectorSlot::Invalid()), |
+ BailoutId::None()); |
+ } |
+ |
// Load the "prototype" from the constructor. |
PrepareEagerCheckpoint(expr->CreateLiteralId()); |
Handle<Name> name = isolate()->factory()->prototype_string(); |
@@ -1603,6 +1618,32 @@ void AstGraphBuilder::VisitClassLiteral(ClassLiteral* expr) { |
// Create nodes to store method values into the literal. |
for (int i = 0; i < expr->properties()->length(); i++) { |
ClassLiteral::Property* property = expr->properties()->at(i); |
+ |
+ if (property->kind() == ClassLiteral::Property::FIELD && |
+ !property->is_static()) { |
+ // Non-static properties produced by the parser have as their 'key' an |
+ // expression producing their name and as their 'value' a variable which |
+ // is refered to by the synthetic initializer function in order to |
+ // determine the name during class instantiation. This is necessary |
+ // because computed names must only be evaluated once, at class definition |
+ // time. |
+ // That is, code which looks like `class C { [f()] = 1; }` is desugared |
+ // into something like |
+ // class C { constructor(){ this.[.class-field-0-name] = 1; } }; |
+ // let .class-field-0-name = f(); |
+ // except that the assignment to .class-field-name-0 occurs interleaved |
+ // with the rest of the class body; it is performed by the block in which |
+ // this comment appears. |
+ Variable* variable = property->value()->AsVariableProxy()->var(); |
+ VisitForValue(property->key()); |
+ Node* key = environment()->Pop(); |
+ BuildVariableAssignment( |
+ variable, key, Token::INIT, |
+ CreateVectorSlotPair(FeedbackVectorSlot::Invalid()), |
+ BailoutId::None()); |
+ continue; |
+ } |
+ |
environment()->Push(environment()->Peek(property->is_static() ? 1 : 0)); |
VisitForValue(property->key()); |
@@ -1652,7 +1693,14 @@ void AstGraphBuilder::VisitClassLiteral(ClassLiteral* expr) { |
break; |
} |
case ClassLiteral::Property::FIELD: { |
- UNREACHABLE(); |
+ DCHECK(property->is_static()); |
+ Node* attr = jsgraph()->Constant(DONT_ENUM); |
+ Node* set_function_name = |
+ jsgraph()->Constant(property->NeedsSetFunctionName()); |
+ const Operator* op = |
+ javascript()->CallRuntime(Runtime::kDefineDataPropertyInLiteral); |
+ Node* call = NewNode(op, receiver, key, value, attr, set_function_name); |
+ PrepareFrameState(call, BailoutId::None()); |
break; |
} |
} |