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_message.h" | 10 #include "vm/dart_api_message.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 if (expected.IsDouble()) { | 29 if (expected.IsDouble()) { |
30 if (actual.IsDouble()) { | 30 if (actual.IsDouble()) { |
31 Double& dbl1 = Double::Handle(); | 31 Double& dbl1 = Double::Handle(); |
32 Double& dbl2 = Double::Handle(); | 32 Double& dbl2 = Double::Handle(); |
33 dbl1 ^= expected.raw(); | 33 dbl1 ^= expected.raw(); |
34 dbl2 ^= actual.raw(); | 34 dbl2 ^= actual.raw(); |
35 return dbl1.value() == dbl2.value(); | 35 return dbl1.value() == dbl2.value(); |
36 } | 36 } |
37 return false; | 37 return false; |
38 } | 38 } |
| 39 if (expected.IsBool()) { |
| 40 if (actual.IsBool()) { |
| 41 Bool& bl1 = Bool::Handle(); |
| 42 Bool& bl2 = Bool::Handle(); |
| 43 bl1 ^= expected.raw(); |
| 44 bl2 ^= actual.raw(); |
| 45 return bl1.value() == bl2.value(); |
| 46 } |
| 47 return false; |
| 48 } |
39 return false; | 49 return false; |
40 } | 50 } |
41 | 51 |
42 | 52 |
43 static uint8_t* malloc_allocator( | 53 static uint8_t* malloc_allocator( |
44 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 54 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
45 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); | 55 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); |
46 } | 56 } |
47 | 57 |
48 | 58 |
49 static uint8_t* zone_allocator( | 59 static uint8_t* zone_allocator( |
50 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 60 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
51 Zone* zone = Isolate::Current()->current_zone(); | 61 Zone* zone = Isolate::Current()->current_zone(); |
52 return zone->Realloc<uint8_t>(ptr, old_size, new_size); | 62 return zone->Realloc<uint8_t>(ptr, old_size, new_size); |
53 } | 63 } |
54 | 64 |
55 | 65 |
56 static Dart_CObject* DecodeMessage(uint8_t* message, | |
57 intptr_t length, | |
58 ReAlloc allocator) { | |
59 ApiMessageReader message_reader(message, length, allocator); | |
60 return message_reader.ReadMessage(); | |
61 } | |
62 | |
63 | |
64 // Compare two Dart_CObject object graphs rooted in first and | 66 // Compare two Dart_CObject object graphs rooted in first and |
65 // second. The second graph will be destroyed by this operation no matter | 67 // second. The second graph will be destroyed by this operation no matter |
66 // whether the graphs are equal or not. | 68 // whether the graphs are equal or not. |
67 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { | 69 static void CompareDartCObjects(Dart_CObject* first, Dart_CObject* second) { |
68 // Return immediately if entering a cycle. | 70 // Return immediately if entering a cycle. |
69 if (second->type == Dart_CObject::kNumberOfTypes) return; | 71 if (second->type == Dart_CObject::kNumberOfTypes) return; |
70 | 72 |
71 EXPECT_NE(first, second); | 73 EXPECT_NE(first, second); |
72 EXPECT_EQ(first->type, second->type); | 74 EXPECT_EQ(first->type, second->type); |
73 switch (first->type) { | 75 switch (first->type) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 117 } |
116 } | 118 } |
117 | 119 |
118 | 120 |
119 static void CheckEncodeDecodeMessage(Dart_CObject* root) { | 121 static void CheckEncodeDecodeMessage(Dart_CObject* root) { |
120 // Encode and decode the message. | 122 // Encode and decode the message. |
121 uint8_t* buffer = NULL; | 123 uint8_t* buffer = NULL; |
122 ApiMessageWriter writer(&buffer, &malloc_allocator); | 124 ApiMessageWriter writer(&buffer, &malloc_allocator); |
123 writer.WriteCMessage(root); | 125 writer.WriteCMessage(root); |
124 | 126 |
125 Dart_CObject* new_root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 127 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator); |
126 writer.BytesWritten(), | 128 Dart_CObject* new_root = api_reader.ReadMessage(); |
127 &zone_allocator); | |
128 | 129 |
129 // Check that the two messages are the same. | 130 // Check that the two messages are the same. |
130 CompareDartCObjects(root, new_root); | 131 CompareDartCObjects(root, new_root); |
131 } | 132 } |
132 | 133 |
133 TEST_CASE(SerializeNull) { | 134 TEST_CASE(SerializeNull) { |
134 Zone zone(Isolate::Current()); | 135 Zone zone(Isolate::Current()); |
135 | 136 |
136 // Write snapshot with object content. | 137 // Write snapshot with object content. |
137 uint8_t* buffer; | 138 uint8_t* buffer; |
138 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 139 MessageWriter writer(&buffer, &zone_allocator); |
139 const Object& null_object = Object::Handle(); | 140 const Object& null_object = Object::Handle(); |
140 writer.WriteObject(null_object.raw()); | 141 writer.WriteMessage(null_object); |
141 writer.FinalizeBuffer(); | 142 intptr_t buffer_len = writer.BytesWritten(); |
142 | |
143 // Create a snapshot object using the buffer. | |
144 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
145 | 143 |
146 // Read object back from the snapshot. | 144 // Read object back from the snapshot. |
147 SnapshotReader reader(snapshot, Isolate::Current()); | 145 SnapshotReader reader(buffer, buffer_len, |
| 146 Snapshot::kMessage, Isolate::Current()); |
148 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 147 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
149 EXPECT(Equals(null_object, serialized_object)); | 148 EXPECT(Equals(null_object, serialized_object)); |
150 | 149 |
151 // Read object back from the snapshot into a C structure. | 150 // Read object back from the snapshot into a C structure. |
152 ApiNativeScope scope; | 151 ApiNativeScope scope; |
153 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 152 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
154 writer.BytesWritten(), | 153 Dart_CObject* root = api_reader.ReadMessage(); |
155 &zone_allocator); | |
156 EXPECT_NOTNULL(root); | 154 EXPECT_NOTNULL(root); |
157 EXPECT_EQ(Dart_CObject::kNull, root->type); | 155 EXPECT_EQ(Dart_CObject::kNull, root->type); |
158 CheckEncodeDecodeMessage(root); | 156 CheckEncodeDecodeMessage(root); |
159 } | 157 } |
160 | 158 |
161 | 159 |
162 TEST_CASE(SerializeSmi1) { | 160 TEST_CASE(SerializeSmi1) { |
163 Zone zone(Isolate::Current()); | 161 Zone zone(Isolate::Current()); |
164 | 162 |
165 // Write snapshot with object content. | 163 // Write snapshot with object content. |
166 uint8_t* buffer; | 164 uint8_t* buffer; |
167 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 165 MessageWriter writer(&buffer, &zone_allocator); |
168 const Smi& smi = Smi::Handle(Smi::New(124)); | 166 const Smi& smi = Smi::Handle(Smi::New(124)); |
169 writer.WriteObject(smi.raw()); | 167 writer.WriteMessage(smi); |
170 writer.FinalizeBuffer(); | 168 intptr_t buffer_len = writer.BytesWritten(); |
171 | |
172 // Create a snapshot object using the buffer. | |
173 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
174 | 169 |
175 // Read object back from the snapshot. | 170 // Read object back from the snapshot. |
176 SnapshotReader reader(snapshot, Isolate::Current()); | 171 SnapshotReader reader(buffer, buffer_len, |
| 172 Snapshot::kMessage, Isolate::Current()); |
177 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 173 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
178 EXPECT(Equals(smi, serialized_object)); | 174 EXPECT(Equals(smi, serialized_object)); |
179 | 175 |
180 // Read object back from the snapshot into a C structure. | 176 // Read object back from the snapshot into a C structure. |
181 ApiNativeScope scope; | 177 ApiNativeScope scope; |
182 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 178 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
183 writer.BytesWritten(), | 179 Dart_CObject* root = api_reader.ReadMessage(); |
184 &zone_allocator); | |
185 EXPECT_NOTNULL(root); | 180 EXPECT_NOTNULL(root); |
186 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 181 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
187 EXPECT_EQ(smi.Value(), root->value.as_int32); | 182 EXPECT_EQ(smi.Value(), root->value.as_int32); |
188 CheckEncodeDecodeMessage(root); | 183 CheckEncodeDecodeMessage(root); |
189 } | 184 } |
190 | 185 |
191 | 186 |
192 TEST_CASE(SerializeSmi2) { | 187 TEST_CASE(SerializeSmi2) { |
193 Zone zone(Isolate::Current()); | 188 Zone zone(Isolate::Current()); |
194 | 189 |
195 // Write snapshot with object content. | 190 // Write snapshot with object content. |
196 uint8_t* buffer; | 191 uint8_t* buffer; |
197 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 192 MessageWriter writer(&buffer, &zone_allocator); |
198 const Smi& smi = Smi::Handle(Smi::New(-1)); | 193 const Smi& smi = Smi::Handle(Smi::New(-1)); |
199 writer.WriteObject(smi.raw()); | 194 writer.WriteMessage(smi); |
200 writer.FinalizeBuffer(); | 195 intptr_t buffer_len = writer.BytesWritten(); |
201 | |
202 // Create a snapshot object using the buffer. | |
203 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
204 | 196 |
205 // Read object back from the snapshot. | 197 // Read object back from the snapshot. |
206 SnapshotReader reader(snapshot, Isolate::Current()); | 198 SnapshotReader reader(buffer, buffer_len, |
| 199 Snapshot::kMessage, Isolate::Current()); |
207 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 200 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
208 EXPECT(Equals(smi, serialized_object)); | 201 EXPECT(Equals(smi, serialized_object)); |
209 | 202 |
210 // Read object back from the snapshot into a C structure. | 203 // Read object back from the snapshot into a C structure. |
211 ApiNativeScope scope; | 204 ApiNativeScope scope; |
212 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 205 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
213 writer.BytesWritten(), | 206 Dart_CObject* root = api_reader.ReadMessage(); |
214 &zone_allocator); | |
215 EXPECT_NOTNULL(root); | 207 EXPECT_NOTNULL(root); |
216 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 208 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
217 EXPECT_EQ(smi.Value(), root->value.as_int32); | 209 EXPECT_EQ(smi.Value(), root->value.as_int32); |
218 CheckEncodeDecodeMessage(root); | 210 CheckEncodeDecodeMessage(root); |
219 } | 211 } |
220 | 212 |
221 | 213 |
222 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { | 214 Dart_CObject* SerializeAndDeserializeMint(const Mint& mint) { |
223 // Write snapshot with object content. | 215 // Write snapshot with object content. |
224 uint8_t* buffer; | 216 uint8_t* buffer; |
225 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 217 MessageWriter writer(&buffer, &zone_allocator); |
226 writer.WriteObject(mint.raw()); | 218 writer.WriteMessage(mint); |
227 writer.FinalizeBuffer(); | 219 intptr_t buffer_len = writer.BytesWritten(); |
228 | |
229 // Create a snapshot object using the buffer. | |
230 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
231 | 220 |
232 // Read object back from the snapshot. | 221 // Read object back from the snapshot. |
233 SnapshotReader reader(snapshot, Isolate::Current()); | 222 SnapshotReader reader(buffer, buffer_len, |
| 223 Snapshot::kMessage, Isolate::Current()); |
234 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 224 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
235 EXPECT(serialized_object.IsMint()); | 225 EXPECT(serialized_object.IsMint()); |
236 | 226 |
237 // Read object back from the snapshot into a C structure. | 227 // Read object back from the snapshot into a C structure. |
238 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 228 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
239 writer.BytesWritten(), | 229 Dart_CObject* root = api_reader.ReadMessage(); |
240 &zone_allocator); | |
241 EXPECT_NOTNULL(root); | 230 EXPECT_NOTNULL(root); |
242 CheckEncodeDecodeMessage(root); | 231 CheckEncodeDecodeMessage(root); |
243 return root; | 232 return root; |
244 } | 233 } |
245 | 234 |
246 | 235 |
247 void CheckMint(int64_t value) { | 236 void CheckMint(int64_t value) { |
248 Zone zone(Isolate::Current()); | 237 Zone zone(Isolate::Current()); |
249 | 238 |
250 const Mint& mint = Mint::Handle(Mint::New(value)); | 239 const Mint& mint = Mint::Handle(Mint::New(value)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 // Min negative mint + 1. | 276 // Min negative mint + 1. |
288 CheckMint(kMinInt64 + 1); | 277 CheckMint(kMinInt64 + 1); |
289 } | 278 } |
290 | 279 |
291 | 280 |
292 TEST_CASE(SerializeDouble) { | 281 TEST_CASE(SerializeDouble) { |
293 Zone zone(Isolate::Current()); | 282 Zone zone(Isolate::Current()); |
294 | 283 |
295 // Write snapshot with object content. | 284 // Write snapshot with object content. |
296 uint8_t* buffer; | 285 uint8_t* buffer; |
297 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 286 MessageWriter writer(&buffer, &zone_allocator); |
298 const Double& dbl = Double::Handle(Double::New(101.29)); | 287 const Double& dbl = Double::Handle(Double::New(101.29)); |
299 writer.WriteObject(dbl.raw()); | 288 writer.WriteMessage(dbl); |
300 writer.FinalizeBuffer(); | 289 intptr_t buffer_len = writer.BytesWritten(); |
301 | |
302 // Create a snapshot object using the buffer. | |
303 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
304 | 290 |
305 // Read object back from the snapshot. | 291 // Read object back from the snapshot. |
306 SnapshotReader reader(snapshot, Isolate::Current()); | 292 SnapshotReader reader(buffer, buffer_len, |
| 293 Snapshot::kMessage, Isolate::Current()); |
307 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 294 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
308 EXPECT(Equals(dbl, serialized_object)); | 295 EXPECT(Equals(dbl, serialized_object)); |
309 | 296 |
310 // Read object back from the snapshot into a C structure. | 297 // Read object back from the snapshot into a C structure. |
311 ApiNativeScope scope; | 298 ApiNativeScope scope; |
312 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 299 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
313 writer.BytesWritten(), | 300 Dart_CObject* root = api_reader.ReadMessage(); |
314 &zone_allocator); | |
315 EXPECT_NOTNULL(root); | 301 EXPECT_NOTNULL(root); |
316 EXPECT_EQ(Dart_CObject::kDouble, root->type); | 302 EXPECT_EQ(Dart_CObject::kDouble, root->type); |
317 EXPECT_EQ(dbl.value(), root->value.as_double); | 303 EXPECT_EQ(dbl.value(), root->value.as_double); |
318 CheckEncodeDecodeMessage(root); | 304 CheckEncodeDecodeMessage(root); |
319 } | 305 } |
320 | 306 |
321 | 307 |
322 TEST_CASE(SerializeBool) { | |
323 Zone zone(Isolate::Current()); | |
324 | |
325 // Write snapshot with object content. | |
326 uint8_t* buffer; | |
327 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | |
328 const Bool& bool1 = Bool::Handle(Bool::True()); | |
329 const Bool& bool2 = Bool::Handle(Bool::False()); | |
330 writer.WriteObject(bool1.raw()); | |
331 writer.WriteObject(bool2.raw()); | |
332 writer.FinalizeBuffer(); | |
333 | |
334 // Create a snapshot object using the buffer. | |
335 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
336 | |
337 // Read object back from the snapshot. | |
338 SnapshotReader reader(snapshot, Isolate::Current()); | |
339 EXPECT(Bool::True() == reader.ReadObject()); | |
340 EXPECT(Bool::False() == reader.ReadObject()); | |
341 } | |
342 | |
343 | |
344 TEST_CASE(SerializeTrue) { | 308 TEST_CASE(SerializeTrue) { |
345 Zone zone(Isolate::Current()); | 309 Zone zone(Isolate::Current()); |
346 | 310 |
347 // Write snapshot with true object. | 311 // Write snapshot with true object. |
348 uint8_t* buffer; | 312 uint8_t* buffer; |
349 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 313 MessageWriter writer(&buffer, &zone_allocator); |
350 const Bool& bl = Bool::Handle(Bool::True()); | 314 const Bool& bl = Bool::Handle(Bool::True()); |
351 writer.WriteObject(bl.raw()); | 315 writer.WriteMessage(bl); |
352 writer.FinalizeBuffer(); | 316 intptr_t buffer_len = writer.BytesWritten(); |
353 | 317 |
354 // Create a snapshot object using the buffer. | 318 // Read object back from the snapshot. |
355 Snapshot::SetupFromBuffer(buffer); | 319 SnapshotReader reader(buffer, buffer_len, |
| 320 Snapshot::kMessage, Isolate::Current()); |
| 321 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 322 fprintf(stderr, "%s / %s\n", bl.ToCString(), serialized_object.ToCString()); |
| 323 |
| 324 EXPECT(Equals(bl, serialized_object)); |
356 | 325 |
357 // Read object back from the snapshot into a C structure. | 326 // Read object back from the snapshot into a C structure. |
358 ApiNativeScope scope; | 327 ApiNativeScope scope; |
359 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 328 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
360 writer.BytesWritten(), | 329 Dart_CObject* root = api_reader.ReadMessage(); |
361 &zone_allocator); | |
362 EXPECT_NOTNULL(root); | 330 EXPECT_NOTNULL(root); |
363 EXPECT_EQ(Dart_CObject::kBool, root->type); | 331 EXPECT_EQ(Dart_CObject::kBool, root->type); |
364 EXPECT_EQ(true, root->value.as_bool); | 332 EXPECT_EQ(true, root->value.as_bool); |
365 CheckEncodeDecodeMessage(root); | 333 CheckEncodeDecodeMessage(root); |
366 } | 334 } |
367 | 335 |
368 | 336 |
369 TEST_CASE(SerializeFalse) { | 337 TEST_CASE(SerializeFalse) { |
370 Zone zone(Isolate::Current()); | 338 Zone zone(Isolate::Current()); |
371 | 339 |
372 // Write snapshot with false object. | 340 // Write snapshot with false object. |
373 uint8_t* buffer; | 341 uint8_t* buffer; |
374 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 342 MessageWriter writer(&buffer, &zone_allocator); |
375 const Bool& bl = Bool::Handle(Bool::False()); | 343 const Bool& bl = Bool::Handle(Bool::False()); |
376 writer.WriteObject(bl.raw()); | 344 writer.WriteMessage(bl); |
377 writer.FinalizeBuffer(); | 345 intptr_t buffer_len = writer.BytesWritten(); |
378 | 346 |
379 // Create a snapshot object using the buffer. | 347 // Read object back from the snapshot. |
380 Snapshot::SetupFromBuffer(buffer); | 348 SnapshotReader reader(buffer, buffer_len, |
| 349 Snapshot::kMessage, Isolate::Current()); |
| 350 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 351 EXPECT(Equals(bl, serialized_object)); |
381 | 352 |
382 // Read object back from the snapshot into a C structure. | 353 // Read object back from the snapshot into a C structure. |
383 ApiNativeScope scope; | 354 ApiNativeScope scope; |
384 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 355 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
385 writer.BytesWritten(), | 356 Dart_CObject* root = api_reader.ReadMessage(); |
386 &zone_allocator); | |
387 EXPECT_NOTNULL(root); | 357 EXPECT_NOTNULL(root); |
388 EXPECT_EQ(Dart_CObject::kBool, root->type); | 358 EXPECT_EQ(Dart_CObject::kBool, root->type); |
389 EXPECT_EQ(false, root->value.as_bool); | 359 EXPECT_EQ(false, root->value.as_bool); |
390 CheckEncodeDecodeMessage(root); | 360 CheckEncodeDecodeMessage(root); |
391 } | 361 } |
392 | 362 |
393 | 363 |
394 static uword allocator(intptr_t size) { | 364 static uword allocator(intptr_t size) { |
395 return reinterpret_cast<uword>(malloc(size)); | 365 return reinterpret_cast<uword>(malloc(size)); |
396 } | 366 } |
397 | 367 |
398 | 368 |
399 TEST_CASE(SerializeBigint) { | 369 TEST_CASE(SerializeBigint) { |
400 Zone zone(Isolate::Current()); | 370 Zone zone(Isolate::Current()); |
401 | 371 |
402 // Write snapshot with object content. | 372 // Write snapshot with object content. |
403 uint8_t* buffer; | 373 uint8_t* buffer; |
404 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 374 MessageWriter writer(&buffer, &zone_allocator); |
405 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); | 375 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); |
406 writer.WriteObject(bigint.raw()); | 376 writer.WriteMessage(bigint); |
407 writer.FinalizeBuffer(); | 377 intptr_t buffer_len = writer.BytesWritten(); |
408 | |
409 // Create a snapshot object using the buffer. | |
410 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
411 | 378 |
412 // Read object back from the snapshot. | 379 // Read object back from the snapshot. |
413 SnapshotReader reader(snapshot, Isolate::Current()); | 380 SnapshotReader reader(buffer, buffer_len, |
| 381 Snapshot::kMessage, Isolate::Current()); |
414 Bigint& obj = Bigint::Handle(); | 382 Bigint& obj = Bigint::Handle(); |
415 obj ^= reader.ReadObject(); | 383 obj ^= reader.ReadObject(); |
416 | 384 |
417 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj)); | 385 EXPECT_EQ(BigintOperations::ToMint(bigint), BigintOperations::ToMint(obj)); |
418 | 386 |
419 // Read object back from the snapshot into a C structure. | 387 // Read object back from the snapshot into a C structure. |
420 ApiNativeScope scope; | 388 ApiNativeScope scope; |
421 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 389 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
422 writer.BytesWritten(), | 390 Dart_CObject* root = api_reader.ReadMessage(); |
423 &zone_allocator); | |
424 // Bigint not supported. | 391 // Bigint not supported. |
425 EXPECT_NOTNULL(root); | 392 EXPECT_NOTNULL(root); |
426 EXPECT_EQ(Dart_CObject::kBigint, root->type); | 393 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
427 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint); | 394 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint); |
428 CheckEncodeDecodeMessage(root); | 395 CheckEncodeDecodeMessage(root); |
429 } | 396 } |
430 | 397 |
431 | 398 |
432 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { | 399 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { |
433 // Write snapshot with object content. | 400 // Write snapshot with object content. |
434 uint8_t* buffer; | 401 uint8_t* buffer; |
435 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 402 MessageWriter writer(&buffer, &zone_allocator); |
436 writer.WriteObject(bigint.raw()); | 403 writer.WriteMessage(bigint); |
437 writer.FinalizeBuffer(); | 404 intptr_t buffer_len = writer.BytesWritten(); |
438 | |
439 // Create a snapshot object using the buffer. | |
440 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
441 | 405 |
442 // Read object back from the snapshot. | 406 // Read object back from the snapshot. |
443 SnapshotReader reader(snapshot, Isolate::Current()); | 407 SnapshotReader reader(buffer, buffer_len, |
| 408 Snapshot::kMessage, Isolate::Current()); |
444 Bigint& serialized_bigint = Bigint::Handle(); | 409 Bigint& serialized_bigint = Bigint::Handle(); |
445 serialized_bigint ^= reader.ReadObject(); | 410 serialized_bigint ^= reader.ReadObject(); |
446 const char *str1 = BigintOperations::ToHexCString(bigint, allocator); | 411 const char *str1 = BigintOperations::ToHexCString(bigint, allocator); |
447 const char *str2 = | 412 const char *str2 = |
448 BigintOperations::ToHexCString(serialized_bigint, allocator); | 413 BigintOperations::ToHexCString(serialized_bigint, allocator); |
449 EXPECT_STREQ(str1, str2); | 414 EXPECT_STREQ(str1, str2); |
450 free(const_cast<char*>(str1)); | 415 free(const_cast<char*>(str1)); |
451 free(const_cast<char*>(str2)); | 416 free(const_cast<char*>(str2)); |
452 | 417 |
453 // Read object back from the snapshot into a C structure. | 418 // Read object back from the snapshot into a C structure. |
454 ApiNativeScope scope; | 419 ApiNativeScope scope; |
455 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 420 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
456 writer.BytesWritten(), | 421 Dart_CObject* root = api_reader.ReadMessage(); |
457 &zone_allocator); | |
458 // Bigint not supported. | 422 // Bigint not supported. |
459 EXPECT_NOTNULL(root); | 423 EXPECT_NOTNULL(root); |
460 CheckEncodeDecodeMessage(root); | 424 CheckEncodeDecodeMessage(root); |
461 return root; | 425 return root; |
462 } | 426 } |
463 | 427 |
464 | 428 |
465 void CheckBigint(const char* bigint_value) { | 429 void CheckBigint(const char* bigint_value) { |
466 Zone zone(Isolate::Current()); | 430 Zone zone(Isolate::Current()); |
467 | 431 |
(...skipping 14 matching lines...) Expand all Loading... |
482 TEST_CASE(SerializeBigint2) { | 446 TEST_CASE(SerializeBigint2) { |
483 CheckBigint("0x0"); | 447 CheckBigint("0x0"); |
484 CheckBigint("0x1"); | 448 CheckBigint("0x1"); |
485 CheckBigint("-0x1"); | 449 CheckBigint("-0x1"); |
486 CheckBigint("0x11111111111111111111"); | 450 CheckBigint("0x11111111111111111111"); |
487 CheckBigint("-0x11111111111111111111"); | 451 CheckBigint("-0x11111111111111111111"); |
488 CheckBigint("0x9876543210987654321098765432109876543210"); | 452 CheckBigint("0x9876543210987654321098765432109876543210"); |
489 CheckBigint("-0x9876543210987654321098765432109876543210"); | 453 CheckBigint("-0x9876543210987654321098765432109876543210"); |
490 } | 454 } |
491 | 455 |
492 | |
493 TEST_CASE(SerializeSingletons) { | 456 TEST_CASE(SerializeSingletons) { |
494 // Write snapshot with object content. | 457 // Write snapshot with object content. |
495 uint8_t* buffer; | 458 uint8_t* buffer; |
496 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); | 459 MessageWriter writer(&buffer, &malloc_allocator); |
497 writer.WriteObject(Object::class_class()); | 460 writer.WriteObject(Object::class_class()); |
498 writer.WriteObject(Object::null_class()); | 461 writer.WriteObject(Object::null_class()); |
499 writer.WriteObject(Object::type_class()); | 462 writer.WriteObject(Object::type_class()); |
500 writer.WriteObject(Object::type_parameter_class()); | 463 writer.WriteObject(Object::type_parameter_class()); |
501 writer.WriteObject(Object::type_arguments_class()); | 464 writer.WriteObject(Object::type_arguments_class()); |
502 writer.WriteObject(Object::instantiated_type_arguments_class()); | 465 writer.WriteObject(Object::instantiated_type_arguments_class()); |
503 writer.WriteObject(Object::function_class()); | 466 writer.WriteObject(Object::function_class()); |
504 writer.WriteObject(Object::field_class()); | 467 writer.WriteObject(Object::field_class()); |
505 writer.WriteObject(Object::token_stream_class()); | 468 writer.WriteObject(Object::token_stream_class()); |
506 writer.WriteObject(Object::script_class()); | 469 writer.WriteObject(Object::script_class()); |
507 writer.WriteObject(Object::library_class()); | 470 writer.WriteObject(Object::library_class()); |
508 writer.WriteObject(Object::code_class()); | 471 writer.WriteObject(Object::code_class()); |
509 writer.WriteObject(Object::instructions_class()); | 472 writer.WriteObject(Object::instructions_class()); |
510 writer.WriteObject(Object::pc_descriptors_class()); | 473 writer.WriteObject(Object::pc_descriptors_class()); |
511 writer.WriteObject(Object::exception_handlers_class()); | 474 writer.WriteObject(Object::exception_handlers_class()); |
512 writer.WriteObject(Object::context_class()); | 475 writer.WriteObject(Object::context_class()); |
513 writer.WriteObject(Object::context_scope_class()); | 476 writer.WriteObject(Object::context_scope_class()); |
514 writer.FinalizeBuffer(); | 477 intptr_t buffer_len = writer.BytesWritten(); |
515 | |
516 // Create a snapshot object using the buffer. | |
517 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
518 | 478 |
519 // Read object back from the snapshot. | 479 // Read object back from the snapshot. |
520 SnapshotReader reader(snapshot, Isolate::Current()); | 480 SnapshotReader reader(buffer, buffer_len, Snapshot::kMessage, |
| 481 Isolate::Current()); |
521 EXPECT(Object::class_class() == reader.ReadObject()); | 482 EXPECT(Object::class_class() == reader.ReadObject()); |
522 EXPECT(Object::null_class() == reader.ReadObject()); | 483 EXPECT(Object::null_class() == reader.ReadObject()); |
523 EXPECT(Object::type_class() == reader.ReadObject()); | 484 EXPECT(Object::type_class() == reader.ReadObject()); |
524 EXPECT(Object::type_parameter_class() == reader.ReadObject()); | 485 EXPECT(Object::type_parameter_class() == reader.ReadObject()); |
525 EXPECT(Object::type_arguments_class() == reader.ReadObject()); | 486 EXPECT(Object::type_arguments_class() == reader.ReadObject()); |
526 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject()); | 487 EXPECT(Object::instantiated_type_arguments_class() == reader.ReadObject()); |
527 EXPECT(Object::function_class() == reader.ReadObject()); | 488 EXPECT(Object::function_class() == reader.ReadObject()); |
528 EXPECT(Object::field_class() == reader.ReadObject()); | 489 EXPECT(Object::field_class() == reader.ReadObject()); |
529 EXPECT(Object::token_stream_class() == reader.ReadObject()); | 490 EXPECT(Object::token_stream_class() == reader.ReadObject()); |
530 EXPECT(Object::script_class() == reader.ReadObject()); | 491 EXPECT(Object::script_class() == reader.ReadObject()); |
531 EXPECT(Object::library_class() == reader.ReadObject()); | 492 EXPECT(Object::library_class() == reader.ReadObject()); |
532 EXPECT(Object::code_class() == reader.ReadObject()); | 493 EXPECT(Object::code_class() == reader.ReadObject()); |
533 EXPECT(Object::instructions_class() == reader.ReadObject()); | 494 EXPECT(Object::instructions_class() == reader.ReadObject()); |
534 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); | 495 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); |
535 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); | 496 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); |
536 EXPECT(Object::context_class() == reader.ReadObject()); | 497 EXPECT(Object::context_class() == reader.ReadObject()); |
537 EXPECT(Object::context_scope_class() == reader.ReadObject()); | 498 EXPECT(Object::context_scope_class() == reader.ReadObject()); |
538 | 499 |
539 free(buffer); | 500 free(buffer); |
540 } | 501 } |
541 | 502 |
542 | 503 |
543 TEST_CASE(SerializeString) { | 504 TEST_CASE(SerializeString) { |
544 Zone zone(Isolate::Current()); | 505 Zone zone(Isolate::Current()); |
545 | 506 |
546 // Write snapshot with object content. | 507 // Write snapshot with object content. |
547 uint8_t* buffer; | 508 uint8_t* buffer; |
548 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 509 MessageWriter writer(&buffer, &zone_allocator); |
549 static const char* cstr = "This string shall be serialized"; | 510 static const char* cstr = "This string shall be serialized"; |
550 String& str = String::Handle(String::New(cstr)); | 511 String& str = String::Handle(String::New(cstr)); |
551 writer.WriteObject(str.raw()); | 512 writer.WriteMessage(str); |
552 writer.FinalizeBuffer(); | 513 intptr_t buffer_len = writer.BytesWritten(); |
553 | |
554 // Create a snapshot object using the buffer. | |
555 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
556 | 514 |
557 // Read object back from the snapshot. | 515 // Read object back from the snapshot. |
558 SnapshotReader reader(snapshot, Isolate::Current()); | 516 SnapshotReader reader(buffer, buffer_len, |
| 517 Snapshot::kMessage, Isolate::Current()); |
559 String& serialized_str = String::Handle(); | 518 String& serialized_str = String::Handle(); |
560 serialized_str ^= reader.ReadObject(); | 519 serialized_str ^= reader.ReadObject(); |
561 EXPECT(str.Equals(serialized_str)); | 520 EXPECT(str.Equals(serialized_str)); |
562 | 521 |
563 // Read object back from the snapshot into a C structure. | 522 // Read object back from the snapshot into a C structure. |
564 ApiNativeScope scope; | 523 ApiNativeScope scope; |
565 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 524 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
566 writer.BytesWritten(), | 525 Dart_CObject* root = api_reader.ReadMessage(); |
567 &zone_allocator); | |
568 EXPECT_EQ(Dart_CObject::kString, root->type); | 526 EXPECT_EQ(Dart_CObject::kString, root->type); |
569 EXPECT_STREQ(cstr, root->value.as_string); | 527 EXPECT_STREQ(cstr, root->value.as_string); |
570 CheckEncodeDecodeMessage(root); | 528 CheckEncodeDecodeMessage(root); |
571 } | 529 } |
572 | 530 |
573 | 531 |
574 TEST_CASE(SerializeArray) { | 532 TEST_CASE(SerializeArray) { |
575 Zone zone(Isolate::Current()); | 533 Zone zone(Isolate::Current()); |
576 | 534 |
577 // Write snapshot with object content. | 535 // Write snapshot with object content. |
578 uint8_t* buffer; | 536 uint8_t* buffer; |
579 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 537 MessageWriter writer(&buffer, &zone_allocator); |
580 const int kArrayLength = 10; | 538 const int kArrayLength = 10; |
581 Array& array = Array::Handle(Array::New(kArrayLength)); | 539 Array& array = Array::Handle(Array::New(kArrayLength)); |
582 Smi& smi = Smi::Handle(); | 540 Smi& smi = Smi::Handle(); |
583 for (int i = 0; i < kArrayLength; i++) { | 541 for (int i = 0; i < kArrayLength; i++) { |
584 smi ^= Smi::New(i); | 542 smi ^= Smi::New(i); |
585 array.SetAt(i, smi); | 543 array.SetAt(i, smi); |
586 } | 544 } |
587 writer.WriteObject(array.raw()); | 545 writer.WriteMessage(array); |
588 writer.FinalizeBuffer(); | 546 intptr_t buffer_len = writer.BytesWritten(); |
589 | |
590 // Create a snapshot object using the buffer. | |
591 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
592 | 547 |
593 // Read object back from the snapshot. | 548 // Read object back from the snapshot. |
594 SnapshotReader reader(snapshot, Isolate::Current()); | 549 SnapshotReader reader(buffer, buffer_len, |
| 550 Snapshot::kMessage, Isolate::Current()); |
595 Array& serialized_array = Array::Handle(); | 551 Array& serialized_array = Array::Handle(); |
596 serialized_array ^= reader.ReadObject(); | 552 serialized_array ^= reader.ReadObject(); |
597 EXPECT(array.Equals(serialized_array)); | 553 EXPECT(array.Equals(serialized_array)); |
598 | 554 |
599 // Read object back from the snapshot into a C structure. | 555 // Read object back from the snapshot into a C structure. |
600 ApiNativeScope scope; | 556 ApiNativeScope scope; |
601 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 557 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
602 writer.BytesWritten(), | 558 Dart_CObject* root = api_reader.ReadMessage(); |
603 &zone_allocator); | |
604 EXPECT_EQ(Dart_CObject::kArray, root->type); | 559 EXPECT_EQ(Dart_CObject::kArray, root->type); |
605 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 560 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
606 for (int i = 0; i < kArrayLength; i++) { | 561 for (int i = 0; i < kArrayLength; i++) { |
607 Dart_CObject* element = root->value.as_array.values[i]; | 562 Dart_CObject* element = root->value.as_array.values[i]; |
608 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 563 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
609 EXPECT_EQ(i, element->value.as_int32); | 564 EXPECT_EQ(i, element->value.as_int32); |
610 } | 565 } |
611 CheckEncodeDecodeMessage(root); | 566 CheckEncodeDecodeMessage(root); |
612 } | 567 } |
613 | 568 |
614 | 569 |
615 TEST_CASE(SerializeEmptyArray) { | 570 TEST_CASE(SerializeEmptyArray) { |
616 Zone zone(Isolate::Current()); | 571 Zone zone(Isolate::Current()); |
617 | 572 |
618 // Write snapshot with object content. | 573 // Write snapshot with object content. |
619 uint8_t* buffer; | 574 uint8_t* buffer; |
620 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 575 MessageWriter writer(&buffer, &zone_allocator); |
621 const int kArrayLength = 0; | 576 const int kArrayLength = 0; |
622 Array& array = Array::Handle(Array::New(kArrayLength)); | 577 Array& array = Array::Handle(Array::New(kArrayLength)); |
623 writer.WriteObject(array.raw()); | 578 writer.WriteMessage(array); |
624 writer.FinalizeBuffer(); | 579 intptr_t buffer_len = writer.BytesWritten(); |
625 | |
626 // Create a snapshot object using the buffer. | |
627 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
628 | 580 |
629 // Read object back from the snapshot. | 581 // Read object back from the snapshot. |
630 SnapshotReader reader(snapshot, Isolate::Current()); | 582 SnapshotReader reader(buffer, buffer_len, |
| 583 Snapshot::kMessage, Isolate::Current()); |
631 Array& serialized_array = Array::Handle(); | 584 Array& serialized_array = Array::Handle(); |
632 serialized_array ^= reader.ReadObject(); | 585 serialized_array ^= reader.ReadObject(); |
633 EXPECT(array.Equals(serialized_array)); | 586 EXPECT(array.Equals(serialized_array)); |
634 | 587 |
635 // Read object back from the snapshot into a C structure. | 588 // Read object back from the snapshot into a C structure. |
636 ApiNativeScope scope; | 589 ApiNativeScope scope; |
637 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 590 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
638 writer.BytesWritten(), | 591 Dart_CObject* root = api_reader.ReadMessage(); |
639 &zone_allocator); | |
640 EXPECT_EQ(Dart_CObject::kArray, root->type); | 592 EXPECT_EQ(Dart_CObject::kArray, root->type); |
641 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 593 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
642 EXPECT(root->value.as_array.values == NULL); | 594 EXPECT(root->value.as_array.values == NULL); |
643 CheckEncodeDecodeMessage(root); | 595 CheckEncodeDecodeMessage(root); |
644 } | 596 } |
645 | 597 |
646 | 598 |
647 TEST_CASE(SerializeByteArray) { | 599 TEST_CASE(SerializeByteArray) { |
648 Zone zone(Isolate::Current()); | 600 Zone zone(Isolate::Current()); |
649 | 601 |
650 // Write snapshot with object content. | 602 // Write snapshot with object content. |
651 uint8_t* buffer; | 603 uint8_t* buffer; |
652 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 604 MessageWriter writer(&buffer, &zone_allocator); |
653 const int kByteArrayLength = 256; | 605 const int kByteArrayLength = 256; |
654 Uint8Array& byte_array = | 606 Uint8Array& byte_array = |
655 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); | 607 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); |
656 for (int i = 0; i < kByteArrayLength; i++) { | 608 for (int i = 0; i < kByteArrayLength; i++) { |
657 byte_array.SetAt(i, i); | 609 byte_array.SetAt(i, i); |
658 } | 610 } |
659 writer.WriteObject(byte_array.raw()); | 611 writer.WriteMessage(byte_array); |
660 writer.FinalizeBuffer(); | 612 intptr_t buffer_len = writer.BytesWritten(); |
661 | |
662 // Create a snapshot object using the buffer. | |
663 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
664 | 613 |
665 // Read object back from the snapshot. | 614 // Read object back from the snapshot. |
666 SnapshotReader reader(snapshot, Isolate::Current()); | 615 SnapshotReader reader(buffer, buffer_len, |
| 616 Snapshot::kMessage, Isolate::Current()); |
667 ByteArray& serialized_byte_array = ByteArray::Handle(); | 617 ByteArray& serialized_byte_array = ByteArray::Handle(); |
668 serialized_byte_array ^= reader.ReadObject(); | 618 serialized_byte_array ^= reader.ReadObject(); |
669 EXPECT(serialized_byte_array.IsByteArray()); | 619 EXPECT(serialized_byte_array.IsByteArray()); |
670 | 620 |
671 // Read object back from the snapshot into a C structure. | 621 // Read object back from the snapshot into a C structure. |
672 ApiNativeScope scope; | 622 ApiNativeScope scope; |
673 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 623 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
674 writer.BytesWritten(), | 624 Dart_CObject* root = api_reader.ReadMessage(); |
675 &zone_allocator); | |
676 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); | 625 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); |
677 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); | 626 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); |
678 for (int i = 0; i < kByteArrayLength; i++) { | 627 for (int i = 0; i < kByteArrayLength; i++) { |
679 EXPECT(root->value.as_byte_array.values[i] == i); | 628 EXPECT(root->value.as_byte_array.values[i] == i); |
680 } | 629 } |
681 CheckEncodeDecodeMessage(root); | 630 CheckEncodeDecodeMessage(root); |
682 } | 631 } |
683 | 632 |
684 | 633 |
685 TEST_CASE(SerializeEmptyByteArray) { | 634 TEST_CASE(SerializeEmptyByteArray) { |
686 Zone zone(Isolate::Current()); | 635 Zone zone(Isolate::Current()); |
687 | 636 |
688 // Write snapshot with object content. | 637 // Write snapshot with object content. |
689 uint8_t* buffer; | 638 uint8_t* buffer; |
690 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 639 MessageWriter writer(&buffer, &zone_allocator); |
691 const int kByteArrayLength = 0; | 640 const int kByteArrayLength = 0; |
692 Uint8Array& byte_array = | 641 Uint8Array& byte_array = |
693 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); | 642 Uint8Array::Handle(Uint8Array::New(kByteArrayLength)); |
694 writer.WriteObject(byte_array.raw()); | 643 writer.WriteMessage(byte_array); |
695 writer.FinalizeBuffer(); | 644 intptr_t buffer_len = writer.BytesWritten(); |
696 | |
697 // Create a snapshot object using the buffer. | |
698 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
699 | 645 |
700 // Read object back from the snapshot. | 646 // Read object back from the snapshot. |
701 SnapshotReader reader(snapshot, Isolate::Current()); | 647 SnapshotReader reader(buffer, buffer_len, |
| 648 Snapshot::kMessage, Isolate::Current()); |
702 ByteArray& serialized_byte_array = ByteArray::Handle(); | 649 ByteArray& serialized_byte_array = ByteArray::Handle(); |
703 serialized_byte_array ^= reader.ReadObject(); | 650 serialized_byte_array ^= reader.ReadObject(); |
704 EXPECT(serialized_byte_array.IsByteArray()); | 651 EXPECT(serialized_byte_array.IsByteArray()); |
705 | 652 |
706 // Read object back from the snapshot into a C structure. | 653 // Read object back from the snapshot into a C structure. |
707 ApiNativeScope scope; | 654 ApiNativeScope scope; |
708 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 655 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
709 writer.BytesWritten(), | 656 Dart_CObject* root = api_reader.ReadMessage(); |
710 &zone_allocator); | |
711 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); | 657 EXPECT_EQ(Dart_CObject::kUint8Array, root->type); |
712 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); | 658 EXPECT_EQ(kByteArrayLength, root->value.as_byte_array.length); |
713 EXPECT(root->value.as_byte_array.values == NULL); | 659 EXPECT(root->value.as_byte_array.values == NULL); |
714 CheckEncodeDecodeMessage(root); | 660 CheckEncodeDecodeMessage(root); |
715 } | 661 } |
716 | 662 |
717 | 663 |
| 664 class TestSnapshotWriter : public SnapshotWriter { |
| 665 public: |
| 666 TestSnapshotWriter(uint8_t** buffer, ReAlloc alloc) |
| 667 : SnapshotWriter(Snapshot::kScript, buffer, alloc) { |
| 668 ASSERT(buffer != NULL); |
| 669 ASSERT(alloc != NULL); |
| 670 } |
| 671 ~TestSnapshotWriter() { } |
| 672 |
| 673 // Writes just a script object |
| 674 void WriteScript(const Script& script) { |
| 675 WriteObject(script.raw()); |
| 676 UnmarkAll(); |
| 677 } |
| 678 |
| 679 private: |
| 680 DISALLOW_COPY_AND_ASSIGN(TestSnapshotWriter); |
| 681 }; |
| 682 |
| 683 |
718 TEST_CASE(SerializeScript) { | 684 TEST_CASE(SerializeScript) { |
719 const char* kScriptChars = | 685 const char* kScriptChars = |
720 "class A {\n" | 686 "class A {\n" |
721 " static bar() { return 42; }\n" | 687 " static bar() { return 42; }\n" |
722 " static fly() { return 5; }\n" | 688 " static fly() { return 5; }\n" |
723 " static s1() { return 'this is a string in the source'; }\n" | 689 " static s1() { return 'this is a string in the source'; }\n" |
724 " static s2() { return 'this is a \"string\" in the source'; }\n" | 690 " static s2() { return 'this is a \"string\" in the source'; }\n" |
725 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n" | 691 " static s3() { return 'this is a \\\'string\\\' in \"the\" source'; }\n" |
726 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n" | 692 " static s4() { return 'this \"is\" a \"string\" in \"the\" source'; }\n" |
727 "}\n"; | 693 "}\n"; |
728 | 694 |
729 String& url = String::Handle(String::New("dart-test:SerializeScript")); | 695 String& url = String::Handle(String::New("dart-test:SerializeScript")); |
730 String& source = String::Handle(String::New(kScriptChars)); | 696 String& source = String::Handle(String::New(kScriptChars)); |
731 Script& script = Script::Handle(Script::New(url, | 697 Script& script = Script::Handle(Script::New(url, |
732 source, | 698 source, |
733 RawScript::kSourceTag)); | 699 RawScript::kSourceTag)); |
734 const String& lib_url = String::Handle(Symbols::New("TestLib")); | 700 const String& lib_url = String::Handle(Symbols::New("TestLib")); |
735 Library& lib = Library::Handle(Library::New(lib_url)); | 701 Library& lib = Library::Handle(Library::New(lib_url)); |
736 lib.Register(); | 702 lib.Register(); |
737 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 703 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
738 | 704 |
739 // Write snapshot with object content. | 705 // Write snapshot with script content. |
740 uint8_t* buffer; | 706 uint8_t* buffer; |
741 SnapshotWriter writer(Snapshot::kScript, &buffer, &malloc_allocator); | 707 TestSnapshotWriter writer(&buffer, &malloc_allocator); |
742 writer.WriteObject(script.raw()); | 708 writer.WriteScript(script); |
743 writer.FinalizeBuffer(); | |
744 | |
745 // Create a snapshot object using the buffer. | |
746 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
747 | 709 |
748 // Read object back from the snapshot. | 710 // Read object back from the snapshot. |
749 SnapshotReader reader(snapshot, Isolate::Current()); | 711 SnapshotReader reader(buffer, writer.BytesWritten(), |
| 712 Snapshot::kScript, Isolate::Current()); |
750 Script& serialized_script = Script::Handle(); | 713 Script& serialized_script = Script::Handle(); |
751 serialized_script ^= reader.ReadObject(); | 714 serialized_script ^= reader.ReadObject(); |
752 | 715 |
753 // Check if the serialized script object matches the original script. | 716 // Check if the serialized script object matches the original script. |
754 String& expected_literal = String::Handle(); | 717 String& expected_literal = String::Handle(); |
755 String& actual_literal = String::Handle(); | 718 String& actual_literal = String::Handle(); |
756 String& str = String::Handle(); | 719 String& str = String::Handle(); |
757 str ^= serialized_script.url(); | 720 str ^= serialized_script.url(); |
758 EXPECT(url.Equals(str)); | 721 EXPECT(url.Equals(str)); |
759 | 722 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
836 | 799 |
837 // Create a test library and Load up a test script in it. | 800 // Create a test library and Load up a test script in it. |
838 TestCase::LoadTestScript(kScriptChars, NULL); | 801 TestCase::LoadTestScript(kScriptChars, NULL); |
839 timer1.Stop(); | 802 timer1.Stop(); |
840 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); | 803 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); |
841 | 804 |
842 // Write snapshot with object content. | 805 // Write snapshot with object content. |
843 Isolate* isolate = Isolate::Current(); | 806 Isolate* isolate = Isolate::Current(); |
844 Zone zone(isolate); | 807 Zone zone(isolate); |
845 HandleScope scope(isolate); | 808 HandleScope scope(isolate); |
846 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); | 809 FullSnapshotWriter writer(&buffer, &malloc_allocator); |
847 writer.WriteFullSnapshot(); | 810 writer.WriteFullSnapshot(); |
848 } | 811 } |
849 | 812 |
850 // Now Create another isolate using the snapshot and execute a method | 813 // Now Create another isolate using the snapshot and execute a method |
851 // from the script. | 814 // from the script. |
852 Timer timer2(true, "Snapshot_test"); | 815 Timer timer2(true, "Snapshot_test"); |
853 timer2.Start(); | 816 timer2.Start(); |
854 TestCase::CreateTestIsolateFromSnapshot(buffer); | 817 TestCase::CreateTestIsolateFromSnapshot(buffer); |
855 { | 818 { |
856 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 819 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
890 Zone zone(isolate); | 853 Zone zone(isolate); |
891 HandleScope scope(isolate); | 854 HandleScope scope(isolate); |
892 | 855 |
893 // Create a test library and Load up a test script in it. | 856 // Create a test library and Load up a test script in it. |
894 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 857 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
895 ClassFinalizer::FinalizePendingClasses(); | 858 ClassFinalizer::FinalizePendingClasses(); |
896 timer1.Stop(); | 859 timer1.Stop(); |
897 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); | 860 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); |
898 | 861 |
899 // Write snapshot with object content. | 862 // Write snapshot with object content. |
900 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); | 863 FullSnapshotWriter writer(&buffer, &malloc_allocator); |
901 writer.WriteFullSnapshot(); | 864 writer.WriteFullSnapshot(); |
902 | 865 |
903 // Invoke a function which returns an object. | 866 // Invoke a function which returns an object. |
904 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("FieldsTest")); | 867 Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("FieldsTest")); |
905 Dart_Handle result = Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL); | 868 Dart_Handle result = Dart_Invoke(cls, Dart_NewString("testMain"), 0, NULL); |
906 EXPECT_VALID(result); | 869 EXPECT_VALID(result); |
907 } | 870 } |
908 | 871 |
909 // Now Create another isolate using the snapshot and execute a method | 872 // Now Create another isolate using the snapshot and execute a method |
910 // from the script. | 873 // from the script. |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1057 uint8_t* buffer = NULL; | 1020 uint8_t* buffer = NULL; |
1058 ApiMessageWriter writer(&buffer, &zone_allocator); | 1021 ApiMessageWriter writer(&buffer, &zone_allocator); |
1059 | 1022 |
1060 static const int kArrayLength = 2; | 1023 static const int kArrayLength = 2; |
1061 intptr_t data[kArrayLength] = {1, 2}; | 1024 intptr_t data[kArrayLength] = {1, 2}; |
1062 int len = kArrayLength; | 1025 int len = kArrayLength; |
1063 writer.WriteMessage(len, data); | 1026 writer.WriteMessage(len, data); |
1064 | 1027 |
1065 // Read object back from the snapshot into a C structure. | 1028 // Read object back from the snapshot into a C structure. |
1066 ApiNativeScope scope; | 1029 ApiNativeScope scope; |
1067 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 1030 ApiMessageReader api_reader(buffer, writer.BytesWritten(), &zone_allocator); |
1068 writer.BytesWritten(), | 1031 Dart_CObject* root = api_reader.ReadMessage(); |
1069 &zone_allocator); | |
1070 EXPECT_EQ(Dart_CObject::kArray, root->type); | 1032 EXPECT_EQ(Dart_CObject::kArray, root->type); |
1071 EXPECT_EQ(kArrayLength, root->value.as_array.length); | 1033 EXPECT_EQ(kArrayLength, root->value.as_array.length); |
1072 for (int i = 0; i < kArrayLength; i++) { | 1034 for (int i = 0; i < kArrayLength; i++) { |
1073 Dart_CObject* element = root->value.as_array.values[i]; | 1035 Dart_CObject* element = root->value.as_array.values[i]; |
1074 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 1036 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
1075 EXPECT_EQ(i + 1, element->value.as_int32); | 1037 EXPECT_EQ(i + 1, element->value.as_int32); |
1076 } | 1038 } |
1077 CheckEncodeDecodeMessage(root); | 1039 CheckEncodeDecodeMessage(root); |
1078 } | 1040 } |
1079 | 1041 |
1080 | 1042 |
1081 // Helper function to call a top level Dart function, serialize the | 1043 // Helper function to call a top level Dart function, serialize the |
1082 // result and deserialize the result into a Dart_CObject structure. | 1044 // result and deserialize the result into a Dart_CObject structure. |
1083 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib, | 1045 static Dart_CObject* GetDeserializedDartMessage(Dart_Handle lib, |
1084 const char* dart_function) { | 1046 const char* dart_function) { |
1085 Dart_Handle result; | 1047 Dart_Handle result; |
1086 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL); | 1048 result = Dart_Invoke(lib, Dart_NewString(dart_function), 0, NULL); |
1087 EXPECT_VALID(result); | 1049 EXPECT_VALID(result); |
1088 | 1050 |
1089 // Serialize the list into a message. | 1051 // Serialize the list into a message. |
1090 uint8_t* buffer; | 1052 uint8_t* buffer; |
1091 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 1053 MessageWriter writer(&buffer, &zone_allocator); |
1092 const Object& list = Object::Handle(Api::UnwrapHandle(result)); | 1054 const Object& list = Object::Handle(Api::UnwrapHandle(result)); |
1093 writer.WriteObject(list.raw()); | 1055 writer.WriteMessage(list); |
1094 writer.FinalizeBuffer(); | 1056 intptr_t buffer_len = writer.BytesWritten(); |
1095 | 1057 |
1096 // Read object back from the snapshot into a C structure. | 1058 // Read object back from the snapshot into a C structure. |
1097 return DecodeMessage(buffer + Snapshot::kHeaderSize, | 1059 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1098 writer.BytesWritten(), | 1060 return api_reader.ReadMessage(); |
1099 &zone_allocator); | |
1100 } | 1061 } |
1101 | 1062 |
1102 | 1063 |
1103 UNIT_TEST_CASE(DartGeneratedMessages) { | 1064 UNIT_TEST_CASE(DartGeneratedMessages) { |
1104 static const char* kCustomIsolateScriptChars = | 1065 static const char* kCustomIsolateScriptChars = |
1105 "getSmi() {\n" | 1066 "getSmi() {\n" |
1106 " return 42;\n" | 1067 " return 42;\n" |
1107 "}\n" | 1068 "}\n" |
1108 "getBigint() {\n" | 1069 "getBigint() {\n" |
1109 " return -0x424242424242424242424242424242424242;\n" | 1070 " return -0x424242424242424242424242424242424242;\n" |
(...skipping 23 matching lines...) Expand all Loading... |
1133 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL); | 1094 string_result = Dart_Invoke(lib, Dart_NewString("getString"), 0, NULL); |
1134 EXPECT_VALID(string_result); | 1095 EXPECT_VALID(string_result); |
1135 EXPECT(Dart_IsString(string_result)); | 1096 EXPECT(Dart_IsString(string_result)); |
1136 | 1097 |
1137 { | 1098 { |
1138 DARTSCOPE_NOCHECKS(isolate); | 1099 DARTSCOPE_NOCHECKS(isolate); |
1139 | 1100 |
1140 { | 1101 { |
1141 Zone zone(Isolate::Current()); | 1102 Zone zone(Isolate::Current()); |
1142 uint8_t* buffer; | 1103 uint8_t* buffer; |
1143 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 1104 MessageWriter writer(&buffer, &zone_allocator); |
1144 Smi& smi = Smi::Handle(); | 1105 Smi& smi = Smi::Handle(); |
1145 smi ^= Api::UnwrapHandle(smi_result); | 1106 smi ^= Api::UnwrapHandle(smi_result); |
1146 writer.WriteObject(smi.raw()); | 1107 writer.WriteMessage(smi); |
1147 writer.FinalizeBuffer(); | 1108 intptr_t buffer_len = writer.BytesWritten(); |
1148 | 1109 |
1149 // Read object back from the snapshot into a C structure. | 1110 // Read object back from the snapshot into a C structure. |
1150 ApiNativeScope scope; | 1111 ApiNativeScope scope; |
1151 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 1112 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1152 writer.BytesWritten(), | 1113 Dart_CObject* root = api_reader.ReadMessage(); |
1153 &zone_allocator); | |
1154 EXPECT_NOTNULL(root); | 1114 EXPECT_NOTNULL(root); |
1155 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 1115 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
1156 EXPECT_EQ(42, root->value.as_int32); | 1116 EXPECT_EQ(42, root->value.as_int32); |
1157 CheckEncodeDecodeMessage(root); | 1117 CheckEncodeDecodeMessage(root); |
1158 } | 1118 } |
1159 { | 1119 { |
1160 Zone zone(Isolate::Current()); | 1120 Zone zone(Isolate::Current()); |
1161 uint8_t* buffer; | 1121 uint8_t* buffer; |
1162 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 1122 MessageWriter writer(&buffer, &zone_allocator); |
1163 Bigint& bigint = Bigint::Handle(); | 1123 Bigint& bigint = Bigint::Handle(); |
1164 bigint ^= Api::UnwrapHandle(bigint_result); | 1124 bigint ^= Api::UnwrapHandle(bigint_result); |
1165 writer.WriteObject(bigint.raw()); | 1125 writer.WriteMessage(bigint); |
1166 writer.FinalizeBuffer(); | 1126 intptr_t buffer_len = writer.BytesWritten(); |
1167 | 1127 |
1168 // Read object back from the snapshot into a C structure. | 1128 // Read object back from the snapshot into a C structure. |
1169 ApiNativeScope scope; | 1129 ApiNativeScope scope; |
1170 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 1130 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1171 writer.BytesWritten(), | 1131 Dart_CObject* root = api_reader.ReadMessage(); |
1172 &zone_allocator); | |
1173 EXPECT_NOTNULL(root); | 1132 EXPECT_NOTNULL(root); |
1174 EXPECT_EQ(Dart_CObject::kBigint, root->type); | 1133 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
1175 EXPECT_STREQ("-424242424242424242424242424242424242", | 1134 EXPECT_STREQ("-424242424242424242424242424242424242", |
1176 root->value.as_bigint); | 1135 root->value.as_bigint); |
1177 CheckEncodeDecodeMessage(root); | 1136 CheckEncodeDecodeMessage(root); |
1178 } | 1137 } |
1179 { | 1138 { |
1180 Zone zone(Isolate::Current()); | 1139 Zone zone(Isolate::Current()); |
1181 uint8_t* buffer; | 1140 uint8_t* buffer; |
1182 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 1141 MessageWriter writer(&buffer, &zone_allocator); |
1183 String& str = String::Handle(); | 1142 String& str = String::Handle(); |
1184 str ^= Api::UnwrapHandle(string_result); | 1143 str ^= Api::UnwrapHandle(string_result); |
1185 writer.WriteObject(str.raw()); | 1144 writer.WriteMessage(str); |
1186 writer.FinalizeBuffer(); | 1145 intptr_t buffer_len = writer.BytesWritten(); |
1187 | 1146 |
1188 // Read object back from the snapshot into a C structure. | 1147 // Read object back from the snapshot into a C structure. |
1189 ApiNativeScope scope; | 1148 ApiNativeScope scope; |
1190 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 1149 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1191 writer.BytesWritten(), | 1150 Dart_CObject* root = api_reader.ReadMessage(); |
1192 &zone_allocator); | |
1193 EXPECT_NOTNULL(root); | 1151 EXPECT_NOTNULL(root); |
1194 EXPECT_EQ(Dart_CObject::kString, root->type); | 1152 EXPECT_EQ(Dart_CObject::kString, root->type); |
1195 EXPECT_STREQ("Hello, world!", root->value.as_string); | 1153 EXPECT_STREQ("Hello, world!", root->value.as_string); |
1196 CheckEncodeDecodeMessage(root); | 1154 CheckEncodeDecodeMessage(root); |
1197 } | 1155 } |
1198 } | 1156 } |
1199 Dart_ExitScope(); | 1157 Dart_ExitScope(); |
1200 Dart_ShutdownIsolate(); | 1158 Dart_ShutdownIsolate(); |
1201 } | 1159 } |
1202 | 1160 |
(...skipping 359 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1562 EXPECT(Dart_ErrorHasException(result)); | 1520 EXPECT(Dart_ErrorHasException(result)); |
1563 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", | 1521 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", |
1564 Dart_GetError(result)); | 1522 Dart_GetError(result)); |
1565 | 1523 |
1566 Dart_ExitScope(); | 1524 Dart_ExitScope(); |
1567 } | 1525 } |
1568 | 1526 |
1569 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 1527 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
1570 | 1528 |
1571 } // namespace dart | 1529 } // namespace dart |
OLD | NEW |