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

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

Issue 9325022: Decode the Dart message into a Dart_CMessage structure before calling the native port callback (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
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | runtime/vm/vm_sources.gypi » ('j') | 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_message.h"
10 #include "vm/dart_api_state.h" 11 #include "vm/dart_api_state.h"
11 #include "vm/snapshot.h" 12 #include "vm/snapshot.h"
12 #include "vm/unit_test.h" 13 #include "vm/unit_test.h"
13 14
14 namespace dart { 15 namespace dart {
15 16
16 // Check if serialized and deserialized objects are equal. 17 // Check if serialized and deserialized objects are equal.
17 static bool Equals(const Object& expected, const Object& actual) { 18 static bool Equals(const Object& expected, const Object& actual) {
18 if (expected.IsNull()) { 19 if (expected.IsNull()) {
19 return actual.IsNull(); 20 return actual.IsNull();
(...skipping 28 matching lines...) Expand all
48 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { 49 uint8_t* ptr, intptr_t old_size, intptr_t new_size) {
49 Zone* zone = Isolate::Current()->current_zone(); 50 Zone* zone = Isolate::Current()->current_zone();
50 return reinterpret_cast<uint8_t*>( 51 return reinterpret_cast<uint8_t*>(
51 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size)); 52 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size));
52 } 53 }
53 54
54 55
55 static Dart_CObject* DecodeMessage(uint8_t* message, 56 static Dart_CObject* DecodeMessage(uint8_t* message,
56 intptr_t length, 57 intptr_t length,
57 ReAlloc allocator) { 58 ReAlloc allocator) {
58 CMessageReader message_reader(message, length, allocator); 59 ApiMessageReader message_reader(message, length, allocator);
59 return message_reader.ReadMessage(); 60 return message_reader.ReadMessage();
60 } 61 }
61 62
62 63
63 // Compare two Dart_CObject object graphs rooted in first and 64 // Compare two Dart_CObject object graphs rooted in first and
64 // second. The second graph will be destroyed by this operation no matter 65 // second. The second graph will be destroyed by this operation no matter
65 // whether the graphs are equal or not. 66 // whether the graphs are equal or not.
66 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { 67 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) {
67 // Return immediately if entering a cycle. 68 // Return immediately if entering a cycle.
68 if (second->type == Dart_CObject::kNumberOfTypes) return; 69 if (second->type == Dart_CObject::kNumberOfTypes) return;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 } 101 }
101 } 102 }
102 103
103 104
104 static void CheckEncodeDecodeMessage(Dart_CObject* root) { 105 static void CheckEncodeDecodeMessage(Dart_CObject* root) {
105 // Encode and decode the message. 106 // Encode and decode the message.
106 uint8_t* buffer = NULL; 107 uint8_t* buffer = NULL;
107 MessageWriter writer(&buffer, &malloc_allocator); 108 MessageWriter writer(&buffer, &malloc_allocator);
108 writer.WriteCMessage(root); 109 writer.WriteCMessage(root);
109 110
110 Zone zone(Isolate::Current());
111 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize, 111 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize,
112 writer.BytesWritten(), 112 writer.BytesWritten(),
113 &zone_allocator); 113 &zone_allocator);
114 114
115 // Check that the two messages are the same. 115 // Check that the two messages are the same.
116 CompareDartCObjects(root, new_root); 116 CompareDartCObjects(root, new_root);
117 } 117 }
118 118
119 TEST_CASE(SerializeNull) { 119 TEST_CASE(SerializeNull) {
120 Zone zone(Isolate::Current()); 120 Zone zone(Isolate::Current());
121 121
122 // Write snapshot with object content. 122 // Write snapshot with object content.
123 uint8_t* buffer; 123 uint8_t* buffer;
124 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 124 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
125 const Object& null_object = Object::Handle(); 125 const Object& null_object = Object::Handle();
126 writer.WriteObject(null_object.raw()); 126 writer.WriteObject(null_object.raw());
127 writer.FinalizeBuffer(); 127 writer.FinalizeBuffer();
128 128
129 // Create a snapshot object using the buffer. 129 // Create a snapshot object using the buffer.
130 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 130 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
131 131
132 // Read object back from the snapshot. 132 // Read object back from the snapshot.
133 SnapshotReader reader(snapshot, Isolate::Current()); 133 SnapshotReader reader(snapshot, Isolate::Current());
134 const Object& serialized_object = Object::Handle(reader.ReadObject()); 134 const Object& serialized_object = Object::Handle(reader.ReadObject());
135 EXPECT(Equals(null_object, serialized_object)); 135 EXPECT(Equals(null_object, serialized_object));
136 136
137 // Read object back from the snapshot into a C structure. 137 // Read object back from the snapshot into a C structure.
138 ApiNativeScope scope;
138 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 139 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
139 writer.BytesWritten(), 140 writer.BytesWritten(),
140 &zone_allocator); 141 &zone_allocator);
141 EXPECT_NOTNULL(root); 142 EXPECT_NOTNULL(root);
142 EXPECT_EQ(Dart_CObject::kNull, root->type); 143 EXPECT_EQ(Dart_CObject::kNull, root->type);
143 CheckEncodeDecodeMessage(root); 144 CheckEncodeDecodeMessage(root);
144 } 145 }
145 146
146 147
147 TEST_CASE(SerializeSmi1) { 148 TEST_CASE(SerializeSmi1) {
148 Zone zone(Isolate::Current()); 149 Zone zone(Isolate::Current());
149 150
150 // Write snapshot with object content. 151 // Write snapshot with object content.
151 uint8_t* buffer; 152 uint8_t* buffer;
152 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 153 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
153 const Smi& smi = Smi::Handle(Smi::New(124)); 154 const Smi& smi = Smi::Handle(Smi::New(124));
154 writer.WriteObject(smi.raw()); 155 writer.WriteObject(smi.raw());
155 writer.FinalizeBuffer(); 156 writer.FinalizeBuffer();
156 157
157 // Create a snapshot object using the buffer. 158 // Create a snapshot object using the buffer.
158 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 159 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
159 160
160 // Read object back from the snapshot. 161 // Read object back from the snapshot.
161 SnapshotReader reader(snapshot, Isolate::Current()); 162 SnapshotReader reader(snapshot, Isolate::Current());
162 const Object& serialized_object = Object::Handle(reader.ReadObject()); 163 const Object& serialized_object = Object::Handle(reader.ReadObject());
163 EXPECT(Equals(smi, serialized_object)); 164 EXPECT(Equals(smi, serialized_object));
164 165
165 // Read object back from the snapshot into a C structure. 166 // Read object back from the snapshot into a C structure.
167 ApiNativeScope scope;
166 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 168 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
167 writer.BytesWritten(), 169 writer.BytesWritten(),
168 &zone_allocator); 170 &zone_allocator);
169 EXPECT_NOTNULL(root); 171 EXPECT_NOTNULL(root);
170 EXPECT_EQ(Dart_CObject::kInt32, root->type); 172 EXPECT_EQ(Dart_CObject::kInt32, root->type);
171 EXPECT_EQ(smi.Value(), root->value.as_int32); 173 EXPECT_EQ(smi.Value(), root->value.as_int32);
172 CheckEncodeDecodeMessage(root); 174 CheckEncodeDecodeMessage(root);
173 } 175 }
174 176
175 177
176 TEST_CASE(SerializeSmi2) { 178 TEST_CASE(SerializeSmi2) {
177 Zone zone(Isolate::Current()); 179 Zone zone(Isolate::Current());
178 180
179 // Write snapshot with object content. 181 // Write snapshot with object content.
180 uint8_t* buffer; 182 uint8_t* buffer;
181 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 183 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
182 const Smi& smi = Smi::Handle(Smi::New(-1)); 184 const Smi& smi = Smi::Handle(Smi::New(-1));
183 writer.WriteObject(smi.raw()); 185 writer.WriteObject(smi.raw());
184 writer.FinalizeBuffer(); 186 writer.FinalizeBuffer();
185 187
186 // Create a snapshot object using the buffer. 188 // Create a snapshot object using the buffer.
187 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 189 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
188 190
189 // Read object back from the snapshot. 191 // Read object back from the snapshot.
190 SnapshotReader reader(snapshot, Isolate::Current()); 192 SnapshotReader reader(snapshot, Isolate::Current());
191 const Object& serialized_object = Object::Handle(reader.ReadObject()); 193 const Object& serialized_object = Object::Handle(reader.ReadObject());
192 EXPECT(Equals(smi, serialized_object)); 194 EXPECT(Equals(smi, serialized_object));
193 195
194 // Read object back from the snapshot into a C structure. 196 // Read object back from the snapshot into a C structure.
197 ApiNativeScope scope;
195 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 198 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
196 writer.BytesWritten(), 199 writer.BytesWritten(),
197 &zone_allocator); 200 &zone_allocator);
198 EXPECT_NOTNULL(root); 201 EXPECT_NOTNULL(root);
199 EXPECT_EQ(Dart_CObject::kInt32, root->type); 202 EXPECT_EQ(Dart_CObject::kInt32, root->type);
200 EXPECT_EQ(smi.Value(), root->value.as_int32); 203 EXPECT_EQ(smi.Value(), root->value.as_int32);
201 CheckEncodeDecodeMessage(root); 204 CheckEncodeDecodeMessage(root);
202 } 205 }
203 206
204 207
205 TEST_CASE(SerializeDouble) { 208 TEST_CASE(SerializeDouble) {
206 Zone zone(Isolate::Current()); 209 Zone zone(Isolate::Current());
207 210
208 // Write snapshot with object content. 211 // Write snapshot with object content.
209 uint8_t* buffer; 212 uint8_t* buffer;
210 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 213 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
211 const Double& dbl = Double::Handle(Double::New(101.29)); 214 const Double& dbl = Double::Handle(Double::New(101.29));
212 writer.WriteObject(dbl.raw()); 215 writer.WriteObject(dbl.raw());
213 writer.FinalizeBuffer(); 216 writer.FinalizeBuffer();
214 217
215 // Create a snapshot object using the buffer. 218 // Create a snapshot object using the buffer.
216 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 219 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
217 220
218 // Read object back from the snapshot. 221 // Read object back from the snapshot.
219 SnapshotReader reader(snapshot, Isolate::Current()); 222 SnapshotReader reader(snapshot, Isolate::Current());
220 const Object& serialized_object = Object::Handle(reader.ReadObject()); 223 const Object& serialized_object = Object::Handle(reader.ReadObject());
221 EXPECT(Equals(dbl, serialized_object)); 224 EXPECT(Equals(dbl, serialized_object));
222 225
223 // Read object back from the snapshot into a C structure. 226 // Read object back from the snapshot into a C structure.
227 ApiNativeScope scope;
224 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 228 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
225 writer.BytesWritten(), 229 writer.BytesWritten(),
226 &zone_allocator); 230 &zone_allocator);
227 EXPECT_NOTNULL(root); 231 EXPECT_NOTNULL(root);
228 EXPECT_EQ(Dart_CObject::kDouble, root->type); 232 EXPECT_EQ(Dart_CObject::kDouble, root->type);
229 EXPECT_EQ(dbl.value(), root->value.as_double); 233 EXPECT_EQ(dbl.value(), root->value.as_double);
230 CheckEncodeDecodeMessage(root); 234 CheckEncodeDecodeMessage(root);
231 } 235 }
232 236
233 237
(...skipping 26 matching lines...) Expand all
260 uint8_t* buffer; 264 uint8_t* buffer;
261 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 265 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
262 const Bool& bl = Bool::Handle(Bool::True()); 266 const Bool& bl = Bool::Handle(Bool::True());
263 writer.WriteObject(bl.raw()); 267 writer.WriteObject(bl.raw());
264 writer.FinalizeBuffer(); 268 writer.FinalizeBuffer();
265 269
266 // Create a snapshot object using the buffer. 270 // Create a snapshot object using the buffer.
267 Snapshot::SetupFromBuffer(buffer); 271 Snapshot::SetupFromBuffer(buffer);
268 272
269 // Read object back from the snapshot into a C structure. 273 // Read object back from the snapshot into a C structure.
274 ApiNativeScope scope;
270 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 275 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
271 writer.BytesWritten(), 276 writer.BytesWritten(),
272 &zone_allocator); 277 &zone_allocator);
273 EXPECT_NOTNULL(root); 278 EXPECT_NOTNULL(root);
274 EXPECT_EQ(Dart_CObject::kBool, root->type); 279 EXPECT_EQ(Dart_CObject::kBool, root->type);
275 EXPECT_EQ(true, root->value.as_bool); 280 EXPECT_EQ(true, root->value.as_bool);
276 CheckEncodeDecodeMessage(root); 281 CheckEncodeDecodeMessage(root);
277 } 282 }
278 283
279 284
280 TEST_CASE(SerializeFalse) { 285 TEST_CASE(SerializeFalse) {
281 Zone zone(Isolate::Current()); 286 Zone zone(Isolate::Current());
282 287
283 // Write snapshot with false object. 288 // Write snapshot with false object.
284 uint8_t* buffer; 289 uint8_t* buffer;
285 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 290 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
286 const Bool& bl = Bool::Handle(Bool::False()); 291 const Bool& bl = Bool::Handle(Bool::False());
287 writer.WriteObject(bl.raw()); 292 writer.WriteObject(bl.raw());
288 writer.FinalizeBuffer(); 293 writer.FinalizeBuffer();
289 294
290 // Create a snapshot object using the buffer. 295 // Create a snapshot object using the buffer.
291 Snapshot::SetupFromBuffer(buffer); 296 Snapshot::SetupFromBuffer(buffer);
292 297
293 // Read object back from the snapshot into a C structure. 298 // Read object back from the snapshot into a C structure.
299 ApiNativeScope scope;
294 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 300 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
295 writer.BytesWritten(), 301 writer.BytesWritten(),
296 &zone_allocator); 302 &zone_allocator);
297 EXPECT_NOTNULL(root); 303 EXPECT_NOTNULL(root);
298 EXPECT_EQ(Dart_CObject::kBool, root->type); 304 EXPECT_EQ(Dart_CObject::kBool, root->type);
299 EXPECT_EQ(false, root->value.as_bool); 305 EXPECT_EQ(false, root->value.as_bool);
300 CheckEncodeDecodeMessage(root); 306 CheckEncodeDecodeMessage(root);
301 } 307 }
302 308
303 309
(...skipping 11 matching lines...) Expand all
315 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 321 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
316 322
317 // Read object back from the snapshot. 323 // Read object back from the snapshot.
318 SnapshotReader reader(snapshot, Isolate::Current()); 324 SnapshotReader reader(snapshot, Isolate::Current());
319 Bigint& obj = Bigint::Handle(); 325 Bigint& obj = Bigint::Handle();
320 obj ^= reader.ReadObject(); 326 obj ^= reader.ReadObject();
321 OS::Print("%lld", BigintOperations::ToInt64(obj)); 327 OS::Print("%lld", BigintOperations::ToInt64(obj));
322 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); 328 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj));
323 329
324 // Read object back from the snapshot into a C structure. 330 // Read object back from the snapshot into a C structure.
331 ApiNativeScope scope;
325 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 332 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
326 writer.BytesWritten(), 333 writer.BytesWritten(),
327 &zone_allocator); 334 &zone_allocator);
328 // Bigint not supported. 335 // Bigint not supported.
329 EXPECT(root == NULL); 336 EXPECT(root == NULL);
330 } 337 }
331 338
332 339
333 TEST_CASE(SerializeSingletons) { 340 TEST_CASE(SerializeSingletons) {
334 // Write snapshot with object content. 341 // Write snapshot with object content.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 // Create a snapshot object using the buffer. 405 // Create a snapshot object using the buffer.
399 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 406 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
400 407
401 // Read object back from the snapshot. 408 // Read object back from the snapshot.
402 SnapshotReader reader(snapshot, Isolate::Current()); 409 SnapshotReader reader(snapshot, Isolate::Current());
403 String& serialized_str = String::Handle(); 410 String& serialized_str = String::Handle();
404 serialized_str ^= reader.ReadObject(); 411 serialized_str ^= reader.ReadObject();
405 EXPECT(str.Equals(serialized_str)); 412 EXPECT(str.Equals(serialized_str));
406 413
407 // Read object back from the snapshot into a C structure. 414 // Read object back from the snapshot into a C structure.
415 ApiNativeScope scope;
408 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 416 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
409 writer.BytesWritten(), 417 writer.BytesWritten(),
410 &zone_allocator); 418 &zone_allocator);
411 EXPECT_EQ(Dart_CObject::kString, root->type); 419 EXPECT_EQ(Dart_CObject::kString, root->type);
412 EXPECT_STREQ(cstr, root->value.as_string); 420 EXPECT_STREQ(cstr, root->value.as_string);
413 CheckEncodeDecodeMessage(root); 421 CheckEncodeDecodeMessage(root);
414 } 422 }
415 423
416 424
417 TEST_CASE(SerializeArray) { 425 TEST_CASE(SerializeArray) {
(...skipping 15 matching lines...) Expand all
433 // Create a snapshot object using the buffer. 441 // Create a snapshot object using the buffer.
434 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 442 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
435 443
436 // Read object back from the snapshot. 444 // Read object back from the snapshot.
437 SnapshotReader reader(snapshot, Isolate::Current()); 445 SnapshotReader reader(snapshot, Isolate::Current());
438 Array& serialized_array = Array::Handle(); 446 Array& serialized_array = Array::Handle();
439 serialized_array ^= reader.ReadObject(); 447 serialized_array ^= reader.ReadObject();
440 EXPECT(array.Equals(serialized_array)); 448 EXPECT(array.Equals(serialized_array));
441 449
442 // Read object back from the snapshot into a C structure. 450 // Read object back from the snapshot into a C structure.
451 ApiNativeScope scope;
443 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 452 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
444 writer.BytesWritten(), 453 writer.BytesWritten(),
445 &zone_allocator); 454 &zone_allocator);
446 EXPECT_EQ(Dart_CObject::kArray, root->type); 455 EXPECT_EQ(Dart_CObject::kArray, root->type);
447 EXPECT_EQ(kArrayLength, root->value.as_array.length); 456 EXPECT_EQ(kArrayLength, root->value.as_array.length);
448 for (int i = 0; i < kArrayLength; i++) { 457 for (int i = 0; i < kArrayLength; i++) {
449 Dart_CObject* element = root->value.as_array.values[i]; 458 Dart_CObject* element = root->value.as_array.values[i];
450 EXPECT_EQ(Dart_CObject::kInt32, element->type); 459 EXPECT_EQ(Dart_CObject::kInt32, element->type);
451 EXPECT_EQ(i, element->value.as_int32); 460 EXPECT_EQ(i, element->value.as_int32);
452 } 461 }
(...skipping 15 matching lines...) Expand all
468 // Create a snapshot object using the buffer. 477 // Create a snapshot object using the buffer.
469 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); 478 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer);
470 479
471 // Read object back from the snapshot. 480 // Read object back from the snapshot.
472 SnapshotReader reader(snapshot, Isolate::Current()); 481 SnapshotReader reader(snapshot, Isolate::Current());
473 Array& serialized_array = Array::Handle(); 482 Array& serialized_array = Array::Handle();
474 serialized_array ^= reader.ReadObject(); 483 serialized_array ^= reader.ReadObject();
475 EXPECT(array.Equals(serialized_array)); 484 EXPECT(array.Equals(serialized_array));
476 485
477 // Read object back from the snapshot into a C structure. 486 // Read object back from the snapshot into a C structure.
487 ApiNativeScope scope;
478 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 488 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
479 writer.BytesWritten(), 489 writer.BytesWritten(),
480 &zone_allocator); 490 &zone_allocator);
481 EXPECT_EQ(Dart_CObject::kArray, root->type); 491 EXPECT_EQ(Dart_CObject::kArray, root->type);
482 EXPECT_EQ(kArrayLength, root->value.as_array.length); 492 EXPECT_EQ(kArrayLength, root->value.as_array.length);
483 EXPECT(root->value.as_array.values == NULL); 493 EXPECT(root->value.as_array.values == NULL);
484 CheckEncodeDecodeMessage(root); 494 CheckEncodeDecodeMessage(root);
485 } 495 }
486 496
487 497
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 Zone zone(Isolate::Current()); 806 Zone zone(Isolate::Current());
797 uint8_t* buffer = NULL; 807 uint8_t* buffer = NULL;
798 MessageWriter writer(&buffer, &zone_allocator); 808 MessageWriter writer(&buffer, &zone_allocator);
799 809
800 static const int kArrayLength = 2; 810 static const int kArrayLength = 2;
801 intptr_t data[kArrayLength] = {1, 2}; 811 intptr_t data[kArrayLength] = {1, 2};
802 int len = kArrayLength; 812 int len = kArrayLength;
803 writer.WriteMessage(len, data); 813 writer.WriteMessage(len, data);
804 814
805 // Read object back from the snapshot into a C structure. 815 // Read object back from the snapshot into a C structure.
816 ApiNativeScope scope;
806 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 817 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
807 writer.BytesWritten(), 818 writer.BytesWritten(),
808 &zone_allocator); 819 &zone_allocator);
809 EXPECT_EQ(Dart_CObject::kArray, root->type); 820 EXPECT_EQ(Dart_CObject::kArray, root->type);
810 EXPECT_EQ(kArrayLength, root->value.as_array.length); 821 EXPECT_EQ(kArrayLength, root->value.as_array.length);
811 for (int i = 0; i < kArrayLength; i++) { 822 for (int i = 0; i < kArrayLength; i++) {
812 Dart_CObject* element = root->value.as_array.values[i]; 823 Dart_CObject* element = root->value.as_array.values[i];
813 EXPECT_EQ(Dart_CObject::kInt32, element->type); 824 EXPECT_EQ(Dart_CObject::kInt32, element->type);
814 EXPECT_EQ(i + 1, element->value.as_int32); 825 EXPECT_EQ(i + 1, element->value.as_int32);
815 } 826 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
885 { 896 {
886 Zone zone(Isolate::Current()); 897 Zone zone(Isolate::Current());
887 uint8_t* buffer; 898 uint8_t* buffer;
888 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 899 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
889 Smi& smi = Smi::Handle(); 900 Smi& smi = Smi::Handle();
890 smi ^= Api::UnwrapHandle(smi_result); 901 smi ^= Api::UnwrapHandle(smi_result);
891 writer.WriteObject(smi.raw()); 902 writer.WriteObject(smi.raw());
892 writer.FinalizeBuffer(); 903 writer.FinalizeBuffer();
893 904
894 // Read object back from the snapshot into a C structure. 905 // Read object back from the snapshot into a C structure.
906 ApiNativeScope scope;
895 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 907 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
896 writer.BytesWritten(), 908 writer.BytesWritten(),
897 &zone_allocator); 909 &zone_allocator);
898 EXPECT_NOTNULL(root); 910 EXPECT_NOTNULL(root);
899 EXPECT_EQ(Dart_CObject::kInt32, root->type); 911 EXPECT_EQ(Dart_CObject::kInt32, root->type);
900 EXPECT_EQ(42, root->value.as_int32); 912 EXPECT_EQ(42, root->value.as_int32);
901 CheckEncodeDecodeMessage(root); 913 CheckEncodeDecodeMessage(root);
902 } 914 }
903 { 915 {
904 Zone zone(Isolate::Current()); 916 Zone zone(Isolate::Current());
905 uint8_t* buffer; 917 uint8_t* buffer;
906 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); 918 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator);
907 String& str = String::Handle(); 919 String& str = String::Handle();
908 str ^= Api::UnwrapHandle(string_result); 920 str ^= Api::UnwrapHandle(string_result);
909 writer.WriteObject(str.raw()); 921 writer.WriteObject(str.raw());
910 writer.FinalizeBuffer(); 922 writer.FinalizeBuffer();
911 923
912 // Read object back from the snapshot into a C structure. 924 // Read object back from the snapshot into a C structure.
925 ApiNativeScope scope;
913 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, 926 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize,
914 writer.BytesWritten(), 927 writer.BytesWritten(),
915 &zone_allocator); 928 &zone_allocator);
916 EXPECT_NOTNULL(root); 929 EXPECT_NOTNULL(root);
917 EXPECT_EQ(Dart_CObject::kString, root->type); 930 EXPECT_EQ(Dart_CObject::kString, root->type);
918 EXPECT_STREQ("Hello, world!", root->value.as_string); 931 EXPECT_STREQ("Hello, world!", root->value.as_string);
919 CheckEncodeDecodeMessage(root); 932 CheckEncodeDecodeMessage(root);
920 } 933 }
921 } 934 }
922 Dart_ExitScope(); 935 Dart_ExitScope();
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
955 EXPECT(isolate != NULL); 968 EXPECT(isolate != NULL);
956 Dart_EnterScope(); 969 Dart_EnterScope();
957 970
958 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 971 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
959 EXPECT_VALID(lib); 972 EXPECT_VALID(lib);
960 973
961 { 974 {
962 DARTSCOPE_NOCHECKS(isolate); 975 DARTSCOPE_NOCHECKS(isolate);
963 { 976 {
964 // Generate a list of nulls from Dart code. 977 // Generate a list of nulls from Dart code.
965 Zone zone(Isolate::Current()); 978 ApiNativeScope scope;
966 Dart_CObject* root = GetDeserializedDartMessage(lib, "getList"); 979 Dart_CObject* root = GetDeserializedDartMessage(lib, "getList");
967 EXPECT_NOTNULL(root); 980 EXPECT_NOTNULL(root);
968 EXPECT_EQ(Dart_CObject::kArray, root->type); 981 EXPECT_EQ(Dart_CObject::kArray, root->type);
969 EXPECT_EQ(kArrayLength, root->value.as_array.length); 982 EXPECT_EQ(kArrayLength, root->value.as_array.length);
970 for (int i = 0; i < kArrayLength; i++) { 983 for (int i = 0; i < kArrayLength; i++) {
971 EXPECT_EQ(Dart_CObject::kNull, root->value.as_array.values[i]->type); 984 EXPECT_EQ(Dart_CObject::kNull, root->value.as_array.values[i]->type);
972 } 985 }
973 CheckEncodeDecodeMessage(root); 986 CheckEncodeDecodeMessage(root);
974 } 987 }
975 { 988 {
976 // Generate a list of ints from Dart code. 989 // Generate a list of ints from Dart code.
977 Zone zone(Isolate::Current()); 990 ApiNativeScope scope;
978 Dart_CObject* root = GetDeserializedDartMessage(lib, "getIntList"); 991 Dart_CObject* root = GetDeserializedDartMessage(lib, "getIntList");
979 EXPECT_NOTNULL(root); 992 EXPECT_NOTNULL(root);
980 EXPECT_EQ(Dart_CObject::kArray, root->type); 993 EXPECT_EQ(Dart_CObject::kArray, root->type);
981 EXPECT_EQ(kArrayLength, root->value.as_array.length); 994 EXPECT_EQ(kArrayLength, root->value.as_array.length);
982 for (int i = 0; i < kArrayLength; i++) { 995 for (int i = 0; i < kArrayLength; i++) {
983 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[i]->type); 996 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[i]->type);
984 EXPECT_EQ(i, root->value.as_array.values[i]->value.as_int32); 997 EXPECT_EQ(i, root->value.as_array.values[i]->value.as_int32);
985 } 998 }
986 CheckEncodeDecodeMessage(root); 999 CheckEncodeDecodeMessage(root);
987 } 1000 }
988 { 1001 {
989 // Generate a list of strings from Dart code. 1002 // Generate a list of strings from Dart code.
990 Zone zone(Isolate::Current()); 1003 ApiNativeScope scope;
991 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList"); 1004 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList");
992 EXPECT_NOTNULL(root); 1005 EXPECT_NOTNULL(root);
993 EXPECT_EQ(Dart_CObject::kArray, root->type); 1006 EXPECT_EQ(Dart_CObject::kArray, root->type);
994 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1007 EXPECT_EQ(kArrayLength, root->value.as_array.length);
995 for (int i = 0; i < kArrayLength; i++) { 1008 for (int i = 0; i < kArrayLength; i++) {
996 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[i]->type); 1009 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[i]->type);
997 char buffer[3]; 1010 char buffer[3];
998 snprintf(buffer, sizeof(buffer), "%d", i); 1011 snprintf(buffer, sizeof(buffer), "%d", i);
999 EXPECT_STREQ(buffer, root->value.as_array.values[i]->value.as_string); 1012 EXPECT_STREQ(buffer, root->value.as_array.values[i]->value.as_string);
1000 } 1013 }
1001 } 1014 }
1002 { 1015 {
1003 // Generate a list of objects of different types from Dart code. 1016 // Generate a list of objects of different types from Dart code.
1004 Zone zone(Isolate::Current()); 1017 ApiNativeScope scope;
1005 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList"); 1018 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
1006 EXPECT_NOTNULL(root); 1019 EXPECT_NOTNULL(root);
1007 EXPECT_EQ(Dart_CObject::kArray, root->type); 1020 EXPECT_EQ(Dart_CObject::kArray, root->type);
1008 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1021 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1009 1022
1010 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[0]->type); 1023 EXPECT_EQ(Dart_CObject::kInt32, root->value.as_array.values[0]->type);
1011 EXPECT_EQ(0, root->value.as_array.values[0]->value.as_int32); 1024 EXPECT_EQ(0, root->value.as_array.values[0]->value.as_int32);
1012 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[1]->type); 1025 EXPECT_EQ(Dart_CObject::kString, root->value.as_array.values[1]->type);
1013 EXPECT_STREQ("1", root->value.as_array.values[1]->value.as_string); 1026 EXPECT_STREQ("1", root->value.as_array.values[1]->value.as_string);
1014 EXPECT_EQ(Dart_CObject::kDouble, root->value.as_array.values[2]->type); 1027 EXPECT_EQ(Dart_CObject::kDouble, root->value.as_array.values[2]->type);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1065 Dart_EnterScope(); 1078 Dart_EnterScope();
1066 1079
1067 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); 1080 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL);
1068 EXPECT_VALID(lib); 1081 EXPECT_VALID(lib);
1069 1082
1070 { 1083 {
1071 DARTSCOPE_NOCHECKS(isolate); 1084 DARTSCOPE_NOCHECKS(isolate);
1072 1085
1073 { 1086 {
1074 // Generate a list of strings from Dart code. 1087 // Generate a list of strings from Dart code.
1075 Zone zone(Isolate::Current()); 1088 ApiNativeScope scope;
1076 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList"); 1089 Dart_CObject* root = GetDeserializedDartMessage(lib, "getStringList");
1077 EXPECT_NOTNULL(root); 1090 EXPECT_NOTNULL(root);
1078 EXPECT_EQ(Dart_CObject::kArray, root->type); 1091 EXPECT_EQ(Dart_CObject::kArray, root->type);
1079 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1092 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1080 for (int i = 0; i < kArrayLength; i++) { 1093 for (int i = 0; i < kArrayLength; i++) {
1081 Dart_CObject* element = root->value.as_array.values[i]; 1094 Dart_CObject* element = root->value.as_array.values[i];
1082 EXPECT_EQ(root->value.as_array.values[0], element); 1095 EXPECT_EQ(root->value.as_array.values[0], element);
1083 EXPECT_EQ(Dart_CObject::kString, element->type); 1096 EXPECT_EQ(Dart_CObject::kString, element->type);
1084 EXPECT_STREQ("Hello, world!", element->value.as_string); 1097 EXPECT_STREQ("Hello, world!", element->value.as_string);
1085 } 1098 }
1086 } 1099 }
1087 { 1100 {
1088 // Generate a list of doubles from Dart code. 1101 // Generate a list of doubles from Dart code.
1089 Zone zone(Isolate::Current()); 1102 ApiNativeScope scope;
1090 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList"); 1103 Dart_CObject* root = GetDeserializedDartMessage(lib, "getDoubleList");
1091 EXPECT_NOTNULL(root); 1104 EXPECT_NOTNULL(root);
1092 EXPECT_EQ(Dart_CObject::kArray, root->type); 1105 EXPECT_EQ(Dart_CObject::kArray, root->type);
1093 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1106 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1094 for (int i = 0; i < kArrayLength; i++) { 1107 for (int i = 0; i < kArrayLength; i++) {
1095 Dart_CObject* element = root->value.as_array.values[i]; 1108 Dart_CObject* element = root->value.as_array.values[i];
1096 EXPECT_EQ(root->value.as_array.values[0], element); 1109 EXPECT_EQ(root->value.as_array.values[0], element);
1097 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1110 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1098 EXPECT_EQ(3.14, element->value.as_double); 1111 EXPECT_EQ(3.14, element->value.as_double);
1099 } 1112 }
1100 } 1113 }
1101 { 1114 {
1102 // Generate a list of objects of different types from Dart code. 1115 // Generate a list of objects of different types from Dart code.
1103 Zone zone(Isolate::Current()); 1116 ApiNativeScope scope;
1104 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList"); 1117 Dart_CObject* root = GetDeserializedDartMessage(lib, "getMixedList");
1105 EXPECT_NOTNULL(root); 1118 EXPECT_NOTNULL(root);
1106 EXPECT_EQ(Dart_CObject::kArray, root->type); 1119 EXPECT_EQ(Dart_CObject::kArray, root->type);
1107 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1120 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1108 for (int i = 0; i < kArrayLength; i++) { 1121 for (int i = 0; i < kArrayLength; i++) {
1109 Dart_CObject* element = root->value.as_array.values[i]; 1122 Dart_CObject* element = root->value.as_array.values[i];
1110 if ((i % 2) == 0) { 1123 if ((i % 2) == 0) {
1111 EXPECT_EQ(root->value.as_array.values[0], element); 1124 EXPECT_EQ(root->value.as_array.values[0], element);
1112 EXPECT_EQ(Dart_CObject::kString, element->type); 1125 EXPECT_EQ(Dart_CObject::kString, element->type);
1113 EXPECT_STREQ("A", element->value.as_string); 1126 EXPECT_STREQ("A", element->value.as_string);
1114 } else { 1127 } else {
1115 EXPECT_EQ(root->value.as_array.values[1], element); 1128 EXPECT_EQ(root->value.as_array.values[1], element);
1116 EXPECT_EQ(Dart_CObject::kDouble, element->type); 1129 EXPECT_EQ(Dart_CObject::kDouble, element->type);
1117 EXPECT_STREQ(2.72, element->value.as_double); 1130 EXPECT_STREQ(2.72, element->value.as_double);
1118 } 1131 }
1119 } 1132 }
1120 } 1133 }
1121 { 1134 {
1122 // Generate a list of objects of different types from Dart code. 1135 // Generate a list of objects of different types from Dart code.
1123 Zone zone(Isolate::Current()); 1136 ApiNativeScope scope;
1124 Dart_CObject* root = GetDeserializedDartMessage(lib, "getSelfRefList"); 1137 Dart_CObject* root = GetDeserializedDartMessage(lib, "getSelfRefList");
1125 EXPECT_NOTNULL(root); 1138 EXPECT_NOTNULL(root);
1126 EXPECT_EQ(Dart_CObject::kArray, root->type); 1139 EXPECT_EQ(Dart_CObject::kArray, root->type);
1127 EXPECT_EQ(kArrayLength, root->value.as_array.length); 1140 EXPECT_EQ(kArrayLength, root->value.as_array.length);
1128 for (int i = 0; i < kArrayLength; i++) { 1141 for (int i = 0; i < kArrayLength; i++) {
1129 Dart_CObject* element = root->value.as_array.values[i]; 1142 Dart_CObject* element = root->value.as_array.values[i];
1130 EXPECT_EQ(Dart_CObject::kArray, element->type); 1143 EXPECT_EQ(Dart_CObject::kArray, element->type);
1131 EXPECT_EQ(root, element); 1144 EXPECT_EQ(root, element);
1132 } 1145 }
1133 } 1146 }
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 EXPECT(Dart_ErrorHasException(result)); 1218 EXPECT(Dart_ErrorHasException(result));
1206 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", 1219 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n",
1207 Dart_GetError(result)); 1220 Dart_GetError(result));
1208 1221
1209 Dart_ExitScope(); 1222 Dart_ExitScope();
1210 } 1223 }
1211 1224
1212 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). 1225 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64).
1213 1226
1214 } // namespace dart 1227 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/snapshot.cc ('k') | runtime/vm/vm_sources.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698