| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 891 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 902 KEYWORD_GROUP('v') \ | 902 KEYWORD_GROUP('v') \ |
| 903 KEYWORD("var", Token::VAR) \ | 903 KEYWORD("var", Token::VAR) \ |
| 904 KEYWORD("void", Token::VOID) \ | 904 KEYWORD("void", Token::VOID) \ |
| 905 KEYWORD_GROUP('w') \ | 905 KEYWORD_GROUP('w') \ |
| 906 KEYWORD("while", Token::WHILE) \ | 906 KEYWORD("while", Token::WHILE) \ |
| 907 KEYWORD("with", Token::WITH) \ | 907 KEYWORD("with", Token::WITH) \ |
| 908 KEYWORD_GROUP('y') \ | 908 KEYWORD_GROUP('y') \ |
| 909 KEYWORD("yield", Token::YIELD) | 909 KEYWORD("yield", Token::YIELD) |
| 910 | 910 |
| 911 | 911 |
| 912 static Token::Value KeywordOrIdentifierToken(const char* input, | 912 static Token::Value KeywordOrIdentifierToken(const uint8_t* input, |
| 913 int input_length, | 913 int input_length, |
| 914 bool harmony_scoping, | 914 bool harmony_scoping, |
| 915 bool harmony_modules) { | 915 bool harmony_modules) { |
| 916 ASSERT(input_length >= 1); | 916 ASSERT(input_length >= 1); |
| 917 const int kMinLength = 2; | 917 const int kMinLength = 2; |
| 918 const int kMaxLength = 10; | 918 const int kMaxLength = 10; |
| 919 if (input_length < kMinLength || input_length > kMaxLength) { | 919 if (input_length < kMinLength || input_length > kMaxLength) { |
| 920 return Token::IDENTIFIER; | 920 return Token::IDENTIFIER; |
| 921 } | 921 } |
| 922 switch (input[0]) { | 922 switch (input[0]) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 AddLiteralChar(next_char); | 978 AddLiteralChar(next_char); |
| 979 continue; | 979 continue; |
| 980 } | 980 } |
| 981 // Fallthrough if no longer able to complete keyword. | 981 // Fallthrough if no longer able to complete keyword. |
| 982 return ScanIdentifierSuffix(&literal); | 982 return ScanIdentifierSuffix(&literal); |
| 983 } | 983 } |
| 984 | 984 |
| 985 literal.Complete(); | 985 literal.Complete(); |
| 986 | 986 |
| 987 if (next_.literal_chars->is_one_byte()) { | 987 if (next_.literal_chars->is_one_byte()) { |
| 988 Vector<const char> chars = next_.literal_chars->one_byte_literal(); | 988 Vector<const uint8_t> chars = next_.literal_chars->one_byte_literal(); |
| 989 return KeywordOrIdentifierToken(chars.start(), | 989 return KeywordOrIdentifierToken(chars.start(), |
| 990 chars.length(), | 990 chars.length(), |
| 991 harmony_scoping_, | 991 harmony_scoping_, |
| 992 harmony_modules_); | 992 harmony_modules_); |
| 993 } | 993 } |
| 994 | 994 |
| 995 return Token::IDENTIFIER; | 995 return Token::IDENTIFIER; |
| 996 } | 996 } |
| 997 | 997 |
| 998 | 998 |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 } | 1116 } |
| 1117 | 1117 |
| 1118 | 1118 |
| 1119 Handle<String> Scanner::AllocateNextLiteralString(Isolate* isolate, | 1119 Handle<String> Scanner::AllocateNextLiteralString(Isolate* isolate, |
| 1120 PretenureFlag tenured) { | 1120 PretenureFlag tenured) { |
| 1121 if (is_next_literal_one_byte()) { | 1121 if (is_next_literal_one_byte()) { |
| 1122 return isolate->factory()->NewStringFromOneByte( | 1122 return isolate->factory()->NewStringFromOneByte( |
| 1123 Vector<const uint8_t>::cast(next_literal_one_byte_string()), tenured); | 1123 Vector<const uint8_t>::cast(next_literal_one_byte_string()), tenured); |
| 1124 } else { | 1124 } else { |
| 1125 return isolate->factory()->NewStringFromTwoByte( | 1125 return isolate->factory()->NewStringFromTwoByte( |
| 1126 next_literal_utf16_string(), tenured); | 1126 next_literal_two_byte_string(), tenured); |
| 1127 } | 1127 } |
| 1128 } | 1128 } |
| 1129 | 1129 |
| 1130 | 1130 |
| 1131 Handle<String> Scanner::AllocateInternalizedString(Isolate* isolate) { | 1131 Handle<String> Scanner::AllocateInternalizedString(Isolate* isolate) { |
| 1132 if (is_literal_one_byte()) { | 1132 if (is_literal_one_byte()) { |
| 1133 return isolate->factory()->InternalizeOneByteString( | 1133 return isolate->factory()->InternalizeOneByteString( |
| 1134 Vector<const uint8_t>::cast(literal_one_byte_string())); | 1134 literal_one_byte_string()); |
| 1135 } else { | 1135 } else { |
| 1136 return isolate->factory()->InternalizeTwoByteString( | 1136 return isolate->factory()->InternalizeTwoByteString( |
| 1137 literal_utf16_string()); | 1137 literal_two_byte_string()); |
| 1138 } | 1138 } |
| 1139 } | 1139 } |
| 1140 | 1140 |
| 1141 | 1141 |
| 1142 double Scanner::DoubleValue() { | 1142 double Scanner::DoubleValue() { |
| 1143 ASSERT(is_literal_one_byte()); | 1143 ASSERT(is_literal_one_byte()); |
| 1144 return StringToDouble( | 1144 return StringToDouble( |
| 1145 unicode_cache_, literal_one_byte_string(), | 1145 unicode_cache_, Vector<const char>::cast(literal_one_byte_string()), |
| 1146 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); | 1146 ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY); |
| 1147 } | 1147 } |
| 1148 | 1148 |
| 1149 | 1149 |
| 1150 int Scanner::FindNumber(DuplicateFinder* finder, int value) { | 1150 int Scanner::FindNumber(DuplicateFinder* finder, int value) { |
| 1151 return finder->AddNumber(literal_one_byte_string(), value); | 1151 return finder->AddNumber(literal_one_byte_string(), value); |
| 1152 } | 1152 } |
| 1153 | 1153 |
| 1154 | 1154 |
| 1155 int Scanner::FindSymbol(DuplicateFinder* finder, int value) { | 1155 int Scanner::FindSymbol(DuplicateFinder* finder, int value) { |
| 1156 if (is_literal_one_byte()) { | 1156 if (is_literal_one_byte()) { |
| 1157 return finder->AddAsciiSymbol(literal_one_byte_string(), value); | 1157 return finder->AddOneByteSymbol(literal_one_byte_string(), value); |
| 1158 } | 1158 } |
| 1159 return finder->AddUtf16Symbol(literal_utf16_string(), value); | 1159 return finder->AddTwoByteSymbol(literal_two_byte_string(), value); |
| 1160 } | 1160 } |
| 1161 | 1161 |
| 1162 | 1162 |
| 1163 void Scanner::LogSymbol(ParserRecorder* log, int position) { | 1163 void Scanner::LogSymbol(ParserRecorder* log, int position) { |
| 1164 if (is_literal_one_byte()) { | 1164 if (is_literal_one_byte()) { |
| 1165 log->LogAsciiSymbol(position, literal_one_byte_string()); | 1165 log->LogOneByteSymbol(position, literal_one_byte_string()); |
| 1166 } else { | 1166 } else { |
| 1167 log->LogUtf16Symbol(position, literal_utf16_string()); | 1167 log->LogTwoByteSymbol(position, literal_two_byte_string()); |
| 1168 } | 1168 } |
| 1169 } | 1169 } |
| 1170 | 1170 |
| 1171 | 1171 |
| 1172 int DuplicateFinder::AddAsciiSymbol(Vector<const char> key, int value) { | 1172 int DuplicateFinder::AddOneByteSymbol(Vector<const uint8_t> key, int value) { |
| 1173 return AddSymbol(Vector<const byte>::cast(key), true, value); | 1173 return AddSymbol(key, true, value); |
| 1174 } | 1174 } |
| 1175 | 1175 |
| 1176 | 1176 |
| 1177 int DuplicateFinder::AddUtf16Symbol(Vector<const uint16_t> key, int value) { | 1177 int DuplicateFinder::AddTwoByteSymbol(Vector<const uint16_t> key, int value) { |
| 1178 return AddSymbol(Vector<const byte>::cast(key), false, value); | 1178 return AddSymbol(Vector<const uint8_t>::cast(key), false, value); |
| 1179 } | 1179 } |
| 1180 | 1180 |
| 1181 | 1181 |
| 1182 int DuplicateFinder::AddSymbol(Vector<const byte> key, | 1182 int DuplicateFinder::AddSymbol(Vector<const uint8_t> key, |
| 1183 bool is_one_byte, | 1183 bool is_one_byte, |
| 1184 int value) { | 1184 int value) { |
| 1185 uint32_t hash = Hash(key, is_one_byte); | 1185 uint32_t hash = Hash(key, is_one_byte); |
| 1186 byte* encoding = BackupKey(key, is_one_byte); | 1186 byte* encoding = BackupKey(key, is_one_byte); |
| 1187 HashMap::Entry* entry = map_.Lookup(encoding, hash, true); | 1187 HashMap::Entry* entry = map_.Lookup(encoding, hash, true); |
| 1188 int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); | 1188 int old_value = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1189 entry->value = | 1189 entry->value = |
| 1190 reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value)); | 1190 reinterpret_cast<void*>(static_cast<intptr_t>(value | old_value)); |
| 1191 return old_value; | 1191 return old_value; |
| 1192 } | 1192 } |
| 1193 | 1193 |
| 1194 | 1194 |
| 1195 int DuplicateFinder::AddNumber(Vector<const char> key, int value) { | 1195 int DuplicateFinder::AddNumber(Vector<const uint8_t> key, int value) { |
| 1196 ASSERT(key.length() > 0); | 1196 ASSERT(key.length() > 0); |
| 1197 // Quick check for already being in canonical form. | 1197 // Quick check for already being in canonical form. |
| 1198 if (IsNumberCanonical(key)) { | 1198 if (IsNumberCanonical(key)) { |
| 1199 return AddAsciiSymbol(key, value); | 1199 return AddOneByteSymbol(key, value); |
| 1200 } | 1200 } |
| 1201 | 1201 |
| 1202 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY; | 1202 int flags = ALLOW_HEX | ALLOW_OCTAL | ALLOW_IMPLICIT_OCTAL | ALLOW_BINARY; |
| 1203 double double_value = StringToDouble(unicode_constants_, key, flags, 0.0); | 1203 double double_value = StringToDouble( |
| 1204 unicode_constants_, Vector<const char>::cast(key), flags, 0.0); |
| 1204 int length; | 1205 int length; |
| 1205 const char* string; | 1206 const char* string; |
| 1206 if (!std::isfinite(double_value)) { | 1207 if (!std::isfinite(double_value)) { |
| 1207 string = "Infinity"; | 1208 string = "Infinity"; |
| 1208 length = 8; // strlen("Infinity"); | 1209 length = 8; // strlen("Infinity"); |
| 1209 } else { | 1210 } else { |
| 1210 string = DoubleToCString(double_value, | 1211 string = DoubleToCString(double_value, |
| 1211 Vector<char>(number_buffer_, kBufferSize)); | 1212 Vector<char>(number_buffer_, kBufferSize)); |
| 1212 length = StrLength(string); | 1213 length = StrLength(string); |
| 1213 } | 1214 } |
| 1214 return AddSymbol(Vector<const byte>(reinterpret_cast<const byte*>(string), | 1215 return AddSymbol(Vector<const byte>(reinterpret_cast<const byte*>(string), |
| 1215 length), true, value); | 1216 length), true, value); |
| 1216 } | 1217 } |
| 1217 | 1218 |
| 1218 | 1219 |
| 1219 bool DuplicateFinder::IsNumberCanonical(Vector<const char> number) { | 1220 bool DuplicateFinder::IsNumberCanonical(Vector<const uint8_t> number) { |
| 1220 // Test for a safe approximation of number literals that are already | 1221 // Test for a safe approximation of number literals that are already |
| 1221 // in canonical form: max 15 digits, no leading zeroes, except an | 1222 // in canonical form: max 15 digits, no leading zeroes, except an |
| 1222 // integer part that is a single zero, and no trailing zeros below | 1223 // integer part that is a single zero, and no trailing zeros below |
| 1223 // the decimal point. | 1224 // the decimal point. |
| 1224 int pos = 0; | 1225 int pos = 0; |
| 1225 int length = number.length(); | 1226 int length = number.length(); |
| 1226 if (number.length() > 15) return false; | 1227 if (number.length() > 15) return false; |
| 1227 if (number[pos] == '0') { | 1228 if (number[pos] == '0') { |
| 1228 pos++; | 1229 pos++; |
| 1229 } else { | 1230 } else { |
| 1230 while (pos < length && | 1231 while (pos < length && |
| 1231 static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++; | 1232 static_cast<unsigned>(number[pos] - '0') <= ('9' - '0')) pos++; |
| 1232 } | 1233 } |
| 1233 if (length == pos) return true; | 1234 if (length == pos) return true; |
| 1234 if (number[pos] != '.') return false; | 1235 if (number[pos] != '.') return false; |
| 1235 pos++; | 1236 pos++; |
| 1236 bool invalid_last_digit = true; | 1237 bool invalid_last_digit = true; |
| 1237 while (pos < length) { | 1238 while (pos < length) { |
| 1238 byte digit = number[pos] - '0'; | 1239 uint8_t digit = number[pos] - '0'; |
| 1239 if (digit > '9' - '0') return false; | 1240 if (digit > '9' - '0') return false; |
| 1240 invalid_last_digit = (digit == 0); | 1241 invalid_last_digit = (digit == 0); |
| 1241 pos++; | 1242 pos++; |
| 1242 } | 1243 } |
| 1243 return !invalid_last_digit; | 1244 return !invalid_last_digit; |
| 1244 } | 1245 } |
| 1245 | 1246 |
| 1246 | 1247 |
| 1247 uint32_t DuplicateFinder::Hash(Vector<const byte> key, bool is_one_byte) { | 1248 uint32_t DuplicateFinder::Hash(Vector<const uint8_t> key, bool is_one_byte) { |
| 1248 // Primitive hash function, almost identical to the one used | 1249 // Primitive hash function, almost identical to the one used |
| 1249 // for strings (except that it's seeded by the length and ASCII-ness). | 1250 // for strings (except that it's seeded by the length and ASCII-ness). |
| 1250 int length = key.length(); | 1251 int length = key.length(); |
| 1251 uint32_t hash = (length << 1) | (is_one_byte ? 1 : 0) ; | 1252 uint32_t hash = (length << 1) | (is_one_byte ? 1 : 0) ; |
| 1252 for (int i = 0; i < length; i++) { | 1253 for (int i = 0; i < length; i++) { |
| 1253 uint32_t c = key[i]; | 1254 uint32_t c = key[i]; |
| 1254 hash = (hash + c) * 1025; | 1255 hash = (hash + c) * 1025; |
| 1255 hash ^= (hash >> 6); | 1256 hash ^= (hash >> 6); |
| 1256 } | 1257 } |
| 1257 return hash; | 1258 return hash; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1273 if (c1 != *s2) return false; | 1274 if (c1 != *s2) return false; |
| 1274 length_one_byte_field = (length_one_byte_field << 7) | (c1 & 0x7f); | 1275 length_one_byte_field = (length_one_byte_field << 7) | (c1 & 0x7f); |
| 1275 s1++; | 1276 s1++; |
| 1276 s2++; | 1277 s2++; |
| 1277 } while ((c1 & 0x80) != 0); | 1278 } while ((c1 & 0x80) != 0); |
| 1278 int length = static_cast<int>(length_one_byte_field >> 1); | 1279 int length = static_cast<int>(length_one_byte_field >> 1); |
| 1279 return memcmp(s1, s2, length) == 0; | 1280 return memcmp(s1, s2, length) == 0; |
| 1280 } | 1281 } |
| 1281 | 1282 |
| 1282 | 1283 |
| 1283 byte* DuplicateFinder::BackupKey(Vector<const byte> bytes, | 1284 byte* DuplicateFinder::BackupKey(Vector<const uint8_t> bytes, |
| 1284 bool is_one_byte) { | 1285 bool is_one_byte) { |
| 1285 uint32_t one_byte_length = (bytes.length() << 1) | (is_one_byte ? 1 : 0); | 1286 uint32_t one_byte_length = (bytes.length() << 1) | (is_one_byte ? 1 : 0); |
| 1286 backing_store_.StartSequence(); | 1287 backing_store_.StartSequence(); |
| 1287 // Emit one_byte_length as base-128 encoded number, with the 7th bit set | 1288 // Emit one_byte_length as base-128 encoded number, with the 7th bit set |
| 1288 // on the byte of every heptet except the last, least significant, one. | 1289 // on the byte of every heptet except the last, least significant, one. |
| 1289 if (one_byte_length >= (1 << 7)) { | 1290 if (one_byte_length >= (1 << 7)) { |
| 1290 if (one_byte_length >= (1 << 14)) { | 1291 if (one_byte_length >= (1 << 14)) { |
| 1291 if (one_byte_length >= (1 << 21)) { | 1292 if (one_byte_length >= (1 << 21)) { |
| 1292 if (one_byte_length >= (1 << 28)) { | 1293 if (one_byte_length >= (1 << 28)) { |
| 1293 backing_store_.Add(static_cast<byte>((one_byte_length >> 28) | 0x80)); | 1294 backing_store_.Add( |
| 1295 static_cast<uint8_t>((one_byte_length >> 28) | 0x80)); |
| 1294 } | 1296 } |
| 1295 backing_store_.Add(static_cast<byte>((one_byte_length >> 21) | 0x80u)); | 1297 backing_store_.Add( |
| 1298 static_cast<uint8_t>((one_byte_length >> 21) | 0x80u)); |
| 1296 } | 1299 } |
| 1297 backing_store_.Add(static_cast<byte>((one_byte_length >> 14) | 0x80u)); | 1300 backing_store_.Add( |
| 1301 static_cast<uint8_t>((one_byte_length >> 14) | 0x80u)); |
| 1298 } | 1302 } |
| 1299 backing_store_.Add(static_cast<byte>((one_byte_length >> 7) | 0x80u)); | 1303 backing_store_.Add(static_cast<uint8_t>((one_byte_length >> 7) | 0x80u)); |
| 1300 } | 1304 } |
| 1301 backing_store_.Add(static_cast<byte>(one_byte_length & 0x7f)); | 1305 backing_store_.Add(static_cast<uint8_t>(one_byte_length & 0x7f)); |
| 1302 | 1306 |
| 1303 backing_store_.AddBlock(bytes); | 1307 backing_store_.AddBlock(bytes); |
| 1304 return backing_store_.EndSequence().start(); | 1308 return backing_store_.EndSequence().start(); |
| 1305 } | 1309 } |
| 1306 | 1310 |
| 1307 } } // namespace v8::internal | 1311 } } // namespace v8::internal |
| OLD | NEW |