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 "src/ast-value-factory.h" |
| 29 |
| 30 #include "src/api.h" |
| 31 #include "src/objects.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 namespace { |
| 37 |
| 38 // For using StringToArrayIndex. |
| 39 class OneByteStringStream { |
| 40 public: |
| 41 explicit OneByteStringStream(Vector<const byte> lb) : |
| 42 literal_bytes_(lb), pos_(0) {} |
| 43 |
| 44 bool HasMore() { return pos_ < literal_bytes_.length(); } |
| 45 uint16_t GetNext() { return literal_bytes_[pos_++]; } |
| 46 |
| 47 private: |
| 48 Vector<const byte> literal_bytes_; |
| 49 int pos_; |
| 50 }; |
| 51 |
| 52 } |
| 53 |
| 54 class AstStringInternalizationKey : public HashTableKey { |
| 55 public: |
| 56 explicit AstStringInternalizationKey(const AstString* string) |
| 57 : string_(string) {} |
| 58 |
| 59 virtual bool IsMatch(Object* other) V8_OVERRIDE { |
| 60 return String::cast(other)->IsOneByteEqualTo(string_->literal_bytes_); |
| 61 } |
| 62 |
| 63 virtual uint32_t Hash() V8_OVERRIDE { |
| 64 return string_->hash() >> Name::kHashShift; |
| 65 } |
| 66 |
| 67 virtual uint32_t HashForObject(Object* key) V8_OVERRIDE { |
| 68 return String::cast(key)->Hash(); |
| 69 } |
| 70 |
| 71 virtual Handle<Object> AsHandle(Isolate* isolate) V8_OVERRIDE { |
| 72 if (string_->is_one_byte_) |
| 73 return isolate->factory()->NewOneByteInternalizedString( |
| 74 string_->literal_bytes_, string_->hash()); |
| 75 return isolate->factory()->NewTwoByteInternalizedString( |
| 76 Vector<const uint16_t>::cast(string_->literal_bytes_), string_->hash()); |
| 77 } |
| 78 |
| 79 private: |
| 80 const AstString* string_; |
| 81 }; |
| 82 |
| 83 |
| 84 |
| 85 bool AstString::AsArrayIndex(uint32_t* index) const { |
| 86 ASSERT(type_ == NORMAL); |
| 87 if (!string_.is_null()) |
| 88 return string_->AsArrayIndex(index); |
| 89 if (!is_one_byte_ || literal_bytes_.length() == 0 || |
| 90 literal_bytes_.length() > String::kMaxArrayIndexSize) |
| 91 return false; |
| 92 OneByteStringStream stream(literal_bytes_); |
| 93 return StringToArrayIndex(&stream, index); |
| 94 } |
| 95 |
| 96 |
| 97 bool AstString::IsOneByteEqualTo(const char* data) const { |
| 98 ASSERT(type_ == NORMAL); |
| 99 int length = static_cast<int>(strlen(data)); |
| 100 if (is_one_byte_ && literal_bytes_.length() == length) { |
| 101 const char* token = reinterpret_cast<const char*>(literal_bytes_.start()); |
| 102 return !strncmp(token, data, length); |
| 103 } |
| 104 return false; |
| 105 } |
| 106 |
| 107 |
| 108 void AstString::Internalize(Isolate* isolate) { |
| 109 if (!string_.is_null()) return; |
| 110 if (type_ == CONS) { |
| 111 // Strings are internalized in creation order, so left and right are already |
| 112 // internalized. |
| 113 string_ = isolate->factory() |
| 114 ->NewConsString(left_->string(), right_->string()) |
| 115 .ToHandleChecked(); |
| 116 return; |
| 117 } |
| 118 if (literal_bytes_.length() == 0) { |
| 119 string_ = isolate->factory()->empty_string(); |
| 120 } else { |
| 121 AstStringInternalizationKey key(this); |
| 122 string_ = StringTable::LookupKey(isolate, &key); |
| 123 } |
| 124 } |
| 125 |
| 126 |
| 127 bool AstString::Compare(void* a, void* b) { |
| 128 AstString* string1 = reinterpret_cast<AstString*>(a); |
| 129 AstString* string2 = reinterpret_cast<AstString*>(b); |
| 130 ASSERT(string1->type_ == NORMAL); |
| 131 ASSERT(string2->type_ == NORMAL); |
| 132 if (string1->is_one_byte_ != string2->is_one_byte_) return false; |
| 133 if (string1->hash_ != string2->hash_) return false; |
| 134 int length = string1->literal_bytes_.length(); |
| 135 if (string2->literal_bytes_.length() != length) return false; |
| 136 return memcmp(string1->literal_bytes_.start(), |
| 137 string2->literal_bytes_.start(), length) == 0; |
| 138 } |
| 139 |
| 140 |
| 141 bool AstValue::IsPropertyName() const { |
| 142 if (type_ == STRING) { |
| 143 uint32_t index; |
| 144 return !string_->AsArrayIndex(&index); |
| 145 } |
| 146 return false; |
| 147 } |
| 148 |
| 149 |
| 150 bool AstValue::BooleanValue() const { |
| 151 switch (type_) { |
| 152 case STRING: |
| 153 ASSERT(string_ != NULL); |
| 154 return !string_->IsEmpty(); |
| 155 case SYMBOL: |
| 156 UNREACHABLE(); |
| 157 break; |
| 158 case NUMBER: |
| 159 return DoubleToBoolean(number_); |
| 160 case SMI: |
| 161 return smi_ != 0; |
| 162 case STRING_ARRAY: |
| 163 UNREACHABLE(); |
| 164 break; |
| 165 case BOOLEAN: |
| 166 return bool_; |
| 167 case NULL_TYPE: |
| 168 return false; |
| 169 case THE_HOLE: |
| 170 UNREACHABLE(); |
| 171 break; |
| 172 case UNDEFINED: |
| 173 return false; |
| 174 } |
| 175 UNREACHABLE(); |
| 176 return false; |
| 177 } |
| 178 |
| 179 |
| 180 void AstValue::Internalize(Isolate* isolate) { |
| 181 switch (type_) { |
| 182 case STRING: |
| 183 ASSERT(string_ != NULL); |
| 184 // Strings are already internalized. |
| 185 ASSERT(!string_->string().is_null()); |
| 186 break; |
| 187 case SYMBOL: |
| 188 value_ = Object::GetProperty( |
| 189 isolate, handle(isolate->native_context()->builtins()), |
| 190 symbol_name_).ToHandleChecked(); |
| 191 break; |
| 192 case NUMBER: |
| 193 value_ = isolate->factory()->NewNumber(number_, TENURED); |
| 194 break; |
| 195 case SMI: |
| 196 value_ = handle(Smi::FromInt(smi_), isolate); |
| 197 break; |
| 198 case BOOLEAN: |
| 199 if (bool_) { |
| 200 value_ = isolate->factory()->true_value(); |
| 201 } else { |
| 202 value_ = isolate->factory()->false_value(); |
| 203 } |
| 204 break; |
| 205 case STRING_ARRAY: { |
| 206 ASSERT(strings_ != NULL); |
| 207 Factory* factory = isolate->factory(); |
| 208 int len = strings_->length(); |
| 209 Handle<FixedArray> elements = factory->NewFixedArray(len, TENURED); |
| 210 for (int i = 0; i < len; i++) { |
| 211 const AstString* string = (*strings_)[i]; |
| 212 Handle<Object> element = string->string(); |
| 213 // Strings are already internalized. |
| 214 ASSERT(!element.is_null()); |
| 215 elements->set(i, *element); |
| 216 } |
| 217 value_ = |
| 218 factory->NewJSArrayWithElements(elements, FAST_ELEMENTS, TENURED); |
| 219 break; |
| 220 } |
| 221 case NULL_TYPE: |
| 222 value_ = isolate->factory()->null_value(); |
| 223 break; |
| 224 case THE_HOLE: |
| 225 value_ = isolate->factory()->the_hole_value(); |
| 226 break; |
| 227 case UNDEFINED: |
| 228 value_ = isolate->factory()->undefined_value(); |
| 229 break; |
| 230 } |
| 231 } |
| 232 |
| 233 |
| 234 const AstString* AstValueFactory::GetOneByteString( |
| 235 const Vector<const uint8_t>& literal) { |
| 236 uint32_t hash = StringHasher::HashSequentialString<uint8_t>( |
| 237 literal.start(), literal.length(), hash_seed_); |
| 238 return GetString(hash, true, literal); |
| 239 } |
| 240 |
| 241 |
| 242 const AstString* AstValueFactory::GetTwoByteString( |
| 243 const Vector<const uint16_t>& literal) { |
| 244 uint32_t hash = StringHasher::HashSequentialString<uint16_t>( |
| 245 literal.start(), literal.length(), hash_seed_); |
| 246 return GetString(hash, false, Vector<const byte>::cast(literal)); |
| 247 } |
| 248 |
| 249 |
| 250 const AstString* AstValueFactory::GetString(Handle<String> literal) { |
| 251 DisallowHeapAllocation no_gc; |
| 252 String::FlatContent content = literal->GetFlatContent(); |
| 253 if (content.IsAscii()) { |
| 254 return GetOneByteString(content.ToOneByteVector()); |
| 255 } |
| 256 ASSERT(content.IsTwoByte()); |
| 257 return GetTwoByteString(content.ToUC16Vector()); |
| 258 } |
| 259 |
| 260 |
| 261 const AstString* AstValueFactory::NewConsString(const AstString* left, |
| 262 const AstString* right) { |
| 263 AstString key(left, right); |
| 264 // This Vector will be valid as long as the Collector is alive (meaning that |
| 265 // the AstString will not be moved). |
| 266 AstString* new_string = new (zone_) AstString(left, right); |
| 267 strings_.Add(new_string); |
| 268 if (isolate_) { |
| 269 new_string[0].Internalize(isolate_); |
| 270 } |
| 271 return new_string; |
| 272 } |
| 273 |
| 274 |
| 275 void AstValueFactory::Internalize(Isolate* isolate) { |
| 276 if (isolate_) { |
| 277 // Everything is already internalized. |
| 278 return; |
| 279 } |
| 280 // Strings need to be internalized before values, because values refer to |
| 281 // strings. |
| 282 for (int i = 0; i < strings_.length(); ++i) { |
| 283 strings_[i]->Internalize(isolate); |
| 284 } |
| 285 for (int i = 0; i < values_.length(); ++i) { |
| 286 values_[i]->Internalize(isolate); |
| 287 } |
| 288 isolate_ = isolate; |
| 289 } |
| 290 |
| 291 |
| 292 const AstValue* AstValueFactory::NewString(const AstString* string) { |
| 293 AstValue* value = new (zone_) AstValue(string); |
| 294 ASSERT(string != NULL); |
| 295 if (isolate_) { |
| 296 value->Internalize(isolate_); |
| 297 } |
| 298 values_.Add(value); |
| 299 return value; |
| 300 } |
| 301 |
| 302 |
| 303 const AstValue* AstValueFactory::NewSymbol(const char* name) { |
| 304 AstValue* value = new (zone_) AstValue(name); |
| 305 if (isolate_) { |
| 306 value->Internalize(isolate_); |
| 307 } |
| 308 values_.Add(value); |
| 309 return value; |
| 310 } |
| 311 |
| 312 |
| 313 const AstValue* AstValueFactory::NewNumber(double number) { |
| 314 AstValue* value = new (zone_) AstValue(number); |
| 315 if (isolate_) { |
| 316 value->Internalize(isolate_); |
| 317 } |
| 318 values_.Add(value); |
| 319 return value; |
| 320 } |
| 321 |
| 322 |
| 323 const AstValue* AstValueFactory::NewSmi(int number) { |
| 324 AstValue* value = |
| 325 new (zone_) AstValue(AstValue::SMI, number); |
| 326 if (isolate_) { |
| 327 value->Internalize(isolate_); |
| 328 } |
| 329 values_.Add(value); |
| 330 return value; |
| 331 } |
| 332 |
| 333 |
| 334 const AstValue* AstValueFactory::NewBoolean(bool b) { |
| 335 AstValue* value = new (zone_) AstValue(b); |
| 336 if (isolate_) { |
| 337 value->Internalize(isolate_); |
| 338 } |
| 339 values_.Add(value); |
| 340 return value; |
| 341 } |
| 342 |
| 343 |
| 344 const AstValue* AstValueFactory::NewStringList( |
| 345 ZoneList<const AstString*>* strings) { |
| 346 AstValue* value = new (zone_) AstValue(strings); |
| 347 if (isolate_) { |
| 348 value->Internalize(isolate_); |
| 349 } |
| 350 values_.Add(value); |
| 351 return value; |
| 352 } |
| 353 |
| 354 |
| 355 const AstValue* AstValueFactory::NewNull() { |
| 356 AstValue* value = new (zone_) AstValue(AstValue::NULL_TYPE); |
| 357 if (isolate_) { |
| 358 value->Internalize(isolate_); |
| 359 } |
| 360 values_.Add(value); |
| 361 return value; |
| 362 } |
| 363 |
| 364 |
| 365 const AstValue* AstValueFactory::NewUndefined() { |
| 366 AstValue* value = new (zone_) AstValue(AstValue::UNDEFINED); |
| 367 if (isolate_) { |
| 368 value->Internalize(isolate_); |
| 369 } |
| 370 values_.Add(value); |
| 371 return value; |
| 372 } |
| 373 |
| 374 |
| 375 const AstValue* AstValueFactory::NewTheHole() { |
| 376 AstValue* value = new (zone_) AstValue(AstValue::THE_HOLE); |
| 377 if (isolate_) { |
| 378 value->Internalize(isolate_); |
| 379 } |
| 380 values_.Add(value); |
| 381 return value; |
| 382 } |
| 383 |
| 384 |
| 385 const AstString* AstValueFactory::GetString( |
| 386 uint32_t hash, bool is_one_byte, const Vector<const byte>& literal_bytes) { |
| 387 // literal_bytes here points to whatever the user passed, and this is OK |
| 388 // because we use vector_compare (which checks the contents) to compare |
| 389 // against the AstStrings which are in the string_table_. We should not return |
| 390 // this AstString. |
| 391 AstString key(is_one_byte, literal_bytes, hash); |
| 392 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true); |
| 393 if (entry->value == NULL) { |
| 394 // Copy literal contents for later comparison. |
| 395 int length = literal_bytes.length(); |
| 396 byte* new_literal_bytes = zone_->NewArray<byte>(length); |
| 397 memcpy(new_literal_bytes, literal_bytes.start(), length); |
| 398 AstString* new_string = new (zone_) AstString( |
| 399 is_one_byte, Vector<const byte>(new_literal_bytes, length), hash); |
| 400 entry->key = new_string; |
| 401 strings_.Add(new_string); |
| 402 if (isolate_) { |
| 403 new_string->Internalize(isolate_); |
| 404 } |
| 405 entry->value = reinterpret_cast<void*>(1); |
| 406 } |
| 407 return reinterpret_cast<AstString*>(entry->key); |
| 408 } |
| 409 |
| 410 |
| 411 } } // namespace v8::internal |
OLD | NEW |