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

Side by Side Diff: src/runtime.cc

Issue 669060: Add runtime function for string to array conversion. (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
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 4195 matching lines...) Expand 10 before | Expand all | Expand 10 after
4206 4206
4207 int right = length; 4207 int right = length;
4208 if (trimRight) { 4208 if (trimRight) {
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
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
4219 // not in the cache. Returns the length of the successfully copied
4220 // prefix.
4221 static int CopyCachedAsciiCharsToArray(const char* chars,
4222 FixedArray* elements,
4223 int length) {
4224 AssertNoAllocation nogc;
4225 FixedArray* ascii_cache = Heap::single_character_string_cache();
4226 Object* undefined = Heap::undefined_value();
4227 for (int i = 0; i < length; ++i) {
4228 Object* value = ascii_cache->get(chars[i]);
4229 if (value == undefined) return i;
4230 ASSERT(!Heap::InNewSpace(value));
4231 elements->set(i, value, SKIP_WRITE_BARRIER);
4232 }
4233 return length;
4234 }
4235
4236
4237 // Converts a String to JSArray.
4238 // For example, "foo" => ["f", "o", "o"].
4239 static Object* Runtime_StringToArray(Arguments args) {
4240 HandleScope scope;
4241 ASSERT(args.length() == 1);
4242 CONVERT_ARG_CHECKED(String, s, 0);
4243
4244 s->TryFlatten();
4245 const int length = s->length();
4246
4247 Handle<FixedArray> elements = Factory::NewUninitializedFixedArray(length);
4248 if (s->IsFlat()) {
4249 if (s->IsAsciiRepresentation()) {
4250 Vector<const char> chars = s->ToAsciiVector();
4251 int num_copied_from_cache = CopyCachedAsciiCharsToArray(chars.start(),
4252 *elements,
4253 length);
4254 for (int i = num_copied_from_cache; i < length; ++i) {
4255 elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]),
4256 UPDATE_WRITE_BARRIER);
Mads Ager (chromium) 2010/03/04 13:30:56 Just omit the WriteBarrierMode here since the defa
Vitaly Repeshko 2010/03/04 14:03:12 Done.
4257 }
4258 } else {
4259 ASSERT(s->IsTwoByteRepresentation());
4260 Vector<const uc16> chars = s->ToUC16Vector();
4261 for (int i = 0; i < length; ++i) {
4262 elements->set(i, *LookupSingleCharacterStringFromCode(chars[i]),
4263 UPDATE_WRITE_BARRIER);
4264 }
4265 }
4266 } else {
4267 for (int i = 0; i < length; ++i) {
4268 elements->set(i, *LookupSingleCharacterStringFromCode(s->Get(i)),
4269 UPDATE_WRITE_BARRIER);
4270 }
4271 }
4272
4273 #ifdef DEBUG
4274 for (int i = 0; i < length; ++i) {
4275 ASSERT(String::cast(elements->get(i))->length() == 1);
4276 }
4277 #endif
4278
4279 return *Factory::NewJSArrayWithElements(elements);
4280 }
4281
4282
4216 bool Runtime::IsUpperCaseChar(uint16_t ch) { 4283 bool Runtime::IsUpperCaseChar(uint16_t ch) {
4217 unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth]; 4284 unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth];
4218 int char_length = to_upper_mapping.get(ch, 0, chars); 4285 int char_length = to_upper_mapping.get(ch, 0, chars);
4219 return char_length == 0; 4286 return char_length == 0;
4220 } 4287 }
4221 4288
4222 4289
4223 static Object* Runtime_NumberToString(Arguments args) { 4290 static Object* Runtime_NumberToString(Arguments args) {
4224 NoHandleAllocation ha; 4291 NoHandleAllocation ha;
4225 ASSERT(args.length() == 1); 4292 ASSERT(args.length() == 1);
(...skipping 4256 matching lines...) Expand 10 before | Expand all | Expand 10 after
8482 } else { 8549 } else {
8483 // Handle last resort GC and make sure to allow future allocations 8550 // Handle last resort GC and make sure to allow future allocations
8484 // to grow the heap without causing GCs (if possible). 8551 // to grow the heap without causing GCs (if possible).
8485 Counters::gc_last_resort_from_js.Increment(); 8552 Counters::gc_last_resort_from_js.Increment();
8486 Heap::CollectAllGarbage(false); 8553 Heap::CollectAllGarbage(false);
8487 } 8554 }
8488 } 8555 }
8489 8556
8490 8557
8491 } } // namespace v8::internal 8558 } } // namespace v8::internal
OLDNEW
« src/factory.cc ('K') | « src/runtime.h ('k') | src/string.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698