Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(244)

Side by Side Diff: runtime/vm/snapshot_test.cc

Issue 9104041: Added API Dart_PostCMessage for posting a Dart_CMessage structure (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments from asiva@ Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« runtime/include/dart_api.h ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 45
46 46
47 static uint8_t* zone_allocator( 47 static uint8_t* zone_allocator(
48 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 48 uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
49 Zone* zone = Isolate::Current()->current_zone(); 49 Zone* zone = Isolate::Current()->current_zone();
50 return reinterpret_cast<uint8_t*>( 50 return reinterpret_cast<uint8_t*>(
51 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size)); 51 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size));
52 } 52 }
53 53
54 54
55 static Dart_CMessage* DecodeMessage(uint8_t* message, 55 static Dart_CObject* 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_CObject* root) {
105 // Encode and decode the message.
106 uint8_t* buffer = NULL;
107 MessageWriter writer(&buffer, &malloc_allocator);
108 writer.WriteCMessage(root);
109
110 Zone zone(Isolate::Current());
111 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize,
112 writer.BytesWritten(),
113 &zone_allocator);
114
115 // Check that the two messages are the same.
116 CompareDartCObjects(root, new_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_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
83 writer.BytesWritten(), 139 writer.BytesWritten(),
84 &zone_allocator); 140 &zone_allocator);
85 Dart_CObject* cobject = cmessage->root; 141 EXPECT_NOTNULL(root);
86 EXPECT_NOTNULL(cobject); 142 EXPECT_EQ(Dart_CObject::kNull, root->type);
87 EXPECT_EQ(Dart_CObject::kNull, cobject->type); 143 CheckEncodeDecodeMessage(root);
88 } 144 }
89 145
90 146
91 TEST_CASE(SerializeSmi1) { 147 TEST_CASE(SerializeSmi1) {
92 Zone zone(Isolate::Current()); 148 Zone zone(Isolate::Current());
93 149
94 // Write snapshot with object content. 150 // Write snapshot with object content.
95 uint8_t* buffer; 151 uint8_t* buffer;
96 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 152 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
97 const Smi& smi = Smi::Handle(Smi::New(124)); 153 const Smi& smi = Smi::Handle(Smi::New(124));
98 writer.WriteObject(smi.raw()); 154 writer.WriteObject(smi.raw());
99 writer.FinalizeBuffer(); 155 writer.FinalizeBuffer();
100 156
101 // Create a snapshot object using the buffer. 157 // Create a snapshot object using the buffer.
102 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 158 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
103 159
104 // Read object back from the snapshot. 160 // Read object back from the snapshot.
105 SnapshotReader reader(snapshot, Isolate::Current()); 161 SnapshotReader reader(snapshot, Isolate::Current());
106 const Object& serialized_object = Object::Handle(reader.ReadObject()); 162 const Object& serialized_object = Object::Handle(reader.ReadObject());
107 EXPECT(Equals(smi, serialized_object)); 163 EXPECT(Equals(smi, serialized_object));
108 164
109 // Read object back from the snapshot into a C structure. 165 // Read object back from the snapshot into a C structure.
110 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 166 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
111 writer.BytesWritten(), 167 writer.BytesWritten(),
112 &zone_allocator); 168 &zone_allocator);
113 Dart_CObject* cobject = cmessage->root; 169 EXPECT_NOTNULL(root);
114 EXPECT_NOTNULL(cobject); 170 EXPECT_EQ(Dart_CObject::kInt32, root->type);
115 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); 171 EXPECT_EQ(smi.Value(), root->value.as_int32);
116 EXPECT_EQ(smi.Value(), cobject->value.as_int32); 172 CheckEncodeDecodeMessage(root);
117 } 173 }
118 174
119 175
120 TEST_CASE(SerializeSmi2) { 176 TEST_CASE(SerializeSmi2) {
121 Zone zone(Isolate::Current()); 177 Zone zone(Isolate::Current());
122 178
123 // Write snapshot with object content. 179 // Write snapshot with object content.
124 uint8_t* buffer; 180 uint8_t* buffer;
125 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 181 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
126 const Smi& smi = Smi::Handle(Smi::New(-1)); 182 const Smi& smi = Smi::Handle(Smi::New(-1));
127 writer.WriteObject(smi.raw()); 183 writer.WriteObject(smi.raw());
128 writer.FinalizeBuffer(); 184 writer.FinalizeBuffer();
129 185
130 // Create a snapshot object using the buffer. 186 // Create a snapshot object using the buffer.
131 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 187 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
132 188
133 // Read object back from the snapshot. 189 // Read object back from the snapshot.
134 SnapshotReader reader(snapshot, Isolate::Current()); 190 SnapshotReader reader(snapshot, Isolate::Current());
135 const Object& serialized_object = Object::Handle(reader.ReadObject()); 191 const Object& serialized_object = Object::Handle(reader.ReadObject());
136 EXPECT(Equals(smi, serialized_object)); 192 EXPECT(Equals(smi, serialized_object));
137 193
138 // Read object back from the snapshot into a C structure. 194 // Read object back from the snapshot into a C structure.
139 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 195 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
140 writer.BytesWritten(), 196 writer.BytesWritten(),
141 &zone_allocator); 197 &zone_allocator);
142 Dart_CObject* cobject = cmessage->root; 198 EXPECT_NOTNULL(root);
143 EXPECT_NOTNULL(cobject); 199 EXPECT_EQ(Dart_CObject::kInt32, root->type);
144 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); 200 EXPECT_EQ(smi.Value(), root->value.as_int32);
145 EXPECT_EQ(smi.Value(), cobject->value.as_int32); 201 CheckEncodeDecodeMessage(root);
146 } 202 }
147 203
148 204
149 TEST_CASE(SerializeDouble) { 205 TEST_CASE(SerializeDouble) {
150 Zone zone(Isolate::Current()); 206 Zone zone(Isolate::Current());
151 207
152 // Write snapshot with object content. 208 // Write snapshot with object content.
153 uint8_t* buffer; 209 uint8_t* buffer;
154 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 210 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
155 const Double& dbl = Double::Handle(Double::New(101.29)); 211 const Double& dbl = Double::Handle(Double::New(101.29));
156 writer.WriteObject(dbl.raw()); 212 writer.WriteObject(dbl.raw());
157 writer.FinalizeBuffer(); 213 writer.FinalizeBuffer();
158 214
159 // Create a snapshot object using the buffer. 215 // Create a snapshot object using the buffer.
160 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 216 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
161 217
162 // Read object back from the snapshot. 218 // Read object back from the snapshot.
163 SnapshotReader reader(snapshot, Isolate::Current()); 219 SnapshotReader reader(snapshot, Isolate::Current());
164 const Object& serialized_object = Object::Handle(reader.ReadObject()); 220 const Object& serialized_object = Object::Handle(reader.ReadObject());
165 EXPECT(Equals(dbl, serialized_object)); 221 EXPECT(Equals(dbl, serialized_object));
166 222
167 // Read object back from the snapshot into a C structure. 223 // Read object back from the snapshot into a C structure.
168 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 224 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
169 writer.BytesWritten(), 225 writer.BytesWritten(),
170 &zone_allocator); 226 &zone_allocator);
171 Dart_CObject* cobject = cmessage->root; 227 EXPECT_NOTNULL(root);
172 EXPECT_NOTNULL(cobject); 228 EXPECT_EQ(Dart_CObject::kDouble, root->type);
173 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); 229 EXPECT_EQ(dbl.value(), root->value.as_double);
174 EXPECT_EQ(dbl.value(), cobject->value.as_double); 230 CheckEncodeDecodeMessage(root);
175 } 231 }
176 232
177 233
178 TEST_CASE(SerializeBool) { 234 TEST_CASE(SerializeBool) {
179 Zone zone(Isolate::Current()); 235 Zone zone(Isolate::Current());
180 236
181 // Write snapshot with object content. 237 // Write snapshot with object content.
182 uint8_t* buffer; 238 uint8_t* buffer;
183 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 239 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
184 const Bool& bool1 = Bool::Handle(Bool::True()); 240 const Bool& bool1 = Bool::Handle(Bool::True());
(...skipping 19 matching lines...) Expand all
204 uint8_t* buffer; 260 uint8_t* buffer;
205 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 261 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
206 const Bool& bl = Bool::Handle(Bool::True()); 262 const Bool& bl = Bool::Handle(Bool::True());
207 writer.WriteObject(bl.raw()); 263 writer.WriteObject(bl.raw());
208 writer.FinalizeBuffer(); 264 writer.FinalizeBuffer();
209 265
210 // Create a snapshot object using the buffer. 266 // Create a snapshot object using the buffer.
211 Snapshot::SetupFromBuffer(buffer); 267 Snapshot::SetupFromBuffer(buffer);
212 268
213 // Read object back from the snapshot into a C structure. 269 // Read object back from the snapshot into a C structure.
214 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 270 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
215 writer.BytesWritten(), 271 writer.BytesWritten(),
216 &zone_allocator); 272 &zone_allocator);
217 Dart_CObject* cobject = cmessage->root; 273 EXPECT_NOTNULL(root);
218 EXPECT_NOTNULL(cobject); 274 EXPECT_EQ(Dart_CObject::kBool, root->type);
219 EXPECT_EQ(Dart_CObject::kBool, cobject->type); 275 EXPECT_EQ(true, root->value.as_bool);
220 EXPECT_EQ(true, cobject->value.as_bool); 276 CheckEncodeDecodeMessage(root);
221 } 277 }
222 278
223 279
224 TEST_CASE(SerializeFalse) { 280 TEST_CASE(SerializeFalse) {
225 Zone zone(Isolate::Current()); 281 Zone zone(Isolate::Current());
226 282
227 // Write snapshot with false object. 283 // Write snapshot with false object.
228 uint8_t* buffer; 284 uint8_t* buffer;
229 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 285 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
230 const Bool& bl = Bool::Handle(Bool::False()); 286 const Bool& bl = Bool::Handle(Bool::False());
231 writer.WriteObject(bl.raw()); 287 writer.WriteObject(bl.raw());
232 writer.FinalizeBuffer(); 288 writer.FinalizeBuffer();
233 289
234 // Create a snapshot object using the buffer. 290 // Create a snapshot object using the buffer.
235 Snapshot::SetupFromBuffer(buffer); 291 Snapshot::SetupFromBuffer(buffer);
236 292
237 // Read object back from the snapshot into a C structure. 293 // Read object back from the snapshot into a C structure.
238 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 294 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
239 writer.BytesWritten(), 295 writer.BytesWritten(),
240 &zone_allocator); 296 &zone_allocator);
241 Dart_CObject* cobject = cmessage->root; 297 EXPECT_NOTNULL(root);
242 EXPECT_NOTNULL(cobject); 298 EXPECT_EQ(Dart_CObject::kBool, root->type);
243 EXPECT_EQ(Dart_CObject::kBool, cobject->type); 299 EXPECT_EQ(false, root->value.as_bool);
244 EXPECT_EQ(false, cobject->value.as_bool); 300 CheckEncodeDecodeMessage(root);
245 } 301 }
246 302
247 303
248 TEST_CASE(SerializeBigint) { 304 TEST_CASE(SerializeBigint) {
249 Zone zone(Isolate::Current()); 305 Zone zone(Isolate::Current());
250 306
251 // Write snapshot with object content. 307 // Write snapshot with object content.
252 uint8_t* buffer; 308 uint8_t* buffer;
253 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 309 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
254 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); 310 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL));
255 writer.WriteObject(bigint.raw()); 311 writer.WriteObject(bigint.raw());
256 writer.FinalizeBuffer(); 312 writer.FinalizeBuffer();
257 313
258 // Create a snapshot object using the buffer. 314 // Create a snapshot object using the buffer.
259 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 315 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
260 316
261 // Read object back from the snapshot. 317 // Read object back from the snapshot.
262 SnapshotReader reader(snapshot, Isolate::Current()); 318 SnapshotReader reader(snapshot, Isolate::Current());
263 Bigint& obj = Bigint::Handle(); 319 Bigint& obj = Bigint::Handle();
264 obj ^= reader.ReadObject(); 320 obj ^= reader.ReadObject();
265 OS::Print("%lld", BigintOperations::ToInt64(obj)); 321 OS::Print("%lld", BigintOperations::ToInt64(obj));
266 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); 322 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj));
267 323
268 // Read object back from the snapshot into a C structure. 324 // Read object back from the snapshot into a C structure.
269 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 325 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
270 writer.BytesWritten(), 326 writer.BytesWritten(),
271 &zone_allocator); 327 &zone_allocator);
272 Dart_CObject* cobject = cmessage->root;
273 // Bigint not supported. 328 // Bigint not supported.
274 EXPECT(cobject == NULL); 329 EXPECT(root == NULL);
275 } 330 }
276 331
277 332
278 TEST_CASE(SerializeSingletons) { 333 TEST_CASE(SerializeSingletons) {
279 // Write snapshot with object content. 334 // Write snapshot with object content.
280 uint8_t* buffer; 335 uint8_t* buffer;
281 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); 336 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator);
282 writer.WriteObject(Object::class_class()); 337 writer.WriteObject(Object::class_class());
283 writer.WriteObject(Object::null_class()); 338 writer.WriteObject(Object::null_class());
284 writer.WriteObject(Object::type_class()); 339 writer.WriteObject(Object::type_class());
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 // Create a snapshot object using the buffer. 398 // Create a snapshot object using the buffer.
344 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 399 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
345 400
346 // Read object back from the snapshot. 401 // Read object back from the snapshot.
347 SnapshotReader reader(snapshot, Isolate::Current()); 402 SnapshotReader reader(snapshot, Isolate::Current());
348 String& serialized_str = String::Handle(); 403 String& serialized_str = String::Handle();
349 serialized_str ^= reader.ReadObject(); 404 serialized_str ^= reader.ReadObject();
350 EXPECT(str.Equals(serialized_str)); 405 EXPECT(str.Equals(serialized_str));
351 406
352 // Read object back from the snapshot into a C structure. 407 // Read object back from the snapshot into a C structure.
353 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 408 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
354 writer.BytesWritten(), 409 writer.BytesWritten(),
355 &zone_allocator); 410 &zone_allocator);
356 Dart_CObject* cobject = cmessage->root; 411 EXPECT_EQ(Dart_CObject::kString, root->type);
357 EXPECT_EQ(Dart_CObject::kString, cobject->type); 412 EXPECT_STREQ(cstr, root->value.as_string);
358 EXPECT_STREQ(cstr, cobject->value.as_string); 413 CheckEncodeDecodeMessage(root);
359 } 414 }
360 415
361 416
362 TEST_CASE(SerializeArray) { 417 TEST_CASE(SerializeArray) {
363 Zone zone(Isolate::Current()); 418 Zone zone(Isolate::Current());
364 419
365 // Write snapshot with object content. 420 // Write snapshot with object content.
366 uint8_t* buffer; 421 uint8_t* buffer;
367 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 422 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
368 const int kArrayLength = 10; 423 const int kArrayLength = 10;
369 Array& array = Array::Handle(Array::New(kArrayLength)); 424 Array& array = Array::Handle(Array::New(kArrayLength));
370 Smi& smi = Smi::Handle(); 425 Smi& smi = Smi::Handle();
371 for (int i = 0; i < kArrayLength; i++) { 426 for (int i = 0; i < kArrayLength; i++) {
372 smi ^= Smi::New(i); 427 smi ^= Smi::New(i);
373 array.SetAt(i, smi); 428 array.SetAt(i, smi);
374 } 429 }
375 writer.WriteObject(array.raw()); 430 writer.WriteObject(array.raw());
376 writer.FinalizeBuffer(); 431 writer.FinalizeBuffer();
377 432
378 // Create a snapshot object using the buffer. 433 // Create a snapshot object using the buffer.
379 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 434 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
380 435
381 // Read object back from the snapshot. 436 // Read object back from the snapshot.
382 SnapshotReader reader(snapshot, Isolate::Current()); 437 SnapshotReader reader(snapshot, Isolate::Current());
383 Array& serialized_array = Array::Handle(); 438 Array& serialized_array = Array::Handle();
384 serialized_array ^= reader.ReadObject(); 439 serialized_array ^= reader.ReadObject();
385 EXPECT(array.Equals(serialized_array)); 440 EXPECT(array.Equals(serialized_array));
386 441
387 // Read object back from the snapshot into a C structure. 442 // Read object back from the snapshot into a C structure.
388 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 443 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
389 writer.BytesWritten(), 444 writer.BytesWritten(),
390 &zone_allocator); 445 &zone_allocator);
391 Dart_CObject* cobject = cmessage->root; 446 EXPECT_EQ(Dart_CObject::kArray, root->type);
392 EXPECT_EQ(Dart_CObject::kArray, cobject->type); 447 EXPECT_EQ(kArrayLength, root->value.as_array.length);
393 EXPECT_EQ(kArrayLength, cobject->value.as_array.length);
394 for (int i = 0; i < kArrayLength; i++) { 448 for (int i = 0; i < kArrayLength; i++) {
395 Dart_CObject* element = cobject->value.as_array.values[i]; 449 Dart_CObject* element = root->value.as_array.values[i];
396 EXPECT_EQ(Dart_CObject::kInt32, element->type); 450 EXPECT_EQ(Dart_CObject::kInt32, element->type);
397 EXPECT_EQ(i, element->value.as_int32); 451 EXPECT_EQ(i, element->value.as_int32);
398 } 452 }
453 CheckEncodeDecodeMessage(root);
399 } 454 }
400 455
401 456
402 TEST_CASE(SerializeEmptyArray) { 457 TEST_CASE(SerializeEmptyArray) {
403 Zone zone(Isolate::Current()); 458 Zone zone(Isolate::Current());
404 459
405 // Write snapshot with object content. 460 // Write snapshot with object content.
406 uint8_t* buffer; 461 uint8_t* buffer;
407 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 462 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
408 const int kArrayLength = 0; 463 const int kArrayLength = 0;
409 Array& array = Array::Handle(Array::New(kArrayLength)); 464 Array& array = Array::Handle(Array::New(kArrayLength));
410 writer.WriteObject(array.raw()); 465 writer.WriteObject(array.raw());
411 writer.FinalizeBuffer(); 466 writer.FinalizeBuffer();
412 467
413 // Create a snapshot object using the buffer. 468 // Create a snapshot object using the buffer.
414 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 469 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
415 470
416 // Read object back from the snapshot. 471 // Read object back from the snapshot.
417 SnapshotReader reader(snapshot, Isolate::Current()); 472 SnapshotReader reader(snapshot, Isolate::Current());
418 Array& serialized_array = Array::Handle(); 473 Array& serialized_array = Array::Handle();
419 serialized_array ^= reader.ReadObject(); 474 serialized_array ^= reader.ReadObject();
420 EXPECT(array.Equals(serialized_array)); 475 EXPECT(array.Equals(serialized_array));
421 476
422 // Read object back from the snapshot into a C structure. 477 // Read object back from the snapshot into a C structure.
423 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 478 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
424 writer.BytesWritten(), 479 writer.BytesWritten(),
425 &zone_allocator); 480 &zone_allocator);
426 Dart_CObject* cobject = cmessage->root; 481 EXPECT_EQ(Dart_CObject::kArray, root->type);
427 EXPECT_EQ(Dart_CObject::kArray, cobject->type); 482 EXPECT_EQ(kArrayLength, root->value.as_array.length);
428 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); 483 EXPECT(root->value.as_array.values == NULL);
429 EXPECT(cobject->value.as_array.values == NULL); 484 CheckEncodeDecodeMessage(root);
430 } 485 }
431 486
432 487
433 TEST_CASE(SerializeScript) { 488 TEST_CASE(SerializeScript) {
434 const char* kScriptChars = 489 const char* kScriptChars =
435 "class A {\n" 490 "class A {\n"
436 " static bar() { return 42; }\n" 491 " static bar() { return 42; }\n"
437 " static fly() { return 5; }\n" 492 " static fly() { return 5; }\n"
438 "}\n"; 493 "}\n";
439 494
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 Zone zone(Isolate::Current()); 796 Zone zone(Isolate::Current());
742 uint8_t* buffer = NULL; 797 uint8_t* buffer = NULL;
743 MessageWriter writer(&buffer, &zone_allocator); 798 MessageWriter writer(&buffer, &zone_allocator);
744 799
745 static const int kArrayLength = 2; 800 static const int kArrayLength = 2;
746 intptr_t data[kArrayLength] = {1, 2}; 801 intptr_t data[kArrayLength] = {1, 2};
747 int len = kArrayLength; 802 int len = kArrayLength;
748 writer.WriteMessage(len, data); 803 writer.WriteMessage(len, data);
749 804
750 // Read object back from the snapshot into a C structure. 805 // Read object back from the snapshot into a C structure.
751 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 806 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
752 writer.BytesWritten(), 807 writer.BytesWritten(),
753 &zone_allocator); 808 &zone_allocator);
754 Dart_CObject* cobject = cmessage->root; 809 EXPECT_EQ(Dart_CObject::kArray, root->type);
755 EXPECT_EQ(Dart_CObject::kArray, cobject->type); 810 EXPECT_EQ(kArrayLength, root->value.as_array.length);
756 EXPECT_EQ(kArrayLength, cobject->value.as_array.length);
757 for (int i = 0; i < kArrayLength; i++) { 811 for (int i = 0; i < kArrayLength; i++) {
758 Dart_CObject* element = cobject->value.as_array.values[i]; 812 Dart_CObject* element = root->value.as_array.values[i];
759 EXPECT_EQ(Dart_CObject::kInt32, element->type); 813 EXPECT_EQ(Dart_CObject::kInt32, element->type);
760 EXPECT_EQ(i + 1, element->value.as_int32); 814 EXPECT_EQ(i + 1, element->value.as_int32);
761 } 815 }
816 CheckEncodeDecodeMessage(root);
762 } 817 }
763 818
764 819
765 // Helper function to call a top level Dart function, serialize the 820 // Helper function to call a top level Dart function, serialize the
766 // result and deserialize the result into a Dart_CObject structure. 821 // result and deserialize the result into a Dart_CObject structure.
767 static Dart_CMessage* GetDeserializedDartMessage(Dart_Handle lib, 822 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib,
768 const char* dart_function) { 823 const char* dart_function) {
769 Dart_Handle result; 824 Dart_Handle result;
770 result = Dart_InvokeStatic(lib, 825 result = Dart_InvokeStatic(lib,
771 Dart_NewString(""), 826 Dart_NewString(""),
772 Dart_NewString(dart_function), 827 Dart_NewString(dart_function),
773 0, 828 0,
774 NULL); 829 NULL);
775 EXPECT_VALID(result); 830 EXPECT_VALID(result);
776 831
777 // Serialize the list into a message. 832 // Serialize the list into a message.
778 uint8_t* buffer; 833 uint8_t* buffer;
779 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 834 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
780 const Object& list = Object::Handle(Api::UnwrapHandle(result)); 835 const Object& list = Object::Handle(Api::UnwrapHandle(result));
781 writer.WriteObject(list.raw()); 836 writer.WriteObject(list.raw());
782 writer.FinalizeBuffer(); 837 writer.FinalizeBuffer();
783 838
784 // Read object back from the snapshot into a C structure. 839 // Read object back from the snapshot into a C structure.
785 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 840 return DecodeMessage(buffer + Snapshot::kHeaderSize,
786 writer.BytesWritten(), 841 writer.BytesWritten(),
787 &zone_allocator); 842 &zone_allocator);
788 return cmessage;
789 } 843 }
790 844
791 845
792 UNIT_TEST_CASE(DartGeneratedMessages) { 846 UNIT_TEST_CASE(DartGeneratedMessages) {
793 static const char* kCustomIsolateScriptChars = 847 static const char* kCustomIsolateScriptChars =
794 "getSmi() {\n" 848 "getSmi() {\n"
795 " return 42;\n" 849 " return 42;\n"
796 "}\n" 850 "}\n"
797 "getString() {\n" 851 "getString() {\n"
798 " return \"Hello, world!\";\n" 852 " return \"Hello, world!\";\n"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 { 885 {
832 Zone zone(Isolate::Current()); 886 Zone zone(Isolate::Current());
833 uint8_t* buffer; 887 uint8_t* buffer;
834 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 888 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
835 Smi& smi = Smi::Handle(); 889 Smi& smi = Smi::Handle();
836 smi ^= Api::UnwrapHandle(smi_result); 890 smi ^= Api::UnwrapHandle(smi_result);
837 writer.WriteObject(smi.raw()); 891 writer.WriteObject(smi.raw());
838 writer.FinalizeBuffer(); 892 writer.FinalizeBuffer();
839 893
840 // Read object back from the snapshot into a C structure. 894 // Read object back from the snapshot into a C structure.
841 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 895 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
842 writer.BytesWritten(), 896 writer.BytesWritten(),
843 &zone_allocator); 897 &zone_allocator);
844 Dart_CObject* cobject = cmessage->root; 898 EXPECT_NOTNULL(root);
845 EXPECT_NOTNULL(cobject); 899 EXPECT_EQ(Dart_CObject::kInt32, root->type);
846 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); 900 EXPECT_EQ(42, root->value.as_int32);
847 EXPECT_EQ(42, cobject->value.as_int32); 901 CheckEncodeDecodeMessage(root);
848 } 902 }
849 { 903 {
850 Zone zone(Isolate::Current()); 904 Zone zone(Isolate::Current());
851 uint8_t* buffer; 905 uint8_t* buffer;
852 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 906 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
853 String& str = String::Handle(); 907 String& str = String::Handle();
854 str ^= Api::UnwrapHandle(string_result); 908 str ^= Api::UnwrapHandle(string_result);
855 writer.WriteObject(str.raw()); 909 writer.WriteObject(str.raw());
856 writer.FinalizeBuffer(); 910 writer.FinalizeBuffer();
857 911
858 // Read object back from the snapshot into a C structure. 912 // Read object back from the snapshot into a C structure.
859 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, 913 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
860 writer.BytesWritten(), 914 writer.BytesWritten(),
861 &zone_allocator); 915 &zone_allocator);
862 Dart_CObject* cobject = cmessage->root; 916 EXPECT_NOTNULL(root);
863 EXPECT_NOTNULL(cobject); 917 EXPECT_EQ(Dart_CObject::kString, root->type);
864 EXPECT_EQ(Dart_CObject::kString, cobject->type); 918 EXPECT_STREQ("Hello, world!", root->value.as_string);
865 EXPECT_STREQ("Hello, world!", cobject->value.as_string); 919 CheckEncodeDecodeMessage(root);
866 } 920 }
867 } 921 }
868 Dart_ExitScope(); 922 Dart_ExitScope();
869 Dart_ShutdownIsolate(); 923 Dart_ShutdownIsolate();
870 } 924 }
871 925
872 926
873 UNIT_TEST_CASE(DartGeneratedListMessages) { 927 UNIT_TEST_CASE(DartGeneratedListMessages) {
874 const int kArrayLength = 10; 928 const int kArrayLength = 10;
875 static const char* kScriptChars = 929 static const char* kScriptChars =
(...skipping 23 matching lines...) Expand all
899 TestCase::CreateTestIsolate(); 953 TestCase::CreateTestIsolate();
900 Isolate* isolate = Isolate::Current(); 954 Isolate* isolate = Isolate::Current();
901 EXPECT(isolate != NULL); 955 EXPECT(isolate != NULL);
902 Dart_EnterScope(); 956 Dart_EnterScope();
903 957
904 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 958 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
905 EXPECT_VALID(lib); 959 EXPECT_VALID(lib);
906 960
907 { 961 {
908 DARTSCOPE_NOCHECKS(isolate); 962 DARTSCOPE_NOCHECKS(isolate);
909
910 { 963 {
911 // Generate a list of nulls from Dart code. 964 // Generate a list of nulls from Dart code.
912 Zone zone(Isolate::Current()); 965 Zone zone(Isolate::Current());
913 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getList"); 966 Dart_CObject* root = GetDeserializedDartMessage(lib, "getList");
914 Dart_CObject* value = cmessage->root; 967 EXPECT_NOTNULL(root);
915 EXPECT_NOTNULL(value); 968 EXPECT_EQ(Dart_CObject::kArray, root->type);
916 EXPECT_EQ(Dart_CObject::kArray, value->type); 969 EXPECT_EQ(kArrayLength, root->value.as_array.length);
917 EXPECT_EQ(kArrayLength, value->value.as_array.length);
918 for (int i = 0; i < kArrayLength; i++) { 970 for (int i = 0; i < kArrayLength; i++) {
919 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); 971 EXPECT_EQ(Dart_CObject::kNull, root->value.as_array.values[i]->type);
920 } 972 }
973 CheckEncodeDecodeMessage(root);
921 } 974 }
922 { 975 {
923 // Generate a list of ints from Dart code. 976 // Generate a list of ints from Dart code.
924 Zone zone(Isolate::Current()); 977 Zone zone(Isolate::Current());
925 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getIntList"); 978 Dart_CObject* root = GetDeserializedDartMessage(lib, "getIntList");
926 Dart_CObject* value = cmessage->root; 979 EXPECT_NOTNULL(root);
927 EXPECT_NOTNULL(value); 980 EXPECT_EQ(Dart_CObject::kArray, root->type);
928 EXPECT_EQ(Dart_CObject::kArray, value->type); 981 EXPECT_EQ(kArrayLength, root->value.as_array.length);
929 EXPECT_EQ(kArrayLength, value->value.as_array.length);
930 for (int i = 0; i < kArrayLength; i++) { 982 for (int i = 0; i < kArrayLength; i++) {
931 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type); 983 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[i]->type);
932 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32); 984 EXPECT_EQ(i, root->value.as_array.values[i]->value.as_int32);
933 } 985 }
986 CheckEncodeDecodeMessage(root);
934 } 987 }
935 { 988 {
936 // Generate a list of strings from Dart code. 989 // Generate a list of strings from Dart code.
937 Zone zone(Isolate::Current()); 990 Zone zone(Isolate::Current());
938 Dart_CMessage* cmessage = 991 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList");
939 GetDeserializedDartMessage(lib, "getStringList"); 992 EXPECT_NOTNULL(root);
940 Dart_CObject* value = cmessage->root; 993 EXPECT_EQ(Dart_CObject::kArray, root->type);
941 EXPECT_NOTNULL(value); 994 EXPECT_EQ(kArrayLength, root->value.as_array.length);
942 EXPECT_EQ(Dart_CObject::kArray, value->type);
943 EXPECT_EQ(kArrayLength, value->value.as_array.length);
944 for (int i = 0; i < kArrayLength; i++) { 995 for (int i = 0; i < kArrayLength; i++) {
945 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[i]->type); 996 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[i]->type);
946 char buffer[3]; 997 char buffer[3];
947 snprintf(buffer, sizeof(buffer), "%d", i); 998 snprintf(buffer, sizeof(buffer), "%d", i);
948 EXPECT_STREQ(buffer, value->value.as_array.values[i]->value.as_string); 999 EXPECT_STREQ(buffer, root->value.as_array.values[i]->value.as_string);
949 } 1000 }
950 } 1001 }
951 { 1002 {
952 // Generate a list of objects of different types from Dart code. 1003 // Generate a list of objects of different types from Dart code.
953 Zone zone(Isolate::Current()); 1004 Zone zone(Isolate::Current());
954 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getMixedList"); 1005 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
955 Dart_CObject* value = cmessage->root; 1006 EXPECT_NOTNULL(root);
956 EXPECT_NOTNULL(value); 1007 EXPECT_EQ(Dart_CObject::kArray, root->type);
957 EXPECT_EQ(Dart_CObject::kArray, value->type); 1008 EXPECT_EQ(kArrayLength, root->value.as_array.length);
958 EXPECT_EQ(kArrayLength, value->value.as_array.length);
959 1009
960 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[0]->type); 1010 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[0]->type);
961 EXPECT_EQ(0, value->value.as_array.values[0]->value.as_int32); 1011 EXPECT_EQ(0, root->value.as_array.values[0]->value.as_int32);
962 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[1]->type); 1012 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[1]->type);
963 EXPECT_STREQ("1", value->value.as_array.values[1]->value.as_string); 1013 EXPECT_STREQ("1", root->value.as_array.values[1]->value.as_string);
964 EXPECT_EQ(Dart_CObject::kDouble, value->value.as_array.values[2]->type); 1014 EXPECT_EQ(Dart_CObject::kDouble, root->value.as_array.values[2]->type);
965 EXPECT_EQ(2.2, value->value.as_array.values[2]->value.as_double); 1015 EXPECT_EQ(2.2, root->value.as_array.values[2]->value.as_double);
966 EXPECT_EQ(Dart_CObject::kBool, value->value.as_array.values[3]->type); 1016 EXPECT_EQ(Dart_CObject::kBool, root->value.as_array.values[3]->type);
967 EXPECT_EQ(true, value->value.as_array.values[3]->value.as_bool); 1017 EXPECT_EQ(true, root->value.as_array.values[3]->value.as_bool);
968 1018
969 for (int i = 0; i < kArrayLength; i++) { 1019 for (int i = 0; i < kArrayLength; i++) {
970 if (i > 3) { 1020 if (i > 3) {
971 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); 1021 EXPECT_EQ(Dart_CObject::kNull, root->value.as_array.values[i]->type);
972 } 1022 }
973 } 1023 }
974 } 1024 }
975 } 1025 }
976 Dart_ExitScope(); 1026 Dart_ExitScope();
977 Dart_ShutdownIsolate(); 1027 Dart_ShutdownIsolate();
978 } 1028 }
979 1029
980 1030
981 UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) { 1031 UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 1066
1017 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1067 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1018 EXPECT_VALID(lib); 1068 EXPECT_VALID(lib);
1019 1069
1020 { 1070 {
1021 DARTSCOPE_NOCHECKS(isolate); 1071 DARTSCOPE_NOCHECKS(isolate);
1022 1072
1023 { 1073 {
1024 // Generate a list of strings from Dart code. 1074 // Generate a list of strings from Dart code.
1025 Zone zone(Isolate::Current()); 1075 Zone zone(Isolate::Current());
1026 Dart_CMessage* cmessage = 1076 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList");
1027 GetDeserializedDartMessage(lib, "getStringList"); 1077 EXPECT_NOTNULL(root);
1028 Dart_CObject* object = cmessage->root; 1078 EXPECT_EQ(Dart_CObject::kArray, root->type);
1029 EXPECT_NOTNULL(object); 1079 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1030 EXPECT_EQ(Dart_CObject::kArray, object->type);
1031 EXPECT_EQ(kArrayLength, object->value.as_array.length);
1032 for (int i = 0; i < kArrayLength; i++) { 1080 for (int i = 0; i < kArrayLength; i++) {
1033 Dart_CObject* element = object->value.as_array.values[i]; 1081 Dart_CObject* element = root->value.as_array.values[i];
1034 EXPECT_EQ(object->value.as_array.values[0], element); 1082 EXPECT_EQ(root->value.as_array.values[0], element);
1035 EXPECT_EQ(Dart_CObject::kString, element->type); 1083 EXPECT_EQ(Dart_CObject::kString, element->type);
1036 EXPECT_STREQ("Hello, world!", element->value.as_string); 1084 EXPECT_STREQ("Hello, world!", element->value.as_string);
1037 } 1085 }
1038 } 1086 }
1039 { 1087 {
1040 // Generate a list of doubles from Dart code. 1088 // Generate a list of doubles from Dart code.
1041 Zone zone(Isolate::Current()); 1089 Zone zone(Isolate::Current());
1042 Dart_CMessage* cmessage = 1090 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList");
1043 GetDeserializedDartMessage(lib, "getDoubleList"); 1091 EXPECT_NOTNULL(root);
1044 Dart_CObject* object = cmessage->root; 1092 EXPECT_EQ(Dart_CObject::kArray, root->type);
1045 EXPECT_NOTNULL(object); 1093 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1046 EXPECT_EQ(Dart_CObject::kArray, object->type);
1047 EXPECT_EQ(kArrayLength, object->value.as_array.length);
1048 for (int i = 0; i < kArrayLength; i++) { 1094 for (int i = 0; i < kArrayLength; i++) {
1049 Dart_CObject* element = object->value.as_array.values[i]; 1095 Dart_CObject* element = root->value.as_array.values[i];
1050 EXPECT_EQ(object->value.as_array.values[0], element); 1096 EXPECT_EQ(root->value.as_array.values[0], element);
1051 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1097 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1052 EXPECT_EQ(3.14, element->value.as_double); 1098 EXPECT_EQ(3.14, element->value.as_double);
1053 } 1099 }
1054 } 1100 }
1055 { 1101 {
1056 // Generate a list of objects of different types from Dart code. 1102 // Generate a list of objects of different types from Dart code.
1057 Zone zone(Isolate::Current()); 1103 Zone zone(Isolate::Current());
1058 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getMixedList"); 1104 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
1059 Dart_CObject* object = cmessage->root; 1105 EXPECT_NOTNULL(root);
1060 EXPECT_NOTNULL(object); 1106 EXPECT_EQ(Dart_CObject::kArray, root->type);
1061 EXPECT_EQ(Dart_CObject::kArray, object->type); 1107 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1062 EXPECT_EQ(kArrayLength, object->value.as_array.length);
1063 for (int i = 0; i < kArrayLength; i++) { 1108 for (int i = 0; i < kArrayLength; i++) {
1064 Dart_CObject* element = object->value.as_array.values[i]; 1109 Dart_CObject* element = root->value.as_array.values[i];
1065 if ((i % 2) == 0) { 1110 if ((i % 2) == 0) {
1066 EXPECT_EQ(object->value.as_array.values[0], element); 1111 EXPECT_EQ(root->value.as_array.values[0], element);
1067 EXPECT_EQ(Dart_CObject::kString, element->type); 1112 EXPECT_EQ(Dart_CObject::kString, element->type);
1068 EXPECT_STREQ("A", element->value.as_string); 1113 EXPECT_STREQ("A", element->value.as_string);
1069 } else { 1114 } else {
1070 EXPECT_EQ(object->value.as_array.values[1], element); 1115 EXPECT_EQ(root->value.as_array.values[1], element);
1071 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1116 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1072 EXPECT_STREQ(2.72, element->value.as_double); 1117 EXPECT_STREQ(2.72, element->value.as_double);
1073 } 1118 }
1074 } 1119 }
1075 } 1120 }
1076 { 1121 {
1077 // Generate a list of objects of different types from Dart code. 1122 // Generate a list of objects of different types from Dart code.
1078 Zone zone(Isolate::Current()); 1123 Zone zone(Isolate::Current());
1079 Dart_CMessage* cmessage = 1124 Dart_CObject* root = GetDeserializedDartMessage(lib, "getSelfRefList");
1080 GetDeserializedDartMessage(lib, "getSelfRefList"); 1125 EXPECT_NOTNULL(root);
1081 Dart_CObject* object = cmessage->root; 1126 EXPECT_EQ(Dart_CObject::kArray, root->type);
1082 EXPECT_NOTNULL(object); 1127 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1083 EXPECT_EQ(Dart_CObject::kArray, object->type);
1084 EXPECT_EQ(kArrayLength, object->value.as_array.length);
1085 for (int i = 0; i < kArrayLength; i++) { 1128 for (int i = 0; i < kArrayLength; i++) {
1086 Dart_CObject* element = object->value.as_array.values[i]; 1129 Dart_CObject* element = root->value.as_array.values[i];
1087 EXPECT_EQ(Dart_CObject::kArray, element->type); 1130 EXPECT_EQ(Dart_CObject::kArray, element->type);
1088 EXPECT_EQ(object, element); 1131 EXPECT_EQ(root, element);
1089 } 1132 }
1090 } 1133 }
1091 } 1134 }
1092 Dart_ExitScope(); 1135 Dart_ExitScope();
1093 Dart_ShutdownIsolate(); 1136 Dart_ShutdownIsolate();
1094 } 1137 }
1095 1138
1139
1140 UNIT_TEST_CASE(PostCObject) {
1141 // Create a native port for posting from C to Dart
1142 TestIsolateScope __test_isolate__;
1143 const char* kScriptChars =
1144 "void main() {\n"
1145 " var messageCount = 0;\n"
1146 " var exception = '';\n"
1147 " var port = new ReceivePort();\n"
1148 " port.receive((message, replyTo) {\n"
1149 " exception += message.toString();\n"
1150 " messageCount++;\n"
1151 " if (messageCount == 7) throw new Exception(exception);\n"
1152 " });\n"
1153 " return port.toSendPort();\n"
1154 "}\n";
1155 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1156 Dart_EnterScope();
1157
1158 // xxx
1159 Dart_Handle send_port = Dart_InvokeStatic(lib,
1160 Dart_NewString(""),
1161 Dart_NewString("main"),
1162 0,
1163 NULL);
1164 EXPECT_VALID(send_port);
1165 Dart_Handle result =
1166 Dart_GetInstanceField(send_port, Dart_NewString("_id"));
1167 ASSERT(!Dart_IsError(result));
1168 ASSERT(Dart_IsInteger(result));
1169 int64_t send_port_id;
1170 Dart_Handle result2 = Dart_IntegerToInt64(result, &send_port_id);
1171 ASSERT(!Dart_IsError(result2));
1172
1173 // Setup single object message.
1174 Dart_CObject object;
1175
1176 object.type = Dart_CObject::kNull;
1177 EXPECT(Dart_PostCObject(send_port_id, &object));
1178
1179 object.type = Dart_CObject::kBool;
1180 object.value.as_bool = true;
1181 EXPECT(Dart_PostCObject(send_port_id, &object));
1182
1183 object.type = Dart_CObject::kBool;
1184 object.value.as_bool = false;
1185 EXPECT(Dart_PostCObject(send_port_id, &object));
1186
1187 object.type = Dart_CObject::kInt32;
1188 object.value.as_int32 = 123;
1189 EXPECT(Dart_PostCObject(send_port_id, &object));
1190
1191 object.type = Dart_CObject::kString;
1192 object.value.as_string = const_cast<char*>("456");
1193 EXPECT(Dart_PostCObject(send_port_id, &object));
1194
1195 object.type = Dart_CObject::kDouble;
1196 object.value.as_double = 3.14;
1197 EXPECT(Dart_PostCObject(send_port_id, &object));
1198
1199 object.type = Dart_CObject::kArray;
1200 object.value.as_array.length = 0;
1201 EXPECT(Dart_PostCObject(send_port_id, &object));
1202
1203 result = Dart_RunLoop();
1204 EXPECT(Dart_IsError(result));
1205 EXPECT(Dart_ErrorHasException(result));
1206 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
1207 Dart_GetError(result));
1208
1209 Dart_ExitScope();
1210 }
1211
1096 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1212 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1097 1213
1098 } // namespace dart 1214 } // namespace dart
OLDNEW
« runtime/include/dart_api.h ('K') | « runtime/vm/snapshot.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698