Index: src/value-serializer.cc |
diff --git a/src/value-serializer.cc b/src/value-serializer.cc |
index 8416ea3cd61dea9ad11ba148f83339153b4e4f4a..3d878900e605ccbf0f193b07ba68c18dcbf9d74d 100644 |
--- a/src/value-serializer.cc |
+++ b/src/value-serializer.cc |
@@ -375,13 +375,64 @@ Maybe<bool> ValueSerializer::WriteJSReceiver(Handle<JSReceiver> receiver) { |
} |
Maybe<bool> ValueSerializer::WriteJSObject(Handle<JSObject> object) { |
+ DCHECK(object->map()->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER); |
Camillo Bruni
2016/09/01 20:50:32
nit: Use DCHECK_GT(left..., right..)
jbroman
2016/09/02 15:31:42
Done.
|
+ const bool can_serialize_fast = |
+ object->HasFastProperties() && object->elements()->length() == 0; |
Camillo Bruni
2016/09/01 20:50:32
I guess this is WIP, elements() can be easily seri
Jakob Kummerow
2016/09/02 10:44:23
It's not that easy. Any element that's an object i
jbroman
2016/09/02 15:31:42
+1. And this matches what json-stringifier does (w
|
+ if (!can_serialize_fast) return WriteJSObjectSlow(object); |
+ |
+ DCHECK(!object->IsJSGlobalProxy()); |
+ DCHECK(!object->HasIndexedInterceptor()); |
+ DCHECK(!object->HasNamedInterceptor()); |
+ Handle<Map> map(object->map(), isolate_); |
+ WriteTag(SerializationTag::kBeginJSObject); |
+ |
+ // Write out fast properties as long as they are only data properties and the |
+ // map doesn't change. |
+ uint32_t properties_written = 0; |
+ for (int i = 0; i < map->NumberOfOwnDescriptors(); i++) { |
+ Handle<Name> key(map->instance_descriptors()->GetKey(i), isolate_); |
+ if (!key->IsString()) continue; |
+ PropertyDetails details = map->instance_descriptors()->GetDetails(i); |
+ if (details.IsDontEnum()) continue; |
+ |
+ Handle<Object> value; |
+ if (V8_LIKELY(details.type() == DATA && *map == object->map())) { |
+ FieldIndex field_index = FieldIndex::ForDescriptor(*map, i); |
+ value = JSObject::FastPropertyAt(object, details.representation(), |
+ field_index); |
+ } else { |
+ // This logic should essentially match WriteJSObjectPropertiesSlow. |
+ bool success; |
+ LookupIterator it = LookupIterator::PropertyOrElement( |
+ isolate_, object, key, &success, LookupIterator::OWN); |
Camillo Bruni
2016/09/01 20:50:32
You can bypass the arrayIndex check in PropertyOrE
jbroman
2016/09/02 15:31:42
Done.
|
+ DCHECK(success); |
+ if (!Object::GetProperty(&it).ToHandle(&value)) return Nothing<bool>(); |
+ |
+ // If the property is no longer found, do not serialize it. |
+ // This could happen if a getter deleted the property. |
+ if (!it.IsFound()) continue; |
Camillo Bruni
2016/09/01 20:50:32
You can push the IsFound() check above the GetProp
jbroman
2016/09/02 15:31:42
Done.
|
+ } |
+ |
+ if (!WriteObject(key).FromMaybe(false) || |
+ !WriteObject(value).FromMaybe(false)) { |
+ return Nothing<bool>(); |
+ } |
+ properties_written++; |
+ } |
+ |
+ WriteTag(SerializationTag::kEndJSObject); |
+ WriteVarint<uint32_t>(properties_written); |
+ return Just(true); |
+} |
+ |
+Maybe<bool> ValueSerializer::WriteJSObjectSlow(Handle<JSObject> object) { |
WriteTag(SerializationTag::kBeginJSObject); |
Handle<FixedArray> keys; |
uint32_t properties_written; |
if (!KeyAccumulator::GetKeys(object, KeyCollectionMode::kOwnOnly, |
ENUMERABLE_STRINGS) |
.ToHandle(&keys) || |
- !WriteJSObjectProperties(object, keys).To(&properties_written)) { |
+ !WriteJSObjectPropertiesSlow(object, keys).To(&properties_written)) { |
return Nothing<bool>(); |
} |
WriteTag(SerializationTag::kEndJSObject); |
@@ -428,7 +479,7 @@ Maybe<bool> ValueSerializer::WriteJSArray(Handle<JSArray> array) { |
Handle<FixedArray> keys = |
accumulator.GetKeys(GetKeysConversion::kConvertToString); |
uint32_t properties_written; |
- if (!WriteJSObjectProperties(array, keys).To(&properties_written)) { |
+ if (!WriteJSObjectPropertiesSlow(array, keys).To(&properties_written)) { |
return Nothing<bool>(); |
} |
WriteTag(SerializationTag::kEndDenseJSArray); |
@@ -442,7 +493,7 @@ Maybe<bool> ValueSerializer::WriteJSArray(Handle<JSArray> array) { |
if (!KeyAccumulator::GetKeys(array, KeyCollectionMode::kOwnOnly, |
ENUMERABLE_STRINGS) |
.ToHandle(&keys) || |
- !WriteJSObjectProperties(array, keys).To(&properties_written)) { |
+ !WriteJSObjectPropertiesSlow(array, keys).To(&properties_written)) { |
return Nothing<bool>(); |
} |
WriteTag(SerializationTag::kEndSparseJSArray); |
@@ -601,7 +652,7 @@ Maybe<bool> ValueSerializer::WriteJSArrayBufferView(JSArrayBufferView* view) { |
return Just(true); |
} |
-Maybe<uint32_t> ValueSerializer::WriteJSObjectProperties( |
+Maybe<uint32_t> ValueSerializer::WriteJSObjectPropertiesSlow( |
Handle<JSObject> object, Handle<FixedArray> keys) { |
uint32_t properties_written = 0; |
int length = keys->length(); |