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