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" |
(...skipping 1118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1129 return ReadHostObject(); | 1129 return ReadHostObject(); |
1130 } | 1130 } |
1131 } | 1131 } |
1132 | 1132 |
1133 MaybeHandle<String> ValueDeserializer::ReadUtf8String() { | 1133 MaybeHandle<String> ValueDeserializer::ReadUtf8String() { |
1134 uint32_t utf8_length; | 1134 uint32_t utf8_length; |
1135 Vector<const uint8_t> utf8_bytes; | 1135 Vector<const uint8_t> utf8_bytes; |
1136 if (!ReadVarint<uint32_t>().To(&utf8_length) || | 1136 if (!ReadVarint<uint32_t>().To(&utf8_length) || |
1137 utf8_length > | 1137 utf8_length > |
1138 static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) || | 1138 static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) || |
1139 !ReadRawBytes(utf8_length).To(&utf8_bytes)) | 1139 !ReadRawBytes(utf8_length).To(&utf8_bytes)) { |
1140 return MaybeHandle<String>(); | 1140 return MaybeHandle<String>(); |
| 1141 } |
1141 return isolate_->factory()->NewStringFromUtf8( | 1142 return isolate_->factory()->NewStringFromUtf8( |
1142 Vector<const char>::cast(utf8_bytes), pretenure_); | 1143 Vector<const char>::cast(utf8_bytes), pretenure_); |
1143 } | 1144 } |
1144 | 1145 |
1145 MaybeHandle<String> ValueDeserializer::ReadTwoByteString() { | 1146 MaybeHandle<String> ValueDeserializer::ReadTwoByteString() { |
1146 uint32_t byte_length; | 1147 uint32_t byte_length; |
1147 Vector<const uint8_t> bytes; | 1148 Vector<const uint8_t> bytes; |
1148 if (!ReadVarint<uint32_t>().To(&byte_length) || | 1149 if (!ReadVarint<uint32_t>().To(&byte_length) || |
1149 byte_length > | 1150 byte_length > |
1150 static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) || | 1151 static_cast<uint32_t>(std::numeric_limits<int32_t>::max()) || |
1151 byte_length % sizeof(uc16) != 0 || !ReadRawBytes(byte_length).To(&bytes)) | 1152 byte_length % sizeof(uc16) != 0 || |
| 1153 !ReadRawBytes(byte_length).To(&bytes)) { |
1152 return MaybeHandle<String>(); | 1154 return MaybeHandle<String>(); |
| 1155 } |
1153 | 1156 |
1154 // Allocate an uninitialized string so that we can do a raw memcpy into the | 1157 // Allocate an uninitialized string so that we can do a raw memcpy into the |
1155 // string on the heap (regardless of alignment). | 1158 // string on the heap (regardless of alignment). |
| 1159 if (byte_length == 0) return isolate_->factory()->empty_string(); |
1156 Handle<SeqTwoByteString> string; | 1160 Handle<SeqTwoByteString> string; |
1157 if (!isolate_->factory() | 1161 if (!isolate_->factory() |
1158 ->NewRawTwoByteString(byte_length / sizeof(uc16), pretenure_) | 1162 ->NewRawTwoByteString(byte_length / sizeof(uc16), pretenure_) |
1159 .ToHandle(&string)) | 1163 .ToHandle(&string)) { |
1160 return MaybeHandle<String>(); | 1164 return MaybeHandle<String>(); |
| 1165 } |
1161 | 1166 |
1162 // Copy the bytes directly into the new string. | 1167 // Copy the bytes directly into the new string. |
1163 // Warning: this uses host endianness. | 1168 // Warning: this uses host endianness. |
1164 memcpy(string->GetChars(), bytes.begin(), bytes.length()); | 1169 memcpy(string->GetChars(), bytes.begin(), bytes.length()); |
1165 return string; | 1170 return string; |
1166 } | 1171 } |
1167 | 1172 |
1168 bool ValueDeserializer::ReadExpectedString(Handle<String> expected) { | 1173 bool ValueDeserializer::ReadExpectedString(Handle<String> expected) { |
1169 // In the case of failure, the position in the stream is reset. | 1174 // In the case of failure, the position in the stream is reset. |
1170 const uint8_t* original_position = position_; | 1175 const uint8_t* original_position = position_; |
(...skipping 701 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1872 if (stack.size() != 1) { | 1877 if (stack.size() != 1) { |
1873 isolate_->Throw(*isolate_->factory()->NewError( | 1878 isolate_->Throw(*isolate_->factory()->NewError( |
1874 MessageTemplate::kDataCloneDeserializationError)); | 1879 MessageTemplate::kDataCloneDeserializationError)); |
1875 return MaybeHandle<Object>(); | 1880 return MaybeHandle<Object>(); |
1876 } | 1881 } |
1877 return scope.CloseAndEscape(stack[0]); | 1882 return scope.CloseAndEscape(stack[0]); |
1878 } | 1883 } |
1879 | 1884 |
1880 } // namespace internal | 1885 } // namespace internal |
1881 } // namespace v8 | 1886 } // namespace v8 |
OLD | NEW |