| OLD | NEW |
| 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 "include/v8-value-serializer-version.h" | 9 #include "include/v8-value-serializer-version.h" |
| 10 #include "src/base/logging.h" | 10 #include "src/base/logging.h" |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 // If we are at the end of the stack, abort. This function may recurse. | 442 // If we are at the end of the stack, abort. This function may recurse. |
| 443 STACK_CHECK(isolate_, Nothing<bool>()); | 443 STACK_CHECK(isolate_, Nothing<bool>()); |
| 444 | 444 |
| 445 HandleScope scope(isolate_); | 445 HandleScope scope(isolate_); |
| 446 switch (instance_type) { | 446 switch (instance_type) { |
| 447 case JS_ARRAY_TYPE: | 447 case JS_ARRAY_TYPE: |
| 448 return WriteJSArray(Handle<JSArray>::cast(receiver)); | 448 return WriteJSArray(Handle<JSArray>::cast(receiver)); |
| 449 case JS_OBJECT_TYPE: | 449 case JS_OBJECT_TYPE: |
| 450 case JS_API_OBJECT_TYPE: { | 450 case JS_API_OBJECT_TYPE: { |
| 451 Handle<JSObject> js_object = Handle<JSObject>::cast(receiver); | 451 Handle<JSObject> js_object = Handle<JSObject>::cast(receiver); |
| 452 Map* map = js_object->map(); | 452 if (JSObject::GetEmbedderFieldCount(js_object->map())) { |
| 453 if (!FLAG_wasm_disable_structured_cloning && | |
| 454 map->GetConstructor() == | |
| 455 isolate_->native_context()->wasm_module_constructor()) { | |
| 456 return WriteWasmModule(js_object); | |
| 457 } else if (JSObject::GetEmbedderFieldCount(map)) { | |
| 458 return WriteHostObject(js_object); | 453 return WriteHostObject(js_object); |
| 459 } else { | 454 } else { |
| 460 return WriteJSObject(js_object); | 455 return WriteJSObject(js_object); |
| 461 } | 456 } |
| 462 } | 457 } |
| 463 case JS_SPECIAL_API_OBJECT_TYPE: | 458 case JS_SPECIAL_API_OBJECT_TYPE: |
| 464 return WriteHostObject(Handle<JSObject>::cast(receiver)); | 459 return WriteHostObject(Handle<JSObject>::cast(receiver)); |
| 465 case JS_DATE_TYPE: | 460 case JS_DATE_TYPE: |
| 466 WriteJSDate(JSDate::cast(*receiver)); | 461 WriteJSDate(JSDate::cast(*receiver)); |
| 467 return ThrowIfOutOfMemory(); | 462 return ThrowIfOutOfMemory(); |
| 468 case JS_VALUE_TYPE: | 463 case JS_VALUE_TYPE: |
| 469 return WriteJSValue(Handle<JSValue>::cast(receiver)); | 464 return WriteJSValue(Handle<JSValue>::cast(receiver)); |
| 470 case JS_REGEXP_TYPE: | 465 case JS_REGEXP_TYPE: |
| 471 WriteJSRegExp(JSRegExp::cast(*receiver)); | 466 WriteJSRegExp(JSRegExp::cast(*receiver)); |
| 472 return ThrowIfOutOfMemory(); | 467 return ThrowIfOutOfMemory(); |
| 473 case JS_MAP_TYPE: | 468 case JS_MAP_TYPE: |
| 474 return WriteJSMap(Handle<JSMap>::cast(receiver)); | 469 return WriteJSMap(Handle<JSMap>::cast(receiver)); |
| 475 case JS_SET_TYPE: | 470 case JS_SET_TYPE: |
| 476 return WriteJSSet(Handle<JSSet>::cast(receiver)); | 471 return WriteJSSet(Handle<JSSet>::cast(receiver)); |
| 477 case JS_ARRAY_BUFFER_TYPE: | 472 case JS_ARRAY_BUFFER_TYPE: |
| 478 return WriteJSArrayBuffer(Handle<JSArrayBuffer>::cast(receiver)); | 473 return WriteJSArrayBuffer(Handle<JSArrayBuffer>::cast(receiver)); |
| 479 case JS_TYPED_ARRAY_TYPE: | 474 case JS_TYPED_ARRAY_TYPE: |
| 480 case JS_DATA_VIEW_TYPE: | 475 case JS_DATA_VIEW_TYPE: |
| 481 return WriteJSArrayBufferView(JSArrayBufferView::cast(*receiver)); | 476 return WriteJSArrayBufferView(JSArrayBufferView::cast(*receiver)); |
| 477 case WASM_MODULE_TYPE: |
| 478 if (!FLAG_wasm_disable_structured_cloning) { |
| 479 // Only write WebAssembly modules if not disabled by a flag. |
| 480 return WriteWasmModule(Handle<WasmModuleObject>::cast(receiver)); |
| 481 } // fall through to error case |
| 482 default: | 482 default: |
| 483 ThrowDataCloneError(MessageTemplate::kDataCloneError, receiver); | 483 ThrowDataCloneError(MessageTemplate::kDataCloneError, receiver); |
| 484 return Nothing<bool>(); | 484 return Nothing<bool>(); |
| 485 } | 485 } |
| 486 return Nothing<bool>(); | 486 return Nothing<bool>(); |
| 487 } | 487 } |
| 488 | 488 |
| 489 Maybe<bool> ValueSerializer::WriteJSObject(Handle<JSObject> object) { | 489 Maybe<bool> ValueSerializer::WriteJSObject(Handle<JSObject> object) { |
| 490 DCHECK_GT(object->map()->instance_type(), LAST_CUSTOM_ELEMENTS_RECEIVER); | 490 DCHECK_GT(object->map()->instance_type(), LAST_CUSTOM_ELEMENTS_RECEIVER); |
| 491 const bool can_serialize_fast = | 491 const bool can_serialize_fast = |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 808 } else { | 808 } else { |
| 809 DCHECK(view->IsJSDataView()); | 809 DCHECK(view->IsJSDataView()); |
| 810 tag = ArrayBufferViewTag::kDataView; | 810 tag = ArrayBufferViewTag::kDataView; |
| 811 } | 811 } |
| 812 WriteVarint(static_cast<uint8_t>(tag)); | 812 WriteVarint(static_cast<uint8_t>(tag)); |
| 813 WriteVarint(NumberToUint32(view->byte_offset())); | 813 WriteVarint(NumberToUint32(view->byte_offset())); |
| 814 WriteVarint(NumberToUint32(view->byte_length())); | 814 WriteVarint(NumberToUint32(view->byte_length())); |
| 815 return ThrowIfOutOfMemory(); | 815 return ThrowIfOutOfMemory(); |
| 816 } | 816 } |
| 817 | 817 |
| 818 Maybe<bool> ValueSerializer::WriteWasmModule(Handle<JSObject> object) { | 818 Maybe<bool> ValueSerializer::WriteWasmModule(Handle<WasmModuleObject> object) { |
| 819 if (delegate_ != nullptr) { | 819 if (delegate_ != nullptr) { |
| 820 // TODO(titzer): introduce a Utils::ToLocal for WasmModuleObject. |
| 820 Maybe<uint32_t> transfer_id = delegate_->GetWasmModuleTransferId( | 821 Maybe<uint32_t> transfer_id = delegate_->GetWasmModuleTransferId( |
| 821 reinterpret_cast<v8::Isolate*>(isolate_), | 822 reinterpret_cast<v8::Isolate*>(isolate_), |
| 822 v8::Local<v8::WasmCompiledModule>::Cast(Utils::ToLocal(object))); | 823 v8::Local<v8::WasmCompiledModule>::Cast( |
| 824 Utils::ToLocal(Handle<JSObject>::cast(object)))); |
| 823 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate_, Nothing<bool>()); | 825 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate_, Nothing<bool>()); |
| 824 uint32_t id = 0; | 826 uint32_t id = 0; |
| 825 if (transfer_id.To(&id)) { | 827 if (transfer_id.To(&id)) { |
| 826 WriteTag(SerializationTag::kWasmModuleTransfer); | 828 WriteTag(SerializationTag::kWasmModuleTransfer); |
| 827 WriteVarint<uint32_t>(id); | 829 WriteVarint<uint32_t>(id); |
| 828 return Just(true); | 830 return Just(true); |
| 829 } | 831 } |
| 830 } | 832 } |
| 831 | 833 |
| 832 Handle<WasmCompiledModule> compiled_part( | 834 Handle<WasmCompiledModule> compiled_part(object->compiled_module(), isolate_); |
| 833 WasmCompiledModule::cast(object->GetEmbedderField(0)), isolate_); | |
| 834 WasmEncodingTag encoding_tag = WasmEncodingTag::kRawBytes; | 835 WasmEncodingTag encoding_tag = WasmEncodingTag::kRawBytes; |
| 835 WriteTag(SerializationTag::kWasmModule); | 836 WriteTag(SerializationTag::kWasmModule); |
| 836 WriteRawBytes(&encoding_tag, sizeof(encoding_tag)); | 837 WriteRawBytes(&encoding_tag, sizeof(encoding_tag)); |
| 837 | 838 |
| 838 Handle<String> wire_bytes(compiled_part->module_bytes(), isolate_); | 839 Handle<String> wire_bytes(compiled_part->module_bytes(), isolate_); |
| 839 int wire_bytes_length = wire_bytes->length(); | 840 int wire_bytes_length = wire_bytes->length(); |
| 840 WriteVarint<uint32_t>(wire_bytes_length); | 841 WriteVarint<uint32_t>(wire_bytes_length); |
| 841 uint8_t* destination; | 842 uint8_t* destination; |
| 842 if (ReserveRawBytes(wire_bytes_length).To(&destination)) { | 843 if (ReserveRawBytes(wire_bytes_length).To(&destination)) { |
| 843 String::WriteToFlat(*wire_bytes, destination, 0, wire_bytes_length); | 844 String::WriteToFlat(*wire_bytes, destination, 0, wire_bytes_length); |
| (...skipping 1190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2034 if (stack.size() != 1) { | 2035 if (stack.size() != 1) { |
| 2035 isolate_->Throw(*isolate_->factory()->NewError( | 2036 isolate_->Throw(*isolate_->factory()->NewError( |
| 2036 MessageTemplate::kDataCloneDeserializationError)); | 2037 MessageTemplate::kDataCloneDeserializationError)); |
| 2037 return MaybeHandle<Object>(); | 2038 return MaybeHandle<Object>(); |
| 2038 } | 2039 } |
| 2039 return scope.CloseAndEscape(stack[0]); | 2040 return scope.CloseAndEscape(stack[0]); |
| 2040 } | 2041 } |
| 2041 | 2042 |
| 2042 } // namespace internal | 2043 } // namespace internal |
| 2043 } // namespace v8 | 2044 } // namespace v8 |
| OLD | NEW |