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

Side by Side Diff: src/runtime.cc

Issue 669156: Removed dangerous Factory::NewUninitializedFixedArray. (Closed)
Patch Set: Created 10 years, 9 months 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
« no previous file with comments | « src/factory.cc ('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 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 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 4198 matching lines...) Expand 10 before | Expand all | Expand 10 after
4209 while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) { 4209 while (right > left && IsTrimWhiteSpace(s->Get(right - 1))) {
4210 right--; 4210 right--;
4211 } 4211 }
4212 } 4212 }
4213 return s->SubString(left, right); 4213 return s->SubString(left, right);
4214 } 4214 }
4215 4215
4216 4216
4217 // Copies ascii characters to the given fixed array looking up 4217 // Copies ascii characters to the given fixed array looking up
4218 // one-char strings in the cache. Gives up on the first char that is 4218 // one-char strings in the cache. Gives up on the first char that is
4219 // not in the cache. Returns the length of the successfully copied 4219 // not in the cache and fills the remainder with smi zeros. Returns
4220 // prefix. 4220 // the length of the successfully copied prefix.
4221 static int CopyCachedAsciiCharsToArray(const char* chars, 4221 static int CopyCachedAsciiCharsToArray(const char* chars,
4222 FixedArray* elements, 4222 FixedArray* elements,
4223 int length) { 4223 int length) {
4224 AssertNoAllocation nogc; 4224 AssertNoAllocation nogc;
4225 FixedArray* ascii_cache = Heap::single_character_string_cache(); 4225 FixedArray* ascii_cache = Heap::single_character_string_cache();
4226 Object* undefined = Heap::undefined_value(); 4226 Object* undefined = Heap::undefined_value();
4227 for (int i = 0; i < length; ++i) { 4227 int i;
4228 for (i = 0; i < length; ++i) {
4228 Object* value = ascii_cache->get(chars[i]); 4229 Object* value = ascii_cache->get(chars[i]);
4229 if (value == undefined) return i; 4230 if (value == undefined) break;
4230 ASSERT(!Heap::InNewSpace(value)); 4231 ASSERT(!Heap::InNewSpace(value));
4231 elements->set(i, value, SKIP_WRITE_BARRIER); 4232 elements->set(i, value, SKIP_WRITE_BARRIER);
4232 } 4233 }
4233 return length; 4234 if (i < length) {
antonm 2010/03/05 12:18:05 maybe lift this filling into the loop itself: if
Vitaly Repeshko 2010/03/05 12:32:10 I'd like to keep the fast loop as simple as possib
4235 ASSERT(kSmiTag == 0);
4236 memset(elements->data_start() + i, 0, length - i);
antonm 2010/03/05 12:18:05 not insisting, but something like Smi::FromInt(0)
Vitaly Repeshko 2010/03/05 12:32:10 Done.
4237 }
4238 #ifdef DEBUG
4239 for (int j = 0; j < length; ++j) {
4240 Object* element = elements->get(j);
4241 ASSERT(element == Smi::FromInt(0) ||
4242 (element->IsString() && String::cast(element)->LooksValid()));
4243 }
4244 #endif
4245 return i;
4234 } 4246 }
4235 4247
4236 4248
4237 // Converts a String to JSArray. 4249 // Converts a String to JSArray.
4238 // For example, "foo" => ["f", "o", "o"]. 4250 // For example, "foo" => ["f", "o", "o"].
4239 static Object* Runtime_StringToArray(Arguments args) { 4251 static Object* Runtime_StringToArray(Arguments args) {
4240 HandleScope scope; 4252 HandleScope scope;
4241 ASSERT(args.length() == 1); 4253 ASSERT(args.length() == 1);
4242 CONVERT_ARG_CHECKED(String, s, 0); 4254 CONVERT_ARG_CHECKED(String, s, 0);
4243 4255
4244 s->TryFlatten(); 4256 s->TryFlatten();
4245 const int length = s->length(); 4257 const int length = s->length();
4246 4258
4247 Handle<FixedArray> elements = Factory::NewUninitializedFixedArray(length); 4259 Handle<FixedArray> elements;
4248 if (s->IsFlat()) { 4260 if (s->IsFlat() && s->IsAsciiRepresentation()) {
4249 if (s->IsAsciiRepresentation()) { 4261 Object* obj = Heap::AllocateUninitializedFixedArray(length);
antonm 2010/03/05 12:18:05 maybe add a comment that uninitialized array will
Vitaly Repeshko 2010/03/05 12:32:10 Done.
4250 Vector<const char> chars = s->ToAsciiVector(); 4262 if (obj->IsFailure()) return obj;
4251 int num_copied_from_cache = CopyCachedAsciiCharsToArray(chars.start(), 4263 FixedArray* raw_elements = FixedArray::cast(obj);
Mads Ager (chromium) 2010/03/05 12:17:16 Let's put elements in a handle right away here and
Vitaly Repeshko 2010/03/05 12:32:10 Done.
4252 *elements, 4264
4253 length); 4265 Vector<const char> chars = s->ToAsciiVector();
4254 for (int i = num_copied_from_cache; i < length; ++i) { 4266 int num_copied_from_cache = CopyCachedAsciiCharsToArray(chars.start(),
4255 elements->set(i, *LookupSingleCharacterStringFromCode(chars[i])); 4267 raw_elements,
4256 } 4268 length);
4257 } else { 4269
4258 ASSERT(s->IsTwoByteRepresentation()); 4270 elements = Handle<FixedArray>(raw_elements);
4259 Vector<const uc16> chars = s->ToUC16Vector(); 4271 for (int i = num_copied_from_cache; i < length; ++i) {
4260 for (int i = 0; i < length; ++i) { 4272 elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]));
4261 elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]));
4262 }
4263 } 4273 }
4264 } else { 4274 } else {
4275 elements = Factory::NewFixedArray(length);
4265 for (int i = 0; i < length; ++i) { 4276 for (int i = 0; i < length; ++i) {
4266 elements->set(i, *LookupSingleCharacterStringFromCode(s->Get(i))); 4277 elements->set(i, *LookupSingleCharacterStringFromCode(s->Get(i)));
4267 } 4278 }
4268 } 4279 }
4269 4280
4270 #ifdef DEBUG 4281 #ifdef DEBUG
4271 for (int i = 0; i < length; ++i) { 4282 for (int i = 0; i < length; ++i) {
4272 ASSERT(String::cast(elements->get(i))->length() == 1); 4283 ASSERT(String::cast(elements->get(i))->length() == 1);
4273 } 4284 }
4274 #endif 4285 #endif
(...skipping 4271 matching lines...) Expand 10 before | Expand all | Expand 10 after
8546 } else { 8557 } else {
8547 // Handle last resort GC and make sure to allow future allocations 8558 // Handle last resort GC and make sure to allow future allocations
8548 // to grow the heap without causing GCs (if possible). 8559 // to grow the heap without causing GCs (if possible).
8549 Counters::gc_last_resort_from_js.Increment(); 8560 Counters::gc_last_resort_from_js.Increment();
8550 Heap::CollectAllGarbage(false); 8561 Heap::CollectAllGarbage(false);
8551 } 8562 }
8552 } 8563 }
8553 8564
8554 8565
8555 } } // namespace v8::internal 8566 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/factory.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698