Commit 2d15eb9a authored by lrn@chromium.org's avatar lrn@chromium.org

Fix bug in object literals with large array indexes as strings.

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@6577 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent f1149734
...@@ -215,17 +215,28 @@ bool IsEqualString(void* first, void* second) { ...@@ -215,17 +215,28 @@ bool IsEqualString(void* first, void* second) {
return (*h1)->Equals(*h2); return (*h1)->Equals(*h2);
} }
bool IsEqualSmi(void* first, void* second) {
ASSERT((*reinterpret_cast<Smi**>(first))->IsSmi()); bool IsEqualNumber(void* first, void* second) {
ASSERT((*reinterpret_cast<Smi**>(second))->IsSmi()); ASSERT((*reinterpret_cast<Object**>(first))->IsNumber());
Handle<Smi> h1(reinterpret_cast<Smi**>(first)); ASSERT((*reinterpret_cast<Object**>(second))->IsNumber());
Handle<Smi> h2(reinterpret_cast<Smi**>(second));
return (*h1)->value() == (*h2)->value(); Handle<Object> h1(reinterpret_cast<Object**>(first));
Handle<Object> h2(reinterpret_cast<Object**>(second));
if (h1->IsSmi()) {
return h2->IsSmi() && *h1 == *h2;
}
if (h2->IsSmi()) return false;
Handle<HeapNumber> n1 = Handle<HeapNumber>::cast(h1);
Handle<HeapNumber> n2 = Handle<HeapNumber>::cast(h2);
ASSERT(isfinite(n1->value()));
ASSERT(isfinite(n2->value()));
return n1->value() == n2->value();
} }
void ObjectLiteral::CalculateEmitStore() { void ObjectLiteral::CalculateEmitStore() {
HashMap properties(&IsEqualString); HashMap properties(&IsEqualString);
HashMap elements(&IsEqualSmi); HashMap elements(&IsEqualNumber);
for (int i = this->properties()->length() - 1; i >= 0; i--) { for (int i = this->properties()->length() - 1; i >= 0; i--) {
ObjectLiteral::Property* property = this->properties()->at(i); ObjectLiteral::Property* property = this->properties()->at(i);
Literal* literal = property->key(); Literal* literal = property->key();
...@@ -238,23 +249,19 @@ void ObjectLiteral::CalculateEmitStore() { ...@@ -238,23 +249,19 @@ void ObjectLiteral::CalculateEmitStore() {
uint32_t hash; uint32_t hash;
HashMap* table; HashMap* table;
void* key; void* key;
uint32_t index;
Smi* smi_key_location;
if (handle->IsSymbol()) { if (handle->IsSymbol()) {
Handle<String> name(String::cast(*handle)); Handle<String> name(String::cast(*handle));
if (name->AsArrayIndex(&index)) { if (name->AsArrayIndex(&hash)) {
smi_key_location = Smi::FromInt(index); Handle<Object> key_handle = Factory::NewNumberFromUint(hash);
key = &smi_key_location; key = key_handle.location();
hash = index;
table = &elements; table = &elements;
} else { } else {
key = name.location(); key = name.location();
hash = name->Hash(); hash = name->Hash();
table = &properties; table = &properties;
} }
} else if (handle->ToArrayIndex(&index)) { } else if (handle->ToArrayIndex(&hash)) {
key = handle.location(); key = handle.location();
hash = index;
table = &elements; table = &elements;
} else { } else {
ASSERT(handle->IsNumber()); ASSERT(handle->IsNumber());
......
...@@ -49,7 +49,8 @@ class HashMap { ...@@ -49,7 +49,8 @@ class HashMap {
typedef bool (*MatchFun) (void* key1, void* key2); typedef bool (*MatchFun) (void* key1, void* key2);
// Dummy constructor. This constructor doesn't set up the hash // Dummy constructor. This constructor doesn't set up the hash
// map properly so don't use it unless you have good reason. // map properly so don't use it unless you have good reason (e.g.,
// you know that the HashMap will never be used).
HashMap(); HashMap();
// initial_capacity is the size of the initial hash map; // initial_capacity is the size of the initial hash map;
......
...@@ -3035,7 +3035,7 @@ Handle<Object> Parser::GetBoilerplateValue(Expression* expression) { ...@@ -3035,7 +3035,7 @@ Handle<Object> Parser::GetBoilerplateValue(Expression* expression) {
// Defined in ast.cc // Defined in ast.cc
bool IsEqualString(void* first, void* second); bool IsEqualString(void* first, void* second);
bool IsEqualSmi(void* first, void* second); bool IsEqualNumber(void* first, void* second);
// Validation per 11.1.5 Object Initialiser // Validation per 11.1.5 Object Initialiser
...@@ -3043,7 +3043,7 @@ class ObjectLiteralPropertyChecker { ...@@ -3043,7 +3043,7 @@ class ObjectLiteralPropertyChecker {
public: public:
ObjectLiteralPropertyChecker(Parser* parser, bool strict) : ObjectLiteralPropertyChecker(Parser* parser, bool strict) :
props(&IsEqualString), props(&IsEqualString),
elems(&IsEqualSmi), elems(&IsEqualNumber),
parser_(parser), parser_(parser),
strict_(strict) { strict_(strict) {
} }
...@@ -3092,13 +3092,12 @@ void ObjectLiteralPropertyChecker::CheckProperty( ...@@ -3092,13 +3092,12 @@ void ObjectLiteralPropertyChecker::CheckProperty(
uint32_t hash; uint32_t hash;
HashMap* map; HashMap* map;
void* key; void* key;
Smi* smi_key_location;
if (handle->IsSymbol()) { if (handle->IsSymbol()) {
Handle<String> name(String::cast(*handle)); Handle<String> name(String::cast(*handle));
if (name->AsArrayIndex(&hash)) { if (name->AsArrayIndex(&hash)) {
smi_key_location = Smi::FromInt(hash); Handle<Object> key_handle = Factory::NewNumberFromUint(hash);
key = &smi_key_location; key = key_handle.location();
map = &elems; map = &elems;
} else { } else {
key = handle.location(); key = handle.location();
......
...@@ -88,3 +88,6 @@ assertEquals(17, eval('[1,2,3,4]; 17')); ...@@ -88,3 +88,6 @@ assertEquals(17, eval('[1,2,3,4]; 17'));
assertEquals(19, eval('var a=1, b=2; [a,b,3,4]; 19')); assertEquals(19, eval('var a=1, b=2; [a,b,3,4]; 19'));
assertEquals(23, eval('var a=1, b=2; c=23; [a,b,3,4]; c')); assertEquals(23, eval('var a=1, b=2; c=23; [a,b,3,4]; c'));
// Test that literals work for non-smi indices.
// Ensure hash-map collision if using value as hash.
var o = {"2345678901" : 42, "2345678901" : 30};
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