Index: src/json-stringifier.cc |
diff --git a/src/json-stringifier.h b/src/json-stringifier.cc |
similarity index 84% |
copy from src/json-stringifier.h |
copy to src/json-stringifier.cc |
index 8d53369f6ae3f75763ffc9b4e9cd10184d1fe55c..cc859929d79db46a6114f705ebe4df7f94c2e1c1 100644 |
--- a/src/json-stringifier.h |
+++ b/src/json-stringifier.cc |
@@ -1,129 +1,18 @@ |
-// Copyright 2012 the V8 project authors. All rights reserved. |
+// Copyright 2016 the V8 project authors. All rights reserved. |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#ifndef V8_JSON_STRINGIFIER_H_ |
-#define V8_JSON_STRINGIFIER_H_ |
+#include "src/json-stringifier.h" |
#include "src/conversions.h" |
#include "src/lookup.h" |
#include "src/messages.h" |
-#include "src/string-builder.h" |
+#include "src/objects-inl.h" |
#include "src/utils.h" |
namespace v8 { |
namespace internal { |
-class BasicJsonStringifier BASE_EMBEDDED { |
- public: |
- BasicJsonStringifier(Isolate* isolate, Handle<String> gap); |
- |
- ~BasicJsonStringifier() { DeleteArray(gap_); } |
- |
- MUST_USE_RESULT MaybeHandle<Object> Stringify(Handle<Object> object); |
- |
- MUST_USE_RESULT INLINE(static MaybeHandle<Object> StringifyString( |
- Isolate* isolate, |
- Handle<String> object)); |
- |
- private: |
- enum Result { UNCHANGED, SUCCESS, EXCEPTION }; |
- |
- MUST_USE_RESULT MaybeHandle<Object> ApplyToJsonFunction( |
- Handle<Object> object, |
- Handle<Object> key); |
- |
- // Entry point to serialize the object. |
- INLINE(Result SerializeObject(Handle<Object> obj)) { |
- return Serialize_<false>(obj, false, factory()->empty_string()); |
- } |
- |
- // Serialize an array element. |
- // The index may serve as argument for the toJSON function. |
- INLINE(Result SerializeElement(Isolate* isolate, |
- Handle<Object> object, |
- int i)) { |
- return Serialize_<false>(object, |
- false, |
- Handle<Object>(Smi::FromInt(i), isolate)); |
- } |
- |
- // Serialize a object property. |
- // The key may or may not be serialized depending on the property. |
- // The key may also serve as argument for the toJSON function. |
- INLINE(Result SerializeProperty(Handle<Object> object, |
- bool deferred_comma, |
- Handle<String> deferred_key)) { |
- DCHECK(!deferred_key.is_null()); |
- return Serialize_<true>(object, deferred_comma, deferred_key); |
- } |
- |
- template <bool deferred_string_key> |
- Result Serialize_(Handle<Object> object, bool comma, Handle<Object> key); |
- |
- void SerializeDeferredKey(bool deferred_comma, Handle<Object> deferred_key) { |
- Separator(!deferred_comma); |
- SerializeString(Handle<String>::cast(deferred_key)); |
- builder_.AppendCharacter(':'); |
- if (gap_ != nullptr) builder_.AppendCharacter(' '); |
- } |
- |
- Result SerializeSmi(Smi* object); |
- |
- Result SerializeDouble(double number); |
- INLINE(Result SerializeHeapNumber(Handle<HeapNumber> object)) { |
- return SerializeDouble(object->value()); |
- } |
- |
- Result SerializeJSValue(Handle<JSValue> object); |
- |
- INLINE(Result SerializeJSArray(Handle<JSArray> object)); |
- INLINE(Result SerializeJSObject(Handle<JSObject> object)); |
- |
- Result SerializeJSProxy(Handle<JSProxy> object); |
- Result SerializeJSReceiverSlow(Handle<JSReceiver> object); |
- Result SerializeArrayLikeSlow(Handle<JSReceiver> object, uint32_t start, |
- uint32_t length); |
- |
- void SerializeString(Handle<String> object); |
- |
- template <typename SrcChar, typename DestChar> |
- INLINE(static void SerializeStringUnchecked_( |
- Vector<const SrcChar> src, |
- IncrementalStringBuilder::NoExtend<DestChar>* dest)); |
- |
- template <typename SrcChar, typename DestChar> |
- INLINE(void SerializeString_(Handle<String> string)); |
- |
- template <typename Char> |
- INLINE(static bool DoNotEscape(Char c)); |
- |
- INLINE(void NewLine()); |
- INLINE(void Indent() { indent_++; }); |
- INLINE(void Unindent() { indent_--; }); |
- INLINE(void Separator(bool first) { |
- if (!first) builder_.AppendCharacter(','); |
- NewLine(); |
- }) |
- |
- Result StackPush(Handle<Object> object); |
- void StackPop(); |
- |
- Factory* factory() { return isolate_->factory(); } |
- |
- Isolate* isolate_; |
- IncrementalStringBuilder builder_; |
- Handle<String> tojson_string_; |
- Handle<JSArray> stack_; |
- Handle<String> gap_string_; |
- uc16* gap_; |
- int indent_; |
- |
- static const int kJsonEscapeTableEntrySize = 8; |
- static const char* const JsonEscapeTable; |
-}; |
- |
- |
// Translation table to escape Latin1 characters. |
// Table entries start at a multiple of 8 and are null-terminated. |
const char* const BasicJsonStringifier::JsonEscapeTable = |
@@ -214,7 +103,6 @@ BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate, Handle<String> gap) |
} |
} |
- |
MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object) { |
Result result = SerializeObject(object); |
if (result == UNCHANGED) return factory()->undefined_value(); |
@@ -223,9 +111,8 @@ MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object) { |
return MaybeHandle<Object>(); |
} |
- |
MaybeHandle<Object> BasicJsonStringifier::StringifyString( |
- Isolate* isolate, Handle<String> object) { |
+ Isolate* isolate, Handle<String> object) { |
static const int kJsonQuoteWorstCaseBlowup = 6; |
static const int kSpaceForQuotes = 2; |
int worst_case_length = |
@@ -265,7 +152,6 @@ MaybeHandle<Object> BasicJsonStringifier::StringifyString( |
} |
} |
- |
MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
Handle<Object> object, Handle<Object> key) { |
LookupIterator it(object, tojson_string_, |
@@ -276,16 +162,14 @@ MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
// Call toJSON function. |
if (key->IsSmi()) key = factory()->NumberToString(key); |
- Handle<Object> argv[] = { key }; |
+ Handle<Object> argv[] = {key}; |
HandleScope scope(isolate_); |
- ASSIGN_RETURN_ON_EXCEPTION( |
- isolate_, object, |
- Execution::Call(isolate_, fun, object, 1, argv), |
- Object); |
+ ASSIGN_RETURN_ON_EXCEPTION(isolate_, object, |
+ Execution::Call(isolate_, fun, object, 1, argv), |
+ Object); |
return scope.CloseAndEscape(object); |
} |
- |
BasicJsonStringifier::Result BasicJsonStringifier::StackPush( |
Handle<Object> object) { |
StackLimitCheck check(isolate_); |
@@ -313,21 +197,17 @@ BasicJsonStringifier::Result BasicJsonStringifier::StackPush( |
return SUCCESS; |
} |
- |
void BasicJsonStringifier::StackPop() { |
int length = Smi::cast(stack_->length())->value(); |
stack_->set_length(Smi::FromInt(length - 1)); |
} |
- |
template <bool deferred_string_key> |
BasicJsonStringifier::Result BasicJsonStringifier::Serialize_( |
Handle<Object> object, bool comma, Handle<Object> key) { |
if (object->IsJSReceiver()) { |
ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
- isolate_, object, |
- ApplyToJsonFunction(object, key), |
- EXCEPTION); |
+ isolate_, object, ApplyToJsonFunction(object, key), EXCEPTION); |
} |
if (object->IsSmi()) { |
@@ -387,7 +267,6 @@ BasicJsonStringifier::Result BasicJsonStringifier::Serialize_( |
return UNCHANGED; |
} |
- |
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
Handle<JSValue> object) { |
String* class_name = object->class_name(); |
@@ -413,7 +292,6 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
return SUCCESS; |
} |
- |
BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
static const int kBufferSize = 100; |
char chars[kBufferSize]; |
@@ -422,7 +300,6 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
return SUCCESS; |
} |
- |
BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble( |
double number) { |
if (std::isinf(number) || std::isnan(number)) { |
@@ -436,7 +313,6 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble( |
return SUCCESS; |
} |
- |
BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
Handle<JSArray> object) { |
HandleScope handle_scope(isolate_); |
@@ -658,7 +534,6 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy( |
return SUCCESS; |
} |
- |
template <typename SrcChar, typename DestChar> |
void BasicJsonStringifier::SerializeStringUnchecked_( |
Vector<const SrcChar> src, |
@@ -677,7 +552,6 @@ void BasicJsonStringifier::SerializeStringUnchecked_( |
} |
} |
- |
template <typename SrcChar, typename DestChar> |
void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
int length = string->length(); |
@@ -708,13 +582,11 @@ void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
builder_.Append<uint8_t, DestChar>('"'); |
} |
- |
template <> |
bool BasicJsonStringifier::DoNotEscape(uint8_t c) { |
return c >= '#' && c <= '~' && c != '\\'; |
} |
- |
template <> |
bool BasicJsonStringifier::DoNotEscape(uint16_t c) { |
return c >= '#' && c != '\\' && c != 0x7f; |
@@ -726,6 +598,19 @@ void BasicJsonStringifier::NewLine() { |
for (int i = 0; i < indent_; i++) builder_.AppendCString(gap_); |
} |
+void BasicJsonStringifier::Separator(bool first) { |
+ if (!first) builder_.AppendCharacter(','); |
+ NewLine(); |
+} |
+ |
+void BasicJsonStringifier::SerializeDeferredKey(bool deferred_comma, |
+ Handle<Object> deferred_key) { |
+ Separator(!deferred_comma); |
+ SerializeString(Handle<String>::cast(deferred_key)); |
+ builder_.AppendCharacter(':'); |
+ if (gap_ != nullptr) builder_.AppendCharacter(' '); |
+} |
+ |
void BasicJsonStringifier::SerializeString(Handle<String> object) { |
object = String::Flatten(object); |
if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) { |
@@ -746,5 +631,3 @@ void BasicJsonStringifier::SerializeString(Handle<String> object) { |
} // namespace internal |
} // namespace v8 |
- |
-#endif // V8_JSON_STRINGIFIER_H_ |