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

Side by Side Diff: src/runtime.cc

Issue 5551002: Simplify JSON stringify and add special case for default replacer and space. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years 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/json.js ('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 4601 matching lines...) Expand 10 before | Expand all | Expand 10 after
4612 int length = characters.length(); 4612 int length = characters.length();
4613 int quoted_length = 0; 4613 int quoted_length = 0;
4614 for (int i = 0; i < length; i++) { 4614 for (int i = 0; i < length; i++) {
4615 unsigned int c = characters[i]; 4615 unsigned int c = characters[i];
4616 if (sizeof(Char) > 1u) { 4616 if (sizeof(Char) > 1u) {
4617 quoted_length += (c >= kQuoteTableLength) ? 1 : JsonQuoteLengths[c]; 4617 quoted_length += (c >= kQuoteTableLength) ? 1 : JsonQuoteLengths[c];
4618 } else { 4618 } else {
4619 quoted_length += JsonQuoteLengths[c]; 4619 quoted_length += JsonQuoteLengths[c];
4620 } 4620 }
4621 } 4621 }
4622 if (quoted_length == length) {
4623 return Heap::undefined_value();
4624 }
4625 Counters::quote_json_char_count.Increment(length); 4622 Counters::quote_json_char_count.Increment(length);
4626 4623
4627 // Add space for quotes. 4624 // Add space for quotes.
4628 quoted_length += 2; 4625 quoted_length += 2;
4629 4626
4630 MaybeObject* new_alloc = AllocateRawString<StringType>(quoted_length); 4627 MaybeObject* new_alloc = AllocateRawString<StringType>(quoted_length);
4631 Object* new_object; 4628 Object* new_object;
4632 if (!new_alloc->ToObject(&new_object)) { 4629 if (!new_alloc->ToObject(&new_object)) {
4633 Counters::quote_json_char_recount.Increment(length); 4630 Counters::quote_json_char_recount.Increment(length);
4634 return new_alloc; 4631 return new_alloc;
4635 } 4632 }
4636 StringType* new_string = StringType::cast(new_object); 4633 StringType* new_string = StringType::cast(new_object);
4637 4634
4638 4635
4639 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize); 4636 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
4640 Char* write_cursor = reinterpret_cast<Char*>( 4637 Char* write_cursor = reinterpret_cast<Char*>(
4641 new_string->address() + SeqAsciiString::kHeaderSize); 4638 new_string->address() + SeqAsciiString::kHeaderSize);
4642 *(write_cursor++) = '"'; 4639 *(write_cursor++) = '"';
4643 const Char* read_cursor = characters.start(); 4640 const Char* read_cursor = characters.start();
4644 const Char* end = read_cursor + length; 4641 if (quoted_length == length + 2) {
4645 while (read_cursor < end) { 4642 CopyChars(write_cursor, read_cursor, length);
4646 Char c = *(read_cursor++); 4643 write_cursor += length;
4647 if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) { 4644 } else {
4648 *(write_cursor++) = c; 4645 const Char* end = read_cursor + length;
4649 } else { 4646 while (read_cursor < end) {
4650 const char* replacement = JsonQuotes[static_cast<unsigned>(c)]; 4647 Char c = *(read_cursor++);
4651 if (!replacement) { 4648 if (sizeof(Char) > 1u && static_cast<unsigned>(c) >= kQuoteTableLength) {
4652 *(write_cursor++) = c; 4649 *(write_cursor++) = c;
4653 } else { 4650 } else {
4654 write_cursor = WriteString(write_cursor, replacement); 4651 const char* replacement = JsonQuotes[static_cast<unsigned>(c)];
4652 if (!replacement) {
4653 *(write_cursor++) = c;
4654 } else {
4655 write_cursor = WriteString(write_cursor, replacement);
4656 }
4655 } 4657 }
4656 } 4658 }
4657 } 4659 }
4658 *(write_cursor++) = '"'; 4660 *(write_cursor++) = '"';
4659 ASSERT_EQ(SeqAsciiString::kHeaderSize + quoted_length * sizeof(Char), 4661 ASSERT_EQ(SeqAsciiString::kHeaderSize + quoted_length * sizeof(Char),
4660 reinterpret_cast<Address>(write_cursor) - new_string->address()); 4662 reinterpret_cast<Address>(write_cursor) - new_string->address());
4661 return new_string; 4663 return new_string;
4662 } 4664 }
4663 4665
4664 4666
(...skipping 10 matching lines...) Expand all
4675 ASSERT(str->IsFlat()); 4677 ASSERT(str->IsFlat());
4676 } 4678 }
4677 if (str->IsTwoByteRepresentation()) { 4679 if (str->IsTwoByteRepresentation()) {
4678 return QuoteJsonString<uc16, SeqTwoByteString>(str->ToUC16Vector()); 4680 return QuoteJsonString<uc16, SeqTwoByteString>(str->ToUC16Vector());
4679 } else { 4681 } else {
4680 return QuoteJsonString<char, SeqAsciiString>(str->ToAsciiVector()); 4682 return QuoteJsonString<char, SeqAsciiString>(str->ToAsciiVector());
4681 } 4683 }
4682 } 4684 }
4683 4685
4684 4686
4687
4685 static MaybeObject* Runtime_StringParseInt(Arguments args) { 4688 static MaybeObject* Runtime_StringParseInt(Arguments args) {
4686 NoHandleAllocation ha; 4689 NoHandleAllocation ha;
4687 4690
4688 CONVERT_CHECKED(String, s, args[0]); 4691 CONVERT_CHECKED(String, s, args[0]);
4689 CONVERT_SMI_CHECKED(radix, args[1]); 4692 CONVERT_SMI_CHECKED(radix, args[1]);
4690 4693
4691 s->TryFlatten(); 4694 s->TryFlatten();
4692 4695
4693 RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); 4696 RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36));
4694 double value = StringToInt(s, radix); 4697 double value = StringToInt(s, radix);
(...skipping 5788 matching lines...) Expand 10 before | Expand all | Expand 10 after
10483 } else { 10486 } else {
10484 // Handle last resort GC and make sure to allow future allocations 10487 // Handle last resort GC and make sure to allow future allocations
10485 // to grow the heap without causing GCs (if possible). 10488 // to grow the heap without causing GCs (if possible).
10486 Counters::gc_last_resort_from_js.Increment(); 10489 Counters::gc_last_resort_from_js.Increment();
10487 Heap::CollectAllGarbage(false); 10490 Heap::CollectAllGarbage(false);
10488 } 10491 }
10489 } 10492 }
10490 10493
10491 10494
10492 } } // namespace v8::internal 10495 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/json.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698