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

Side by Side Diff: src/value-serializer.cc

Issue 2248893003: Blink-compatible deserialization of old object format. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@vs4
Patch Set: correct to size_t Created 4 years, 4 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
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/value-serializer.h" 5 #include "src/value-serializer.h"
6 6
7 #include <type_traits> 7 #include <type_traits>
8 8
9 #include "src/base/logging.h" 9 #include "src/base/logging.h"
10 #include "src/factory.h" 10 #include "src/factory.h"
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 const uint8_t* peek_position = position_; 320 const uint8_t* peek_position = position_;
321 SerializationTag tag; 321 SerializationTag tag;
322 do { 322 do {
323 if (peek_position >= end_) return Nothing<SerializationTag>(); 323 if (peek_position >= end_) return Nothing<SerializationTag>();
324 tag = static_cast<SerializationTag>(*peek_position); 324 tag = static_cast<SerializationTag>(*peek_position);
325 peek_position++; 325 peek_position++;
326 } while (tag == SerializationTag::kPadding); 326 } while (tag == SerializationTag::kPadding);
327 return Just(tag); 327 return Just(tag);
328 } 328 }
329 329
330 void ValueDeserializer::ConsumeTag(SerializationTag peeked_tag) {
331 SerializationTag actual_tag = ReadTag().ToChecked();
332 DCHECK(actual_tag == peeked_tag);
333 (void)actual_tag;
Camillo Bruni 2016/08/16 09:04:07 nit: you can use our very pretty USE macro here :)
jbroman 2016/08/16 21:34:14 I was looking for one, but didn't find it. Thanks.
334 }
335
330 Maybe<SerializationTag> ValueDeserializer::ReadTag() { 336 Maybe<SerializationTag> ValueDeserializer::ReadTag() {
331 SerializationTag tag; 337 SerializationTag tag;
332 do { 338 do {
333 if (position_ >= end_) return Nothing<SerializationTag>(); 339 if (position_ >= end_) return Nothing<SerializationTag>();
334 tag = static_cast<SerializationTag>(*position_); 340 tag = static_cast<SerializationTag>(*position_);
335 position_++; 341 position_++;
336 } while (tag == SerializationTag::kPadding); 342 } while (tag == SerializationTag::kPadding);
337 return Just(tag); 343 return Just(tag);
338 } 344 }
339 345
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 DCHECK(HasObjectWithID(id)); 511 DCHECK(HasObjectWithID(id));
506 return scope.CloseAndEscape(handle); 512 return scope.CloseAndEscape(handle);
507 } 513 }
508 514
509 Maybe<uint32_t> ValueDeserializer::ReadJSObjectProperties( 515 Maybe<uint32_t> ValueDeserializer::ReadJSObjectProperties(
510 Handle<JSObject> object, SerializationTag end_tag) { 516 Handle<JSObject> object, SerializationTag end_tag) {
511 for (uint32_t num_properties = 0;; num_properties++) { 517 for (uint32_t num_properties = 0;; num_properties++) {
512 SerializationTag tag; 518 SerializationTag tag;
513 if (!PeekTag().To(&tag)) return Nothing<uint32_t>(); 519 if (!PeekTag().To(&tag)) return Nothing<uint32_t>();
514 if (tag == end_tag) { 520 if (tag == end_tag) {
515 SerializationTag consumed_tag = ReadTag().ToChecked(); 521 ConsumeTag(end_tag);
516 (void)consumed_tag;
517 DCHECK(tag == consumed_tag);
518 return Just(num_properties); 522 return Just(num_properties);
519 } 523 }
520 524
521 Handle<Object> key; 525 Handle<Object> key;
522 if (!ReadObject().ToHandle(&key)) return Nothing<uint32_t>(); 526 if (!ReadObject().ToHandle(&key)) return Nothing<uint32_t>();
523 Handle<Object> value; 527 Handle<Object> value;
524 if (!ReadObject().ToHandle(&value)) return Nothing<uint32_t>(); 528 if (!ReadObject().ToHandle(&value)) return Nothing<uint32_t>();
525 529
526 bool success; 530 bool success;
527 LookupIterator it = LookupIterator::PropertyOrElement( 531 LookupIterator it = LookupIterator::PropertyOrElement(
(...skipping 26 matching lines...) Expand all
554 used_as_prototype); 558 used_as_prototype);
555 559
556 // If the dictionary was reallocated, update the global handle. 560 // If the dictionary was reallocated, update the global handle.
557 if (!new_dictionary.is_identical_to(id_map_)) { 561 if (!new_dictionary.is_identical_to(id_map_)) {
558 GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location()); 562 GlobalHandles::Destroy(Handle<Object>::cast(id_map_).location());
559 id_map_ = Handle<SeededNumberDictionary>::cast( 563 id_map_ = Handle<SeededNumberDictionary>::cast(
560 isolate_->global_handles()->Create(*new_dictionary)); 564 isolate_->global_handles()->Create(*new_dictionary));
561 } 565 }
562 } 566 }
563 567
568 static MaybeHandle<JSObject> CreateJSObjectFromKeyValuePairs(
569 Isolate* isolate, Handle<Object>* data, uint32_t num_properties) {
570 Handle<JSObject> object =
571 isolate->factory()->NewJSObject(isolate->object_function());
Camillo Bruni 2016/08/16 09:04:07 Performance Hint: This is a rather slow (but obvio
jbroman 2016/08/16 21:34:14 No problem. I've learned a lot the last couple wee
572 for (unsigned i = 0; i < 2 * num_properties; i += 2) {
573 Handle<Object> key = data[i];
574 Handle<Object> value = data[i + 1];
575 bool success;
576 LookupIterator it = LookupIterator::PropertyOrElement(
577 isolate, object, key, &success, LookupIterator::OWN);
578 if (!success ||
579 JSObject::DefineOwnPropertyIgnoreAttributes(&it, value, NONE).is_null())
580 return MaybeHandle<JSObject>();
Camillo Bruni 2016/08/16 09:04:07 nit: please add braces here, we don't allow traili
jbroman 2016/08/16 21:34:14 OK. As I mentioned in the other review, I couldn't
581 }
582 return object;
583 }
584
585 MaybeHandle<Object>
586 ValueDeserializer::ReadObjectUsingEntireBufferForLegacyFormat() {
587 if (version_ > 0) return MaybeHandle<Object>();
588
589 HandleScope scope(isolate_);
590 std::vector<Handle<Object>> stack;
591 while (position_ < end_) {
592 SerializationTag tag;
593 if (!PeekTag().To(&tag)) break;
594
595 Handle<Object> new_object;
596 switch (tag) {
597 case SerializationTag::kEndJSObject: {
598 ConsumeTag(SerializationTag::kEndJSObject);
599
600 // JS Object: Read the last 2*n values from the stack and use them as
601 // key-value pairs.
602 uint32_t num_properties;
603 if (!ReadVarint<uint32_t>().To(&num_properties) ||
604 stack.size() / 2 < num_properties)
605 return MaybeHandle<Object>();
Camillo Bruni 2016/08/16 09:04:07 nit: missing braces
jbroman 2016/08/16 21:34:14 Done.
606
607 size_t begin_properties = stack.size() - 2 * num_properties;
608 Handle<Object>* data =
609 num_properties ? &stack[begin_properties] : nullptr;
610 if (!CreateJSObjectFromKeyValuePairs(isolate_, data, num_properties)
611 .ToHandle(&new_object))
612 return MaybeHandle<Object>();
Camillo Bruni 2016/08/16 09:04:07 nit: braces
jbroman 2016/08/16 21:34:14 Done.
613
614 stack.resize(begin_properties);
615 break;
616 }
617 default:
618 if (!ReadObject().ToHandle(&new_object)) return MaybeHandle<Object>();
619 break;
620 }
621 stack.push_back(new_object);
622 }
623
624 // Nothing remains but padding.
625 #ifdef DEBUG
626 while (position_ < end_)
627 DCHECK(*position_++ == static_cast<uint8_t>(SerializationTag::kPadding));
Camillo Bruni 2016/08/16 09:04:07 nit: braces
jbroman 2016/08/16 21:34:14 Done.
628 #endif
629 position_ = end_;
630
631 if (stack.size() != 1) return MaybeHandle<Object>();
632 return scope.CloseAndEscape(stack[0]);
633 }
634
564 } // namespace internal 635 } // namespace internal
565 } // namespace v8 636 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698