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