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

Side by Side Diff: src/json-stringifier.h

Issue 11189112: Reland JSON.stringify reimplementation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: makev8 ia32.release.check Created 8 years, 2 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 | Annotate | Revision Log
« no previous file with comments | « src/json.js ('k') | src/runtime.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2012 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 #ifndef V8_JSON_STRINGIFIER_H_
29 #define V8_JSON_STRINGIFIER_H_
30
31 #include "v8.h"
32 #include "v8utils.h"
33 #include "v8conversions.h"
34
35 namespace v8 {
36 namespace internal {
37
38 class BasicJsonStringifier BASE_EMBEDDED {
39 public:
40 explicit BasicJsonStringifier(Isolate* isolate);
41
42 MaybeObject* Stringify(Handle<Object> object);
43
44 private:
45 static const int kInitialPartLength = 32;
46 static const int kMaxPartLength = 16 * 1024;
47 static const int kPartLengthGrowthFactor = 2;
48 static const int kStackLimit = 8 * 1024;
49
50 enum Result { UNCHANGED, SUCCESS, BAILOUT, CIRCULAR, STACK_OVERFLOW };
51
52 template <bool is_ascii> void Extend();
53
54 void ChangeEncoding();
55
56 void ShrinkCurrentPart();
57
58 template <bool is_ascii, typename Char>
59 INLINE(void Append_(Char c));
60
61 template <bool is_ascii, typename Char>
62 INLINE(void AppendUnchecked_(Char c));
63
64 template <bool is_ascii, typename Char>
65 INLINE(void Append_(const Char* chars));
66
67 template <bool is_ascii, typename Char>
68 INLINE(void AppendUnchecked_(const Char* chars));
69
70 INLINE(void Append(char c)) {
71 if (is_ascii_) {
72 Append_<true>(c);
73 } else {
74 Append_<false>(c);
75 }
76 }
77
78 INLINE(void Append(const char* chars)) {
79 if (is_ascii_) {
80 Append_<true>(chars);
81 } else {
82 Append_<false>(chars);
83 }
84 }
85
86 INLINE(Handle<Object> GetProperty(Handle<JSObject> object,
87 Handle<String> key));
88
89 INLINE(bool MayHaveToJsonFunction(Handle<JSObject> object));
90
91 INLINE(Result Serialize(Handle<Object> object)) {
92 return Serialize_<false>(object);
93 }
94
95 INLINE(Result SerializeDeferred(Handle<Object> object,
96 bool deferred_comma,
97 Handle<String> deferred_key)) {
98 ASSERT(!deferred_key.is_null());
99 return Serialize_<true>(object, deferred_comma, deferred_key);
100 }
101
102 template <bool deferred_key>
103 Result Serialize_(Handle<Object> object,
104 bool comma = false,
105 Handle<String> key = Handle<String>::null());
106
107 INLINE(void SerializeDeferredKey(bool deferred_comma,
108 Handle<String> deferred_key)) {
109 if (deferred_comma) Append(',');
110 SerializeString(deferred_key);
111 Append(':');
112 }
113
114 INLINE(Result SerializeSmi(Smi* object));
115
116 INLINE(Result SerializeDouble(double number));
117 INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) {
118 return SerializeDouble(object->value());
119 }
120
121 Result SerializeArray(Handle<JSArray> object);
122 Result SerializeObject(Handle<JSObject> object);
123
124 void SerializeString(Handle<String> object);
125
126 template <bool is_ascii, typename Char>
127 INLINE(void SerializeString_(Vector<const Char> vector,
128 Handle<String> string));
129
130 template <typename Char>
131 INLINE(Vector<const Char> GetCharVector(Handle<String> string));
132
133 INLINE(Result StackPush(Handle<Object> object));
134 INLINE(void StackPop());
135
136 INLINE(Handle<String> accumulator()) {
137 return Handle<String>(String::cast(accumulator_store_->value()));
138 }
139
140 INLINE(void set_accumulator(Handle<String> string)) {
141 return accumulator_store_->set_value(*string);
142 }
143
144 Isolate* isolate_;
145 // We use a value wrapper for the string accumulator to keep the
146 // (indirect) handle to it in the outermost handle scope.
147 Handle<JSValue> accumulator_store_;
148 Handle<String> current_part_;
149 Handle<String> tojson_symbol_;
150 Handle<JSArray> stack_;
151 int current_index_;
152 int part_length_;
153 bool is_ascii_;
154
155 static const int kJsonQuotesCharactersPerEntry = 8;
156 static const char* const JsonQuotes;
157 };
158
159
160 const char* const BasicJsonStringifier::JsonQuotes =
161 "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 "
162 "\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 "
163 "\\b\0 \\t\0 \\n\0 \\u000b\0 "
164 "\\f\0 \\r\0 \\u000e\0 \\u000f\0 "
165 "\\u0010\0 \\u0011\0 \\u0012\0 \\u0013\0 "
166 "\\u0014\0 \\u0015\0 \\u0016\0 \\u0017\0 "
167 "\\u0018\0 \\u0019\0 \\u001a\0 \\u001b\0 "
168 "\\u001c\0 \\u001d\0 \\u001e\0 \\u001f\0 "
169 " \0 !\0 \\\"\0 #\0 "
170 "$\0 %\0 &\0 '\0 "
171 "(\0 )\0 *\0 +\0 "
172 ",\0 -\0 .\0 /\0 "
173 "0\0 1\0 2\0 3\0 "
174 "4\0 5\0 6\0 7\0 "
175 "8\0 9\0 :\0 ;\0 "
176 "<\0 =\0 >\0 ?\0 "
177 "@\0 A\0 B\0 C\0 "
178 "D\0 E\0 F\0 G\0 "
179 "H\0 I\0 J\0 K\0 "
180 "L\0 M\0 N\0 O\0 "
181 "P\0 Q\0 R\0 S\0 "
182 "T\0 U\0 V\0 W\0 "
183 "X\0 Y\0 Z\0 [\0 "
184 "\\\\\0 ]\0 ^\0 _\0 "
185 "`\0 a\0 b\0 c\0 "
186 "d\0 e\0 f\0 g\0 "
187 "h\0 i\0 j\0 k\0 "
188 "l\0 m\0 n\0 o\0 "
189 "p\0 q\0 r\0 s\0 "
190 "t\0 u\0 v\0 w\0 "
191 "x\0 y\0 z\0 {\0 "
192 "|\0 }\0 ~\0 \177\0 ";
193
194
195 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
196 : isolate_(isolate), current_index_(0), is_ascii_(true) {
197 accumulator_store_ = Handle<JSValue>::cast(
198 isolate_->factory()->ToObject(isolate_->factory()->empty_string()));
199 part_length_ = kInitialPartLength;
200 current_part_ =
201 isolate_->factory()->NewRawAsciiString(kInitialPartLength);
202 tojson_symbol_ = isolate_->factory()->LookupAsciiSymbol("toJSON");
203 stack_ = isolate_->factory()->NewJSArray(8);
204 }
205
206
207 MaybeObject* BasicJsonStringifier::Stringify(Handle<Object> object) {
208 switch (Serialize(object)) {
209 case SUCCESS:
210 ShrinkCurrentPart();
211 return *isolate_->factory()->NewConsString(accumulator(), current_part_);
212 case UNCHANGED:
213 return isolate_->heap()->undefined_value();
214 case CIRCULAR:
215 return isolate_->Throw(*isolate_->factory()->NewTypeError(
216 "circular_structure", HandleVector<Object>(NULL, 0)));
217 case STACK_OVERFLOW:
218 return isolate_->StackOverflow();
219 default:
220 return Smi::FromInt(0);
221 }
222 }
223
224
225 template <bool is_ascii, typename Char>
226 void BasicJsonStringifier::Append_(Char c) {
227 if (is_ascii) {
228 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet(
229 current_index_++, c);
230 } else {
231 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet(
232 current_index_++, c);
233 }
234 if (current_index_ == part_length_) Extend<is_ascii>();
235 }
236
237
238 template <bool is_ascii, typename Char>
239 void BasicJsonStringifier::AppendUnchecked_(Char c) {
240 if (is_ascii) {
241 SeqAsciiString::cast(*current_part_)->SeqAsciiStringSet(
242 current_index_++, c);
243 } else {
244 SeqTwoByteString::cast(*current_part_)->SeqTwoByteStringSet(
245 current_index_++, c);
246 }
247 ASSERT(current_index_ < part_length_);
248 }
249
250
251 template <bool is_ascii, typename Char>
252 void BasicJsonStringifier::Append_(const Char* chars) {
253 for ( ; *chars != '\0'; chars++) Append_<is_ascii, Char>(*chars);
254 }
255
256
257 template <bool is_ascii, typename Char>
258 void BasicJsonStringifier::AppendUnchecked_(const Char* chars) {
259 for ( ; *chars != '\0'; chars++) AppendUnchecked_<is_ascii, Char>(*chars);
260 }
261
262
263 Handle<Object> BasicJsonStringifier::GetProperty(Handle<JSObject> object,
264 Handle<String> key) {
265 LookupResult lookup(isolate_);
266 object->LocalLookupRealNamedProperty(*key, &lookup);
267 if (!lookup.IsProperty()) return isolate_->factory()->undefined_value();
268 switch (lookup.type()) {
269 case NORMAL: {
270 Object* value = lookup.holder()->GetNormalizedProperty(&lookup);
271 ASSERT(!value->IsTheHole());
272 return Handle<Object>(value);
273 }
274 case FIELD: {
275 Object* value = lookup.holder()->FastPropertyAt(lookup.GetFieldIndex());
276 ASSERT(!value->IsTheHole());
277 return Handle<Object>(value);
278 }
279 case CONSTANT_FUNCTION:
280 return Handle<Object>(lookup.GetConstantFunction());
281 case CALLBACKS:
282 case HANDLER:
283 case INTERCEPTOR:
284 return Handle<Object>::null();
285 case TRANSITION:
286 case NONEXISTENT:
287 UNREACHABLE();
288 break;
289 }
290 return Handle<Object>::null();
291 }
292
293
294 bool BasicJsonStringifier::MayHaveToJsonFunction(Handle<JSObject> object) {
295 LookupResult lookup(isolate_);
296 object->LookupRealNamedProperty(*tojson_symbol_, &lookup);
297 if (!lookup.IsProperty()) return false;
298 Object* value;
299 switch (lookup.type()) {
300 case NORMAL:
301 value = lookup.holder()->GetNormalizedProperty(&lookup);
302 break;
303 case FIELD:
304 value = lookup.holder()->FastPropertyAt(lookup.GetFieldIndex());
305 break;
306 default:
307 return true;
308 }
309 ASSERT(!value->IsTheHole());
310 return value->IsSpecFunction();
311 }
312
313
314 BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
315 Handle<Object> object) {
316 int length = Smi::cast(stack_->length())->value();
317 if (length > kStackLimit) return STACK_OVERFLOW;
318 FixedArray* elements = FixedArray::cast(stack_->elements());
319 for (int i = 0; i < length; i++) {
320 if (elements->get(i) == *object) {
321 return CIRCULAR;
322 }
323 }
324 stack_->EnsureSize(length + 1);
325 FixedArray::cast(stack_->elements())->set(length, *object);
326 stack_->set_length(Smi::FromInt(length + 1));
327 return SUCCESS;
328 }
329
330
331 void BasicJsonStringifier::StackPop() {
332 int length = Smi::cast(stack_->length())->value();
333 stack_->set_length(Smi::FromInt(length - 1));
334 }
335
336
337 template <bool deferred_key>
338 BasicJsonStringifier::Result BasicJsonStringifier::Serialize_(
339 Handle<Object> object, bool comma, Handle<String> key) {
340 if (object->IsJSObject()) {
341 // We don't deal with custom toJSON functions.
342 if (MayHaveToJsonFunction(Handle<JSObject>::cast(object))) return BAILOUT;
343
344 if (object->IsJSFunction()) {
345 return UNCHANGED;
346 } else if (object->IsJSArray()) {
347 if (deferred_key) SerializeDeferredKey(comma, key);
348 return SerializeArray(Handle<JSArray>::cast(object));
349 } else if (object->IsJSValue()) {
350 // JSValue with a custom prototype.
351 if (object->GetPrototype()->IsJSReceiver()) return BAILOUT;
352 // Unpack value wrapper and fall through.
353 object = Handle<Object>(JSValue::cast(*object)->value());
354 } else {
355 if (deferred_key) SerializeDeferredKey(comma, key);
356 return SerializeObject(Handle<JSObject>::cast(object));
357 }
358 }
359
360 if (object->IsString()) {
361 if (deferred_key) SerializeDeferredKey(comma, key);
362 SerializeString(Handle<String>::cast(object));
363 return SUCCESS;
364 } else if (object->IsSmi()) {
365 if (deferred_key) SerializeDeferredKey(comma, key);
366 return SerializeSmi(Smi::cast(*object));
367 } else if (object->IsHeapNumber()) {
368 if (deferred_key) SerializeDeferredKey(comma, key);
369 return SerializeHeapNumber(Handle<HeapNumber>::cast(object));
370 } else if (object->IsOddball()) {
371 switch (Oddball::cast(*object)->kind()) {
372 case Oddball::kFalse:
373 if (deferred_key) SerializeDeferredKey(comma, key);
374 Append("false");
375 return SUCCESS;
376 case Oddball::kTrue:
377 if (deferred_key) SerializeDeferredKey(comma, key);
378 Append("true");
379 return SUCCESS;
380 case Oddball::kNull:
381 if (deferred_key) SerializeDeferredKey(comma, key);
382 Append("null");
383 return SUCCESS;
384 }
385 }
386
387 return UNCHANGED;
388 }
389
390
391 BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
392 static const int kBufferSize = 100;
393 char chars[kBufferSize];
394 Vector<char> buffer(chars, kBufferSize);
395 Append(IntToCString(object->value(), buffer));
396 return SUCCESS;
397 }
398
399
400 BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
401 double number) {
402 if (isinf(number) || isnan(number)) {
403 Append("null");
404 return SUCCESS;
405 }
406 static const int kBufferSize = 100;
407 char chars[kBufferSize];
408 Vector<char> buffer(chars, kBufferSize);
409 Append(DoubleToCString(number, buffer));
410 return SUCCESS;
411 }
412
413
414 BasicJsonStringifier::Result BasicJsonStringifier::SerializeArray(
415 Handle<JSArray> object) {
416 HandleScope handle_scope(isolate_);
417 if (StackPush(object) == CIRCULAR) return CIRCULAR;
418 int length = Smi::cast(object->length())->value();
419 Append('[');
420 switch (object->GetElementsKind()) {
421 case FAST_SMI_ELEMENTS: {
422 Handle<FixedArray> elements = Handle<FixedArray>(
423 FixedArray::cast(object->elements()));
424 for (int i = 0; i < length; i++) {
425 if (i > 0) Append(',');
426 SerializeSmi(Smi::cast(elements->get(i)));
427 }
428 break;
429 }
430 case FAST_HOLEY_SMI_ELEMENTS: {
431 Handle<FixedArray> elements = Handle<FixedArray>(
432 FixedArray::cast(object->elements()));
433 for (int i = 0; i < length; i++) {
434 if (i > 0) Append(',');
435 if (elements->is_the_hole(i)) {
436 Append("null");
437 } else {
438 SerializeSmi(Smi::cast(elements->get(i)));
439 }
440 }
441 break;
442 }
443 case FAST_HOLEY_DOUBLE_ELEMENTS:
444 case FAST_DOUBLE_ELEMENTS: {
445 Handle<FixedDoubleArray> elements = Handle<FixedDoubleArray>(
446 FixedDoubleArray::cast(object->elements()));
447 for (int i = 0; i < length; i++) {
448 if (i > 0) Append(',');
449 SerializeDouble(elements->get_scalar(i));
450 }
451 break;
452 }
453 case FAST_HOLEY_ELEMENTS:
454 case FAST_ELEMENTS: {
455 Handle<FixedArray> elements = Handle<FixedArray>(
456 FixedArray::cast(object->elements()));
457 for (int i = 0; i < length; i++) {
458 if (i > 0) Append(',');
459 Result result = Serialize(Handle<Object>(elements->get(i)));
460 if (result == SUCCESS) continue;
461 if (result == UNCHANGED) {
462 Append("null");
463 } else {
464 return result;
465 }
466 }
467 break;
468 }
469 default:
470 return BAILOUT;
471 }
472 Append(']');
473 StackPop();
474 current_part_ = handle_scope.CloseAndEscape(current_part_);
475 return SUCCESS;
476 }
477
478
479 BasicJsonStringifier::Result BasicJsonStringifier::SerializeObject(
480 Handle<JSObject> object) {
481 HandleScope handle_scope(isolate_);
482 Result stack_push = StackPush(object);
483 if (stack_push != SUCCESS) return stack_push;
484 if (object->IsJSGlobalProxy()) return BAILOUT;
485 bool threw = false;
486 Handle<FixedArray> contents =
487 GetKeysInFixedArrayFor(object, LOCAL_ONLY, &threw);
488 if (threw) return BAILOUT;
489 Append('{');
490 int length = contents->length();
491 bool comma = false;
492 for (int i = 0; i < length; i++) {
493 Object* key = contents->get(i);
494 Handle<String> key_handle;
495 Handle<Object> property;
496 if (key->IsString()) {
497 key_handle = Handle<String>(String::cast(key));
498 property = GetProperty(object, key_handle);
499 } else {
500 ASSERT(key->IsNumber());
501 key_handle = isolate_->factory()->NumberToString(Handle<Object>(key));
502 uint32_t index;
503 if (key->IsSmi()) {
504 property = Object::GetElement(object, Smi::cast(key)->value());
505 } else if (key_handle->AsArrayIndex(&index)) {
506 property = Object::GetElement(object, index);
507 } else {
508 property = GetProperty(object, key_handle);
509 }
510 }
511 if (property.is_null()) return BAILOUT;
512 Result result = SerializeDeferred(property, comma, key_handle);
513 if (!comma && result == SUCCESS) comma = true;
514 if (result >= BAILOUT) return result;
515 }
516 Append('}');
517 StackPop();
518 current_part_ = handle_scope.CloseAndEscape(current_part_);
519 return SUCCESS;
520 }
521
522
523 void BasicJsonStringifier::ShrinkCurrentPart() {
524 ASSERT(current_index_ < part_length_);
525 if (current_index_ == 0) {
526 current_part_ = isolate_->factory()->empty_string();
527 return;
528 }
529
530 int string_size, allocated_string_size;
531 if (is_ascii_) {
532 allocated_string_size = SeqAsciiString::SizeFor(part_length_);
533 string_size = SeqAsciiString::SizeFor(current_index_);
534 } else {
535 allocated_string_size = SeqTwoByteString::SizeFor(part_length_);
536 string_size = SeqTwoByteString::SizeFor(current_index_);
537 }
538
539 int delta = allocated_string_size - string_size;
540 current_part_->set_length(current_index_);
541
542 // String sizes are pointer size aligned, so that we can use filler objects
543 // that are a multiple of pointer size.
544 Address end_of_string = current_part_->address() + string_size;
545 isolate_->heap()->CreateFillerObjectAt(end_of_string, delta);
546 if (Marking::IsBlack(Marking::MarkBitFrom(*current_part_))) {
547 MemoryChunk::IncrementLiveBytesFromMutator(
548 current_part_->address(), -delta);
549 }
550 }
551
552
553 template <bool is_ascii>
554 void BasicJsonStringifier::Extend() {
555 set_accumulator(
556 isolate_->factory()->NewConsString(accumulator(), current_part_));
557 if (part_length_ <= kMaxPartLength / kPartLengthGrowthFactor) {
558 part_length_ *= kPartLengthGrowthFactor;
559 }
560 if (is_ascii) {
561 current_part_ =
562 isolate_->factory()->NewRawAsciiString(part_length_);
563 } else {
564 current_part_ =
565 isolate_->factory()->NewRawTwoByteString(part_length_);
566 }
567 current_index_ = 0;
568 }
569
570
571 void BasicJsonStringifier::ChangeEncoding() {
572 ShrinkCurrentPart();
573 set_accumulator(
574 isolate_->factory()->NewConsString(accumulator(), current_part_));
575 current_part_ =
576 isolate_->factory()->NewRawTwoByteString(part_length_);
577 current_index_ = 0;
578 is_ascii_ = false;
579 }
580
581
582 template <bool is_ascii, typename Char>
583 void BasicJsonStringifier::SerializeString_(Vector<const Char> vector,
584 Handle<String> string) {
585 int length = vector.length();
586 if (current_index_ + (length << 3) < (part_length_ - 2)) {
587 AssertNoAllocation no_allocation_scope;
588 AppendUnchecked_<is_ascii, char>('"');
589 for (int i = 0; i < length; i++) {
590 Char c = vector[i];
591 if ((c >= '#' && c <= '~' && c != '\\') ||
592 (!is_ascii && ((c & 0xFF80) != 0))) {
593 AppendUnchecked_<is_ascii, Char>(c);
594 } else {
595 AppendUnchecked_<is_ascii, char>(
596 &JsonQuotes[c * kJsonQuotesCharactersPerEntry]);
597 }
598 }
599 AppendUnchecked_<is_ascii, char>('"');
600 } else {
601 Append_<is_ascii, char>('"');
602 String* string_location = *string;
603 for (int i = 0; i < length; i++) {
604 Char c = vector[i];
605 if ((c >= '#' && c <= '~' && c != '\\') ||
606 (!is_ascii && ((c & 0xFF80) != 0))) {
607 Append_<is_ascii, Char>(c);
608 } else {
609 Append_<is_ascii, char>(&JsonQuotes[c * kJsonQuotesCharactersPerEntry]);
610 }
611 // If GC moved the string, we need to refresh the vector.
612 if (*string != string_location) {
613 vector = GetCharVector<Char>(string);
614 string_location = *string;
615 }
616 }
617 Append_<is_ascii, char>('"');
618 }
619 }
620
621 template <>
622 Vector<const char> BasicJsonStringifier::GetCharVector(Handle<String> string) {
623 String::FlatContent flat = string->GetFlatContent();
624 ASSERT(flat.IsAscii());
625 return flat.ToAsciiVector();
626 }
627
628 template <>
629 Vector<const uc16> BasicJsonStringifier::GetCharVector(Handle<String> string) {
630 String::FlatContent flat = string->GetFlatContent();
631 ASSERT(flat.IsTwoByte());
632 return flat.ToUC16Vector();
633 }
634
635
636 void BasicJsonStringifier::SerializeString(Handle<String> object) {
637 FlattenString(object);
638 String::FlatContent flat = object->GetFlatContent();
639 if (is_ascii_) {
640 if (flat.IsAscii()) {
641 SerializeString_<true, char>(flat.ToAsciiVector(), object);
642 } else {
643 ChangeEncoding();
644 SerializeString(object);
645 }
646 } else {
647 if (flat.IsAscii()) {
648 SerializeString_<false, char>(flat.ToAsciiVector(), object);
649 } else {
650 SerializeString_<false, uc16>(flat.ToUC16Vector(), object);
651 }
652 }
653 }
654
655 } } // namespace v8::internal
656
657 #endif // V8_JSON_STRINGIFIER_H_
OLDNEW
« no previous file with comments | « src/json.js ('k') | src/runtime.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698