| Index: src/value-serializer.cc
|
| diff --git a/src/value-serializer.cc b/src/value-serializer.cc
|
| index d0ca6f1601e47577001971a808ba87c55cda0cfb..da8ad5e56421634bbbd9434f917369cf5f8d5a84 100644
|
| --- a/src/value-serializer.cc
|
| +++ b/src/value-serializer.cc
|
| @@ -217,26 +217,18 @@
|
| }
|
|
|
| void ValueSerializer::WriteRawBytes(const void* source, size_t length) {
|
| - uint8_t* dest;
|
| - if (ReserveRawBytes(length).To(&dest)) {
|
| - memcpy(dest, source, length);
|
| - }
|
| -}
|
| -
|
| -Maybe<uint8_t*> ValueSerializer::ReserveRawBytes(size_t bytes) {
|
| + memcpy(ReserveRawBytes(length), source, length);
|
| +}
|
| +
|
| +uint8_t* ValueSerializer::ReserveRawBytes(size_t bytes) {
|
| size_t old_size = buffer_size_;
|
| size_t new_size = old_size + bytes;
|
| - if (new_size > buffer_capacity_) {
|
| - bool ok;
|
| - if (!ExpandBuffer(new_size).To(&ok)) {
|
| - return Nothing<uint8_t*>();
|
| - }
|
| - }
|
| + if (new_size > buffer_capacity_) ExpandBuffer(new_size);
|
| buffer_size_ = new_size;
|
| - return Just(&buffer_[old_size]);
|
| -}
|
| -
|
| -Maybe<bool> ValueSerializer::ExpandBuffer(size_t required_capacity) {
|
| + return &buffer_[old_size];
|
| +}
|
| +
|
| +void ValueSerializer::ExpandBuffer(size_t required_capacity) {
|
| DCHECK_GT(required_capacity, buffer_capacity_);
|
| size_t requested_capacity =
|
| std::max(required_capacity, buffer_capacity_ * 2) + 64;
|
| @@ -249,15 +241,9 @@
|
| new_buffer = realloc(buffer_, requested_capacity);
|
| provided_capacity = requested_capacity;
|
| }
|
| - if (new_buffer) {
|
| - DCHECK(provided_capacity >= requested_capacity);
|
| - buffer_ = reinterpret_cast<uint8_t*>(new_buffer);
|
| - buffer_capacity_ = provided_capacity;
|
| - return Just(true);
|
| - } else {
|
| - out_of_memory_ = true;
|
| - return Nothing<bool>();
|
| - }
|
| + DCHECK_GE(provided_capacity, requested_capacity);
|
| + buffer_ = reinterpret_cast<uint8_t*>(new_buffer);
|
| + buffer_capacity_ = provided_capacity;
|
| }
|
|
|
| void ValueSerializer::WriteUint32(uint32_t value) {
|
| @@ -288,21 +274,20 @@
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteObject(Handle<Object> object) {
|
| - out_of_memory_ = false;
|
| if (object->IsSmi()) {
|
| WriteSmi(Smi::cast(*object));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| DCHECK(object->IsHeapObject());
|
| switch (HeapObject::cast(*object)->map()->instance_type()) {
|
| case ODDBALL_TYPE:
|
| WriteOddball(Oddball::cast(*object));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| case HEAP_NUMBER_TYPE:
|
| case MUTABLE_HEAP_NUMBER_TYPE:
|
| WriteHeapNumber(HeapNumber::cast(*object));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| case JS_TYPED_ARRAY_TYPE:
|
| case JS_DATA_VIEW_TYPE: {
|
| // Despite being JSReceivers, these have their wrapped buffer serialized
|
| @@ -323,7 +308,7 @@
|
| default:
|
| if (object->IsString()) {
|
| WriteString(Handle<String>::cast(object));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| } else if (object->IsJSReceiver()) {
|
| return WriteJSReceiver(Handle<JSReceiver>::cast(object));
|
| } else {
|
| @@ -384,11 +369,9 @@
|
| v8::Local<v8::String> api_string = Utils::ToLocal(string);
|
| uint32_t utf8_length = api_string->Utf8Length();
|
| WriteVarint(utf8_length);
|
| - uint8_t* dest;
|
| - if (ReserveRawBytes(utf8_length).To(&dest)) {
|
| - api_string->WriteUtf8(reinterpret_cast<char*>(dest), utf8_length,
|
| - nullptr, v8::String::NO_NULL_TERMINATION);
|
| - }
|
| + api_string->WriteUtf8(
|
| + reinterpret_cast<char*>(ReserveRawBytes(utf8_length)), utf8_length,
|
| + nullptr, v8::String::NO_NULL_TERMINATION);
|
| }
|
| } else if (flat.IsTwoByte()) {
|
| Vector<const uc16> chars = flat.ToUC16Vector();
|
| @@ -409,7 +392,7 @@
|
| if (uint32_t id = *id_map_entry) {
|
| WriteTag(SerializationTag::kObjectReference);
|
| WriteVarint(id - 1);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| // Otherwise, allocate an ID for it.
|
| @@ -449,12 +432,12 @@
|
| return WriteHostObject(Handle<JSObject>::cast(receiver));
|
| case JS_DATE_TYPE:
|
| WriteJSDate(JSDate::cast(*receiver));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| case JS_VALUE_TYPE:
|
| return WriteJSValue(Handle<JSValue>::cast(receiver));
|
| case JS_REGEXP_TYPE:
|
| WriteJSRegExp(JSRegExp::cast(*receiver));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| case JS_MAP_TYPE:
|
| return WriteJSMap(Handle<JSMap>::cast(receiver));
|
| case JS_SET_TYPE:
|
| @@ -515,7 +498,7 @@
|
|
|
| WriteTag(SerializationTag::kEndJSObject);
|
| WriteVarint<uint32_t>(properties_written);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteJSObjectSlow(Handle<JSObject> object) {
|
| @@ -530,7 +513,7 @@
|
| }
|
| WriteTag(SerializationTag::kEndJSObject);
|
| WriteVarint<uint32_t>(properties_written);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteJSArray(Handle<JSArray> array) {
|
| @@ -635,7 +618,7 @@
|
| WriteVarint<uint32_t>(properties_written);
|
| WriteVarint<uint32_t>(length);
|
| }
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| void ValueSerializer::WriteJSDate(JSDate* date) {
|
| @@ -660,17 +643,15 @@
|
| Utils::ToLocal(handle(String::cast(inner_value), isolate_));
|
| uint32_t utf8_length = api_string->Utf8Length();
|
| WriteVarint(utf8_length);
|
| - uint8_t* dest;
|
| - if (ReserveRawBytes(utf8_length).To(&dest)) {
|
| - api_string->WriteUtf8(reinterpret_cast<char*>(dest), utf8_length, nullptr,
|
| - v8::String::NO_NULL_TERMINATION);
|
| - }
|
| + api_string->WriteUtf8(reinterpret_cast<char*>(ReserveRawBytes(utf8_length)),
|
| + utf8_length, nullptr,
|
| + v8::String::NO_NULL_TERMINATION);
|
| } else {
|
| DCHECK(inner_value->IsSymbol());
|
| ThrowDataCloneError(MessageTemplate::kDataCloneError, value);
|
| return Nothing<bool>();
|
| }
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| void ValueSerializer::WriteJSRegExp(JSRegExp* regexp) {
|
| @@ -679,11 +660,8 @@
|
| Utils::ToLocal(handle(regexp->Pattern(), isolate_));
|
| uint32_t utf8_length = api_string->Utf8Length();
|
| WriteVarint(utf8_length);
|
| - uint8_t* dest;
|
| - if (ReserveRawBytes(utf8_length).To(&dest)) {
|
| - api_string->WriteUtf8(reinterpret_cast<char*>(dest), utf8_length, nullptr,
|
| - v8::String::NO_NULL_TERMINATION);
|
| - }
|
| + api_string->WriteUtf8(reinterpret_cast<char*>(ReserveRawBytes(utf8_length)),
|
| + utf8_length, nullptr, v8::String::NO_NULL_TERMINATION);
|
| WriteVarint(static_cast<uint32_t>(regexp->GetFlags()));
|
| }
|
|
|
| @@ -715,7 +693,7 @@
|
| }
|
| WriteTag(SerializationTag::kEndJSMap);
|
| WriteVarint<uint32_t>(length);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteJSSet(Handle<JSSet> set) {
|
| @@ -745,7 +723,7 @@
|
| }
|
| WriteTag(SerializationTag::kEndJSSet);
|
| WriteVarint<uint32_t>(length);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteJSArrayBuffer(
|
| @@ -763,14 +741,14 @@
|
|
|
| WriteTag(SerializationTag::kSharedArrayBuffer);
|
| WriteVarint(index.FromJust());
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| uint32_t* transfer_entry = array_buffer_transfer_map_.Find(array_buffer);
|
| if (transfer_entry) {
|
| WriteTag(SerializationTag::kArrayBufferTransfer);
|
| WriteVarint(*transfer_entry);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
| if (array_buffer->was_neutered()) {
|
| ThrowDataCloneError(MessageTemplate::kDataCloneErrorNeuteredArrayBuffer);
|
| @@ -784,7 +762,7 @@
|
| WriteTag(SerializationTag::kArrayBuffer);
|
| WriteVarint<uint32_t>(byte_length);
|
| WriteRawBytes(array_buffer->backing_store(), byte_length);
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteJSArrayBufferView(JSArrayBufferView* view) {
|
| @@ -806,7 +784,7 @@
|
| WriteVarint(static_cast<uint8_t>(tag));
|
| WriteVarint(NumberToUint32(view->byte_offset()));
|
| WriteVarint(NumberToUint32(view->byte_length()));
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteWasmModule(Handle<JSObject> object) {
|
| @@ -819,10 +797,8 @@
|
| Handle<String> wire_bytes(compiled_part->module_bytes(), isolate_);
|
| int wire_bytes_length = wire_bytes->length();
|
| WriteVarint<uint32_t>(wire_bytes_length);
|
| - uint8_t* destination;
|
| - if (ReserveRawBytes(wire_bytes_length).To(&destination)) {
|
| - String::WriteToFlat(*wire_bytes, destination, 0, wire_bytes_length);
|
| - }
|
| + uint8_t* destination = ReserveRawBytes(wire_bytes_length);
|
| + String::WriteToFlat(*wire_bytes, destination, 0, wire_bytes_length);
|
|
|
| std::unique_ptr<ScriptData> script_data =
|
| WasmCompiledModuleSerializer::SerializeWasmModule(isolate_,
|
| @@ -831,7 +807,7 @@
|
| WriteVarint<uint32_t>(script_data_length);
|
| WriteRawBytes(script_data->data(), script_data_length);
|
|
|
| - return ThrowIfOutOfMemory();
|
| + return Just(true);
|
| }
|
|
|
| Maybe<bool> ValueSerializer::WriteHostObject(Handle<JSObject> object) {
|
| @@ -880,14 +856,6 @@
|
| MessageTemplate::Template template_index) {
|
| return ThrowDataCloneError(template_index,
|
| isolate_->factory()->empty_string());
|
| -}
|
| -
|
| -Maybe<bool> ValueSerializer::ThrowIfOutOfMemory() {
|
| - if (out_of_memory_) {
|
| - ThrowDataCloneError(MessageTemplate::kDataCloneErrorOutOfMemory);
|
| - return Nothing<bool>();
|
| - }
|
| - return Just(true);
|
| }
|
|
|
| void ValueSerializer::ThrowDataCloneError(
|
|
|