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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 } | 256 } |
257 | 257 |
258 Local<Object> NewHostObject(Local<Context> context, int argc, | 258 Local<Object> NewHostObject(Local<Context> context, int argc, |
259 Local<Value> argv[]) { | 259 Local<Value> argv[]) { |
260 return host_object_constructor_template_->GetFunction(context) | 260 return host_object_constructor_template_->GetFunction(context) |
261 .ToLocalChecked() | 261 .ToLocalChecked() |
262 ->NewInstance(context, argc, argv) | 262 ->NewInstance(context, argc, argv) |
263 .ToLocalChecked(); | 263 .ToLocalChecked(); |
264 } | 264 } |
265 | 265 |
| 266 Local<Object> NewDummyUint8Array() { |
| 267 static uint8_t data[] = {4, 5, 6}; |
| 268 Local<ArrayBuffer> ab = |
| 269 ArrayBuffer::New(isolate(), static_cast<void*>(data), sizeof(data)); |
| 270 return Uint8Array::New(ab, 0, sizeof(data)); |
| 271 } |
| 272 |
266 private: | 273 private: |
267 Local<Context> serialization_context_; | 274 Local<Context> serialization_context_; |
268 Local<Context> deserialization_context_; | 275 Local<Context> deserialization_context_; |
269 Local<FunctionTemplate> host_object_constructor_template_; | 276 Local<FunctionTemplate> host_object_constructor_template_; |
270 i::Isolate* isolate_; | 277 i::Isolate* isolate_; |
271 | 278 |
272 DISALLOW_COPY_AND_ASSIGN(ValueSerializerTest); | 279 DISALLOW_COPY_AND_ASSIGN(ValueSerializerTest); |
273 }; | 280 }; |
274 | 281 |
275 TEST_F(ValueSerializerTest, DecodeInvalid) { | 282 TEST_F(ValueSerializerTest, DecodeInvalid) { |
(...skipping 2254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2530 })); | 2537 })); |
2531 RoundTripTest( | 2538 RoundTripTest( |
2532 "({ a: new ExampleHostObject(), get b() { return this.a; }})", | 2539 "({ a: new ExampleHostObject(), get b() { return this.a; }})", |
2533 [this](Local<Value> value) { | 2540 [this](Local<Value> value) { |
2534 EXPECT_TRUE(EvaluateScriptForResultBool( | 2541 EXPECT_TRUE(EvaluateScriptForResultBool( |
2535 "result.a instanceof ExampleHostObject")); | 2542 "result.a instanceof ExampleHostObject")); |
2536 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2543 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
2537 }); | 2544 }); |
2538 } | 2545 } |
2539 | 2546 |
| 2547 class ValueSerializerTestWithHostArrayBufferView |
| 2548 : public ValueSerializerTestWithHostObject { |
| 2549 protected: |
| 2550 void BeforeEncode(ValueSerializer* serializer) override { |
| 2551 ValueSerializerTestWithHostObject::BeforeEncode(serializer); |
| 2552 serializer_->SetTreatArrayBufferViewsAsHostObjects(true); |
| 2553 } |
| 2554 }; |
| 2555 |
| 2556 TEST_F(ValueSerializerTestWithHostArrayBufferView, RoundTripUint8ArrayInput) { |
| 2557 EXPECT_CALL(serializer_delegate_, WriteHostObject(isolate(), _)) |
| 2558 .WillOnce(Invoke([this](Isolate*, Local<Object> object) { |
| 2559 EXPECT_TRUE(object->IsUint8Array()); |
| 2560 WriteExampleHostObjectTag(); |
| 2561 return Just(true); |
| 2562 })); |
| 2563 EXPECT_CALL(deserializer_delegate_, ReadHostObject(isolate())) |
| 2564 .WillOnce(Invoke([this](Isolate*) { |
| 2565 EXPECT_TRUE(ReadExampleHostObjectTag()); |
| 2566 return NewDummyUint8Array(); |
| 2567 })); |
| 2568 RoundTripTest( |
| 2569 "({ a: new Uint8Array([1, 2, 3]), get b() { return this.a; }})", |
| 2570 [this](Local<Value> value) { |
| 2571 EXPECT_TRUE( |
| 2572 EvaluateScriptForResultBool("result.a instanceof Uint8Array")); |
| 2573 EXPECT_TRUE( |
| 2574 EvaluateScriptForResultBool("result.a.toString() === '4,5,6'")); |
| 2575 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
| 2576 }); |
| 2577 } |
| 2578 |
2540 // It's expected that WebAssembly has more exhaustive tests elsewhere; this | 2579 // It's expected that WebAssembly has more exhaustive tests elsewhere; this |
2541 // mostly checks that the logic to embed it in structured clone serialization | 2580 // mostly checks that the logic to embed it in structured clone serialization |
2542 // works correctly. | 2581 // works correctly. |
2543 | 2582 |
2544 class ValueSerializerTestWithWasm : public ValueSerializerTest { | 2583 class ValueSerializerTestWithWasm : public ValueSerializerTest { |
2545 protected: | 2584 protected: |
2546 static void SetUpTestCase() { | 2585 static void SetUpTestCase() { |
2547 g_saved_flag = i::FLAG_expose_wasm; | 2586 g_saved_flag = i::FLAG_expose_wasm; |
2548 i::FLAG_expose_wasm = true; | 2587 i::FLAG_expose_wasm = true; |
2549 ValueSerializerTest::SetUpTestCase(); | 2588 ValueSerializerTest::SetUpTestCase(); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2691 InvalidDecodeTest(raw); | 2730 InvalidDecodeTest(raw); |
2692 } | 2731 } |
2693 | 2732 |
2694 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { | 2733 TEST_F(ValueSerializerTestWithWasm, DecodeWasmModuleWithInvalidDataLength) { |
2695 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); | 2734 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x7f, 0x00}); |
2696 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); | 2735 InvalidDecodeTest({0xff, 0x09, 0x3f, 0x00, 0x57, 0x79, 0x00, 0x7f}); |
2697 } | 2736 } |
2698 | 2737 |
2699 } // namespace | 2738 } // namespace |
2700 } // namespace v8 | 2739 } // namespace v8 |
OLD | NEW |