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

Unified Diff: src/value-serializer.cc

Issue 2264403004: Blink-compatible serialization of ArrayBuffer. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: review comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/value-serializer.h ('k') | test/unittests/value-serializer-unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/value-serializer.cc
diff --git a/src/value-serializer.cc b/src/value-serializer.cc
index 066d4250a88e734abe5f77bb727d8b4afb233d07..756732043a404595d9412b9079d64678416906e8 100644
--- a/src/value-serializer.cc
+++ b/src/value-serializer.cc
@@ -90,6 +90,8 @@ enum class SerializationTag : uint8_t {
kBeginJSSet = '\'',
// End of a JS set. length:uint32_t.
kEndJSSet = ',',
+ // Array buffer. byteLength:uint32_t, then raw data.
+ kArrayBuffer = 'B',
};
ValueSerializer::ValueSerializer(Isolate* isolate)
@@ -158,6 +160,11 @@ void ValueSerializer::WriteTwoByteString(Vector<const uc16> chars) {
reinterpret_cast<const uint8_t*>(chars.end()));
}
+void ValueSerializer::WriteRawBytes(const void* source, size_t length) {
+ const uint8_t* begin = reinterpret_cast<const uint8_t*>(source);
+ buffer_.insert(buffer_.end(), begin, begin + length);
+}
+
uint8_t* ValueSerializer::ReserveRawBytes(size_t bytes) {
if (!bytes) return nullptr;
auto old_size = buffer_.size();
@@ -301,6 +308,8 @@ Maybe<bool> ValueSerializer::WriteJSReceiver(Handle<JSReceiver> receiver) {
return WriteJSMap(Handle<JSMap>::cast(receiver));
case JS_SET_TYPE:
return WriteJSSet(Handle<JSSet>::cast(receiver));
+ case JS_ARRAY_BUFFER_TYPE:
+ return WriteJSArrayBuffer(JSArrayBuffer::cast(*receiver));
default:
UNIMPLEMENTED();
break;
@@ -490,6 +499,18 @@ Maybe<bool> ValueSerializer::WriteJSSet(Handle<JSSet> set) {
return Just(true);
}
+Maybe<bool> ValueSerializer::WriteJSArrayBuffer(JSArrayBuffer* array_buffer) {
+ if (array_buffer->was_neutered()) return Nothing<bool>();
+ double byte_length = array_buffer->byte_length()->Number();
+ if (byte_length > std::numeric_limits<uint32_t>::max()) {
+ return Nothing<bool>();
+ }
+ WriteTag(SerializationTag::kArrayBuffer);
+ WriteVarint<uint32_t>(byte_length);
+ WriteRawBytes(array_buffer->backing_store(), byte_length);
+ return Just(true);
+}
+
Maybe<uint32_t> ValueSerializer::WriteJSObjectProperties(
Handle<JSObject> object, Handle<FixedArray> keys) {
uint32_t properties_written = 0;
@@ -683,6 +704,8 @@ MaybeHandle<Object> ValueDeserializer::ReadObject() {
return ReadJSMap();
case SerializationTag::kBeginJSSet:
return ReadJSSet();
+ case SerializationTag::kArrayBuffer:
+ return ReadJSArrayBuffer();
default:
return MaybeHandle<Object>();
}
@@ -951,6 +974,24 @@ MaybeHandle<JSSet> ValueDeserializer::ReadJSSet() {
return scope.CloseAndEscape(set);
}
+MaybeHandle<JSArrayBuffer> ValueDeserializer::ReadJSArrayBuffer() {
+ uint32_t id = next_id_++;
+ uint32_t byte_length;
+ Vector<const uint8_t> bytes;
+ if (!ReadVarint<uint32_t>().To(&byte_length) ||
+ byte_length > static_cast<size_t>(end_ - position_)) {
+ return MaybeHandle<JSArrayBuffer>();
+ }
+ const bool should_initialize = false;
+ Handle<JSArrayBuffer> array_buffer = isolate_->factory()->NewJSArrayBuffer();
+ JSArrayBuffer::SetupAllocatingData(array_buffer, isolate_, byte_length,
+ should_initialize);
+ memcpy(array_buffer->backing_store(), position_, byte_length);
+ position_ += byte_length;
+ AddObjectWithID(id, array_buffer);
+ return array_buffer;
+}
+
Maybe<uint32_t> ValueDeserializer::ReadJSObjectProperties(
Handle<JSObject> object, SerializationTag end_tag) {
for (uint32_t num_properties = 0;; num_properties++) {
« no previous file with comments | « src/value-serializer.h ('k') | test/unittests/value-serializer-unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698