Index: src/json-stringifier.cc |
diff --git a/src/json-stringifier.cc b/src/json-stringifier.cc |
index 955b7e3c222eedd210ba6ac9f294797382e66663..b7059d6f110482ec9a8d06466a2a7114840e984b 100644 |
--- a/src/json-stringifier.cc |
+++ b/src/json-stringifier.cc |
@@ -15,7 +15,7 @@ namespace internal { |
// Translation table to escape Latin1 characters. |
// Table entries start at a multiple of 8 and are null-terminated. |
-const char* const BasicJsonStringifier::JsonEscapeTable = |
+const char* const JsonStringifier::JsonEscapeTable = |
"\\u0000\0 \\u0001\0 \\u0002\0 \\u0003\0 " |
"\\u0004\0 \\u0005\0 \\u0006\0 \\u0007\0 " |
"\\b\0 \\t\0 \\n\0 \\u000b\0 " |
@@ -81,15 +81,15 @@ const char* const BasicJsonStringifier::JsonEscapeTable = |
"\370\0 \371\0 \372\0 \373\0 " |
"\374\0 \375\0 \376\0 \377\0 "; |
-BasicJsonStringifier::BasicJsonStringifier(Isolate* isolate) |
+JsonStringifier::JsonStringifier(Isolate* isolate) |
: isolate_(isolate), builder_(isolate), gap_(nullptr), indent_(0) { |
tojson_string_ = factory()->toJSON_string(); |
stack_ = factory()->NewJSArray(8); |
} |
-MaybeHandle<Object> BasicJsonStringifier::Stringify(Handle<Object> object, |
- Handle<Object> replacer, |
- Handle<Object> gap) { |
+MaybeHandle<Object> JsonStringifier::Stringify(Handle<Object> object, |
+ Handle<Object> replacer, |
+ Handle<Object> gap) { |
if (!InitializeReplacer(replacer)) return MaybeHandle<Object>(); |
if (!gap->IsUndefined() && !InitializeGap(gap)) return MaybeHandle<Object>(); |
Result result = SerializeObject(object); |
@@ -108,8 +108,9 @@ bool IsInList(Handle<String> key, List<Handle<String> >* list) { |
return false; |
} |
-bool BasicJsonStringifier::InitializeReplacer(Handle<Object> replacer) { |
+bool JsonStringifier::InitializeReplacer(Handle<Object> replacer) { |
DCHECK(property_list_.is_null()); |
+ DCHECK(replacer_function_.is_null()); |
Maybe<bool> is_array = Object::IsArray(replacer); |
if (is_array.IsNothing()) return false; |
if (is_array.FromJust()) { |
@@ -144,11 +145,13 @@ bool BasicJsonStringifier::InitializeReplacer(Handle<Object> replacer) { |
property_list_->set(i, *list[i]); |
} |
property_list_ = handle_scope.CloseAndEscape(property_list_); |
+ } else if (replacer->IsCallable()) { |
+ replacer_function_ = Handle<JSReceiver>::cast(replacer); |
} |
return true; |
} |
-bool BasicJsonStringifier::InitializeGap(Handle<Object> gap) { |
+bool JsonStringifier::InitializeGap(Handle<Object> gap) { |
DCHECK_NULL(gap_); |
HandleScope scope(isolate_); |
if (gap->IsJSValue()) { |
@@ -188,49 +191,9 @@ bool BasicJsonStringifier::InitializeGap(Handle<Object> gap) { |
return true; |
} |
-MaybeHandle<Object> BasicJsonStringifier::StringifyString( |
- Isolate* isolate, Handle<String> object) { |
- static const int kJsonQuoteWorstCaseBlowup = 6; |
- static const int kSpaceForQuotes = 2; |
- int worst_case_length = |
- object->length() * kJsonQuoteWorstCaseBlowup + kSpaceForQuotes; |
- |
- if (worst_case_length > 32 * KB) { // Slow path if too large. |
- BasicJsonStringifier stringifier(isolate); |
- Handle<Object> undefined = isolate->factory()->undefined_value(); |
- return stringifier.Stringify(object, undefined, undefined); |
- } |
- |
- object = String::Flatten(object); |
- DCHECK(object->IsFlat()); |
- Handle<SeqString> result; |
- if (object->IsOneByteRepresentationUnderneath()) { |
- result = isolate->factory() |
- ->NewRawOneByteString(worst_case_length) |
- .ToHandleChecked(); |
- IncrementalStringBuilder::NoExtendString<uint8_t> no_extend( |
- result, worst_case_length); |
- no_extend.Append('\"'); |
- SerializeStringUnchecked_(object->GetFlatContent().ToOneByteVector(), |
- &no_extend); |
- no_extend.Append('\"'); |
- return no_extend.Finalize(); |
- } else { |
- result = isolate->factory() |
- ->NewRawTwoByteString(worst_case_length) |
- .ToHandleChecked(); |
- IncrementalStringBuilder::NoExtendString<uc16> no_extend(result, |
- worst_case_length); |
- no_extend.Append('\"'); |
- SerializeStringUnchecked_(object->GetFlatContent().ToUC16Vector(), |
- &no_extend); |
- no_extend.Append('\"'); |
- return no_extend.Finalize(); |
- } |
-} |
- |
-MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
- Handle<Object> object, Handle<Object> key) { |
+MaybeHandle<Object> JsonStringifier::ApplyToJsonFunction(Handle<Object> object, |
+ Handle<Object> key) { |
+ HandleScope scope(isolate_); |
LookupIterator it(object, tojson_string_, |
LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR); |
Handle<Object> fun; |
@@ -240,15 +203,39 @@ MaybeHandle<Object> BasicJsonStringifier::ApplyToJsonFunction( |
// Call toJSON function. |
if (key->IsSmi()) key = factory()->NumberToString(key); |
Handle<Object> argv[] = {key}; |
- HandleScope scope(isolate_); |
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) { |
+MaybeHandle<Object> JsonStringifier::ApplyReplacerFunction( |
+ Handle<Object> object, Handle<Object> key) { |
+ HandleScope scope(isolate_); |
+ if (key->IsSmi()) key = factory()->NumberToString(key); |
+ Handle<Object> argv[] = {key, object}; |
+ Handle<JSReceiver> holder = CurrentHolder(object); |
+ ASSIGN_RETURN_ON_EXCEPTION( |
+ isolate_, object, |
+ Execution::Call(isolate_, replacer_function_, holder, 2, argv), Object); |
+ return scope.CloseAndEscape(object); |
+} |
+ |
+Handle<JSReceiver> JsonStringifier::CurrentHolder(Handle<Object> value) { |
+ int length = Smi::cast(stack_->length())->value(); |
+ if (length == 0) { |
+ Handle<JSObject> holder = |
+ factory()->NewJSObject(isolate_->object_function()); |
+ JSObject::AddProperty(holder, factory()->empty_string(), value, NONE); |
+ return holder; |
+ } else { |
+ FixedArray* elements = FixedArray::cast(stack_->elements()); |
+ return Handle<JSReceiver>(JSReceiver::cast(elements->get(length - 1)), |
+ isolate_); |
+ } |
+} |
+ |
+JsonStringifier::Result JsonStringifier::StackPush(Handle<Object> object) { |
StackLimitCheck check(isolate_); |
if (check.HasOverflowed()) { |
isolate_->StackOverflow(); |
@@ -274,18 +261,23 @@ BasicJsonStringifier::Result BasicJsonStringifier::StackPush( |
return SUCCESS; |
} |
-void BasicJsonStringifier::StackPop() { |
+void JsonStringifier::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) { |
+JsonStringifier::Result JsonStringifier::Serialize_(Handle<Object> object, |
+ bool comma, |
+ Handle<Object> key) { |
if (object->IsJSReceiver()) { |
ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
isolate_, object, ApplyToJsonFunction(object, key), EXCEPTION); |
} |
+ if (!replacer_function_.is_null()) { |
+ ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
+ isolate_, object, ApplyReplacerFunction(object, key), EXCEPTION); |
+ } |
if (object->IsSmi()) { |
if (deferred_string_key) SerializeDeferredKey(comma, key); |
@@ -344,7 +336,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::Serialize_( |
return UNCHANGED; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
+JsonStringifier::Result JsonStringifier::SerializeJSValue( |
Handle<JSValue> object) { |
String* class_name = object->class_name(); |
if (class_name == isolate_->heap()->String_string()) { |
@@ -369,7 +361,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSValue( |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
+JsonStringifier::Result JsonStringifier::SerializeSmi(Smi* object) { |
static const int kBufferSize = 100; |
char chars[kBufferSize]; |
Vector<char> buffer(chars, kBufferSize); |
@@ -377,8 +369,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeSmi(Smi* object) { |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble( |
- double number) { |
+JsonStringifier::Result JsonStringifier::SerializeDouble(double number) { |
if (std::isinf(number) || std::isnan(number)) { |
builder_.AppendCString("null"); |
return SUCCESS; |
@@ -390,7 +381,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeDouble( |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
+JsonStringifier::Result JsonStringifier::SerializeJSArray( |
Handle<JSArray> object) { |
HandleScope handle_scope(isolate_); |
Result stack_push = StackPush(object); |
@@ -400,59 +391,65 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
DCHECK(!object->IsAccessCheckNeeded()); |
builder_.AppendCharacter('['); |
Indent(); |
- switch (object->GetElementsKind()) { |
- case FAST_SMI_ELEMENTS: { |
- Handle<FixedArray> elements(FixedArray::cast(object->elements()), |
- isolate_); |
- for (uint32_t i = 0; i < length; i++) { |
- Separator(i == 0); |
- SerializeSmi(Smi::cast(elements->get(i))); |
- } |
- break; |
- } |
- case FAST_DOUBLE_ELEMENTS: { |
- // Empty array is FixedArray but not FixedDoubleArray. |
- if (length == 0) break; |
- Handle<FixedDoubleArray> elements( |
- FixedDoubleArray::cast(object->elements()), isolate_); |
- for (uint32_t i = 0; i < length; i++) { |
- Separator(i == 0); |
- SerializeDouble(elements->get_scalar(i)); |
+ uint32_t i = 0; |
+ if (replacer_function_.is_null()) { |
+ switch (object->GetElementsKind()) { |
+ case FAST_SMI_ELEMENTS: { |
+ Handle<FixedArray> elements(FixedArray::cast(object->elements()), |
+ isolate_); |
+ while (i < length) { |
+ Separator(i == 0); |
+ SerializeSmi(Smi::cast(elements->get(i))); |
+ i++; |
+ } |
+ break; |
} |
- break; |
- } |
- case FAST_ELEMENTS: { |
- Handle<Object> old_length(object->length(), isolate_); |
- for (uint32_t i = 0; i < length; i++) { |
- if (object->length() != *old_length || |
- object->GetElementsKind() != FAST_ELEMENTS) { |
- Result result = SerializeArrayLikeSlow(object, i, length); |
- if (result != SUCCESS) return result; |
- break; |
+ case FAST_DOUBLE_ELEMENTS: { |
+ // Empty array is FixedArray but not FixedDoubleArray. |
+ if (length == 0) break; |
+ Handle<FixedDoubleArray> elements( |
+ FixedDoubleArray::cast(object->elements()), isolate_); |
+ while (i < length) { |
+ Separator(i == 0); |
+ SerializeDouble(elements->get_scalar(i)); |
+ i++; |
} |
- Separator(i == 0); |
- Result result = SerializeElement( |
- isolate_, |
- Handle<Object>(FixedArray::cast(object->elements())->get(i), |
- isolate_), |
- i); |
- if (result == SUCCESS) continue; |
- if (result == UNCHANGED) { |
- builder_.AppendCString("null"); |
- } else { |
- return result; |
+ break; |
+ } |
+ case FAST_ELEMENTS: { |
+ Handle<Object> old_length(object->length(), isolate_); |
+ while (i < length) { |
+ if (object->length() != *old_length || |
+ object->GetElementsKind() != FAST_ELEMENTS) { |
+ // Fall back to slow path. |
+ break; |
+ } |
+ Separator(i == 0); |
+ Result result = SerializeElement( |
+ isolate_, |
+ Handle<Object>(FixedArray::cast(object->elements())->get(i), |
+ isolate_), |
+ i); |
+ if (result == UNCHANGED) { |
+ builder_.AppendCString("null"); |
+ } else if (result != SUCCESS) { |
+ return result; |
+ } |
+ i++; |
} |
+ break; |
} |
- break; |
- } |
- // The FAST_HOLEY_* cases could be handled in a faster way. They resemble |
- // the non-holey cases except that a lookup is necessary for holes. |
- default: { |
- Result result = SerializeArrayLikeSlow(object, 0, length); |
- if (result != SUCCESS) return result; |
- break; |
+ // The FAST_HOLEY_* cases could be handled in a faster way. They resemble |
+ // the non-holey cases except that a lookup is necessary for holes. |
+ default: |
+ break; |
} |
} |
+ if (i < length) { |
+ // Slow path for non-fast elements and fall-back in edge case. |
+ Result result = SerializeArrayLikeSlow(object, i, length); |
+ if (result != SUCCESS) return result; |
+ } |
Unindent(); |
if (length > 0) NewLine(); |
builder_.AppendCharacter(']'); |
@@ -460,7 +457,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSArray( |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeArrayLikeSlow( |
+JsonStringifier::Result JsonStringifier::SerializeArrayLikeSlow( |
Handle<JSReceiver> object, uint32_t start, uint32_t length) { |
for (uint32_t i = start; i < length; i++) { |
Separator(i == 0); |
@@ -468,22 +465,18 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeArrayLikeSlow( |
ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
isolate_, element, JSReceiver::GetElement(isolate_, object, i), |
EXCEPTION); |
- if (element->IsUndefined()) { |
+ Result result = SerializeElement(isolate_, element, i); |
+ if (result == SUCCESS) continue; |
+ if (result == UNCHANGED) { |
builder_.AppendCString("null"); |
} else { |
- Result result = SerializeElement(isolate_, element, i); |
- if (result == SUCCESS) continue; |
- if (result == UNCHANGED) { |
- builder_.AppendCString("null"); |
- } else { |
- return result; |
- } |
+ return result; |
} |
} |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject( |
+JsonStringifier::Result JsonStringifier::SerializeJSObject( |
Handle<JSObject> object) { |
HandleScope handle_scope(isolate_); |
Result stack_push = StackPush(object); |
@@ -512,12 +505,8 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject( |
Handle<Object> property; |
if (details.type() == DATA && *map == js_obj->map()) { |
FieldIndex field_index = FieldIndex::ForDescriptor(*map, i); |
- if (js_obj->IsUnboxedDoubleField(field_index)) { |
- double value = js_obj->RawFastDoublePropertyAt(field_index); |
- property = isolate_->factory()->NewHeapNumber(value); |
- } else { |
- property = handle(js_obj->RawFastPropertyAt(field_index), isolate_); |
- } |
+ property = JSObject::FastPropertyAt(js_obj, details.representation(), |
+ field_index); |
} else { |
ASSIGN_RETURN_ON_EXCEPTION_VALUE( |
isolate_, property, Object::GetPropertyOrElement(js_obj, key), |
@@ -538,7 +527,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSObject( |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSReceiverSlow( |
+JsonStringifier::Result JsonStringifier::SerializeJSReceiverSlow( |
Handle<JSReceiver> object) { |
Handle<FixedArray> contents = property_list_; |
if (contents.is_null()) { |
@@ -580,7 +569,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSReceiverSlow( |
return SUCCESS; |
} |
-BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy( |
+JsonStringifier::Result JsonStringifier::SerializeJSProxy( |
Handle<JSProxy> object) { |
Result stack_push = StackPush(object); |
if (stack_push != SUCCESS) return stack_push; |
@@ -615,7 +604,7 @@ BasicJsonStringifier::Result BasicJsonStringifier::SerializeJSProxy( |
} |
template <typename SrcChar, typename DestChar> |
-void BasicJsonStringifier::SerializeStringUnchecked_( |
+void JsonStringifier::SerializeStringUnchecked_( |
Vector<const SrcChar> src, |
IncrementalStringBuilder::NoExtend<DestChar>* dest) { |
// Assert that uc16 character is not truncated down to 8 bit. |
@@ -633,7 +622,7 @@ void BasicJsonStringifier::SerializeStringUnchecked_( |
} |
template <typename SrcChar, typename DestChar> |
-void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
+void JsonStringifier::SerializeString_(Handle<String> string) { |
int length = string->length(); |
builder_.Append<uint8_t, DestChar>('"'); |
// We make a rough estimate to find out if the current string can be |
@@ -663,35 +652,35 @@ void BasicJsonStringifier::SerializeString_(Handle<String> string) { |
} |
template <> |
-bool BasicJsonStringifier::DoNotEscape(uint8_t c) { |
+bool JsonStringifier::DoNotEscape(uint8_t c) { |
return c >= '#' && c <= '~' && c != '\\'; |
} |
template <> |
-bool BasicJsonStringifier::DoNotEscape(uint16_t c) { |
+bool JsonStringifier::DoNotEscape(uint16_t c) { |
return c >= '#' && c != '\\' && c != 0x7f; |
} |
-void BasicJsonStringifier::NewLine() { |
+void JsonStringifier::NewLine() { |
if (gap_ == nullptr) return; |
builder_.AppendCharacter('\n'); |
for (int i = 0; i < indent_; i++) builder_.AppendCString(gap_); |
} |
-void BasicJsonStringifier::Separator(bool first) { |
+void JsonStringifier::Separator(bool first) { |
if (!first) builder_.AppendCharacter(','); |
NewLine(); |
} |
-void BasicJsonStringifier::SerializeDeferredKey(bool deferred_comma, |
- Handle<Object> deferred_key) { |
+void JsonStringifier::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) { |
+void JsonStringifier::SerializeString(Handle<String> object) { |
object = String::Flatten(object); |
if (builder_.CurrentEncoding() == String::ONE_BYTE_ENCODING) { |
if (object->IsOneByteRepresentationUnderneath()) { |