Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(706)

Side by Side Diff: src/ast-value-factory.cc

Issue 699343004: Parser & internalization fix: ensure no heap allocs during GetString(Handle<String>). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: . Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ast-value-factory.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 1 // Copyright 2014 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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 case THE_HOLE: 205 case THE_HOLE:
206 value_ = isolate->factory()->the_hole_value(); 206 value_ = isolate->factory()->the_hole_value();
207 break; 207 break;
208 case UNDEFINED: 208 case UNDEFINED:
209 value_ = isolate->factory()->undefined_value(); 209 value_ = isolate->factory()->undefined_value();
210 break; 210 break;
211 } 211 }
212 } 212 }
213 213
214 214
215 const AstRawString* AstValueFactory::GetOneByteString( 215 AstRawString* AstValueFactory::GetOneByteStringInternal(
216 Vector<const uint8_t> literal) { 216 Vector<const uint8_t> literal) {
217 uint32_t hash = StringHasher::HashSequentialString<uint8_t>( 217 uint32_t hash = StringHasher::HashSequentialString<uint8_t>(
218 literal.start(), literal.length(), hash_seed_); 218 literal.start(), literal.length(), hash_seed_);
219 return GetString(hash, true, literal); 219 return GetString(hash, true, literal);
220 } 220 }
221 221
222 222
223 const AstRawString* AstValueFactory::GetTwoByteString( 223 AstRawString* AstValueFactory::GetTwoByteStringInternal(
224 Vector<const uint16_t> literal) { 224 Vector<const uint16_t> literal) {
225 uint32_t hash = StringHasher::HashSequentialString<uint16_t>( 225 uint32_t hash = StringHasher::HashSequentialString<uint16_t>(
226 literal.start(), literal.length(), hash_seed_); 226 literal.start(), literal.length(), hash_seed_);
227 return GetString(hash, false, Vector<const byte>::cast(literal)); 227 return GetString(hash, false, Vector<const byte>::cast(literal));
228 } 228 }
229 229
230 230
231 const AstRawString* AstValueFactory::GetString(Handle<String> literal) { 231 const AstRawString* AstValueFactory::GetString(Handle<String> literal) {
232 // For the FlatContent to stay valid, we shouldn't do any heap
233 // allocation. Make sure we won't try to internalize the string in GetString.
234 AstRawString* result = NULL;
235 Isolate* saved_isolate = isolate_;
236 isolate_ = NULL;
232 DisallowHeapAllocation no_gc; 237 DisallowHeapAllocation no_gc;
233 String::FlatContent content = literal->GetFlatContent(); 238 String::FlatContent content = literal->GetFlatContent();
234 if (content.IsOneByte()) { 239 if (content.IsOneByte()) {
235 return GetOneByteString(content.ToOneByteVector()); 240 result = GetOneByteStringInternal(content.ToOneByteVector());
241 } else {
242 DCHECK(content.IsTwoByte());
243 result = GetTwoByteStringInternal(content.ToUC16Vector());
236 } 244 }
237 DCHECK(content.IsTwoByte()); 245 isolate_ = saved_isolate;
238 return GetTwoByteString(content.ToUC16Vector()); 246 result->string_ = literal;
247 return result;
239 } 248 }
240 249
241 250
242 const AstConsString* AstValueFactory::NewConsString( 251 const AstConsString* AstValueFactory::NewConsString(
243 const AstString* left, const AstString* right) { 252 const AstString* left, const AstString* right) {
244 // This Vector will be valid as long as the Collector is alive (meaning that 253 // This Vector will be valid as long as the Collector is alive (meaning that
245 // the AstRawString will not be moved). 254 // the AstRawString will not be moved).
246 AstConsString* new_string = new (zone_) AstConsString(left, right); 255 AstConsString* new_string = new (zone_) AstConsString(left, right);
247 strings_.Add(new_string); 256 strings_.Add(new_string);
248 if (isolate_) { 257 if (isolate_) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 } 350 }
342 351
343 352
344 const AstValue* AstValueFactory::NewTheHole() { 353 const AstValue* AstValueFactory::NewTheHole() {
345 GENERATE_VALUE_GETTER(the_hole_value_, AstValue::THE_HOLE); 354 GENERATE_VALUE_GETTER(the_hole_value_, AstValue::THE_HOLE);
346 } 355 }
347 356
348 357
349 #undef GENERATE_VALUE_GETTER 358 #undef GENERATE_VALUE_GETTER
350 359
351 const AstRawString* AstValueFactory::GetString( 360 AstRawString* AstValueFactory::GetString(uint32_t hash, bool is_one_byte,
352 uint32_t hash, bool is_one_byte, Vector<const byte> literal_bytes) { 361 Vector<const byte> literal_bytes) {
353 // literal_bytes here points to whatever the user passed, and this is OK 362 // literal_bytes here points to whatever the user passed, and this is OK
354 // because we use vector_compare (which checks the contents) to compare 363 // because we use vector_compare (which checks the contents) to compare
355 // against the AstRawStrings which are in the string_table_. We should not 364 // against the AstRawStrings which are in the string_table_. We should not
356 // return this AstRawString. 365 // return this AstRawString.
357 AstRawString key(is_one_byte, literal_bytes, hash); 366 AstRawString key(is_one_byte, literal_bytes, hash);
358 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true); 367 HashMap::Entry* entry = string_table_.Lookup(&key, hash, true);
359 if (entry->value == NULL) { 368 if (entry->value == NULL) {
360 // Copy literal contents for later comparison. 369 // Copy literal contents for later comparison.
361 int length = literal_bytes.length(); 370 int length = literal_bytes.length();
362 byte* new_literal_bytes = zone_->NewArray<byte>(length); 371 byte* new_literal_bytes = zone_->NewArray<byte>(length);
363 memcpy(new_literal_bytes, literal_bytes.start(), length); 372 memcpy(new_literal_bytes, literal_bytes.start(), length);
364 AstRawString* new_string = new (zone_) AstRawString( 373 AstRawString* new_string = new (zone_) AstRawString(
365 is_one_byte, Vector<const byte>(new_literal_bytes, length), hash); 374 is_one_byte, Vector<const byte>(new_literal_bytes, length), hash);
366 entry->key = new_string; 375 entry->key = new_string;
367 strings_.Add(new_string); 376 strings_.Add(new_string);
368 if (isolate_) { 377 if (isolate_) {
369 new_string->Internalize(isolate_); 378 new_string->Internalize(isolate_);
370 } 379 }
371 entry->value = reinterpret_cast<void*>(1); 380 entry->value = reinterpret_cast<void*>(1);
372 } 381 }
373 return reinterpret_cast<AstRawString*>(entry->key); 382 return reinterpret_cast<AstRawString*>(entry->key);
374 } 383 }
375 384
376 385
377 } } // namespace v8::internal 386 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/ast-value-factory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698