Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Unified Diff: src/x64/full-codegen-x64.cc

Issue 332443002: Add support for computed property names in object literals (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Added tests Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/x64/full-codegen-x64.cc
diff --git a/src/x64/full-codegen-x64.cc b/src/x64/full-codegen-x64.cc
index 8de422608bd7bb8334351614a253b8fdd7662f7a..e122dbf006d591a371ca413f66f44c0559b7a0c8 100644
--- a/src/x64/full-codegen-x64.cc
+++ b/src/x64/full-codegen-x64.cc
@@ -1666,11 +1666,13 @@ void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
expr->CalculateEmitStore(zone());
AccessorTable accessor_table(zone());
- for (int i = 0; i < expr->properties()->length(); i++) {
- ObjectLiteral::Property* property = expr->properties()->at(i);
+ int property_index = 0;
+ for (; property_index < expr->properties()->length(); property_index++) {
+ ObjectLiteral::Property* property = expr->properties()->at(property_index);
if (property->IsCompileTimeValue()) continue;
+ if (property->kind() == ObjectLiteral::Property::COMPUTED_NAME) break;
- Literal* key = property->key();
+ Literal* key = property->key()->AsLiteral();
Expression* value = property->value();
if (!result_saved) {
__ Push(rax); // Save result on the stack
@@ -1678,6 +1680,7 @@ void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
}
switch (property->kind()) {
case ObjectLiteral::Property::CONSTANT:
+ case ObjectLiteral::Property::COMPUTED_NAME:
UNREACHABLE();
case ObjectLiteral::Property::MATERIALIZED_LITERAL:
ASSERT(!CompileTimeValue::IsCompileTimeValue(value));
@@ -1736,6 +1739,79 @@ void FullCodeGenerator::VisitObjectLiteral(ObjectLiteral* expr) {
__ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
}
+ // Object literals have two parts. The "static" part on the left contains no
+ // computed property names, and so we can compute its map ahead of time; see
+ // runtime.cc::CreateObjectLiteralBoilerplate. The second "dynamic" part
+ // starts with the first computed property name, and continues with all
+ // properties to its right. All the code from above initializes the static
+ // component of the object literal, and arranges for the map of the result to
+ // reflect the static order in which the keys appear. For the dynamic
+ // properties, we compile them into a series of "SetOwnProperty" runtime
+ // calls. This will preserve insertion order.
+ for (; property_index < expr->properties()->length(); property_index++) {
+ ObjectLiteral::Property* property = expr->properties()->at(property_index);
+
+ Expression* key = property->key();
+ Expression* value = property->value();
+ if (!result_saved) {
+ __ Push(rax); // Save result on the stack
+ result_saved = true;
+ }
+
+ switch (property->kind()) {
+ case ObjectLiteral::Property::CONSTANT:
+ case ObjectLiteral::Property::COMPUTED_NAME:
+ case ObjectLiteral::Property::MATERIALIZED_LITERAL:
+ case ObjectLiteral::Property::COMPUTED:
+ __ Push(Operand(rsp, 0)); // Duplicate receiver.
+ VisitForStackValue(key);
+ VisitForStackValue(value);
+ if (property->emit_store()) {
+ __ CallRuntime(Runtime::kSetOwnProperty, 3);
+ } else {
+ __ Drop(3);
+ }
+ break;
+ case ObjectLiteral::Property::PROTOTYPE:
+ __ Push(Operand(rsp, 0)); // Duplicate receiver.
+ VisitForStackValue(value);
+ if (property->emit_store()) {
+ __ CallRuntime(Runtime::kSetPrototype, 2);
+ } else {
+ __ Drop(2);
+ }
+ break;
+ // TODO(wingo): Allow computed names for accessor properties. Currently
+ // disallowed by the parser.
+ case ObjectLiteral::Property::GETTER: {
+ ObjectLiteral::Accessors *accessors =
+ accessor_table.lookup(key->AsLiteral())->second;
+ accessors->getter = value;
+
+ __ Push(Operand(rsp, 0)); // Duplicate receiver.
+ VisitForStackValue(key);
+ EmitAccessor(accessors->getter);
+ EmitAccessor(accessors->setter);
+ __ Push(Smi::FromInt(NONE));
+ __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
+ break;
+ }
+ case ObjectLiteral::Property::SETTER: {
+ ObjectLiteral::Accessors *accessors =
+ accessor_table.lookup(key->AsLiteral())->second;
+ accessors->setter = value;
+
+ __ Push(Operand(rsp, 0)); // Duplicate receiver.
+ VisitForStackValue(key);
+ EmitAccessor(accessors->getter);
+ EmitAccessor(accessors->setter);
+ __ Push(Smi::FromInt(NONE));
+ __ CallRuntime(Runtime::kDefineOrRedefineAccessorProperty, 5);
+ break;
+ }
+ }
+ }
+
if (expr->has_function()) {
ASSERT(result_saved);
__ Push(Operand(rsp, 0));
« src/preparser.h ('K') | « src/typing.cc ('k') | src/x87/full-codegen-x87.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698