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 2246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2257 }); | 2257 }); |
2258 RoundTripTest( | 2258 RoundTripTest( |
2259 "new ExampleHostObject(0xFFFFFFFF, 0x12345678)", | 2259 "new ExampleHostObject(0xFFFFFFFF, 0x12345678)", |
2260 [this](Local<Value> value) { | 2260 [this](Local<Value> value) { |
2261 EXPECT_TRUE(EvaluateScriptForResultBool("result.value === 0xFFFFFFFF")); | 2261 EXPECT_TRUE(EvaluateScriptForResultBool("result.value === 0xFFFFFFFF")); |
2262 EXPECT_TRUE( | 2262 EXPECT_TRUE( |
2263 EvaluateScriptForResultBool("result.value2 === 0x12345678")); | 2263 EvaluateScriptForResultBool("result.value2 === 0x12345678")); |
2264 }); | 2264 }); |
2265 } | 2265 } |
2266 | 2266 |
| 2267 TEST_F(ValueSerializerTestWithHostObject, RoundTripDouble) { |
| 2268 // The host can serialize data as double. |
| 2269 EXPECT_CALL(serializer_delegate_, WriteHostObject(isolate(), _)) |
| 2270 .WillRepeatedly(Invoke([this](Isolate*, Local<Object> object) { |
| 2271 double value = 0; |
| 2272 EXPECT_TRUE(object->GetInternalField(0) |
| 2273 ->NumberValue(serialization_context()) |
| 2274 .To(&value)); |
| 2275 WriteExampleHostObjectTag(); |
| 2276 serializer_->WriteDouble(value); |
| 2277 return Just(true); |
| 2278 })); |
| 2279 EXPECT_CALL(deserializer_delegate_, ReadHostObject(isolate())) |
| 2280 .WillRepeatedly(Invoke([this](Isolate*) { |
| 2281 EXPECT_TRUE(ReadExampleHostObjectTag()); |
| 2282 double value = 0; |
| 2283 EXPECT_TRUE(deserializer_->ReadDouble(&value)); |
| 2284 Local<Value> argv[] = {Number::New(isolate(), value)}; |
| 2285 return NewHostObject(deserialization_context(), arraysize(argv), argv); |
| 2286 })); |
| 2287 RoundTripTest("new ExampleHostObject(-3.5)", [this](Local<Value> value) { |
| 2288 ASSERT_TRUE(value->IsObject()); |
| 2289 ASSERT_TRUE(Object::Cast(*value)->InternalFieldCount()); |
| 2290 EXPECT_TRUE(EvaluateScriptForResultBool( |
| 2291 "Object.getPrototypeOf(result) === ExampleHostObject.prototype")); |
| 2292 EXPECT_TRUE(EvaluateScriptForResultBool("result.value === -3.5")); |
| 2293 }); |
| 2294 RoundTripTest("new ExampleHostObject(NaN)", [this](Local<Value> value) { |
| 2295 EXPECT_TRUE(EvaluateScriptForResultBool("Number.isNaN(result.value)")); |
| 2296 }); |
| 2297 RoundTripTest("new ExampleHostObject(Infinity)", [this](Local<Value> value) { |
| 2298 EXPECT_TRUE(EvaluateScriptForResultBool("result.value === Infinity")); |
| 2299 }); |
| 2300 RoundTripTest("new ExampleHostObject(-0)", [this](Local<Value> value) { |
| 2301 EXPECT_TRUE(EvaluateScriptForResultBool("1/result.value === -Infinity")); |
| 2302 }); |
| 2303 } |
| 2304 |
2267 TEST_F(ValueSerializerTestWithHostObject, RoundTripRawBytes) { | 2305 TEST_F(ValueSerializerTestWithHostObject, RoundTripRawBytes) { |
2268 // The host can serialize arbitrary raw bytes. | 2306 // The host can serialize arbitrary raw bytes. |
2269 const struct { | 2307 const struct { |
2270 uint64_t u64; | 2308 uint64_t u64; |
2271 uint32_t u32; | 2309 uint32_t u32; |
2272 char str[12]; | 2310 char str[12]; |
2273 } sample_data = {0x1234567812345678, 0x87654321, "Hello world"}; | 2311 } sample_data = {0x1234567812345678, 0x87654321, "Hello world"}; |
2274 EXPECT_CALL(serializer_delegate_, WriteHostObject(isolate(), _)) | 2312 EXPECT_CALL(serializer_delegate_, WriteHostObject(isolate(), _)) |
2275 .WillRepeatedly( | 2313 .WillRepeatedly( |
2276 Invoke([this, &sample_data](Isolate*, Local<Object> object) { | 2314 Invoke([this, &sample_data](Isolate*, Local<Object> object) { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2315 "({ a: new ExampleHostObject(), get b() { return this.a; }})", | 2353 "({ a: new ExampleHostObject(), get b() { return this.a; }})", |
2316 [this](Local<Value> value) { | 2354 [this](Local<Value> value) { |
2317 EXPECT_TRUE(EvaluateScriptForResultBool( | 2355 EXPECT_TRUE(EvaluateScriptForResultBool( |
2318 "result.a instanceof ExampleHostObject")); | 2356 "result.a instanceof ExampleHostObject")); |
2319 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); | 2357 EXPECT_TRUE(EvaluateScriptForResultBool("result.a === result.b")); |
2320 }); | 2358 }); |
2321 } | 2359 } |
2322 | 2360 |
2323 } // namespace | 2361 } // namespace |
2324 } // namespace v8 | 2362 } // namespace v8 |
OLD | NEW |