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 "src/base/logging.h" | 9 #include "src/base/logging.h" |
10 #include "src/conversions.h" | 10 #include "src/conversions.h" |
11 #include "src/factory.h" | 11 #include "src/factory.h" |
12 #include "src/flags.h" | 12 #include "src/flags.h" |
13 #include "src/handles-inl.h" | 13 #include "src/handles-inl.h" |
14 #include "src/isolate.h" | 14 #include "src/isolate.h" |
15 #include "src/objects-inl.h" | 15 #include "src/objects-inl.h" |
16 #include "src/objects.h" | 16 #include "src/objects.h" |
17 #include "src/snapshot/code-serializer.h" | 17 #include "src/snapshot/code-serializer.h" |
18 #include "src/transitions.h" | 18 #include "src/transitions.h" |
19 #include "src/wasm/wasm-module.h" | 19 #include "src/wasm/wasm-module.h" |
20 #include "src/wasm/wasm-objects.h" | 20 #include "src/wasm/wasm-objects.h" |
21 #include "src/wasm/wasm-result.h" | 21 #include "src/wasm/wasm-result.h" |
22 | 22 |
23 namespace v8 { | 23 namespace v8 { |
24 namespace internal { | 24 namespace internal { |
25 | 25 |
26 // Version 9: (imported from Blink) | 26 // Version 9: (imported from Blink) |
27 // Version 10: one-byte (Latin-1) strings | 27 // Version 10: one-byte (Latin-1) strings |
28 // Version 11: properly separate undefined from the hole in arrays | 28 // Version 11: properly separate undefined from the hole in arrays |
29 // Version 12: regexp and string objects share normal string encoding | 29 // Version 12: regexp and string objects share normal string encoding |
30 static const uint32_t kLatestVersion = 12; | 30 // Version 13: host objects have an explicit tag (rather than handling all |
| 31 // unknown tags) |
| 32 static const uint32_t kLatestVersion = 13; |
31 | 33 |
32 static const int kPretenureThreshold = 100 * KB; | 34 static const int kPretenureThreshold = 100 * KB; |
33 | 35 |
34 template <typename T> | 36 template <typename T> |
35 static size_t BytesNeededForVarint(T value) { | 37 static size_t BytesNeededForVarint(T value) { |
36 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, | 38 static_assert(std::is_integral<T>::value && std::is_unsigned<T>::value, |
37 "Only unsigned integer types can be written as varints."); | 39 "Only unsigned integer types can be written as varints."); |
38 size_t result = 0; | 40 size_t result = 0; |
39 do { | 41 do { |
40 result++; | 42 result++; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // ObjectReference to one) serialized just before it. This is a quirk arising | 119 // ObjectReference to one) serialized just before it. This is a quirk arising |
118 // from the previous stack-based implementation. | 120 // from the previous stack-based implementation. |
119 kArrayBufferView = 'V', | 121 kArrayBufferView = 'V', |
120 // Shared array buffer. transferID:uint32_t | 122 // Shared array buffer. transferID:uint32_t |
121 kSharedArrayBuffer = 'u', | 123 kSharedArrayBuffer = 'u', |
122 // Compiled WebAssembly module. encodingType:(one-byte tag). | 124 // Compiled WebAssembly module. encodingType:(one-byte tag). |
123 // If encodingType == 'y' (raw bytes): | 125 // If encodingType == 'y' (raw bytes): |
124 // wasmWireByteLength:uint32_t, then raw data | 126 // wasmWireByteLength:uint32_t, then raw data |
125 // compiledDataLength:uint32_t, then raw data | 127 // compiledDataLength:uint32_t, then raw data |
126 kWasmModule = 'W', | 128 kWasmModule = 'W', |
| 129 // The delegate is responsible for processing all following data. |
| 130 // This "escapes" to whatever wire format the delegate chooses. |
| 131 kHostObject = '\\', |
127 }; | 132 }; |
128 | 133 |
129 namespace { | 134 namespace { |
130 | 135 |
131 enum class ArrayBufferViewTag : uint8_t { | 136 enum class ArrayBufferViewTag : uint8_t { |
132 kInt8Array = 'b', | 137 kInt8Array = 'b', |
133 kUint8Array = 'B', | 138 kUint8Array = 'B', |
134 kUint8ClampedArray = 'C', | 139 kUint8ClampedArray = 'C', |
135 kInt16Array = 'w', | 140 kInt16Array = 'w', |
136 kUint16Array = 'W', | 141 kUint16Array = 'W', |
(...skipping 679 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 WasmCompiledModuleSerializer::SerializeWasmModule(isolate_, | 821 WasmCompiledModuleSerializer::SerializeWasmModule(isolate_, |
817 compiled_part); | 822 compiled_part); |
818 int script_data_length = script_data->length(); | 823 int script_data_length = script_data->length(); |
819 WriteVarint<uint32_t>(script_data_length); | 824 WriteVarint<uint32_t>(script_data_length); |
820 WriteRawBytes(script_data->data(), script_data_length); | 825 WriteRawBytes(script_data->data(), script_data_length); |
821 | 826 |
822 return ThrowIfOutOfMemory(); | 827 return ThrowIfOutOfMemory(); |
823 } | 828 } |
824 | 829 |
825 Maybe<bool> ValueSerializer::WriteHostObject(Handle<JSObject> object) { | 830 Maybe<bool> ValueSerializer::WriteHostObject(Handle<JSObject> object) { |
| 831 WriteTag(SerializationTag::kHostObject); |
826 if (!delegate_) { | 832 if (!delegate_) { |
827 isolate_->Throw(*isolate_->factory()->NewError( | 833 isolate_->Throw(*isolate_->factory()->NewError( |
828 isolate_->error_function(), MessageTemplate::kDataCloneError, object)); | 834 isolate_->error_function(), MessageTemplate::kDataCloneError, object)); |
829 return Nothing<bool>(); | 835 return Nothing<bool>(); |
830 } | 836 } |
831 v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_); | 837 v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate_); |
832 Maybe<bool> result = | 838 Maybe<bool> result = |
833 delegate_->WriteHostObject(v8_isolate, Utils::ToLocal(object)); | 839 delegate_->WriteHostObject(v8_isolate, Utils::ToLocal(object)); |
834 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate_, Nothing<bool>()); | 840 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate_, Nothing<bool>()); |
835 DCHECK(!result.IsNothing()); | 841 DCHECK(!result.IsNothing()); |
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1137 case SerializationTag::kArrayBufferTransfer: { | 1143 case SerializationTag::kArrayBufferTransfer: { |
1138 const bool is_shared = false; | 1144 const bool is_shared = false; |
1139 return ReadTransferredJSArrayBuffer(is_shared); | 1145 return ReadTransferredJSArrayBuffer(is_shared); |
1140 } | 1146 } |
1141 case SerializationTag::kSharedArrayBuffer: { | 1147 case SerializationTag::kSharedArrayBuffer: { |
1142 const bool is_shared = true; | 1148 const bool is_shared = true; |
1143 return ReadTransferredJSArrayBuffer(is_shared); | 1149 return ReadTransferredJSArrayBuffer(is_shared); |
1144 } | 1150 } |
1145 case SerializationTag::kWasmModule: | 1151 case SerializationTag::kWasmModule: |
1146 return ReadWasmModule(); | 1152 return ReadWasmModule(); |
| 1153 case SerializationTag::kHostObject: |
| 1154 return ReadHostObject(); |
1147 default: | 1155 default: |
1148 // TODO(jbroman): Introduce an explicit tag for host objects to avoid | 1156 // Before there was an explicit tag for host objects, all unknown tags |
1149 // having to treat every unknown tag as a potential host object. | 1157 // were delegated to the host. |
1150 position_--; | 1158 if (version_ < 13) { |
1151 return ReadHostObject(); | 1159 position_--; |
| 1160 return ReadHostObject(); |
| 1161 } |
| 1162 return MaybeHandle<Object>(); |
1152 } | 1163 } |
1153 } | 1164 } |
1154 | 1165 |
1155 MaybeHandle<String> ValueDeserializer::ReadString() { | 1166 MaybeHandle<String> ValueDeserializer::ReadString() { |
1156 if (version_ < 12) return ReadUtf8String(); | 1167 if (version_ < 12) return ReadUtf8String(); |
1157 Handle<Object> object; | 1168 Handle<Object> object; |
1158 if (!ReadObject().ToHandle(&object) || !object->IsString()) { | 1169 if (!ReadObject().ToHandle(&object) || !object->IsString()) { |
1159 return MaybeHandle<String>(); | 1170 return MaybeHandle<String>(); |
1160 } | 1171 } |
1161 return Handle<String>::cast(object); | 1172 return Handle<String>::cast(object); |
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1944 if (stack.size() != 1) { | 1955 if (stack.size() != 1) { |
1945 isolate_->Throw(*isolate_->factory()->NewError( | 1956 isolate_->Throw(*isolate_->factory()->NewError( |
1946 MessageTemplate::kDataCloneDeserializationError)); | 1957 MessageTemplate::kDataCloneDeserializationError)); |
1947 return MaybeHandle<Object>(); | 1958 return MaybeHandle<Object>(); |
1948 } | 1959 } |
1949 return scope.CloseAndEscape(stack[0]); | 1960 return scope.CloseAndEscape(stack[0]); |
1950 } | 1961 } |
1951 | 1962 |
1952 } // namespace internal | 1963 } // namespace internal |
1953 } // namespace v8 | 1964 } // namespace v8 |
OLD | NEW |