| 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 <algorithm> |     7 #include <algorithm> | 
|     8 #include <string> |     8 #include <string> | 
|     9  |     9  | 
|    10 #include "include/v8.h" |    10 #include "include/v8.h" | 
| (...skipping 29 matching lines...) Expand all  Loading... | 
|    40  |    40  | 
|    41   // Variant for the common case where a script is used to build the original |    41   // Variant for the common case where a script is used to build the original | 
|    42   // value. |    42   // value. | 
|    43   template <typename OutputFunctor> |    43   template <typename OutputFunctor> | 
|    44   void RoundTripTest(const char* source, const OutputFunctor& output_functor) { |    44   void RoundTripTest(const char* source, const OutputFunctor& output_functor) { | 
|    45     RoundTripTest([this, source]() { return EvaluateScriptForInput(source); }, |    45     RoundTripTest([this, source]() { return EvaluateScriptForInput(source); }, | 
|    46                   output_functor); |    46                   output_functor); | 
|    47   } |    47   } | 
|    48  |    48  | 
|    49   Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { |    49   Maybe<std::vector<uint8_t>> DoEncode(Local<Value> value) { | 
|    50     // This approximates what the API implementation would do. |    50     Local<Context> context = serialization_context(); | 
|    51     // TODO(jbroman): Use the public API once it exists. |    51     ValueSerializer serializer(isolate()); | 
|    52     i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |  | 
|    53     i::HandleScope handle_scope(internal_isolate); |  | 
|    54     i::ValueSerializer serializer(internal_isolate); |  | 
|    55     serializer.WriteHeader(); |    52     serializer.WriteHeader(); | 
|    56     if (serializer.WriteObject(Utils::OpenHandle(*value)).FromMaybe(false)) { |    53     if (!serializer.WriteValue(context, value).FromMaybe(false)) { | 
|    57       return Just(serializer.ReleaseBuffer()); |    54       return Nothing<std::vector<uint8_t>>(); | 
|    58     } |    55     } | 
|    59     if (internal_isolate->has_pending_exception()) { |    56     return Just(serializer.ReleaseBuffer()); | 
|    60       internal_isolate->OptionalRescheduleException(true); |  | 
|    61     } |  | 
|    62     return Nothing<std::vector<uint8_t>>(); |  | 
|    63   } |    57   } | 
|    64  |    58  | 
|    65   template <typename InputFunctor, typename EncodedDataFunctor> |    59   template <typename InputFunctor, typename EncodedDataFunctor> | 
|    66   void EncodeTest(const InputFunctor& input_functor, |    60   void EncodeTest(const InputFunctor& input_functor, | 
|    67                   const EncodedDataFunctor& encoded_data_functor) { |    61                   const EncodedDataFunctor& encoded_data_functor) { | 
|    68     Context::Scope scope(serialization_context()); |    62     Context::Scope scope(serialization_context()); | 
|    69     TryCatch try_catch(isolate()); |    63     TryCatch try_catch(isolate()); | 
|    70     Local<Value> input_value = input_functor(); |    64     Local<Value> input_value = input_functor(); | 
|    71     std::vector<uint8_t> buffer; |    65     std::vector<uint8_t> buffer; | 
|    72     ASSERT_TRUE(DoEncode(input_value).To(&buffer)); |    66     ASSERT_TRUE(DoEncode(input_value).To(&buffer)); | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
|    83     functor(try_catch.Message()); |    77     functor(try_catch.Message()); | 
|    84   } |    78   } | 
|    85  |    79  | 
|    86   void InvalidEncodeTest(const char* source) { |    80   void InvalidEncodeTest(const char* source) { | 
|    87     InvalidEncodeTest(source, [](Local<Message>) {}); |    81     InvalidEncodeTest(source, [](Local<Message>) {}); | 
|    88   } |    82   } | 
|    89  |    83  | 
|    90   template <typename OutputFunctor> |    84   template <typename OutputFunctor> | 
|    91   void DecodeTest(const std::vector<uint8_t>& data, |    85   void DecodeTest(const std::vector<uint8_t>& data, | 
|    92                   const OutputFunctor& output_functor) { |    86                   const OutputFunctor& output_functor) { | 
|    93     Context::Scope scope(deserialization_context()); |    87     Local<Context> context = deserialization_context(); | 
 |    88     Context::Scope scope(context); | 
|    94     TryCatch try_catch(isolate()); |    89     TryCatch try_catch(isolate()); | 
|    95     // TODO(jbroman): Use the public API once it exists. |    90     ValueDeserializer deserializer(isolate(), &data[0], | 
|    96     i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |    91                                    static_cast<int>(data.size())); | 
|    97     i::HandleScope handle_scope(internal_isolate); |    92     deserializer.SetSupportsLegacyWireFormat(true); | 
|    98     i::ValueDeserializer deserializer( |  | 
|    99         internal_isolate, |  | 
|   100         i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); |  | 
|   101     ASSERT_TRUE(deserializer.ReadHeader().FromMaybe(false)); |    93     ASSERT_TRUE(deserializer.ReadHeader().FromMaybe(false)); | 
|   102     Local<Value> result; |    94     Local<Value> result; | 
|   103     ASSERT_TRUE(ToLocal<Value>(deserializer.ReadObject(), &result)); |    95     ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); | 
|   104     ASSERT_FALSE(result.IsEmpty()); |    96     ASSERT_FALSE(result.IsEmpty()); | 
|   105     ASSERT_FALSE(try_catch.HasCaught()); |    97     ASSERT_FALSE(try_catch.HasCaught()); | 
|   106     ASSERT_TRUE(deserialization_context() |    98     ASSERT_TRUE( | 
|   107                     ->Global() |    99         context->Global() | 
|   108                     ->CreateDataProperty(deserialization_context_, |   100             ->CreateDataProperty(context, StringFromUtf8("result"), result) | 
|   109                                          StringFromUtf8("result"), result) |   101             .FromMaybe(false)); | 
|   110                     .FromMaybe(false)); |  | 
|   111     output_functor(result); |   102     output_functor(result); | 
|   112     ASSERT_FALSE(try_catch.HasCaught()); |   103     ASSERT_FALSE(try_catch.HasCaught()); | 
|   113   } |   104   } | 
|   114  |   105  | 
|   115   template <typename OutputFunctor> |   106   template <typename OutputFunctor> | 
|   116   void DecodeTestForVersion0(const std::vector<uint8_t>& data, |   107   void DecodeTestForVersion0(const std::vector<uint8_t>& data, | 
|   117                              const OutputFunctor& output_functor) { |   108                              const OutputFunctor& output_functor) { | 
|   118     Context::Scope scope(deserialization_context()); |   109     Local<Context> context = deserialization_context(); | 
 |   110     Context::Scope scope(context); | 
|   119     TryCatch try_catch(isolate()); |   111     TryCatch try_catch(isolate()); | 
|   120     // TODO(jbroman): Use the public API once it exists. |   112     ValueDeserializer deserializer(isolate(), &data[0], | 
|   121     i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |   113                                    static_cast<int>(data.size())); | 
|   122     i::HandleScope handle_scope(internal_isolate); |   114     deserializer.SetSupportsLegacyWireFormat(true); | 
|   123     i::ValueDeserializer deserializer( |  | 
|   124         internal_isolate, |  | 
|   125         i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); |  | 
|   126     // TODO(jbroman): Enable legacy support. |  | 
|   127     ASSERT_TRUE(deserializer.ReadHeader().FromMaybe(false)); |   115     ASSERT_TRUE(deserializer.ReadHeader().FromMaybe(false)); | 
|   128     // TODO(jbroman): Check version 0. |   116     ASSERT_EQ(0, deserializer.GetWireFormatVersion()); | 
|   129     Local<Value> result; |   117     Local<Value> result; | 
|   130     ASSERT_TRUE(ToLocal<Value>( |   118     ASSERT_TRUE(deserializer.ReadValue(context).ToLocal(&result)); | 
|   131         deserializer.ReadObjectUsingEntireBufferForLegacyFormat(), &result)); |  | 
|   132     ASSERT_FALSE(result.IsEmpty()); |   119     ASSERT_FALSE(result.IsEmpty()); | 
|   133     ASSERT_FALSE(try_catch.HasCaught()); |   120     ASSERT_FALSE(try_catch.HasCaught()); | 
|   134     ASSERT_TRUE(deserialization_context() |   121     ASSERT_TRUE( | 
|   135                     ->Global() |   122         context->Global() | 
|   136                     ->CreateDataProperty(deserialization_context_, |   123             ->CreateDataProperty(context, StringFromUtf8("result"), result) | 
|   137                                          StringFromUtf8("result"), result) |   124             .FromMaybe(false)); | 
|   138                     .FromMaybe(false)); |  | 
|   139     output_functor(result); |   125     output_functor(result); | 
|   140     ASSERT_FALSE(try_catch.HasCaught()); |   126     ASSERT_FALSE(try_catch.HasCaught()); | 
|   141   } |   127   } | 
|   142  |   128  | 
|   143   void InvalidDecodeTest(const std::vector<uint8_t>& data) { |   129   void InvalidDecodeTest(const std::vector<uint8_t>& data) { | 
|   144     Context::Scope scope(deserialization_context()); |   130     Local<Context> context = deserialization_context(); | 
 |   131     Context::Scope scope(context); | 
|   145     TryCatch try_catch(isolate()); |   132     TryCatch try_catch(isolate()); | 
|   146     i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate()); |   133     ValueDeserializer deserializer(isolate(), &data[0], | 
|   147     i::HandleScope handle_scope(internal_isolate); |   134                                    static_cast<int>(data.size())); | 
|   148     i::ValueDeserializer deserializer( |   135     deserializer.SetSupportsLegacyWireFormat(true); | 
|   149         internal_isolate, |  | 
|   150         i::Vector<const uint8_t>(&data[0], static_cast<int>(data.size()))); |  | 
|   151     Maybe<bool> header_result = deserializer.ReadHeader(); |   136     Maybe<bool> header_result = deserializer.ReadHeader(); | 
|   152     if (header_result.IsNothing()) return; |   137     if (header_result.IsNothing()) return; | 
|   153     ASSERT_TRUE(header_result.ToChecked()); |   138     ASSERT_TRUE(header_result.ToChecked()); | 
|   154     ASSERT_TRUE(deserializer.ReadObject().is_null()); |   139     ASSERT_TRUE(deserializer.ReadValue(context).IsEmpty()); | 
|   155   } |   140   } | 
|   156  |   141  | 
|   157   Local<Value> EvaluateScriptForInput(const char* utf8_source) { |   142   Local<Value> EvaluateScriptForInput(const char* utf8_source) { | 
|   158     Local<String> source = StringFromUtf8(utf8_source); |   143     Local<String> source = StringFromUtf8(utf8_source); | 
|   159     Local<Script> script = |   144     Local<Script> script = | 
|   160         Script::Compile(serialization_context_, source).ToLocalChecked(); |   145         Script::Compile(serialization_context_, source).ToLocalChecked(); | 
|   161     return script->Run(serialization_context_).ToLocalChecked(); |   146     return script->Run(serialization_context_).ToLocalChecked(); | 
|   162   } |   147   } | 
|   163  |   148  | 
|   164   bool EvaluateScriptForResultBool(const char* utf8_source) { |   149   bool EvaluateScriptForResultBool(const char* utf8_source) { | 
| (...skipping 1152 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1317        0x3f, 0x01, 0x52, 0x03, 0x66, 0x6f, 0x6f, 0x03, 0x3f, 0x02, |  1302        0x3f, 0x01, 0x52, 0x03, 0x66, 0x6f, 0x6f, 0x03, 0x3f, 0x02, | 
|  1318        0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02, 0x00}, |  1303        0x53, 0x01, 0x62, 0x3f, 0x02, 0x5e, 0x01, 0x7b, 0x02, 0x00}, | 
|  1319       [this](Local<Value> value) { |  1304       [this](Local<Value> value) { | 
|  1320         EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof RegExp")); |  1305         EXPECT_TRUE(EvaluateScriptForResultBool("result.a instanceof RegExp")); | 
|  1321         EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |  1306         EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 
|  1322       }); |  1307       }); | 
|  1323 } |  1308 } | 
|  1324  |  1309  | 
|  1325 }  // namespace |  1310 }  // namespace | 
|  1326 }  // namespace v8 |  1311 }  // namespace v8 | 
| OLD | NEW |