OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #include "parser-symbol-table.h" |
| 29 |
| 30 #include "api.h" |
| 31 #include "objects.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 namespace { |
| 37 |
| 38 template <typename Char> |
| 39 int vector_hash(Vector<const Char> string) { |
| 40 int hash = 0; |
| 41 for (int i = 0; i < string.length(); i++) { |
| 42 int c = static_cast<int>(string[i]); |
| 43 hash += c; |
| 44 hash += (hash << 10); |
| 45 hash ^= (hash >> 6); |
| 46 } |
| 47 return hash; |
| 48 } |
| 49 |
| 50 |
| 51 bool vector_compare(void* a, void* b) { |
| 52 ParserSymbolTable::Symbol* string1 = |
| 53 reinterpret_cast<ParserSymbolTable::Symbol*>(a); |
| 54 ParserSymbolTable::Symbol* string2 = |
| 55 reinterpret_cast<ParserSymbolTable::Symbol*>(b); |
| 56 if (string1->is_one_byte != string2->is_one_byte) return false; |
| 57 if (string1->hash != string2->hash) return false; |
| 58 int length = string1->literal_bytes.length(); |
| 59 if (string2->literal_bytes.length() != length) return false; |
| 60 return memcmp(string1->literal_bytes.start(), |
| 61 string2->literal_bytes.start(), length) == 0; |
| 62 } |
| 63 |
| 64 } |
| 65 |
| 66 |
| 67 ParserSymbolTable::ParserSymbolTable() |
| 68 : literal_chars_(0), |
| 69 symbol_keys_(0), |
| 70 string_table_(vector_compare), |
| 71 isolate_(NULL) { |
| 72 { |
| 73 const char* data = "(anonymous function)"; |
| 74 anonymous_function_string_ = GetOneByteSymbol( |
| 75 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 20)); |
| 76 } |
| 77 { |
| 78 const char* data = "arguments"; |
| 79 arguments_string_ = GetOneByteSymbol( |
| 80 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 9)); |
| 81 } |
| 82 { |
| 83 const char* data = ".for"; |
| 84 dot_for_string_ = GetOneByteSymbol( |
| 85 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 4)); |
| 86 } |
| 87 { |
| 88 const char* data = ".iterator"; |
| 89 dot_iterator_string_ = GetOneByteSymbol( |
| 90 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 9)); |
| 91 } |
| 92 { |
| 93 const char* data = ".module"; |
| 94 dot_module_string_ = GetOneByteSymbol( |
| 95 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 7)); |
| 96 } |
| 97 { |
| 98 const char* data = ".result"; |
| 99 dot_result_string_ = GetOneByteSymbol( |
| 100 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 7)); |
| 101 } |
| 102 { |
| 103 const char* data = ""; |
| 104 empty_string_ = GetOneByteSymbol( |
| 105 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 0)); |
| 106 } |
| 107 { |
| 108 const char* data = "eval"; |
| 109 eval_string_ = GetOneByteSymbol( |
| 110 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 4)); |
| 111 } |
| 112 { |
| 113 const char* data = "InitializeConstGlobal"; |
| 114 initialize_const_global_string_ = GetOneByteSymbol( |
| 115 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 21)); |
| 116 } |
| 117 { |
| 118 const char* data = "InitializeVarGlobal"; |
| 119 initialize_var_global_string_ = GetOneByteSymbol( |
| 120 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 19)); |
| 121 } |
| 122 { |
| 123 const char* data = "MakeReferenceError"; |
| 124 make_reference_error_string_ = GetOneByteSymbol( |
| 125 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 18)); |
| 126 } |
| 127 { |
| 128 const char* data = "MakeSyntaxError"; |
| 129 make_syntax_error_string_ = GetOneByteSymbol( |
| 130 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 15)); |
| 131 } |
| 132 { |
| 133 const char* data = "MakeTypeError"; |
| 134 make_type_error_string_ = GetOneByteSymbol( |
| 135 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 13)); |
| 136 } |
| 137 { |
| 138 const char* data = "module"; |
| 139 module_string_ = GetOneByteSymbol( |
| 140 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 6)); |
| 141 } |
| 142 { |
| 143 const char* data = "native"; |
| 144 native_string_ = GetOneByteSymbol( |
| 145 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 6)); |
| 146 } |
| 147 { |
| 148 const char* data = "prototype"; |
| 149 prototype_string_ = GetOneByteSymbol( |
| 150 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 9)); |
| 151 } |
| 152 { |
| 153 const char* data = "this"; |
| 154 this_string_ = GetOneByteSymbol( |
| 155 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 4)); |
| 156 } |
| 157 { |
| 158 const char* data = "use strict"; |
| 159 use_strict_string_ = GetOneByteSymbol( |
| 160 Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), 10)); |
| 161 } |
| 162 } |
| 163 |
| 164 |
| 165 ParserSymbolTable::Symbol* ParserSymbolTable::GetOneByteSymbol( |
| 166 Vector<const uint8_t> literal) { |
| 167 return GetSymbol(vector_hash(literal), true, literal); |
| 168 } |
| 169 |
| 170 |
| 171 ParserSymbolTable::Symbol* ParserSymbolTable::GetTwoByteSymbol( |
| 172 Vector<const uint16_t> literal) { |
| 173 return GetSymbol(vector_hash(literal), false, |
| 174 Vector<const byte>::cast(literal)); |
| 175 } |
| 176 |
| 177 |
| 178 ParserSymbolTable::Symbol* ParserSymbolTable::GetSymbol( |
| 179 Handle<String> literal) { |
| 180 DisallowHeapAllocation no_gc; |
| 181 String::FlatContent content = literal->GetFlatContent(); |
| 182 if (content.IsAscii()) { |
| 183 return GetOneByteSymbol(content.ToOneByteVector()); |
| 184 } |
| 185 ASSERT(content.IsTwoByte()); |
| 186 return GetTwoByteSymbol(content.ToUC16Vector()); |
| 187 } |
| 188 |
| 189 |
| 190 bool ParserSymbolTable::SymbolMatches(Symbol* symbol, const char* data, |
| 191 int length) { |
| 192 if (symbol != NULL && symbol->is_one_byte && |
| 193 symbol->literal_bytes.length() == length) { |
| 194 const char* token = |
| 195 reinterpret_cast<const char*>(symbol->literal_bytes.start()); |
| 196 return !strncmp(token, data, length); |
| 197 } |
| 198 return false; |
| 199 } |
| 200 |
| 201 |
| 202 void ParserSymbolTable::Internalize(Isolate* isolate) { |
| 203 for (HashMap::Entry* p = string_table_.Start(); p != NULL; |
| 204 p = string_table_.Next(p)) { |
| 205 ParserSymbolTable::Symbol* symbol = |
| 206 reinterpret_cast<ParserSymbolTable::Symbol*>(p->key); |
| 207 Internalize(symbol, isolate); |
| 208 } |
| 209 // FIXME: can we free the backing store now? Maybe not. |
| 210 isolate_ = isolate; |
| 211 } |
| 212 |
| 213 |
| 214 bool ParserSymbolTable::IsArrayIndexSlow(Symbol* symbol, uint32_t* index) { |
| 215 ASSERT(symbol != NULL); |
| 216 if (!symbol->is_one_byte) return false; |
| 217 if (symbol->literal_bytes.length() == 0 || |
| 218 symbol->literal_bytes.length() > String::kMaxArrayIndexSize) |
| 219 return false; |
| 220 |
| 221 uint16_t ch = symbol->literal_bytes.at(0); |
| 222 // If the string begins with a '0' character, it must only consist |
| 223 // of it to be a legal array index. |
| 224 if (ch == '0') { |
| 225 if (index != NULL) *index = 0; |
| 226 return symbol->literal_bytes.length() == 1; |
| 227 } |
| 228 |
| 229 int d = ch - '0'; |
| 230 if (d < 0 || d > 9) return false; |
| 231 uint32_t result = d; |
| 232 for (int i = 1; i < symbol->literal_bytes.length(); ++i) { |
| 233 d = symbol->literal_bytes.at(i) - '0'; |
| 234 if (d < 0 || d > 9) return false; |
| 235 // Check that the new result is below the 32 bit limit. |
| 236 if (result > 429496729U - ((d > 5) ? 1 : 0)) return false; |
| 237 result = (result * 10) + d; |
| 238 } |
| 239 if (index != NULL) *index = result; |
| 240 return true; |
| 241 } |
| 242 |
| 243 |
| 244 ParserSymbolTable::Symbol* ParserSymbolTable::GetSymbol( |
| 245 int hash, bool is_one_byte, Vector<const byte> literal_bytes) { |
| 246 // literal_bytes here points to whatever the user passed, and this is OK |
| 247 // because we use vector_compare (which checks the contents) to compare |
| 248 // against the Symbols which are in the string_table_. We should not return |
| 249 // this Symbol. |
| 250 Symbol key(is_one_byte, literal_bytes, hash); |
| 251 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true); |
| 252 if (entry->value == NULL) { |
| 253 // Copy literal contents for later comparison. |
| 254 key.literal_bytes = |
| 255 Vector<const byte>::cast(literal_chars_.AddBlock(literal_bytes)); |
| 256 // This Vector will be valid as long as the Collector is alive (meaning that |
| 257 // the Symbol will not be moved). |
| 258 Vector<Symbol> symbol = symbol_keys_.AddBlock(1, key); |
| 259 entry->key = &symbol[0]; |
| 260 if (isolate_) { |
| 261 Internalize(&symbol[0], isolate_); |
| 262 } |
| 263 entry->value = reinterpret_cast<void*>(1); |
| 264 } |
| 265 return reinterpret_cast<Symbol*>(entry->key); |
| 266 } |
| 267 |
| 268 |
| 269 void ParserSymbolTable::Internalize(ParserSymbolTable::Symbol* symbol, Isolate*
isolate) { |
| 270 ASSERT(symbol != NULL); |
| 271 if (symbol->literal_bytes.length() == 0) { |
| 272 symbol->string_ = isolate->factory()->empty_string(); |
| 273 } |
| 274 else if (symbol->is_one_byte) { |
| 275 symbol->string_ = isolate->factory()->InternalizeOneByteString(symbol->liter
al_bytes); |
| 276 } else { |
| 277 symbol->string_ = isolate->factory()->InternalizeTwoByteString( |
| 278 Vector<const uint16_t>::cast(symbol->literal_bytes)); |
| 279 } |
| 280 } |
| 281 |
| 282 |
| 283 } } // namespace v8::internal |
OLD | NEW |