OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "include/dart_debugger_api.h" | 5 #include "include/dart_debugger_api.h" |
6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_api_state.h" | 10 #include "vm/dart_api_state.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 | 54 |
55 static Dart_CMessage* DecodeMessage(uint8_t* message, | 55 static Dart_CMessage* DecodeMessage(uint8_t* message, |
56 intptr_t length, | 56 intptr_t length, |
57 ReAlloc allocator) { | 57 ReAlloc allocator) { |
58 CMessageReader message_reader(message, length, allocator); | 58 CMessageReader message_reader(message, length, allocator); |
59 return message_reader.ReadMessage(); | 59 return message_reader.ReadMessage(); |
60 } | 60 } |
61 | 61 |
62 | 62 |
| 63 // Compare two Dart_CObject object graphs rooted in first and |
| 64 // second. The second graph will be destroyed by this operation no matter |
| 65 // whether the graphs are equal or not. |
| 66 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { |
| 67 // Return immediately if entering a cycle. |
| 68 if (second->type == Dart_CObject::kNumberOfTypes) return; |
| 69 |
| 70 EXPECT_NE(first, second); |
| 71 EXPECT_EQ(first->type, second->type); |
| 72 switch (first->type) { |
| 73 case Dart_CObject::kNull: |
| 74 // Nothing more to compare. |
| 75 break; |
| 76 case Dart_CObject::kBool: |
| 77 EXPECT_EQ(first->value.as_bool, second->value.as_bool); |
| 78 break; |
| 79 case Dart_CObject::kInt32: |
| 80 EXPECT_EQ(first->value.as_int32, second->value.as_int32); |
| 81 break; |
| 82 case Dart_CObject::kDouble: |
| 83 EXPECT_EQ(first->value.as_double, second->value.as_double); |
| 84 break; |
| 85 case Dart_CObject::kString: |
| 86 EXPECT_STREQ(first->value.as_string, second->value.as_string); |
| 87 break; |
| 88 case Dart_CObject::kArray: |
| 89 // Use invalid type as a visited marker to avoid infinite |
| 90 // recursion on graphs with cycles. |
| 91 second->type = Dart_CObject::kNumberOfTypes; |
| 92 EXPECT_EQ(first->value.as_array.length, second->value.as_array.length); |
| 93 for (int i = 0; i < first->value.as_array.length; i++) { |
| 94 CompareDartCObjects(first->value.as_array.values[i], |
| 95 second->value.as_array.values[i]); |
| 96 } |
| 97 break; |
| 98 default: |
| 99 EXPECT(false); |
| 100 } |
| 101 } |
| 102 |
| 103 |
| 104 static void CheckEncodeDecodeMessage(Dart_CMessage* cmessage) { |
| 105 // Encode and decode the message. |
| 106 uint8_t* buffer = NULL; |
| 107 MessageWriter writer(&buffer, &malloc_allocator); |
| 108 writer.WriteCMessage(cmessage->root); |
| 109 |
| 110 Zone zone(Isolate::Current()); |
| 111 Dart_CMessage* new_cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
| 112 writer.BytesWritten(), |
| 113 &zone_allocator); |
| 114 |
| 115 // Check that the two messages are the same. |
| 116 CompareDartCObjects(cmessage->root, new_cmessage->root); |
| 117 } |
| 118 |
63 TEST_CASE(SerializeNull) { | 119 TEST_CASE(SerializeNull) { |
64 Zone zone(Isolate::Current()); | 120 Zone zone(Isolate::Current()); |
65 | 121 |
66 // Write snapshot with object content. | 122 // Write snapshot with object content. |
67 uint8_t* buffer; | 123 uint8_t* buffer; |
68 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 124 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
69 const Object& null_object = Object::Handle(); | 125 const Object& null_object = Object::Handle(); |
70 writer.WriteObject(null_object.raw()); | 126 writer.WriteObject(null_object.raw()); |
71 writer.FinalizeBuffer(); | 127 writer.FinalizeBuffer(); |
72 | 128 |
73 // Create a snapshot object using the buffer. | 129 // Create a snapshot object using the buffer. |
74 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 130 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
75 | 131 |
76 // Read object back from the snapshot. | 132 // Read object back from the snapshot. |
77 SnapshotReader reader(snapshot, Isolate::Current()); | 133 SnapshotReader reader(snapshot, Isolate::Current()); |
78 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 134 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
79 EXPECT(Equals(null_object, serialized_object)); | 135 EXPECT(Equals(null_object, serialized_object)); |
80 | 136 |
81 // Read object back from the snapshot into a C structure. | 137 // Read object back from the snapshot into a C structure. |
82 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 138 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
83 writer.BytesWritten(), | 139 writer.BytesWritten(), |
84 &zone_allocator); | 140 &zone_allocator); |
85 Dart_CObject* cobject = cmessage->root; | 141 Dart_CObject* cobject = cmessage->root; |
86 EXPECT_NOTNULL(cobject); | 142 EXPECT_NOTNULL(cobject); |
87 EXPECT_EQ(Dart_CObject::kNull, cobject->type); | 143 EXPECT_EQ(Dart_CObject::kNull, cobject->type); |
| 144 CheckEncodeDecodeMessage(cmessage); |
88 } | 145 } |
89 | 146 |
90 | 147 |
91 TEST_CASE(SerializeSmi1) { | 148 TEST_CASE(SerializeSmi1) { |
92 Zone zone(Isolate::Current()); | 149 Zone zone(Isolate::Current()); |
93 | 150 |
94 // Write snapshot with object content. | 151 // Write snapshot with object content. |
95 uint8_t* buffer; | 152 uint8_t* buffer; |
96 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 153 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
97 const Smi& smi = Smi::Handle(Smi::New(124)); | 154 const Smi& smi = Smi::Handle(Smi::New(124)); |
98 writer.WriteObject(smi.raw()); | 155 writer.WriteObject(smi.raw()); |
99 writer.FinalizeBuffer(); | 156 writer.FinalizeBuffer(); |
100 | 157 |
101 // Create a snapshot object using the buffer. | 158 // Create a snapshot object using the buffer. |
102 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 159 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
103 | 160 |
104 // Read object back from the snapshot. | 161 // Read object back from the snapshot. |
105 SnapshotReader reader(snapshot, Isolate::Current()); | 162 SnapshotReader reader(snapshot, Isolate::Current()); |
106 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 163 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
107 EXPECT(Equals(smi, serialized_object)); | 164 EXPECT(Equals(smi, serialized_object)); |
108 | 165 |
109 // Read object back from the snapshot into a C structure. | 166 // Read object back from the snapshot into a C structure. |
110 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 167 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
111 writer.BytesWritten(), | 168 writer.BytesWritten(), |
112 &zone_allocator); | 169 &zone_allocator); |
113 Dart_CObject* cobject = cmessage->root; | 170 Dart_CObject* cobject = cmessage->root; |
114 EXPECT_NOTNULL(cobject); | 171 EXPECT_NOTNULL(cobject); |
115 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | 172 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
116 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | 173 EXPECT_EQ(smi.Value(), cobject->value.as_int32); |
| 174 CheckEncodeDecodeMessage(cmessage); |
117 } | 175 } |
118 | 176 |
119 | 177 |
120 TEST_CASE(SerializeSmi2) { | 178 TEST_CASE(SerializeSmi2) { |
121 Zone zone(Isolate::Current()); | 179 Zone zone(Isolate::Current()); |
122 | 180 |
123 // Write snapshot with object content. | 181 // Write snapshot with object content. |
124 uint8_t* buffer; | 182 uint8_t* buffer; |
125 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 183 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
126 const Smi& smi = Smi::Handle(Smi::New(-1)); | 184 const Smi& smi = Smi::Handle(Smi::New(-1)); |
127 writer.WriteObject(smi.raw()); | 185 writer.WriteObject(smi.raw()); |
128 writer.FinalizeBuffer(); | 186 writer.FinalizeBuffer(); |
129 | 187 |
130 // Create a snapshot object using the buffer. | 188 // Create a snapshot object using the buffer. |
131 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 189 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
132 | 190 |
133 // Read object back from the snapshot. | 191 // Read object back from the snapshot. |
134 SnapshotReader reader(snapshot, Isolate::Current()); | 192 SnapshotReader reader(snapshot, Isolate::Current()); |
135 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 193 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
136 EXPECT(Equals(smi, serialized_object)); | 194 EXPECT(Equals(smi, serialized_object)); |
137 | 195 |
138 // Read object back from the snapshot into a C structure. | 196 // Read object back from the snapshot into a C structure. |
139 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 197 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
140 writer.BytesWritten(), | 198 writer.BytesWritten(), |
141 &zone_allocator); | 199 &zone_allocator); |
142 Dart_CObject* cobject = cmessage->root; | 200 Dart_CObject* cobject = cmessage->root; |
143 EXPECT_NOTNULL(cobject); | 201 EXPECT_NOTNULL(cobject); |
144 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | 202 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
145 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | 203 EXPECT_EQ(smi.Value(), cobject->value.as_int32); |
| 204 CheckEncodeDecodeMessage(cmessage); |
146 } | 205 } |
147 | 206 |
148 | 207 |
149 TEST_CASE(SerializeDouble) { | 208 TEST_CASE(SerializeDouble) { |
150 Zone zone(Isolate::Current()); | 209 Zone zone(Isolate::Current()); |
151 | 210 |
152 // Write snapshot with object content. | 211 // Write snapshot with object content. |
153 uint8_t* buffer; | 212 uint8_t* buffer; |
154 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 213 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
155 const Double& dbl = Double::Handle(Double::New(101.29)); | 214 const Double& dbl = Double::Handle(Double::New(101.29)); |
156 writer.WriteObject(dbl.raw()); | 215 writer.WriteObject(dbl.raw()); |
157 writer.FinalizeBuffer(); | 216 writer.FinalizeBuffer(); |
158 | 217 |
159 // Create a snapshot object using the buffer. | 218 // Create a snapshot object using the buffer. |
160 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 219 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
161 | 220 |
162 // Read object back from the snapshot. | 221 // Read object back from the snapshot. |
163 SnapshotReader reader(snapshot, Isolate::Current()); | 222 SnapshotReader reader(snapshot, Isolate::Current()); |
164 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 223 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
165 EXPECT(Equals(dbl, serialized_object)); | 224 EXPECT(Equals(dbl, serialized_object)); |
166 | 225 |
167 // Read object back from the snapshot into a C structure. | 226 // Read object back from the snapshot into a C structure. |
168 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 227 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
169 writer.BytesWritten(), | 228 writer.BytesWritten(), |
170 &zone_allocator); | 229 &zone_allocator); |
171 Dart_CObject* cobject = cmessage->root; | 230 Dart_CObject* cobject = cmessage->root; |
172 EXPECT_NOTNULL(cobject); | 231 EXPECT_NOTNULL(cobject); |
173 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); | 232 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); |
174 EXPECT_EQ(dbl.value(), cobject->value.as_double); | 233 EXPECT_EQ(dbl.value(), cobject->value.as_double); |
| 234 CheckEncodeDecodeMessage(cmessage); |
175 } | 235 } |
176 | 236 |
177 | 237 |
178 TEST_CASE(SerializeBool) { | 238 TEST_CASE(SerializeBool) { |
179 Zone zone(Isolate::Current()); | 239 Zone zone(Isolate::Current()); |
180 | 240 |
181 // Write snapshot with object content. | 241 // Write snapshot with object content. |
182 uint8_t* buffer; | 242 uint8_t* buffer; |
183 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 243 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
184 const Bool& bool1 = Bool::Handle(Bool::True()); | 244 const Bool& bool1 = Bool::Handle(Bool::True()); |
(...skipping 26 matching lines...) Expand all Loading... |
211 Snapshot::SetupFromBuffer(buffer); | 271 Snapshot::SetupFromBuffer(buffer); |
212 | 272 |
213 // Read object back from the snapshot into a C structure. | 273 // Read object back from the snapshot into a C structure. |
214 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 274 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
215 writer.BytesWritten(), | 275 writer.BytesWritten(), |
216 &zone_allocator); | 276 &zone_allocator); |
217 Dart_CObject* cobject = cmessage->root; | 277 Dart_CObject* cobject = cmessage->root; |
218 EXPECT_NOTNULL(cobject); | 278 EXPECT_NOTNULL(cobject); |
219 EXPECT_EQ(Dart_CObject::kBool, cobject->type); | 279 EXPECT_EQ(Dart_CObject::kBool, cobject->type); |
220 EXPECT_EQ(true, cobject->value.as_bool); | 280 EXPECT_EQ(true, cobject->value.as_bool); |
| 281 CheckEncodeDecodeMessage(cmessage); |
221 } | 282 } |
222 | 283 |
223 | 284 |
224 TEST_CASE(SerializeFalse) { | 285 TEST_CASE(SerializeFalse) { |
225 Zone zone(Isolate::Current()); | 286 Zone zone(Isolate::Current()); |
226 | 287 |
227 // Write snapshot with false object. | 288 // Write snapshot with false object. |
228 uint8_t* buffer; | 289 uint8_t* buffer; |
229 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 290 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
230 const Bool& bl = Bool::Handle(Bool::False()); | 291 const Bool& bl = Bool::Handle(Bool::False()); |
231 writer.WriteObject(bl.raw()); | 292 writer.WriteObject(bl.raw()); |
232 writer.FinalizeBuffer(); | 293 writer.FinalizeBuffer(); |
233 | 294 |
234 // Create a snapshot object using the buffer. | 295 // Create a snapshot object using the buffer. |
235 Snapshot::SetupFromBuffer(buffer); | 296 Snapshot::SetupFromBuffer(buffer); |
236 | 297 |
237 // Read object back from the snapshot into a C structure. | 298 // Read object back from the snapshot into a C structure. |
238 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 299 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
239 writer.BytesWritten(), | 300 writer.BytesWritten(), |
240 &zone_allocator); | 301 &zone_allocator); |
241 Dart_CObject* cobject = cmessage->root; | 302 Dart_CObject* cobject = cmessage->root; |
242 EXPECT_NOTNULL(cobject); | 303 EXPECT_NOTNULL(cobject); |
243 EXPECT_EQ(Dart_CObject::kBool, cobject->type); | 304 EXPECT_EQ(Dart_CObject::kBool, cobject->type); |
244 EXPECT_EQ(false, cobject->value.as_bool); | 305 EXPECT_EQ(false, cobject->value.as_bool); |
| 306 CheckEncodeDecodeMessage(cmessage); |
245 } | 307 } |
246 | 308 |
247 | 309 |
248 TEST_CASE(SerializeBigint) { | 310 TEST_CASE(SerializeBigint) { |
249 Zone zone(Isolate::Current()); | 311 Zone zone(Isolate::Current()); |
250 | 312 |
251 // Write snapshot with object content. | 313 // Write snapshot with object content. |
252 uint8_t* buffer; | 314 uint8_t* buffer; |
253 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 315 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
254 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); | 316 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 serialized_str ^= reader.ReadObject(); | 411 serialized_str ^= reader.ReadObject(); |
350 EXPECT(str.Equals(serialized_str)); | 412 EXPECT(str.Equals(serialized_str)); |
351 | 413 |
352 // Read object back from the snapshot into a C structure. | 414 // Read object back from the snapshot into a C structure. |
353 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 415 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
354 writer.BytesWritten(), | 416 writer.BytesWritten(), |
355 &zone_allocator); | 417 &zone_allocator); |
356 Dart_CObject* cobject = cmessage->root; | 418 Dart_CObject* cobject = cmessage->root; |
357 EXPECT_EQ(Dart_CObject::kString, cobject->type); | 419 EXPECT_EQ(Dart_CObject::kString, cobject->type); |
358 EXPECT_STREQ(cstr, cobject->value.as_string); | 420 EXPECT_STREQ(cstr, cobject->value.as_string); |
| 421 CheckEncodeDecodeMessage(cmessage); |
359 } | 422 } |
360 | 423 |
361 | 424 |
362 TEST_CASE(SerializeArray) { | 425 TEST_CASE(SerializeArray) { |
363 Zone zone(Isolate::Current()); | 426 Zone zone(Isolate::Current()); |
364 | 427 |
365 // Write snapshot with object content. | 428 // Write snapshot with object content. |
366 uint8_t* buffer; | 429 uint8_t* buffer; |
367 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 430 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
368 const int kArrayLength = 10; | 431 const int kArrayLength = 10; |
(...skipping 20 matching lines...) Expand all Loading... |
389 writer.BytesWritten(), | 452 writer.BytesWritten(), |
390 &zone_allocator); | 453 &zone_allocator); |
391 Dart_CObject* cobject = cmessage->root; | 454 Dart_CObject* cobject = cmessage->root; |
392 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | 455 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
393 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | 456 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
394 for (int i = 0; i < kArrayLength; i++) { | 457 for (int i = 0; i < kArrayLength; i++) { |
395 Dart_CObject* element = cobject->value.as_array.values[i]; | 458 Dart_CObject* element = cobject->value.as_array.values[i]; |
396 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 459 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
397 EXPECT_EQ(i, element->value.as_int32); | 460 EXPECT_EQ(i, element->value.as_int32); |
398 } | 461 } |
| 462 CheckEncodeDecodeMessage(cmessage); |
399 } | 463 } |
400 | 464 |
401 | 465 |
402 TEST_CASE(SerializeEmptyArray) { | 466 TEST_CASE(SerializeEmptyArray) { |
403 Zone zone(Isolate::Current()); | 467 Zone zone(Isolate::Current()); |
404 | 468 |
405 // Write snapshot with object content. | 469 // Write snapshot with object content. |
406 uint8_t* buffer; | 470 uint8_t* buffer; |
407 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 471 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
408 const int kArrayLength = 0; | 472 const int kArrayLength = 0; |
(...skipping 11 matching lines...) Expand all Loading... |
420 EXPECT(array.Equals(serialized_array)); | 484 EXPECT(array.Equals(serialized_array)); |
421 | 485 |
422 // Read object back from the snapshot into a C structure. | 486 // Read object back from the snapshot into a C structure. |
423 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 487 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
424 writer.BytesWritten(), | 488 writer.BytesWritten(), |
425 &zone_allocator); | 489 &zone_allocator); |
426 Dart_CObject* cobject = cmessage->root; | 490 Dart_CObject* cobject = cmessage->root; |
427 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | 491 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
428 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | 492 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
429 EXPECT(cobject->value.as_array.values == NULL); | 493 EXPECT(cobject->value.as_array.values == NULL); |
| 494 CheckEncodeDecodeMessage(cmessage); |
430 } | 495 } |
431 | 496 |
432 | 497 |
433 TEST_CASE(SerializeScript) { | 498 TEST_CASE(SerializeScript) { |
434 const char* kScriptChars = | 499 const char* kScriptChars = |
435 "class A {\n" | 500 "class A {\n" |
436 " static bar() { return 42; }\n" | 501 " static bar() { return 42; }\n" |
437 " static fly() { return 5; }\n" | 502 " static fly() { return 5; }\n" |
438 "}\n"; | 503 "}\n"; |
439 | 504 |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 writer.BytesWritten(), | 817 writer.BytesWritten(), |
753 &zone_allocator); | 818 &zone_allocator); |
754 Dart_CObject* cobject = cmessage->root; | 819 Dart_CObject* cobject = cmessage->root; |
755 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | 820 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
756 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | 821 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
757 for (int i = 0; i < kArrayLength; i++) { | 822 for (int i = 0; i < kArrayLength; i++) { |
758 Dart_CObject* element = cobject->value.as_array.values[i]; | 823 Dart_CObject* element = cobject->value.as_array.values[i]; |
759 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 824 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
760 EXPECT_EQ(i + 1, element->value.as_int32); | 825 EXPECT_EQ(i + 1, element->value.as_int32); |
761 } | 826 } |
| 827 CheckEncodeDecodeMessage(cmessage); |
762 } | 828 } |
763 | 829 |
764 | 830 |
765 // Helper function to call a top level Dart function, serialize the | 831 // Helper function to call a top level Dart function, serialize the |
766 // result and deserialize the result into a Dart_CObject structure. | 832 // result and deserialize the result into a Dart_CObject structure. |
767 static Dart_CMessage* GetDeserializedDartMessage(Dart_Handle lib, | 833 static Dart_CMessage* GetDeserializedDartMessage(Dart_Handle lib, |
768 const char* dart_function) { | 834 const char* dart_function) { |
769 Dart_Handle result; | 835 Dart_Handle result; |
770 result = Dart_InvokeStatic(lib, | 836 result = Dart_InvokeStatic(lib, |
771 Dart_NewString(""), | 837 Dart_NewString(""), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 writer.FinalizeBuffer(); | 904 writer.FinalizeBuffer(); |
839 | 905 |
840 // Read object back from the snapshot into a C structure. | 906 // Read object back from the snapshot into a C structure. |
841 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 907 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
842 writer.BytesWritten(), | 908 writer.BytesWritten(), |
843 &zone_allocator); | 909 &zone_allocator); |
844 Dart_CObject* cobject = cmessage->root; | 910 Dart_CObject* cobject = cmessage->root; |
845 EXPECT_NOTNULL(cobject); | 911 EXPECT_NOTNULL(cobject); |
846 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | 912 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
847 EXPECT_EQ(42, cobject->value.as_int32); | 913 EXPECT_EQ(42, cobject->value.as_int32); |
| 914 CheckEncodeDecodeMessage(cmessage); |
848 } | 915 } |
849 { | 916 { |
850 Zone zone(Isolate::Current()); | 917 Zone zone(Isolate::Current()); |
851 uint8_t* buffer; | 918 uint8_t* buffer; |
852 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 919 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
853 String& str = String::Handle(); | 920 String& str = String::Handle(); |
854 str ^= Api::UnwrapHandle(string_result); | 921 str ^= Api::UnwrapHandle(string_result); |
855 writer.WriteObject(str.raw()); | 922 writer.WriteObject(str.raw()); |
856 writer.FinalizeBuffer(); | 923 writer.FinalizeBuffer(); |
857 | 924 |
858 // Read object back from the snapshot into a C structure. | 925 // Read object back from the snapshot into a C structure. |
859 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, | 926 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
860 writer.BytesWritten(), | 927 writer.BytesWritten(), |
861 &zone_allocator); | 928 &zone_allocator); |
862 Dart_CObject* cobject = cmessage->root; | 929 Dart_CObject* cobject = cmessage->root; |
863 EXPECT_NOTNULL(cobject); | 930 EXPECT_NOTNULL(cobject); |
864 EXPECT_EQ(Dart_CObject::kString, cobject->type); | 931 EXPECT_EQ(Dart_CObject::kString, cobject->type); |
865 EXPECT_STREQ("Hello, world!", cobject->value.as_string); | 932 EXPECT_STREQ("Hello, world!", cobject->value.as_string); |
| 933 CheckEncodeDecodeMessage(cmessage); |
866 } | 934 } |
867 } | 935 } |
868 Dart_ExitScope(); | 936 Dart_ExitScope(); |
869 Dart_ShutdownIsolate(); | 937 Dart_ShutdownIsolate(); |
870 } | 938 } |
871 | 939 |
872 | 940 |
873 UNIT_TEST_CASE(DartGeneratedListMessages) { | 941 UNIT_TEST_CASE(DartGeneratedListMessages) { |
874 const int kArrayLength = 10; | 942 const int kArrayLength = 10; |
875 static const char* kScriptChars = | 943 static const char* kScriptChars = |
(...skipping 23 matching lines...) Expand all Loading... |
899 TestCase::CreateTestIsolate(); | 967 TestCase::CreateTestIsolate(); |
900 Isolate* isolate = Isolate::Current(); | 968 Isolate* isolate = Isolate::Current(); |
901 EXPECT(isolate != NULL); | 969 EXPECT(isolate != NULL); |
902 Dart_EnterScope(); | 970 Dart_EnterScope(); |
903 | 971 |
904 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 972 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
905 EXPECT_VALID(lib); | 973 EXPECT_VALID(lib); |
906 | 974 |
907 { | 975 { |
908 DARTSCOPE_NOCHECKS(isolate); | 976 DARTSCOPE_NOCHECKS(isolate); |
909 | |
910 { | 977 { |
911 // Generate a list of nulls from Dart code. | 978 // Generate a list of nulls from Dart code. |
912 Zone zone(Isolate::Current()); | 979 Zone zone(Isolate::Current()); |
913 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getList"); | 980 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getList"); |
914 Dart_CObject* value = cmessage->root; | 981 Dart_CObject* value = cmessage->root; |
915 EXPECT_NOTNULL(value); | 982 EXPECT_NOTNULL(value); |
916 EXPECT_EQ(Dart_CObject::kArray, value->type); | 983 EXPECT_EQ(Dart_CObject::kArray, value->type); |
917 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 984 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
918 for (int i = 0; i < kArrayLength; i++) { | 985 for (int i = 0; i < kArrayLength; i++) { |
919 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); | 986 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); |
920 } | 987 } |
| 988 CheckEncodeDecodeMessage(cmessage); |
921 } | 989 } |
922 { | 990 { |
923 // Generate a list of ints from Dart code. | 991 // Generate a list of ints from Dart code. |
924 Zone zone(Isolate::Current()); | 992 Zone zone(Isolate::Current()); |
925 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getIntList"); | 993 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getIntList"); |
926 Dart_CObject* value = cmessage->root; | 994 Dart_CObject* value = cmessage->root; |
927 EXPECT_NOTNULL(value); | 995 EXPECT_NOTNULL(value); |
928 EXPECT_EQ(Dart_CObject::kArray, value->type); | 996 EXPECT_EQ(Dart_CObject::kArray, value->type); |
929 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 997 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
930 for (int i = 0; i < kArrayLength; i++) { | 998 for (int i = 0; i < kArrayLength; i++) { |
931 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type); | 999 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type); |
932 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32); | 1000 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32); |
933 } | 1001 } |
| 1002 CheckEncodeDecodeMessage(cmessage); |
934 } | 1003 } |
935 { | 1004 { |
936 // Generate a list of strings from Dart code. | 1005 // Generate a list of strings from Dart code. |
937 Zone zone(Isolate::Current()); | 1006 Zone zone(Isolate::Current()); |
938 Dart_CMessage* cmessage = | 1007 Dart_CMessage* cmessage = |
939 GetDeserializedDartMessage(lib, "getStringList"); | 1008 GetDeserializedDartMessage(lib, "getStringList"); |
940 Dart_CObject* value = cmessage->root; | 1009 Dart_CObject* value = cmessage->root; |
941 EXPECT_NOTNULL(value); | 1010 EXPECT_NOTNULL(value); |
942 EXPECT_EQ(Dart_CObject::kArray, value->type); | 1011 EXPECT_EQ(Dart_CObject::kArray, value->type); |
943 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 1012 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 Dart_CObject* element = object->value.as_array.values[i]; | 1155 Dart_CObject* element = object->value.as_array.values[i]; |
1087 EXPECT_EQ(Dart_CObject::kArray, element->type); | 1156 EXPECT_EQ(Dart_CObject::kArray, element->type); |
1088 EXPECT_EQ(object, element); | 1157 EXPECT_EQ(object, element); |
1089 } | 1158 } |
1090 } | 1159 } |
1091 } | 1160 } |
1092 Dart_ExitScope(); | 1161 Dart_ExitScope(); |
1093 Dart_ShutdownIsolate(); | 1162 Dart_ShutdownIsolate(); |
1094 } | 1163 } |
1095 | 1164 |
| 1165 |
| 1166 UNIT_TEST_CASE(PostCMessage) { |
| 1167 // Create a native port for posting from C to Dart |
| 1168 TestIsolateScope __test_isolate__; |
| 1169 const char* kScriptChars = |
| 1170 "void main() {\n" |
| 1171 " var messageCount = 0;\n" |
| 1172 " var exception = '';\n" |
| 1173 " var port = new ReceivePort();\n" |
| 1174 " port.receive((message, replyTo) {\n" |
| 1175 " exception += message.toString();\n" |
| 1176 " messageCount++;\n" |
| 1177 " if (messageCount == 7) throw new Exception(exception);\n" |
| 1178 " });\n" |
| 1179 " return port.toSendPort();\n" |
| 1180 "}\n"; |
| 1181 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 1182 Dart_EnterScope(); |
| 1183 |
| 1184 // xxx |
| 1185 Dart_Handle send_port = Dart_InvokeStatic(lib, |
| 1186 Dart_NewString(""), |
| 1187 Dart_NewString("main"), |
| 1188 0, |
| 1189 NULL); |
| 1190 EXPECT_VALID(send_port); |
| 1191 Dart_Handle result = |
| 1192 Dart_GetInstanceField(send_port, Dart_NewString("_id")); |
| 1193 ASSERT(!Dart_IsError(result)); |
| 1194 ASSERT(Dart_IsInteger(result)); |
| 1195 int64_t send_port_id; |
| 1196 Dart_Handle result2 = Dart_IntegerToInt64(result, &send_port_id); |
| 1197 ASSERT(!Dart_IsError(result2)); |
| 1198 |
| 1199 // Setup single object message. |
| 1200 Dart_CObject object; |
| 1201 Dart_CMessage message; |
| 1202 message.root = &object; |
| 1203 |
| 1204 object.type = Dart_CObject::kNull; |
| 1205 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1206 |
| 1207 object.type = Dart_CObject::kBool; |
| 1208 object.value.as_bool = true; |
| 1209 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1210 |
| 1211 object.type = Dart_CObject::kBool; |
| 1212 object.value.as_bool = false; |
| 1213 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1214 |
| 1215 object.type = Dart_CObject::kInt32; |
| 1216 object.value.as_int32 = 123; |
| 1217 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1218 |
| 1219 object.type = Dart_CObject::kString; |
| 1220 object.value.as_string = const_cast<char*>("456"); |
| 1221 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1222 |
| 1223 object.type = Dart_CObject::kDouble; |
| 1224 object.value.as_double = 3.14; |
| 1225 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1226 |
| 1227 object.type = Dart_CObject::kArray; |
| 1228 object.value.as_array.length = 0; |
| 1229 EXPECT(Dart_PostCMessage(send_port_id, &message)); |
| 1230 |
| 1231 result = Dart_RunLoop(); |
| 1232 EXPECT(Dart_IsError(result)); |
| 1233 EXPECT(Dart_ErrorHasException(result)); |
| 1234 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", |
| 1235 Dart_GetError(result)); |
| 1236 |
| 1237 Dart_ExitScope(); |
| 1238 } |
| 1239 |
1096 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 1240 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
1097 | 1241 |
1098 } // namespace dart | 1242 } // namespace dart |
OLD | NEW |