Commit 62675789 authored by lrn@chromium.org's avatar lrn@chromium.org

Removed support for object literal get/set with number/string property name.

It doesn't work correctly for array indices.

Review URL: http://codereview.chromium.org/3109002

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5232 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent bdfdf8be
...@@ -3587,10 +3587,8 @@ ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter, ...@@ -3587,10 +3587,8 @@ ObjectLiteral::Property* Parser::ParseObjectLiteralGetSet(bool is_getter,
// { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... } // { ... , get foo() { ... }, ... , set foo(v) { ... v ... } , ... }
// We have already read the "get" or "set" keyword. // We have already read the "get" or "set" keyword.
Token::Value next = Next(); Token::Value next = Next();
if (next == Token::IDENTIFIER || // TODO(820): Allow NUMBER and STRING as well (and handle array indices).
next == Token::STRING || if (next == Token::IDENTIFIER || Token::IsKeyword(next)) {
next == Token::NUMBER ||
Token::IsKeyword(next)) {
Handle<String> name = Handle<String> name =
factory()->LookupSymbol(scanner_.literal_string(), factory()->LookupSymbol(scanner_.literal_string(),
scanner_.literal_length()); scanner_.literal_length());
...@@ -3652,8 +3650,7 @@ Expression* Parser::ParseObjectLiteral(bool* ok) { ...@@ -3652,8 +3650,7 @@ Expression* Parser::ParseObjectLiteral(bool* ok) {
factory()->LookupSymbol(scanner_.literal_string(), factory()->LookupSymbol(scanner_.literal_string(),
scanner_.literal_length()); scanner_.literal_length());
uint32_t index; uint32_t index;
if (!string.is_null() && if (!string.is_null() && string->AsArrayIndex(&index)) {
string->AsArrayIndex(&index)) {
key = NewNumberLiteral(index); key = NewNumberLiteral(index);
break; break;
} }
......
...@@ -305,13 +305,14 @@ static Handle<Object> CreateObjectLiteralBoilerplate( ...@@ -305,13 +305,14 @@ static Handle<Object> CreateObjectLiteralBoilerplate(
} }
Handle<Object> result; Handle<Object> result;
uint32_t element_index = 0; uint32_t element_index = 0;
if (key->ToArrayIndex(&element_index)) { if (key->IsSymbol()) {
// Array index (uint32). // If key is a symbol it is not an array element.
result = SetElement(boilerplate, element_index, value);
} else if (key->IsSymbol()) {
// The key is not an array index.
Handle<String> name(String::cast(*key)); Handle<String> name(String::cast(*key));
ASSERT(!name->AsArrayIndex(&element_index));
result = SetProperty(boilerplate, name, value, NONE); result = SetProperty(boilerplate, name, value, NONE);
} else if (key->ToArrayIndex(&element_index)) {
// Array index (uint32).
result = SetElement(boilerplate, element_index, value);
} else { } else {
// Non-uint32 number. // Non-uint32 number.
ASSERT(key->IsNumber()); ASSERT(key->IsNumber());
......
...@@ -146,7 +146,7 @@ function testKeywordProperty(keyword) { ...@@ -146,7 +146,7 @@ function testKeywordProperty(keyword) {
eval("var " + keyword + " = 42;"); eval("var " + keyword + " = 42;");
assertUnreachable("Not a keyword: " + keyword); assertUnreachable("Not a keyword: " + keyword);
} catch (e) { } } catch (e) { }
// Simple property, read and write. // Simple property, read and write.
var x = eval("({" + keyword + ": 42})"); var x = eval("({" + keyword + ": 42})");
assertEquals(42, x[keyword]); assertEquals(42, x[keyword]);
...@@ -154,7 +154,7 @@ function testKeywordProperty(keyword) { ...@@ -154,7 +154,7 @@ function testKeywordProperty(keyword) {
eval("x." + keyword + " = 37"); eval("x." + keyword + " = 37");
assertEquals(37, x[keyword]); assertEquals(37, x[keyword]);
assertEquals(37, eval("x." + keyword)); assertEquals(37, eval("x." + keyword));
// Getter/setter property, read and write. // Getter/setter property, read and write.
var y = eval("({value : 42, get " + keyword + "(){return this.value}," + var y = eval("({value : 42, get " + keyword + "(){return this.value}," +
" set " + keyword + "(v) { this.value = v; }})"); " set " + keyword + "(v) { this.value = v; }})");
...@@ -163,12 +163,12 @@ function testKeywordProperty(keyword) { ...@@ -163,12 +163,12 @@ function testKeywordProperty(keyword) {
eval("y." + keyword + " = 37"); eval("y." + keyword + " = 37");
assertEquals(37, y[keyword]); assertEquals(37, y[keyword]);
assertEquals(37, eval("y." + keyword)); assertEquals(37, eval("y." + keyword));
// Quoted keyword works is read back by unquoted as well. // Quoted keyword works is read back by unquoted as well.
var z = eval("({\"" + keyword + "\": 42})"); var z = eval("({\"" + keyword + "\": 42})");
assertEquals(42, z[keyword]); assertEquals(42, z[keyword]);
assertEquals(42, eval("z." + keyword)); assertEquals(42, eval("z." + keyword));
// Function property, called. // Function property, called.
var was_called; var was_called;
function test_call() { this.was_called = true; was_called = true; } function test_call() { this.was_called = true; was_called = true; }
...@@ -187,26 +187,4 @@ function testKeywordProperty(keyword) { ...@@ -187,26 +187,4 @@ function testKeywordProperty(keyword) {
for (var i = 0; i < keywords.length; i++) { for (var i = 0; i < keywords.length; i++) {
testKeywordProperty(keywords[i]); testKeywordProperty(keywords[i]);
} }
\ No newline at end of file
// Test getter and setter properties with string/number literal names.
var obj = {get 42() { return 42; },
get 3.14() { return "PI"; },
get "PI"() { return 3.14; },
readback: 0,
set 37(v) { this.readback = v; },
set 1.44(v) { this.readback = v; },
set "Poo"(v) { this.readback = v; }}
assertEquals(42, obj[42]);
assertEquals("PI", obj[3.14]);
assertEquals(3.14, obj["PI"]);
obj[37] = "t1";
assertEquals("t1", obj.readback);
obj[1.44] = "t2";
assertEquals("t2", obj.readback);
obj["Poo"] = "t3";
assertEquals("t3", obj.readback);
...@@ -183,8 +183,8 @@ S8.5_A2.1: PASS, FAIL if $system == linux, FAIL if $system == macos ...@@ -183,8 +183,8 @@ S8.5_A2.1: PASS, FAIL if $system == linux, FAIL if $system == macos
# These tests check for ES3 semantics, and differ from ES5. # These tests check for ES3 semantics, and differ from ES5.
# When we follow ES5 semantics, it's ok to fail the test. # When we follow ES5 semantics, it's ok to fail the test.
# Allow keywords as names of properties in object initialisers and # Allow keywords as names of properties in object initialisers and
# in dot-notation property access. # in dot-notation property access.
S11.1.5_A4.1: FAIL_OK S11.1.5_A4.1: FAIL_OK
S11.1.5_A4.2: FAIL_OK S11.1.5_A4.2: FAIL_OK
......
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