1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_JSON_STRINGIFIER_H_
6 #define V8_JSON_STRINGIFIER_H_
7 
8 #include "src/conversions.h"
9 #include "src/lookup.h"
10 #include "src/messages.h"
11 #include "src/string-builder.h"
12 #include "src/utils.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 class BasicJsonStringifier BASE_EMBEDDED {
18  public:
19   explicit BasicJsonStringifier(Isolate* isolate);
20 
21   MUST_USE_RESULT MaybeHandle<Object> Stringify(Handle<Object> object);
22 
23   MUST_USE_RESULT INLINE(static MaybeHandle<Object> StringifyString(
24       Isolate* isolate,
25       Handle<String> object));
26 
27  private:
28   enum Result { UNCHANGED, SUCCESS, EXCEPTION };
29 
30   MUST_USE_RESULT MaybeHandle<Object> ApplyToJsonFunction(
31       Handle<Object> object,
32       Handle<Object> key);
33 
34   Result SerializeGeneric(Handle<Object> object,
35                           Handle<Object> key,
36                           bool deferred_comma,
37                           bool deferred_key);
38 
39   // Entry point to serialize the object.
INLINE(Result SerializeObject (Handle<Object> obj))40   INLINE(Result SerializeObject(Handle<Object> obj)) {
41     return Serialize_<false>(obj, false, factory()->empty_string());
42   }
43 
44   // Serialize an array element.
45   // The index may serve as argument for the toJSON function.
INLINE(Result SerializeElement (Isolate * isolate,Handle<Object> object,int i))46   INLINE(Result SerializeElement(Isolate* isolate,
47                                  Handle<Object> object,
48                                  int i)) {
49     return Serialize_<false>(object,
50                              false,
51                              Handle<Object>(Smi::FromInt(i), isolate));
52   }
53 
54   // Serialize a object property.
55   // The key may or may not be serialized depending on the property.
56   // The key may also serve as argument for the toJSON function.
INLINE(Result SerializeProperty (Handle<Object> object,bool deferred_comma,Handle<String> deferred_key))57   INLINE(Result SerializeProperty(Handle<Object> object,
58                                   bool deferred_comma,
59                                   Handle<String> deferred_key)) {
60     DCHECK(!deferred_key.is_null());
61     return Serialize_<true>(object, deferred_comma, deferred_key);
62   }
63 
64   template <bool deferred_string_key>
65   Result Serialize_(Handle<Object> object, bool comma, Handle<Object> key);
66 
SerializeDeferredKey(bool deferred_comma,Handle<Object> deferred_key)67   void SerializeDeferredKey(bool deferred_comma, Handle<Object> deferred_key) {
68     if (deferred_comma) builder_.AppendCharacter(',');
69     SerializeString(Handle<String>::cast(deferred_key));
70     builder_.AppendCharacter(':');
71   }
72 
73   Result SerializeSmi(Smi* object);
74 
75   Result SerializeDouble(double number);
INLINE(Result SerializeHeapNumber (Handle<HeapNumber> object))76   INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) {
77     return SerializeDouble(object->value());
78   }
79 
80   Result SerializeJSValue(Handle<JSValue> object);
81 
82   INLINE(Result SerializeJSArray(Handle<JSArray> object));
83   INLINE(Result SerializeJSObject(Handle<JSObject> object));
84 
85   Result SerializeJSArraySlow(Handle<JSArray> object, uint32_t start,
86                               uint32_t length);
87 
88   void SerializeString(Handle<String> object);
89 
90   template <typename SrcChar, typename DestChar>
91   INLINE(static void SerializeStringUnchecked_(
92       Vector<const SrcChar> src,
93       IncrementalStringBuilder::NoExtend<DestChar>* dest));
94 
95   template <typename SrcChar, typename DestChar>
96   INLINE(void SerializeString_(Handle<String> string));
97 
98   template <typename Char>
99   INLINE(static bool DoNotEscape(Char c));
100 
101   Result StackPush(Handle<Object> object);
102   void StackPop();
103 
factory()104   Factory* factory() { return isolate_->factory(); }
105 
106   Isolate* isolate_;
107   IncrementalStringBuilder builder_;
108   Handle<String> tojson_string_;
109   Handle<JSArray> stack_;
110 
111   static const int kJsonEscapeTableEntrySize = 8;
112   static const char* const JsonEscapeTable;
113 };
114 
115 
116 // Translation table to escape Latin1 characters.
117 // Table entries start at a multiple of 8 and are null-terminated.
118 const char* const BasicJsonStringifier::JsonEscapeTable =
119     "\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 "
120     "\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 "
121     "\\b\0     \\t\0     \\n\0     \\u000b\0 "
122     "\\f\0     \\r\0     \\u000e\0 \\u000f\0 "
123     "\\u0010\0 \\u0011\0 \\u0012\0 \\u0013\0 "
124     "\\u0014\0 \\u0015\0 \\u0016\0 \\u0017\0 "
125     "\\u0018\0 \\u0019\0 \\u001a\0 \\u001b\0 "
126     "\\u001c\0 \\u001d\0 \\u001e\0 \\u001f\0 "
127     " \0      !\0      \\\"\0     #\0      "
128     "$\0      %\0      &\0      '\0      "
129     "(\0      )\0      *\0      +\0      "
130     ",\0      -\0      .\0      /\0      "
131     "0\0      1\0      2\0      3\0      "
132     "4\0      5\0      6\0      7\0      "
133     "8\0      9\0      :\0      ;\0      "
134     "<\0      =\0      >\0      ?\0      "
135     "@\0      A\0      B\0      C\0      "
136     "D\0      E\0      F\0      G\0      "
137     "H\0      I\0      J\0      K\0      "
138     "L\0      M\0      N\0      O\0      "
139     "P\0      Q\0      R\0      S\0      "
140     "T\0      U\0      V\0      W\0      "
141     "X\0      Y\0      Z\0      [\0      "
142     "\\\\\0     ]\0      ^\0      _\0      "
143     "`\0      a\0      b\0      c\0      "
144     "d\0      e\0      f\0      g\0      "
145     "h\0      i\0      j\0      k\0      "
146     "l\0      m\0      n\0      o\0      "
147     "p\0      q\0      r\0      s\0      "
148     "t\0      u\0      v\0      w\0      "
149     "x\0      y\0      z\0      {\0      "
150     "|\0      }\0      ~\0      \177\0      "
151     "\200\0      \201\0      \202\0      \203\0      "
152     "\204\0      \205\0      \206\0      \207\0      "
153     "\210\0      \211\0      \212\0      \213\0      "
154     "\214\0      \215\0      \216\0      \217\0      "
155     "\220\0      \221\0      \222\0      \223\0      "
156     "\224\0      \225\0      \226\0      \227\0      "
157     "\230\0      \231\0      \232\0      \233\0      "
158     "\234\0      \235\0      \236\0      \237\0      "
159     "\240\0      \241\0      \242\0      \243\0      "
160     "\244\0      \245\0      \246\0      \247\0      "
161     "\250\0      \251\0      \252\0      \253\0      "
162     "\254\0      \255\0      \256\0      \257\0      "
163     "\260\0      \261\0      \262\0      \263\0      "
164     "\264\0      \265\0      \266\0      \267\0      "
165     "\270\0      \271\0      \272\0      \273\0      "
166     "\274\0      \275\0      \276\0      \277\0      "
167     "\300\0      \301\0      \302\0      \303\0      "
168     "\304\0      \305\0      \306\0      \307\0      "
169     "\310\0      \311\0      \312\0      \313\0      "
170     "\314\0      \315\0      \316\0      \317\0      "
171     "\320\0      \321\0      \322\0      \323\0      "
172     "\324\0      \325\0      \326\0      \327\0      "
173     "\330\0      \331\0      \332\0      \333\0      "
174     "\334\0      \335\0      \336\0      \337\0      "
175     "\340\0      \341\0      \342\0      \343\0      "
176     "\344\0      \345\0      \346\0      \347\0      "
177     "\350\0      \351\0      \352\0      \353\0      "
178     "\354\0      \355\0      \356\0      \357\0      "
179     "\360\0      \361\0      \362\0      \363\0      "
180     "\364\0      \365\0      \366\0      \367\0      "
181     "\370\0      \371\0      \372\0      \373\0      "
182     "\374\0      \375\0      \376\0      \377\0      ";
183 
184 
BasicJsonStringifier(Isolate * isolate)185 BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate)
186     : isolate_(isolate), builder_(isolate) {
187   tojson_string_ = factory()->toJSON_string();
188   stack_ = factory()->NewJSArray(8);
189 }
190 
191 
Stringify(Handle<Object> object)192 MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object) {
193   Result result = SerializeObject(object);
194   if (result == UNCHANGED) return factory()->undefined_value();
195   if (result == SUCCESS) return builder_.Finish();
196   DCHECK(result == EXCEPTION);
197   return MaybeHandle<Object>();
198 }
199 
200 
StringifyString(Isolate * isolate,Handle<String> object)201 MaybeHandle<Object> BasicJsonStringifier::StringifyString(
202     Isolate* isolate,  Handle<String> object) {
203   static const int kJsonQuoteWorstCaseBlowup = 6;
204   static const int kSpaceForQuotes = 2;
205   int worst_case_length =
206       object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes;
207 
208   if (worst_case_length > 32 * KB) {  // Slow path if too large.
209     BasicJsonStringifier stringifier(isolate);
210     return stringifier.Stringify(object);
211   }
212 
213   object = String::Flatten(object);
214   DCHECK(object->IsFlat());
215   Handle<SeqString> result;
216   if (object->IsOneByteRepresentationUnderneath()) {
217     result = isolate->factory()
218                  ->NewRawOneByteString(worst_case_length)
219                  .ToHandleChecked();
220     IncrementalStringBuilder::NoExtendString<uint8_t> no_extend(
221         result, worst_case_length);
222     no_extend.Append('\"');
223     SerializeStringUnchecked_(object->GetFlatContent().ToOneByteVector(),
224                               &no_extend);
225     no_extend.Append('\"');
226     return no_extend.Finalize();
227   } else {
228     result = isolate->factory()
229                  ->NewRawTwoByteString(worst_case_length)
230                  .ToHandleChecked();
231     IncrementalStringBuilder::NoExtendString<uc16> no_extend(result,
232                                                              worst_case_length);
233     no_extend.Append('\"');
234     SerializeStringUnchecked_(object->GetFlatContent().ToUC16Vector(),
235                               &no_extend);
236     no_extend.Append('\"');
237     return no_extend.Finalize();
238   }
239 }
240 
241 
ApplyToJsonFunction(Handle<Object> object,Handle<Object> key)242 MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction(
243     Handle<Object> object, Handle<Object> key) {
244   LookupIterator it(object, tojson_string_,
245                     LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
246   Handle<Object> fun;
247   ASSIGN_RETURN_ON_EXCEPTION(isolate_, fun, Object::GetProperty(&it), Object);
248   if (!fun->IsJSFunction()) return object;
249 
250   // Call toJSON function.
251   if (key->IsSmi()) key = factory()->NumberToString(key);
252   Handle<Object> argv[] = { key };
253   HandleScope scope(isolate_);
254   ASSIGN_RETURN_ON_EXCEPTION(
255       isolate_, object,
256       Execution::Call(isolate_, fun, object, 1, argv),
257       Object);
258   return scope.CloseAndEscape(object);
259 }
260 
261 
StackPush(Handle<Object> object)262 BasicJsonStringifier::Result BasicJsonStringifier::StackPush(
263     Handle<Object> object) {
264   StackLimitCheck check(isolate_);
265   if (check.HasOverflowed()) {
266     isolate_->StackOverflow();
267     return EXCEPTION;
268   }
269 
270   int length = Smi::cast(stack_->length())->value();
271   {
272     DisallowHeapAllocation no_allocation;
273     FixedArray* elements = FixedArray::cast(stack_->elements());
274     for (int i = 0; i < length; i++) {
275       if (elements->get(i) == *object) {
276         AllowHeapAllocation allow_to_return_error;
277         Handle<Object> error =
278             factory()->NewTypeError(MessageTemplate::kCircularStructure);
279         isolate_->Throw(*error);
280         return EXCEPTION;
281       }
282     }
283   }
284   JSArray::SetLength(stack_, length + 1);
285   FixedArray::cast(stack_->elements())->set(length, *object);
286   return SUCCESS;
287 }
288 
289 
StackPop()290 void BasicJsonStringifier::StackPop() {
291   int length = Smi::cast(stack_->length())->value();
292   stack_->set_length(Smi::FromInt(length - 1));
293 }
294 
295 
296 template <bool deferred_string_key>
Serialize_(Handle<Object> object,bool comma,Handle<Object> key)297 BasicJsonStringifier::Result BasicJsonStringifier::Serialize_(
298     Handle<Object> object, bool comma, Handle<Object> key) {
299   if (object->IsJSObject()) {
300     ASSIGN_RETURN_ON_EXCEPTION_VALUE(
301         isolate_, object,
302         ApplyToJsonFunction(object, key),
303         EXCEPTION);
304   }
305 
306   if (object->IsSmi()) {
307     if (deferred_string_key) SerializeDeferredKey(comma, key);
308     return SerializeSmi(Smi::cast(*object));
309   }
310 
311   switch (HeapObject::cast(*object)->map()->instance_type()) {
312     case HEAP_NUMBER_TYPE:
313     case MUTABLE_HEAP_NUMBER_TYPE:
314       if (deferred_string_key) SerializeDeferredKey(comma, key);
315       return SerializeHeapNumber(Handle<HeapNumber>::cast(object));
316     case ODDBALL_TYPE:
317       switch (Oddball::cast(*object)->kind()) {
318         case Oddball::kFalse:
319           if (deferred_string_key) SerializeDeferredKey(comma, key);
320           builder_.AppendCString("false");
321           return SUCCESS;
322         case Oddball::kTrue:
323           if (deferred_string_key) SerializeDeferredKey(comma, key);
324           builder_.AppendCString("true");
325           return SUCCESS;
326         case Oddball::kNull:
327           if (deferred_string_key) SerializeDeferredKey(comma, key);
328           builder_.AppendCString("null");
329           return SUCCESS;
330         default:
331           return UNCHANGED;
332       }
333     case JS_ARRAY_TYPE:
334       if (object->IsAccessCheckNeeded()) break;
335       if (deferred_string_key) SerializeDeferredKey(comma, key);
336       return SerializeJSArray(Handle<JSArray>::cast(object));
337     case JS_VALUE_TYPE:
338       if (deferred_string_key) SerializeDeferredKey(comma, key);
339       return SerializeJSValue(Handle<JSValue>::cast(object));
340     default:
341       if (object->IsString()) {
342         if (deferred_string_key) SerializeDeferredKey(comma, key);
343         SerializeString(Handle<String>::cast(object));
344         return SUCCESS;
345       } else if (object->IsJSObject()) {
346         if (object->IsCallable()) return UNCHANGED;
347         // Go to slow path for global proxy and objects requiring access checks.
348         if (object->IsAccessCheckNeeded() || object->IsJSGlobalProxy()) break;
349         if (deferred_string_key) SerializeDeferredKey(comma, key);
350         return SerializeJSObject(Handle<JSObject>::cast(object));
351       }
352   }
353 
354   return SerializeGeneric(object, key, comma, deferred_string_key);
355 }
356 
357 
SerializeGeneric(Handle<Object> object,Handle<Object> key,bool deferred_comma,bool deferred_key)358 BasicJsonStringifier::Result BasicJsonStringifier::SerializeGeneric(
359     Handle<Object> object,
360     Handle<Object> key,
361     bool deferred_comma,
362     bool deferred_key) {
363   Handle<JSFunction> fun = isolate_->json_serialize_adapter();
364   Handle<Object> argv[] = { key, object };
365   Handle<Object> result;
366   ASSIGN_RETURN_ON_EXCEPTION_VALUE(
367       isolate_, result, Execution::Call(isolate_, fun, object, 2, argv),
368       EXCEPTION);
369   if (result->IsUndefined()) return UNCHANGED;
370   if (deferred_key) {
371     if (key->IsSmi()) key = factory()->NumberToString(key);
372     SerializeDeferredKey(deferred_comma, key);
373   }
374 
375   builder_.AppendString(Handle<String>::cast(result));
376   return SUCCESS;
377 }
378 
379 
SerializeJSValue(Handle<JSValue> object)380 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue(
381     Handle<JSValue> object) {
382   String* class_name = object->class_name();
383   if (class_name == isolate_->heap()->String_string()) {
384     Handle<Object> value;
385     ASSIGN_RETURN_ON_EXCEPTION_VALUE(
386         isolate_, value, Object::ToString(isolate_, object), EXCEPTION);
387     SerializeString(Handle<String>::cast(value));
388   } else if (class_name == isolate_->heap()->Number_string()) {
389     Handle<Object> value;
390     ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate_, value, Object::ToNumber(object),
391                                      EXCEPTION);
392     if (value->IsSmi()) return SerializeSmi(Smi::cast(*value));
393     SerializeHeapNumber(Handle<HeapNumber>::cast(value));
394   } else if (class_name == isolate_->heap()->Boolean_string()) {
395     Object* value = JSValue::cast(*object)->value();
396     DCHECK(value->IsBoolean());
397     builder_.AppendCString(value->IsTrue() ? "true" : "false");
398   } else {
399     // ES6 24.3.2.1 step 10.c, serialize as an ordinary JSObject.
400     CHECK(!object->IsAccessCheckNeeded());
401     CHECK(!object->IsJSGlobalProxy());
402     return SerializeJSObject(object);
403   }
404   return SUCCESS;
405 }
406 
407 
SerializeSmi(Smi * object)408 BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) {
409   static const int kBufferSize = 100;
410   char chars[kBufferSize];
411   Vector<char> buffer(chars, kBufferSize);
412   builder_.AppendCString(IntToCString(object->value(), buffer));
413   return SUCCESS;
414 }
415 
416 
SerializeDouble(double number)417 BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble(
418     double number) {
419   if (std::isinf(number) || std::isnan(number)) {
420     builder_.AppendCString("null");
421     return SUCCESS;
422   }
423   static const int kBufferSize = 100;
424   char chars[kBufferSize];
425   Vector<char> buffer(chars, kBufferSize);
426   builder_.AppendCString(DoubleToCString(number, buffer));
427   return SUCCESS;
428 }
429 
430 
SerializeJSArray(Handle<JSArray> object)431 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray(
432     Handle<JSArray> object) {
433   HandleScope handle_scope(isolate_);
434   Result stack_push = StackPush(object);
435   if (stack_push != SUCCESS) return stack_push;
436   uint32_t length = 0;
437   CHECK(object->length()->ToArrayLength(&length));
438   builder_.AppendCharacter('[');
439   switch (object->GetElementsKind()) {
440     case FAST_SMI_ELEMENTS: {
441       Handle<FixedArray> elements(FixedArray::cast(object->elements()),
442                                   isolate_);
443       for (uint32_t i = 0; i < length; i++) {
444         if (i > 0) builder_.AppendCharacter(',');
445         SerializeSmi(Smi::cast(elements->get(i)));
446       }
447       break;
448     }
449     case FAST_DOUBLE_ELEMENTS: {
450       // Empty array is FixedArray but not FixedDoubleArray.
451       if (length == 0) break;
452       Handle<FixedDoubleArray> elements(
453           FixedDoubleArray::cast(object->elements()), isolate_);
454       for (uint32_t i = 0; i < length; i++) {
455         if (i > 0) builder_.AppendCharacter(',');
456         SerializeDouble(elements->get_scalar(i));
457       }
458       break;
459     }
460     case FAST_ELEMENTS: {
461       Handle<Object> old_length(object->length(), isolate_);
462       for (uint32_t i = 0; i < length; i++) {
463         if (object->length() != *old_length ||
464             object->GetElementsKind() != FAST_ELEMENTS) {
465           Result result = SerializeJSArraySlow(object, i, length);
466           if (result != SUCCESS) return result;
467           break;
468         }
469         if (i > 0) builder_.AppendCharacter(',');
470         Result result = SerializeElement(
471             isolate_,
472             Handle<Object>(FixedArray::cast(object->elements())->get(i),
473                            isolate_),
474             i);
475         if (result == SUCCESS) continue;
476         if (result == UNCHANGED) {
477           builder_.AppendCString("null");
478         } else {
479           return result;
480         }
481       }
482       break;
483     }
484     // The FAST_HOLEY_* cases could be handled in a faster way. They resemble
485     // the non-holey cases except that a lookup is necessary for holes.
486     default: {
487       Result result = SerializeJSArraySlow(object, 0, length);
488       if (result != SUCCESS) return result;
489       break;
490     }
491   }
492   builder_.AppendCharacter(']');
493   StackPop();
494   return SUCCESS;
495 }
496 
497 
SerializeJSArraySlow(Handle<JSArray> object,uint32_t start,uint32_t length)498 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArraySlow(
499     Handle<JSArray> object, uint32_t start, uint32_t length) {
500   for (uint32_t i = start; i < length; i++) {
501     if (i > 0) builder_.AppendCharacter(',');
502     Handle<Object> element;
503     ASSIGN_RETURN_ON_EXCEPTION_VALUE(
504         isolate_, element,
505         Object::GetElement(isolate_, object, i),
506         EXCEPTION);
507     if (element->IsUndefined()) {
508       builder_.AppendCString("null");
509     } else {
510       Result result = SerializeElement(isolate_, element, i);
511       if (result == SUCCESS) continue;
512       if (result == UNCHANGED) {
513         builder_.AppendCString("null");
514       } else {
515         return result;
516       }
517     }
518   }
519   return SUCCESS;
520 }
521 
522 
SerializeJSObject(Handle<JSObject> object)523 BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject(
524     Handle<JSObject> object) {
525   HandleScope handle_scope(isolate_);
526   Result stack_push = StackPush(object);
527   if (stack_push != SUCCESS) return stack_push;
528   DCHECK(!object->IsJSGlobalProxy() && !object->IsJSGlobalObject());
529 
530   builder_.AppendCharacter('{');
531   bool comma = false;
532 
533   if (object->HasFastProperties() &&
534       !object->HasIndexedInterceptor() &&
535       !object->HasNamedInterceptor() &&
536       object->elements()->length() == 0) {
537     Handle<Map> map(object->map());
538     for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) {
539       Handle<Name> name(map->instance_descriptors()->GetKey(i), isolate_);
540       // TODO(rossberg): Should this throw?
541       if (!name->IsString()) continue;
542       Handle<String> key = Handle<String>::cast(name);
543       PropertyDetails details = map->instance_descriptors()->GetDetails(i);
544       if (details.IsDontEnum()) continue;
545       Handle<Object> property;
546       if (details.type() == DATA && *map == object->map()) {
547         FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
548         Isolate* isolate = object->GetIsolate();
549         if (object->IsUnboxedDoubleField(field_index)) {
550           double value = object->RawFastDoublePropertyAt(field_index);
551           property = isolate->factory()->NewHeapNumber(value);
552 
553         } else {
554           property = handle(object->RawFastPropertyAt(field_index), isolate);
555         }
556       } else {
557         ASSIGN_RETURN_ON_EXCEPTION_VALUE(
558             isolate_, property,
559             Object::GetPropertyOrElement(object, key),
560             EXCEPTION);
561       }
562       Result result = SerializeProperty(property, comma, key);
563       if (!comma && result == SUCCESS) comma = true;
564       if (result == EXCEPTION) return result;
565     }
566   } else {
567     Handle<FixedArray> contents;
568     ASSIGN_RETURN_ON_EXCEPTION_VALUE(
569         isolate_, contents,
570         JSReceiver::GetKeys(object, JSReceiver::OWN_ONLY, ENUMERABLE_STRINGS),
571         EXCEPTION);
572 
573     for (int i = 0; i < contents->length(); i++) {
574       Object* key = contents->get(i);
575       Handle<String> key_handle;
576       MaybeHandle<Object> maybe_property;
577       if (key->IsString()) {
578         key_handle = Handle<String>(String::cast(key), isolate_);
579         maybe_property = Object::GetPropertyOrElement(object, key_handle);
580       } else {
581         DCHECK(key->IsNumber());
582         key_handle = factory()->NumberToString(Handle<Object>(key, isolate_));
583         if (key->IsSmi()) {
584           maybe_property = Object::GetElement(
585               isolate_, object, Smi::cast(key)->value());
586         } else {
587           maybe_property = Object::GetPropertyOrElement(object, key_handle);
588         }
589       }
590       Handle<Object> property;
591       ASSIGN_RETURN_ON_EXCEPTION_VALUE(
592           isolate_, property, maybe_property, EXCEPTION);
593       Result result = SerializeProperty(property, comma, key_handle);
594       if (!comma && result == SUCCESS) comma = true;
595       if (result == EXCEPTION) return result;
596     }
597   }
598 
599   builder_.AppendCharacter('}');
600   StackPop();
601   return SUCCESS;
602 }
603 
604 
605 template <typename SrcChar, typename DestChar>
SerializeStringUnchecked_(Vector<const SrcChar> src,IncrementalStringBuilder::NoExtend<DestChar> * dest)606 void BasicJsonStringifier::SerializeStringUnchecked_(
607     Vector<const SrcChar> src,
608     IncrementalStringBuilder::NoExtend<DestChar>* dest) {
609   // Assert that uc16 character is not truncated down to 8 bit.
610   // The <uc16, char> version of this method must not be called.
611   DCHECK(sizeof(DestChar) >= sizeof(SrcChar));
612 
613   for (int i = 0; i < src.length(); i++) {
614     SrcChar c = src[i];
615     if (DoNotEscape(c)) {
616       dest->Append(c);
617     } else {
618       dest->AppendCString(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]);
619     }
620   }
621 }
622 
623 
624 template <typename SrcChar, typename DestChar>
SerializeString_(Handle<String> string)625 void BasicJsonStringifier::SerializeString_(Handle<String> string) {
626   int length = string->length();
627   builder_.Append<uint8_t, DestChar>('"');
628   // We make a rough estimate to find out if the current string can be
629   // serialized without allocating a new string part. The worst case length of
630   // an escaped character is 6.  Shifting the remainin string length right by 3
631   // is a more pessimistic estimate, but faster to calculate.
632   int worst_case_length = length << 3;
633   if (builder_.CurrentPartCanFit(worst_case_length)) {
634     DisallowHeapAllocation no_gc;
635     Vector<const SrcChar> vector = string->GetCharVector<SrcChar>();
636     IncrementalStringBuilder::NoExtendBuilder<DestChar> no_extend(
637         &builder_, worst_case_length);
638     SerializeStringUnchecked_(vector, &no_extend);
639   } else {
640     FlatStringReader reader(isolate_, string);
641     for (int i = 0; i < reader.length(); i++) {
642       SrcChar c = reader.Get<SrcChar>(i);
643       if (DoNotEscape(c)) {
644         builder_.Append<SrcChar, DestChar>(c);
645       } else {
646         builder_.AppendCString(&JsonEscapeTable[c * kJsonEscapeTableEntrySize]);
647       }
648     }
649   }
650 
651   builder_.Append<uint8_t, DestChar>('"');
652 }
653 
654 
655 template <>
DoNotEscape(uint8_t c)656 bool BasicJsonStringifier::DoNotEscape(uint8_t c) {
657   return c >= '#' && c <= '~' && c != '\\';
658 }
659 
660 
661 template <>
DoNotEscape(uint16_t c)662 bool BasicJsonStringifier::DoNotEscape(uint16_t c) {
663   return c >= '#' && c != '\\' && c != 0x7f;
664 }
665 
666 
SerializeString(Handle<String> object)667 void BasicJsonStringifier::SerializeString(Handle<String> object) {
668   object = String::Flatten(object);
669   if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) {
670     if (object->IsOneByteRepresentationUnderneath()) {
671       SerializeString_<uint8_t, uint8_t>(object);
672     } else {
673       builder_.ChangeEncoding();
674       SerializeString(object);
675     }
676   } else {
677     if (object->IsOneByteRepresentationUnderneath()) {
678       SerializeString_<uint8_t, uc16>(object);
679     } else {
680       SerializeString_<uc16, uc16>(object);
681     }
682   }
683 }
684 
685 }  // namespace internal
686 }  // namespace v8
687 
688 #endif  // V8_JSON_STRINGIFIER_H_
689