Index: src/preparser.cc |
diff --git a/src/preparser.cc b/src/preparser.cc |
index 11f5595440fcde4771e62c5d05a2aef9300fe2f0..2486632a9ea145fcf94cffdf4ce282e369f745ef 100644 |
--- a/src/preparser.cc |
+++ b/src/preparser.cc |
@@ -1231,6 +1231,39 @@ PreParser::Expression PreParser::ParseArrayLiteral(bool* ok) { |
return Expression::Default(); |
} |
+void PreParser::CheckDuplicate(DuplicateFinder* finder, |
+ i::Token::Value property, |
+ int type, |
+ bool* ok) { |
+ int old_type; |
+ if (property == i::Token::NUMBER) { |
+ old_type = finder->AddNumber(scanner_->literal_ascii_string(), type); |
+ } else if (scanner_->is_literal_ascii()) { |
+ old_type = finder->AddAsciiSymbol(scanner_->literal_ascii_string(), |
+ type); |
+ } else { |
+ old_type = finder->AddUtf16Symbol(scanner_->literal_utf16_string(), type); |
+ } |
+ if (HasConflict(old_type, type)) { |
+ if (IsDataDataConflict(old_type, type)) { |
+ // Both are data properties. |
+ if (is_classic_mode()) return; |
+ ReportMessageAt(scanner_->location(), |
+ "strict_duplicate_property", NULL); |
+ } else if (IsDataAccessorConflict(old_type, type)) { |
+ // Both a data and an accessor property with the same name. |
+ ReportMessageAt(scanner_->location(), |
+ "accessor_data_property", NULL); |
+ } else { |
+ ASSERT(IsAccessorAccessorConflict(old_type, type)); |
+ // Both accessors of the same type. |
+ ReportMessageAt(scanner_->location(), |
+ "accessor_get_set", NULL); |
+ } |
+ *ok = false; |
+ } |
+} |
+ |
PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) { |
// ObjectLiteral :: |
@@ -1239,9 +1272,8 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) { |
// | (('get' | 'set') (IdentifierName | String | Number) FunctionLiteral) |
// )*[','] '}' |
- i::ObjectLiteralChecker<PreParser> checker(this, scanner_, language_mode()); |
- |
Expect(i::Token::LBRACE, CHECK_OK); |
+ DuplicateFinder duplicate_finder(scanner_->unicode_cache()); |
while (peek() != i::Token::RBRACE) { |
i::Token::Value next = peek(); |
switch (next) { |
@@ -1266,31 +1298,30 @@ PreParser::Expression PreParser::ParseObjectLiteral(bool* ok) { |
if (!is_keyword) { |
LogSymbol(); |
} |
- i::PropertyKind type = is_getter ? i::kGetterProperty |
- : i::kSetterProperty; |
- checker.CheckProperty(name, type, CHECK_OK); |
+ PropertyType type = is_getter ? kGetterProperty : kSetterProperty; |
+ CheckDuplicate(&duplicate_finder, name, type, CHECK_OK); |
ParseFunctionLiteral(false, CHECK_OK); |
if (peek() != i::Token::RBRACE) { |
Expect(i::Token::COMMA, CHECK_OK); |
} |
continue; // restart the while |
} |
- checker.CheckProperty(next, i::kValueProperty, CHECK_OK); |
+ CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
break; |
} |
case i::Token::STRING: |
Consume(next); |
- checker.CheckProperty(next, i::kValueProperty, CHECK_OK); |
+ CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
GetStringSymbol(); |
break; |
case i::Token::NUMBER: |
Consume(next); |
- checker.CheckProperty(next, i::kValueProperty, CHECK_OK); |
+ CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
break; |
default: |
if (i::Token::IsKeyword(next)) { |
Consume(next); |
- checker.CheckProperty(next, i::kValueProperty, CHECK_OK); |
+ CheckDuplicate(&duplicate_finder, next, kValueProperty, CHECK_OK); |
} else { |
// Unexpected token. |
*ok = false; |
@@ -1371,7 +1402,7 @@ PreParser::Expression PreParser::ParseFunctionLiteral(bool is_generator, |
Expect(i::Token::LPAREN, CHECK_OK); |
int start_position = scanner_->location().beg_pos; |
bool done = (peek() == i::Token::RPAREN); |
- i::DuplicateFinder duplicate_finder(scanner_->unicode_cache()); |
+ DuplicateFinder duplicate_finder(scanner_->unicode_cache()); |
while (!done) { |
Identifier id = ParseIdentifier(CHECK_OK); |
if (!id.IsValidStrictVariable()) { |
@@ -1663,4 +1694,139 @@ bool PreParser::peek_any_identifier() { |
next == i::Token::YIELD; |
} |
+ |
+int DuplicateFinder::AddAsciiSymbol(i::Vector<const char> key, int value) { |
+ return AddSymbol(i::Vector<const byte>::cast(key), true, value); |
+} |
+ |
+ |
+int DuplicateFinder::AddUtf16Symbol(i::Vector<const uint16_t> key, int value) { |
+ return AddSymbol(i::Vector<const byte>::cast(key), false, value); |
+} |
+ |
+int DuplicateFinder::AddSymbol(i::Vector<const byte> key, |
+ bool is_ascii, |
+ int value) { |
+ uint32_t hash = Hash(key, is_ascii); |
+ byte* encoding = BackupKey(key, is_ascii); |
+ i::HashMap::Entry* entry = map_.Lookup(encoding, hash, true); |
+ int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
+ entry->value = |
+ reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value)); |
+ return old_value; |
+} |
+ |
+ |
+int DuplicateFinder::AddNumber(i::Vector<const char> key, int value) { |
+ ASSERT(key.length() > 0); |
+ // Quick check for already being in canonical form. |
+ if (IsNumberCanonical(key)) { |
+ return AddAsciiSymbol(key, value); |
+ } |
+ |
+ int flags = i::ALLOW_HEX | i::ALLOW_OCTAL | i::ALLOW_IMPLICIT_OCTAL | |
+ i::ALLOW_BINARY; |
+ double double_value = StringToDouble(unicode_constants_, key, flags, 0.0); |
+ int length; |
+ const char* string; |
+ if (!std::isfinite(double_value)) { |
+ string = "Infinity"; |
+ length = 8; // strlen("Infinity"); |
+ } else { |
+ string = DoubleToCString(double_value, |
+ i::Vector<char>(number_buffer_, kBufferSize)); |
+ length = i::StrLength(string); |
+ } |
+ return AddSymbol(i::Vector<const byte>(reinterpret_cast<const byte*>(string), |
+ length), true, value); |
+} |
+ |
+ |
+bool DuplicateFinder::IsNumberCanonical(i::Vector<const char> number) { |
+ // Test for a safe approximation of number literals that are already |
+ // in canonical form: max 15 digits, no leading zeroes, except an |
+ // integer part that is a single zero, and no trailing zeros below |
+ // the decimal point. |
+ int pos = 0; |
+ int length = number.length(); |
+ if (number.length() > 15) return false; |
+ if (number[pos] == '0') { |
+ pos++; |
+ } else { |
+ while (pos < length && |
+ static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++; |
+ } |
+ if (length == pos) return true; |
+ if (number[pos] != '.') return false; |
+ pos++; |
+ bool invalid_last_digit = true; |
+ while (pos < length) { |
+ byte digit = number[pos] - '0'; |
+ if (digit > '9' - '0') return false; |
+ invalid_last_digit = (digit == 0); |
+ pos++; |
+ } |
+ return !invalid_last_digit; |
+} |
+ |
+ |
+uint32_t DuplicateFinder::Hash(i::Vector<const byte> key, bool is_ascii) { |
+ // Primitive hash function, almost identical to the one used |
+ // for strings (except that it's seeded by the length and ASCII-ness). |
+ int length = key.length(); |
+ uint32_t hash = (length << 1) | (is_ascii ? 1 : 0) ; |
+ for (int i = 0; i < length; i++) { |
+ uint32_t c = key[i]; |
+ hash = (hash + c) * 1025; |
+ hash ^= (hash >> 6); |
+ } |
+ return hash; |
+} |
+ |
+ |
+bool DuplicateFinder::Match(void* first, void* second) { |
+ // Decode lengths. |
+ // Length + ASCII-bit is encoded as base 128, most significant heptet first, |
+ // with a 8th bit being non-zero while there are more heptets. |
+ // The value encodes the number of bytes following, and whether the original |
+ // was ASCII. |
+ byte* s1 = reinterpret_cast<byte*>(first); |
+ byte* s2 = reinterpret_cast<byte*>(second); |
+ uint32_t length_ascii_field = 0; |
+ byte c1; |
+ do { |
+ c1 = *s1; |
+ if (c1 != *s2) return false; |
+ length_ascii_field = (length_ascii_field << 7) | (c1 & 0x7f); |
+ s1++; |
+ s2++; |
+ } while ((c1 & 0x80) != 0); |
+ int length = static_cast<int>(length_ascii_field >> 1); |
+ return memcmp(s1, s2, length) == 0; |
+} |
+ |
+ |
+byte* DuplicateFinder::BackupKey(i::Vector<const byte> bytes, |
+ bool is_ascii) { |
+ uint32_t ascii_length = (bytes.length() << 1) | (is_ascii ? 1 : 0); |
+ backing_store_.StartSequence(); |
+ // Emit ascii_length as base-128 encoded number, with the 7th bit set |
+ // on the byte of every heptet except the last, least significant, one. |
+ if (ascii_length >= (1 << 7)) { |
+ if (ascii_length >= (1 << 14)) { |
+ if (ascii_length >= (1 << 21)) { |
+ if (ascii_length >= (1 << 28)) { |
+ backing_store_.Add(static_cast<byte>((ascii_length >> 28) | 0x80)); |
+ } |
+ backing_store_.Add(static_cast<byte>((ascii_length >> 21) | 0x80u)); |
+ } |
+ backing_store_.Add(static_cast<byte>((ascii_length >> 14) | 0x80u)); |
+ } |
+ backing_store_.Add(static_cast<byte>((ascii_length >> 7) | 0x80u)); |
+ } |
+ backing_store_.Add(static_cast<byte>(ascii_length & 0x7f)); |
+ |
+ backing_store_.AddBlock(bytes); |
+ return backing_store_.EndSequence().start(); |
+} |
} } // v8::preparser |