OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "include/dart_debugger_api.h" | 5 #include "include/dart_debugger_api.h" |
6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_api_state.h" | 10 #include "vm/dart_api_state.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 dbl1 ^= expected.raw(); | 31 dbl1 ^= expected.raw(); |
32 dbl2 ^= actual.raw(); | 32 dbl2 ^= actual.raw(); |
33 return dbl1.value() == dbl2.value(); | 33 return dbl1.value() == dbl2.value(); |
34 } | 34 } |
35 return false; | 35 return false; |
36 } | 36 } |
37 return false; | 37 return false; |
38 } | 38 } |
39 | 39 |
40 | 40 |
41 static uint8_t* allocator(uint8_t* ptr, intptr_t old_size, intptr_t new_size) { | 41 static uint8_t* malloc_allocator( |
| 42 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
42 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); | 43 return reinterpret_cast<uint8_t*>(realloc(ptr, new_size)); |
43 } | 44 } |
44 | 45 |
45 | 46 |
| 47 static uint8_t* zone_allocator( |
| 48 uint8_t* ptr, intptr_t old_size, intptr_t new_size) { |
| 49 Zone* zone = Isolate::Current()->current_zone(); |
| 50 return reinterpret_cast<uint8_t*>( |
| 51 zone->Reallocate(reinterpret_cast<uword>(ptr), old_size, new_size)); |
| 52 } |
| 53 |
| 54 |
| 55 static Dart_CMessage* DecodeMessage(uint8_t* message, |
| 56 intptr_t length, |
| 57 ReAlloc allocator) { |
| 58 CMessageReader message_reader(message, length, allocator); |
| 59 return message_reader.ReadMessage(); |
| 60 } |
| 61 |
| 62 |
46 TEST_CASE(SerializeNull) { | 63 TEST_CASE(SerializeNull) { |
| 64 Zone zone(Isolate::Current()); |
| 65 |
47 // Write snapshot with object content. | 66 // Write snapshot with object content. |
48 uint8_t* buffer; | 67 uint8_t* buffer; |
49 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 68 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
50 const Object& null_object = Object::Handle(); | 69 const Object& null_object = Object::Handle(); |
51 writer.WriteObject(null_object.raw()); | 70 writer.WriteObject(null_object.raw()); |
52 writer.FinalizeBuffer(); | 71 writer.FinalizeBuffer(); |
53 | 72 |
54 // Create a snapshot object using the buffer. | 73 // Create a snapshot object using the buffer. |
55 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 74 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
56 | 75 |
57 // Read object back from the snapshot. | 76 // Read object back from the snapshot. |
58 SnapshotReader reader(snapshot, Isolate::Current()); | 77 SnapshotReader reader(snapshot, Isolate::Current()); |
59 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 78 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
60 EXPECT(Equals(null_object, serialized_object)); | 79 EXPECT(Equals(null_object, serialized_object)); |
61 | 80 |
62 // Read object back from the snapshot into a C structure. | 81 // Read object back from the snapshot into a C structure. |
63 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 82 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
64 writer.BytesWritten(), | 83 writer.BytesWritten(), |
65 &allocator); | 84 &zone_allocator); |
66 Dart_CObject* cobject = mreader.ReadObject(); | 85 Dart_CObject* cobject = cmessage->root; |
67 EXPECT_NOTNULL(cobject); | 86 EXPECT_NOTNULL(cobject); |
68 EXPECT_EQ(Dart_CObject::kNull, cobject->type); | 87 EXPECT_EQ(Dart_CObject::kNull, cobject->type); |
69 free(cobject); | |
70 } | 88 } |
71 | 89 |
72 | 90 |
73 TEST_CASE(SerializeSmi1) { | 91 TEST_CASE(SerializeSmi1) { |
| 92 Zone zone(Isolate::Current()); |
| 93 |
74 // Write snapshot with object content. | 94 // Write snapshot with object content. |
75 uint8_t* buffer; | 95 uint8_t* buffer; |
76 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 96 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
77 const Smi& smi = Smi::Handle(Smi::New(124)); | 97 const Smi& smi = Smi::Handle(Smi::New(124)); |
78 writer.WriteObject(smi.raw()); | 98 writer.WriteObject(smi.raw()); |
79 writer.FinalizeBuffer(); | 99 writer.FinalizeBuffer(); |
80 | 100 |
81 // Create a snapshot object using the buffer. | 101 // Create a snapshot object using the buffer. |
82 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 102 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
83 | 103 |
84 // Read object back from the snapshot. | 104 // Read object back from the snapshot. |
85 SnapshotReader reader(snapshot, Isolate::Current()); | 105 SnapshotReader reader(snapshot, Isolate::Current()); |
86 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 106 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
87 EXPECT(Equals(smi, serialized_object)); | 107 EXPECT(Equals(smi, serialized_object)); |
88 | 108 |
89 // Read object back from the snapshot into a C structure. | 109 // Read object back from the snapshot into a C structure. |
90 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 110 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
91 writer.BytesWritten(), | 111 writer.BytesWritten(), |
92 &allocator); | 112 &zone_allocator); |
93 Dart_CObject* cobject = mreader.ReadObject(); | 113 Dart_CObject* cobject = cmessage->root; |
94 EXPECT_NOTNULL(cobject); | 114 EXPECT_NOTNULL(cobject); |
95 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | 115 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
96 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | 116 EXPECT_EQ(smi.Value(), cobject->value.as_int32); |
97 free(cobject); | |
98 } | 117 } |
99 | 118 |
100 | 119 |
101 TEST_CASE(SerializeSmi2) { | 120 TEST_CASE(SerializeSmi2) { |
| 121 Zone zone(Isolate::Current()); |
| 122 |
102 // Write snapshot with object content. | 123 // Write snapshot with object content. |
103 uint8_t* buffer; | 124 uint8_t* buffer; |
104 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 125 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
105 const Smi& smi = Smi::Handle(Smi::New(-1)); | 126 const Smi& smi = Smi::Handle(Smi::New(-1)); |
106 writer.WriteObject(smi.raw()); | 127 writer.WriteObject(smi.raw()); |
107 writer.FinalizeBuffer(); | 128 writer.FinalizeBuffer(); |
108 | 129 |
109 // Create a snapshot object using the buffer. | 130 // Create a snapshot object using the buffer. |
110 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 131 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
111 | 132 |
112 // Read object back from the snapshot. | 133 // Read object back from the snapshot. |
113 SnapshotReader reader(snapshot, Isolate::Current()); | 134 SnapshotReader reader(snapshot, Isolate::Current()); |
114 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 135 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
115 EXPECT(Equals(smi, serialized_object)); | 136 EXPECT(Equals(smi, serialized_object)); |
116 | 137 |
117 // Read object back from the snapshot into a C structure. | 138 // Read object back from the snapshot into a C structure. |
118 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 139 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
119 writer.BytesWritten(), | 140 writer.BytesWritten(), |
120 &allocator); | 141 &zone_allocator); |
121 Dart_CObject* cobject = mreader.ReadObject(); | 142 Dart_CObject* cobject = cmessage->root; |
122 EXPECT_NOTNULL(cobject); | 143 EXPECT_NOTNULL(cobject); |
123 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | 144 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
124 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | 145 EXPECT_EQ(smi.Value(), cobject->value.as_int32); |
125 free(cobject); | |
126 } | 146 } |
127 | 147 |
128 | 148 |
129 TEST_CASE(SerializeDouble) { | 149 TEST_CASE(SerializeDouble) { |
| 150 Zone zone(Isolate::Current()); |
| 151 |
130 // Write snapshot with object content. | 152 // Write snapshot with object content. |
131 uint8_t* buffer; | 153 uint8_t* buffer; |
132 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 154 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
133 const Double& dbl = Double::Handle(Double::New(101.29)); | 155 const Double& dbl = Double::Handle(Double::New(101.29)); |
134 writer.WriteObject(dbl.raw()); | 156 writer.WriteObject(dbl.raw()); |
135 writer.FinalizeBuffer(); | 157 writer.FinalizeBuffer(); |
136 | 158 |
137 // Create a snapshot object using the buffer. | 159 // Create a snapshot object using the buffer. |
138 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 160 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
139 | 161 |
140 // Read object back from the snapshot. | 162 // Read object back from the snapshot. |
141 SnapshotReader reader(snapshot, Isolate::Current()); | 163 SnapshotReader reader(snapshot, Isolate::Current()); |
142 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 164 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
143 EXPECT(Equals(dbl, serialized_object)); | 165 EXPECT(Equals(dbl, serialized_object)); |
144 | 166 |
145 // Read object back from the snapshot into a C structure. | 167 // Read object back from the snapshot into a C structure. |
146 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 168 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
147 writer.BytesWritten(), | 169 writer.BytesWritten(), |
148 &allocator); | 170 &zone_allocator); |
149 Dart_CObject* cobject = mreader.ReadObject(); | 171 Dart_CObject* cobject = cmessage->root; |
150 EXPECT_NOTNULL(cobject); | 172 EXPECT_NOTNULL(cobject); |
151 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); | 173 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); |
152 EXPECT_EQ(dbl.value(), cobject->value.as_double); | 174 EXPECT_EQ(dbl.value(), cobject->value.as_double); |
153 free(cobject); | |
154 } | 175 } |
155 | 176 |
156 | 177 |
157 TEST_CASE(SerializeBool) { | 178 TEST_CASE(SerializeBool) { |
| 179 Zone zone(Isolate::Current()); |
| 180 |
158 // Write snapshot with object content. | 181 // Write snapshot with object content. |
159 uint8_t* buffer; | 182 uint8_t* buffer; |
160 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 183 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
161 const Bool& bool1 = Bool::Handle(Bool::True()); | 184 const Bool& bool1 = Bool::Handle(Bool::True()); |
162 const Bool& bool2 = Bool::Handle(Bool::False()); | 185 const Bool& bool2 = Bool::Handle(Bool::False()); |
163 writer.WriteObject(bool1.raw()); | 186 writer.WriteObject(bool1.raw()); |
164 writer.WriteObject(bool2.raw()); | 187 writer.WriteObject(bool2.raw()); |
165 writer.FinalizeBuffer(); | 188 writer.FinalizeBuffer(); |
166 | 189 |
167 // Create a snapshot object using the buffer. | 190 // Create a snapshot object using the buffer. |
168 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 191 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
169 | 192 |
170 // Read object back from the snapshot. | 193 // Read object back from the snapshot. |
171 SnapshotReader reader(snapshot, Isolate::Current()); | 194 SnapshotReader reader(snapshot, Isolate::Current()); |
172 EXPECT(Bool::True() == reader.ReadObject()); | 195 EXPECT(Bool::True() == reader.ReadObject()); |
173 EXPECT(Bool::False() == reader.ReadObject()); | 196 EXPECT(Bool::False() == reader.ReadObject()); |
| 197 } |
| 198 |
| 199 |
| 200 TEST_CASE(SerializeTrue) { |
| 201 Zone zone(Isolate::Current()); |
| 202 |
| 203 // Write snapshot with true object. |
| 204 uint8_t* buffer; |
| 205 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
| 206 const Bool& bl = Bool::Handle(Bool::True()); |
| 207 writer.WriteObject(bl.raw()); |
| 208 writer.FinalizeBuffer(); |
| 209 |
| 210 // Create a snapshot object using the buffer. |
| 211 Snapshot::SetupFromBuffer(buffer); |
174 | 212 |
175 // Read object back from the snapshot into a C structure. | 213 // Read object back from the snapshot into a C structure. |
176 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 214 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
177 writer.BytesWritten(), | 215 writer.BytesWritten(), |
178 &allocator); | 216 &zone_allocator); |
179 Dart_CObject* cobject1 = mreader.ReadObject(); | 217 Dart_CObject* cobject = cmessage->root; |
180 EXPECT_NOTNULL(cobject1); | 218 EXPECT_NOTNULL(cobject); |
181 EXPECT_EQ(Dart_CObject::kBool, cobject1->type); | 219 EXPECT_EQ(Dart_CObject::kBool, cobject->type); |
182 EXPECT_EQ(true, cobject1->value.as_bool); | 220 EXPECT_EQ(true, cobject->value.as_bool); |
183 Dart_CObject* cobject2 = mreader.ReadObject(); | 221 } |
184 EXPECT_NOTNULL(cobject2); | 222 |
185 EXPECT_EQ(Dart_CObject::kBool, cobject2->type); | 223 |
186 EXPECT_EQ(false, cobject2->value.as_bool); | 224 TEST_CASE(SerializeFalse) { |
187 free(cobject1); | 225 Zone zone(Isolate::Current()); |
188 free(cobject2); | 226 |
| 227 // Write snapshot with false object. |
| 228 uint8_t* buffer; |
| 229 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
| 230 const Bool& bl = Bool::Handle(Bool::False()); |
| 231 writer.WriteObject(bl.raw()); |
| 232 writer.FinalizeBuffer(); |
| 233 |
| 234 // Create a snapshot object using the buffer. |
| 235 Snapshot::SetupFromBuffer(buffer); |
| 236 |
| 237 // Read object back from the snapshot into a C structure. |
| 238 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
| 239 writer.BytesWritten(), |
| 240 &zone_allocator); |
| 241 Dart_CObject* cobject = cmessage->root; |
| 242 EXPECT_NOTNULL(cobject); |
| 243 EXPECT_EQ(Dart_CObject::kBool, cobject->type); |
| 244 EXPECT_EQ(false, cobject->value.as_bool); |
189 } | 245 } |
190 | 246 |
191 | 247 |
192 TEST_CASE(SerializeBigint) { | 248 TEST_CASE(SerializeBigint) { |
| 249 Zone zone(Isolate::Current()); |
| 250 |
193 // Write snapshot with object content. | 251 // Write snapshot with object content. |
194 uint8_t* buffer; | 252 uint8_t* buffer; |
195 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 253 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
196 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); | 254 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); |
197 writer.WriteObject(bigint.raw()); | 255 writer.WriteObject(bigint.raw()); |
198 writer.FinalizeBuffer(); | 256 writer.FinalizeBuffer(); |
199 | 257 |
200 // Create a snapshot object using the buffer. | 258 // Create a snapshot object using the buffer. |
201 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 259 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
202 | 260 |
203 // Read object back from the snapshot. | 261 // Read object back from the snapshot. |
204 SnapshotReader reader(snapshot, Isolate::Current()); | 262 SnapshotReader reader(snapshot, Isolate::Current()); |
205 Bigint& obj = Bigint::Handle(); | 263 Bigint& obj = Bigint::Handle(); |
206 obj ^= reader.ReadObject(); | 264 obj ^= reader.ReadObject(); |
207 OS::Print("%lld", BigintOperations::ToInt64(obj)); | 265 OS::Print("%lld", BigintOperations::ToInt64(obj)); |
208 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); | 266 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); |
209 | 267 |
210 // Read object back from the snapshot into a C structure. | 268 // Read object back from the snapshot into a C structure. |
211 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 269 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
212 writer.BytesWritten(), | 270 writer.BytesWritten(), |
213 &allocator); | 271 &zone_allocator); |
214 Dart_CObject* cobject = mreader.ReadObject(); | 272 Dart_CObject* cobject = cmessage->root; |
215 // Bigint not supported. | 273 // Bigint not supported. |
216 EXPECT(cobject == NULL); | 274 EXPECT(cobject == NULL); |
217 } | 275 } |
218 | 276 |
219 | 277 |
220 TEST_CASE(SerializeSingletons) { | 278 TEST_CASE(SerializeSingletons) { |
221 // Write snapshot with object content. | 279 // Write snapshot with object content. |
222 uint8_t* buffer; | 280 uint8_t* buffer; |
223 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 281 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); |
224 writer.WriteObject(Object::class_class()); | 282 writer.WriteObject(Object::class_class()); |
225 writer.WriteObject(Object::null_class()); | 283 writer.WriteObject(Object::null_class()); |
226 writer.WriteObject(Object::type_class()); | 284 writer.WriteObject(Object::type_class()); |
227 writer.WriteObject(Object::type_parameter_class()); | 285 writer.WriteObject(Object::type_parameter_class()); |
228 writer.WriteObject(Object::instantiated_type_class()); | 286 writer.WriteObject(Object::instantiated_type_class()); |
229 writer.WriteObject(Object::abstract_type_arguments_class()); | 287 writer.WriteObject(Object::abstract_type_arguments_class()); |
230 writer.WriteObject(Object::type_arguments_class()); | 288 writer.WriteObject(Object::type_arguments_class()); |
231 writer.WriteObject(Object::instantiated_type_arguments_class()); | 289 writer.WriteObject(Object::instantiated_type_arguments_class()); |
232 writer.WriteObject(Object::function_class()); | 290 writer.WriteObject(Object::function_class()); |
233 writer.WriteObject(Object::field_class()); | 291 writer.WriteObject(Object::field_class()); |
(...skipping 25 matching lines...) Expand all Loading... |
259 EXPECT(Object::field_class() == reader.ReadObject()); | 317 EXPECT(Object::field_class() == reader.ReadObject()); |
260 EXPECT(Object::token_stream_class() == reader.ReadObject()); | 318 EXPECT(Object::token_stream_class() == reader.ReadObject()); |
261 EXPECT(Object::script_class() == reader.ReadObject()); | 319 EXPECT(Object::script_class() == reader.ReadObject()); |
262 EXPECT(Object::library_class() == reader.ReadObject()); | 320 EXPECT(Object::library_class() == reader.ReadObject()); |
263 EXPECT(Object::code_class() == reader.ReadObject()); | 321 EXPECT(Object::code_class() == reader.ReadObject()); |
264 EXPECT(Object::instructions_class() == reader.ReadObject()); | 322 EXPECT(Object::instructions_class() == reader.ReadObject()); |
265 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); | 323 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); |
266 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); | 324 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); |
267 EXPECT(Object::context_class() == reader.ReadObject()); | 325 EXPECT(Object::context_class() == reader.ReadObject()); |
268 EXPECT(Object::context_scope_class() == reader.ReadObject()); | 326 EXPECT(Object::context_scope_class() == reader.ReadObject()); |
| 327 |
| 328 free(buffer); |
269 } | 329 } |
270 | 330 |
271 | 331 |
272 TEST_CASE(SerializeString) { | 332 TEST_CASE(SerializeString) { |
| 333 Zone zone(Isolate::Current()); |
| 334 |
273 // Write snapshot with object content. | 335 // Write snapshot with object content. |
274 uint8_t* buffer; | 336 uint8_t* buffer; |
275 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 337 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
276 static const char* cstr = "This string shall be serialized"; | 338 static const char* cstr = "This string shall be serialized"; |
277 String& str = String::Handle(String::New(cstr)); | 339 String& str = String::Handle(String::New(cstr)); |
278 writer.WriteObject(str.raw()); | 340 writer.WriteObject(str.raw()); |
279 writer.FinalizeBuffer(); | 341 writer.FinalizeBuffer(); |
280 | 342 |
281 // Create a snapshot object using the buffer. | 343 // Create a snapshot object using the buffer. |
282 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 344 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
283 | 345 |
284 // Read object back from the snapshot. | 346 // Read object back from the snapshot. |
285 SnapshotReader reader(snapshot, Isolate::Current()); | 347 SnapshotReader reader(snapshot, Isolate::Current()); |
286 String& serialized_str = String::Handle(); | 348 String& serialized_str = String::Handle(); |
287 serialized_str ^= reader.ReadObject(); | 349 serialized_str ^= reader.ReadObject(); |
288 EXPECT(str.Equals(serialized_str)); | 350 EXPECT(str.Equals(serialized_str)); |
289 | 351 |
290 // Read object back from the snapshot into a C structure. | 352 // Read object back from the snapshot into a C structure. |
291 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 353 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
292 writer.BytesWritten(), | 354 writer.BytesWritten(), |
293 &allocator); | 355 &zone_allocator); |
294 Dart_CObject* cobject = mreader.ReadObject(); | 356 Dart_CObject* cobject = cmessage->root; |
295 EXPECT_EQ(Dart_CObject::kString, cobject->type); | 357 EXPECT_EQ(Dart_CObject::kString, cobject->type); |
296 EXPECT_STREQ(cstr, cobject->value.as_string); | 358 EXPECT_STREQ(cstr, cobject->value.as_string); |
297 free(cobject); | |
298 } | 359 } |
299 | 360 |
300 | 361 |
301 TEST_CASE(SerializeArray) { | 362 TEST_CASE(SerializeArray) { |
| 363 Zone zone(Isolate::Current()); |
| 364 |
302 // Write snapshot with object content. | 365 // Write snapshot with object content. |
303 uint8_t* buffer; | 366 uint8_t* buffer; |
304 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 367 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
305 const int kArrayLength = 10; | 368 const int kArrayLength = 10; |
306 Array& array = Array::Handle(Array::New(kArrayLength)); | 369 Array& array = Array::Handle(Array::New(kArrayLength)); |
307 Smi& smi = Smi::Handle(); | 370 Smi& smi = Smi::Handle(); |
308 for (int i = 0; i < kArrayLength; i++) { | 371 for (int i = 0; i < kArrayLength; i++) { |
309 smi ^= Smi::New(i); | 372 smi ^= Smi::New(i); |
310 array.SetAt(i, smi); | 373 array.SetAt(i, smi); |
311 } | 374 } |
312 writer.WriteObject(array.raw()); | 375 writer.WriteObject(array.raw()); |
313 writer.FinalizeBuffer(); | 376 writer.FinalizeBuffer(); |
314 | 377 |
315 // Create a snapshot object using the buffer. | 378 // Create a snapshot object using the buffer. |
316 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 379 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
317 | 380 |
318 // Read object back from the snapshot. | 381 // Read object back from the snapshot. |
319 SnapshotReader reader(snapshot, Isolate::Current()); | 382 SnapshotReader reader(snapshot, Isolate::Current()); |
320 Array& serialized_array = Array::Handle(); | 383 Array& serialized_array = Array::Handle(); |
321 serialized_array ^= reader.ReadObject(); | 384 serialized_array ^= reader.ReadObject(); |
322 EXPECT(array.Equals(serialized_array)); | 385 EXPECT(array.Equals(serialized_array)); |
323 | 386 |
324 // Read object back from the snapshot into a C structure. | 387 // Read object back from the snapshot into a C structure. |
325 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 388 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
326 writer.BytesWritten(), | 389 writer.BytesWritten(), |
327 &allocator); | 390 &zone_allocator); |
328 Dart_CObject* cobject = mreader.ReadObject(); | 391 Dart_CObject* cobject = cmessage->root; |
329 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | 392 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
330 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | 393 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
331 for (int i = 0; i < kArrayLength; i++) { | 394 for (int i = 0; i < kArrayLength; i++) { |
332 Dart_CObject* element = cobject->value.as_array.values[i]; | 395 Dart_CObject* element = cobject->value.as_array.values[i]; |
333 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 396 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
334 EXPECT_EQ(i, element->value.as_int32); | 397 EXPECT_EQ(i, element->value.as_int32); |
335 free(element); | |
336 } | 398 } |
337 free(cobject); | |
338 } | 399 } |
339 | 400 |
340 | 401 |
341 TEST_CASE(SerializeEmptyArray) { | 402 TEST_CASE(SerializeEmptyArray) { |
| 403 Zone zone(Isolate::Current()); |
| 404 |
342 // Write snapshot with object content. | 405 // Write snapshot with object content. |
343 uint8_t* buffer; | 406 uint8_t* buffer; |
344 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 407 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
345 const int kArrayLength = 0; | 408 const int kArrayLength = 0; |
346 Array& array = Array::Handle(Array::New(kArrayLength)); | 409 Array& array = Array::Handle(Array::New(kArrayLength)); |
347 writer.WriteObject(array.raw()); | 410 writer.WriteObject(array.raw()); |
348 writer.FinalizeBuffer(); | 411 writer.FinalizeBuffer(); |
349 | 412 |
350 // Create a snapshot object using the buffer. | 413 // Create a snapshot object using the buffer. |
351 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 414 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
352 | 415 |
353 // Read object back from the snapshot. | 416 // Read object back from the snapshot. |
354 SnapshotReader reader(snapshot, Isolate::Current()); | 417 SnapshotReader reader(snapshot, Isolate::Current()); |
355 Array& serialized_array = Array::Handle(); | 418 Array& serialized_array = Array::Handle(); |
356 serialized_array ^= reader.ReadObject(); | 419 serialized_array ^= reader.ReadObject(); |
357 EXPECT(array.Equals(serialized_array)); | 420 EXPECT(array.Equals(serialized_array)); |
358 | 421 |
359 // Read object back from the snapshot into a C structure. | 422 // Read object back from the snapshot into a C structure. |
360 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 423 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
361 writer.BytesWritten(), | 424 writer.BytesWritten(), |
362 &allocator); | 425 &zone_allocator); |
363 Dart_CObject* cobject = mreader.ReadObject(); | 426 Dart_CObject* cobject = cmessage->root; |
364 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | 427 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
365 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | 428 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
366 EXPECT(cobject->value.as_array.values == NULL); | 429 EXPECT(cobject->value.as_array.values == NULL); |
367 free(cobject); | |
368 } | 430 } |
369 | 431 |
370 | 432 |
371 TEST_CASE(SerializeScript) { | 433 TEST_CASE(SerializeScript) { |
372 const char* kScriptChars = | 434 const char* kScriptChars = |
373 "class A {\n" | 435 "class A {\n" |
374 " static bar() { return 42; }\n" | 436 " static bar() { return 42; }\n" |
375 " static fly() { return 5; }\n" | 437 " static fly() { return 5; }\n" |
376 "}\n"; | 438 "}\n"; |
377 | 439 |
378 String& url = String::Handle(String::New("dart-test:SerializeScript")); | 440 String& url = String::Handle(String::New("dart-test:SerializeScript")); |
379 String& source = String::Handle(String::New(kScriptChars)); | 441 String& source = String::Handle(String::New(kScriptChars)); |
380 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); | 442 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
381 const String& lib_url = String::Handle(String::NewSymbol("TestLib")); | 443 const String& lib_url = String::Handle(String::NewSymbol("TestLib")); |
382 Library& lib = Library::Handle(Library::New(lib_url)); | 444 Library& lib = Library::Handle(Library::New(lib_url)); |
383 lib.Register(); | 445 lib.Register(); |
384 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 446 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
385 | 447 |
386 // Write snapshot with object content. | 448 // Write snapshot with object content. |
387 uint8_t* buffer; | 449 uint8_t* buffer; |
388 SnapshotWriter writer(Snapshot::kScript, &buffer, &allocator); | 450 SnapshotWriter writer(Snapshot::kScript, &buffer, &malloc_allocator); |
389 writer.WriteObject(script.raw()); | 451 writer.WriteObject(script.raw()); |
390 writer.FinalizeBuffer(); | 452 writer.FinalizeBuffer(); |
391 | 453 |
392 // Create a snapshot object using the buffer. | 454 // Create a snapshot object using the buffer. |
393 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 455 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
394 | 456 |
395 // Read object back from the snapshot. | 457 // Read object back from the snapshot. |
396 SnapshotReader reader(snapshot, Isolate::Current()); | 458 SnapshotReader reader(snapshot, Isolate::Current()); |
397 Script& serialized_script = Script::Handle(); | 459 Script& serialized_script = Script::Handle(); |
398 serialized_script ^= reader.ReadObject(); | 460 serialized_script ^= reader.ReadObject(); |
399 | 461 |
400 // Check if the serialized script object matches the original script. | 462 // Check if the serialized script object matches the original script. |
401 String& str = String::Handle(); | 463 String& str = String::Handle(); |
402 str ^= serialized_script.source(); | 464 str ^= serialized_script.source(); |
403 EXPECT(source.Equals(str)); | 465 EXPECT(source.Equals(str)); |
404 str ^= serialized_script.url(); | 466 str ^= serialized_script.url(); |
405 EXPECT(url.Equals(str)); | 467 EXPECT(url.Equals(str)); |
406 const TokenStream& expected_tokens = TokenStream::Handle(script.tokens()); | 468 const TokenStream& expected_tokens = TokenStream::Handle(script.tokens()); |
407 const TokenStream& serialized_tokens = | 469 const TokenStream& serialized_tokens = |
408 TokenStream::Handle(serialized_script.tokens()); | 470 TokenStream::Handle(serialized_script.tokens()); |
409 EXPECT_EQ(expected_tokens.Length(), serialized_tokens.Length()); | 471 EXPECT_EQ(expected_tokens.Length(), serialized_tokens.Length()); |
410 String& expected_literal = String::Handle(); | 472 String& expected_literal = String::Handle(); |
411 String& actual_literal = String::Handle(); | 473 String& actual_literal = String::Handle(); |
412 for (intptr_t i = 0; i < expected_tokens.Length(); i++) { | 474 for (intptr_t i = 0; i < expected_tokens.Length(); i++) { |
413 EXPECT_EQ(expected_tokens.KindAt(i), serialized_tokens.KindAt(i)); | 475 EXPECT_EQ(expected_tokens.KindAt(i), serialized_tokens.KindAt(i)); |
414 expected_literal ^= expected_tokens.LiteralAt(i); | 476 expected_literal ^= expected_tokens.LiteralAt(i); |
415 actual_literal ^= serialized_tokens.LiteralAt(i); | 477 actual_literal ^= serialized_tokens.LiteralAt(i); |
416 EXPECT(expected_literal.Equals(actual_literal)); | 478 EXPECT(expected_literal.Equals(actual_literal)); |
417 } | 479 } |
| 480 |
| 481 free(buffer); |
418 } | 482 } |
419 | 483 |
420 | 484 |
421 // Only ia32 and x64 can run execution tests. | 485 // Only ia32 and x64 can run execution tests. |
422 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) | 486 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) |
423 UNIT_TEST_CASE(FullSnapshot) { | 487 UNIT_TEST_CASE(FullSnapshot) { |
424 const char* kScriptChars = | 488 const char* kScriptChars = |
425 "class Fields {\n" | 489 "class Fields {\n" |
426 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n" | 490 " Fields(int i, int j) : fld1 = i, fld2 = j {}\n" |
427 " int fld1;\n" | 491 " int fld1;\n" |
(...skipping 19 matching lines...) Expand all Loading... |
447 | 511 |
448 // Create a test library and Load up a test script in it. | 512 // Create a test library and Load up a test script in it. |
449 TestCase::LoadTestScript(kScriptChars, NULL); | 513 TestCase::LoadTestScript(kScriptChars, NULL); |
450 timer1.Stop(); | 514 timer1.Stop(); |
451 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); | 515 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); |
452 | 516 |
453 // Write snapshot with object content. | 517 // Write snapshot with object content. |
454 Isolate* isolate = Isolate::Current(); | 518 Isolate* isolate = Isolate::Current(); |
455 Zone zone(isolate); | 519 Zone zone(isolate); |
456 HandleScope scope(isolate); | 520 HandleScope scope(isolate); |
457 SnapshotWriter writer(Snapshot::kFull, &buffer, &allocator); | 521 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); |
458 writer.WriteFullSnapshot(); | 522 writer.WriteFullSnapshot(); |
459 } | 523 } |
460 | 524 |
461 // Now Create another isolate using the snapshot and execute a method | 525 // Now Create another isolate using the snapshot and execute a method |
462 // from the script. | 526 // from the script. |
463 Timer timer2(true, "Snapshot_test"); | 527 Timer timer2(true, "Snapshot_test"); |
464 timer2.Start(); | 528 timer2.Start(); |
465 TestCase::CreateTestIsolateFromSnapshot(buffer); | 529 TestCase::CreateTestIsolateFromSnapshot(buffer); |
466 { | 530 { |
467 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. | 531 Dart_EnterScope(); // Start a Dart API scope for invoking API functions. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 Zone zone(isolate); | 567 Zone zone(isolate); |
504 HandleScope scope(isolate); | 568 HandleScope scope(isolate); |
505 | 569 |
506 // Create a test library and Load up a test script in it. | 570 // Create a test library and Load up a test script in it. |
507 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 571 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
508 ClassFinalizer::FinalizePendingClasses(); | 572 ClassFinalizer::FinalizePendingClasses(); |
509 timer1.Stop(); | 573 timer1.Stop(); |
510 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); | 574 OS::PrintErr("Without Snapshot: %dus\n", timer1.TotalElapsedTime()); |
511 | 575 |
512 // Write snapshot with object content. | 576 // Write snapshot with object content. |
513 SnapshotWriter writer(Snapshot::kFull, &buffer, &allocator); | 577 SnapshotWriter writer(Snapshot::kFull, &buffer, &malloc_allocator); |
514 writer.WriteFullSnapshot(); | 578 writer.WriteFullSnapshot(); |
515 | 579 |
516 // Invoke a function which returns an object. | 580 // Invoke a function which returns an object. |
517 Dart_Handle result = Dart_InvokeStatic(lib, | 581 Dart_Handle result = Dart_InvokeStatic(lib, |
518 Dart_NewString("FieldsTest"), | 582 Dart_NewString("FieldsTest"), |
519 Dart_NewString("testMain"), | 583 Dart_NewString("testMain"), |
520 0, | 584 0, |
521 NULL); | 585 NULL); |
522 EXPECT_VALID(result); | 586 EXPECT_VALID(result); |
523 } | 587 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 EXPECT_VALID(result); | 731 EXPECT_VALID(result); |
668 Dart_ExitScope(); | 732 Dart_ExitScope(); |
669 } | 733 } |
670 Dart_ShutdownIsolate(); | 734 Dart_ShutdownIsolate(); |
671 free(full_snapshot); | 735 free(full_snapshot); |
672 free(script_snapshot); | 736 free(script_snapshot); |
673 } | 737 } |
674 | 738 |
675 | 739 |
676 TEST_CASE(IntArrayMessage) { | 740 TEST_CASE(IntArrayMessage) { |
| 741 Zone zone(Isolate::Current()); |
677 uint8_t* buffer = NULL; | 742 uint8_t* buffer = NULL; |
678 MessageWriter writer(&buffer, &allocator); | 743 MessageWriter writer(&buffer, &zone_allocator); |
679 | 744 |
680 static const int kArrayLength = 2; | 745 static const int kArrayLength = 2; |
681 intptr_t data[kArrayLength] = {1, 2}; | 746 intptr_t data[kArrayLength] = {1, 2}; |
682 int len = kArrayLength; | 747 int len = kArrayLength; |
683 writer.WriteMessage(len, data); | 748 writer.WriteMessage(len, data); |
684 | 749 |
685 // Read object back from the snapshot into a C structure. | 750 // Read object back from the snapshot into a C structure. |
686 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 751 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
687 writer.BytesWritten(), | 752 writer.BytesWritten(), |
688 &allocator); | 753 &zone_allocator); |
689 Dart_CObject* value = mreader.ReadObject(); | 754 Dart_CObject* cobject = cmessage->root; |
690 EXPECT_EQ(Dart_CObject::kArray, value->type); | 755 EXPECT_EQ(Dart_CObject::kArray, cobject->type); |
691 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 756 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); |
692 for (int i = 0; i < kArrayLength; i++) { | 757 for (int i = 0; i < kArrayLength; i++) { |
693 Dart_CObject* element = value->value.as_array.values[i]; | 758 Dart_CObject* element = cobject->value.as_array.values[i]; |
694 EXPECT_EQ(Dart_CObject::kInt32, element->type); | 759 EXPECT_EQ(Dart_CObject::kInt32, element->type); |
695 EXPECT_EQ(i + 1, element->value.as_int32); | 760 EXPECT_EQ(i + 1, element->value.as_int32); |
696 free(element); | |
697 } | 761 } |
698 free(value); | |
699 } | 762 } |
700 | 763 |
701 | 764 |
702 // Helper function to call a top level Dart function, serialize the | 765 // Helper function to call a top level Dart function, serialize the |
703 // result and deserialize the result into a Dart_CObject structure. | 766 // result and deserialize the result into a Dart_CObject structure. |
704 static Dart_CObject* GetDeserializedDartObject(Dart_Handle lib, | 767 static Dart_CMessage* GetDeserializedDartMessage(Dart_Handle lib, |
705 const char* dart_function) { | 768 const char* dart_function) { |
706 Dart_Handle result; | 769 Dart_Handle result; |
707 result = Dart_InvokeStatic(lib, | 770 result = Dart_InvokeStatic(lib, |
708 Dart_NewString(""), | 771 Dart_NewString(""), |
709 Dart_NewString(dart_function), | 772 Dart_NewString(dart_function), |
710 0, | 773 0, |
711 NULL); | 774 NULL); |
712 EXPECT_VALID(result); | 775 EXPECT_VALID(result); |
713 | 776 |
714 // Serialize the list into a message. | 777 // Serialize the list into a message. |
715 uint8_t* buffer; | 778 uint8_t* buffer; |
716 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 779 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
717 const Object& list = Object::Handle(Api::UnwrapHandle(result)); | 780 const Object& list = Object::Handle(Api::UnwrapHandle(result)); |
718 writer.WriteObject(list.raw()); | 781 writer.WriteObject(list.raw()); |
719 writer.FinalizeBuffer(); | 782 writer.FinalizeBuffer(); |
720 | 783 |
721 // Read object back from the snapshot into a C structure. | 784 // Read object back from the snapshot into a C structure. |
722 CMessageReader reader(buffer + Snapshot::kHeaderSize, | 785 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
723 writer.BytesWritten(), | 786 writer.BytesWritten(), |
724 &allocator); | 787 &zone_allocator); |
725 Dart_CObject* value = reader.ReadObject(); | 788 return cmessage; |
726 free(buffer); | |
727 return value; | |
728 } | 789 } |
729 | 790 |
730 | 791 |
731 UNIT_TEST_CASE(DartGeneratedMessages) { | 792 UNIT_TEST_CASE(DartGeneratedMessages) { |
732 static const char* kCustomIsolateScriptChars = | 793 static const char* kCustomIsolateScriptChars = |
733 "getSmi() {\n" | 794 "getSmi() {\n" |
734 " return 42;\n" | 795 " return 42;\n" |
735 "}\n" | 796 "}\n" |
736 "getString() {\n" | 797 "getString() {\n" |
737 " return \"Hello, world!\";\n" | 798 " return \"Hello, world!\";\n" |
(...skipping 23 matching lines...) Expand all Loading... |
761 Dart_NewString("getString"), | 822 Dart_NewString("getString"), |
762 0, | 823 0, |
763 NULL); | 824 NULL); |
764 EXPECT_VALID(string_result); | 825 EXPECT_VALID(string_result); |
765 EXPECT(Dart_IsString(string_result)); | 826 EXPECT(Dart_IsString(string_result)); |
766 | 827 |
767 { | 828 { |
768 DARTSCOPE_NOCHECKS(isolate); | 829 DARTSCOPE_NOCHECKS(isolate); |
769 | 830 |
770 { | 831 { |
| 832 Zone zone(Isolate::Current()); |
771 uint8_t* buffer; | 833 uint8_t* buffer; |
772 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 834 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
773 Smi& smi = Smi::Handle(); | 835 Smi& smi = Smi::Handle(); |
774 smi ^= Api::UnwrapHandle(smi_result); | 836 smi ^= Api::UnwrapHandle(smi_result); |
775 writer.WriteObject(smi.raw()); | 837 writer.WriteObject(smi.raw()); |
776 writer.FinalizeBuffer(); | 838 writer.FinalizeBuffer(); |
777 | 839 |
778 // Read object back from the snapshot into a C structure. | 840 // Read object back from the snapshot into a C structure. |
779 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 841 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
780 writer.BytesWritten(), | 842 writer.BytesWritten(), |
781 &allocator); | 843 &zone_allocator); |
782 Dart_CObject* value = mreader.ReadObject(); | 844 Dart_CObject* cobject = cmessage->root; |
783 EXPECT_NOTNULL(value); | 845 EXPECT_NOTNULL(cobject); |
784 EXPECT_EQ(Dart_CObject::kInt32, value->type); | 846 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); |
785 EXPECT_EQ(42, value->value.as_int32); | 847 EXPECT_EQ(42, cobject->value.as_int32); |
786 free(value); | |
787 free(buffer); | |
788 } | 848 } |
789 { | 849 { |
| 850 Zone zone(Isolate::Current()); |
790 uint8_t* buffer; | 851 uint8_t* buffer; |
791 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 852 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
792 String& str = String::Handle(); | 853 String& str = String::Handle(); |
793 str ^= Api::UnwrapHandle(string_result); | 854 str ^= Api::UnwrapHandle(string_result); |
794 writer.WriteObject(str.raw()); | 855 writer.WriteObject(str.raw()); |
795 writer.FinalizeBuffer(); | 856 writer.FinalizeBuffer(); |
796 | 857 |
797 // Read object back from the snapshot into a C structure. | 858 // Read object back from the snapshot into a C structure. |
798 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | 859 Dart_CMessage* cmessage = DecodeMessage(buffer + Snapshot::kHeaderSize, |
799 writer.BytesWritten(), | 860 writer.BytesWritten(), |
800 &allocator); | 861 &zone_allocator); |
801 Dart_CObject* value = mreader.ReadObject(); | 862 Dart_CObject* cobject = cmessage->root; |
802 EXPECT_NOTNULL(value); | 863 EXPECT_NOTNULL(cobject); |
803 EXPECT_EQ(Dart_CObject::kString, value->type); | 864 EXPECT_EQ(Dart_CObject::kString, cobject->type); |
804 EXPECT_STREQ("Hello, world!", value->value.as_string); | 865 EXPECT_STREQ("Hello, world!", cobject->value.as_string); |
805 free(value); | |
806 free(buffer); | |
807 } | 866 } |
808 } | 867 } |
809 Dart_ExitScope(); | 868 Dart_ExitScope(); |
810 Dart_ShutdownIsolate(); | 869 Dart_ShutdownIsolate(); |
811 } | 870 } |
812 | 871 |
813 | 872 |
814 UNIT_TEST_CASE(DartGeneratedListMessages) { | 873 UNIT_TEST_CASE(DartGeneratedListMessages) { |
815 const int kArrayLength = 10; | 874 const int kArrayLength = 10; |
816 static const char* kScriptChars = | 875 static const char* kScriptChars = |
(...skipping 26 matching lines...) Expand all Loading... |
843 Dart_EnterScope(); | 902 Dart_EnterScope(); |
844 | 903 |
845 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 904 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
846 EXPECT_VALID(lib); | 905 EXPECT_VALID(lib); |
847 | 906 |
848 { | 907 { |
849 DARTSCOPE_NOCHECKS(isolate); | 908 DARTSCOPE_NOCHECKS(isolate); |
850 | 909 |
851 { | 910 { |
852 // Generate a list of nulls from Dart code. | 911 // Generate a list of nulls from Dart code. |
853 Dart_CObject* value = GetDeserializedDartObject(lib, "getList"); | 912 Zone zone(Isolate::Current()); |
| 913 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getList"); |
| 914 Dart_CObject* value = cmessage->root; |
854 EXPECT_NOTNULL(value); | 915 EXPECT_NOTNULL(value); |
855 EXPECT_EQ(Dart_CObject::kArray, value->type); | 916 EXPECT_EQ(Dart_CObject::kArray, value->type); |
856 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 917 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
857 for (int i = 0; i < kArrayLength; i++) { | 918 for (int i = 0; i < kArrayLength; i++) { |
858 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); | 919 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); |
859 free(value->value.as_array.values[i]); | |
860 } | 920 } |
861 free(value); | |
862 } | 921 } |
863 { | 922 { |
864 // Generate a list of ints from Dart code. | 923 // Generate a list of ints from Dart code. |
865 Dart_CObject* value = GetDeserializedDartObject(lib, "getIntList"); | 924 Zone zone(Isolate::Current()); |
| 925 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getIntList"); |
| 926 Dart_CObject* value = cmessage->root; |
866 EXPECT_NOTNULL(value); | 927 EXPECT_NOTNULL(value); |
867 EXPECT_EQ(Dart_CObject::kArray, value->type); | 928 EXPECT_EQ(Dart_CObject::kArray, value->type); |
868 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 929 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
869 for (int i = 0; i < kArrayLength; i++) { | 930 for (int i = 0; i < kArrayLength; i++) { |
870 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type); | 931 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[i]->type); |
871 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32); | 932 EXPECT_EQ(i, value->value.as_array.values[i]->value.as_int32); |
872 free(value->value.as_array.values[i]); | |
873 } | 933 } |
874 free(value); | |
875 } | 934 } |
876 { | 935 { |
877 // Generate a list of strings from Dart code. | 936 // Generate a list of strings from Dart code. |
878 Dart_CObject* value = GetDeserializedDartObject(lib, "getStringList"); | 937 Zone zone(Isolate::Current()); |
| 938 Dart_CMessage* cmessage = |
| 939 GetDeserializedDartMessage(lib, "getStringList"); |
| 940 Dart_CObject* value = cmessage->root; |
879 EXPECT_NOTNULL(value); | 941 EXPECT_NOTNULL(value); |
880 EXPECT_EQ(Dart_CObject::kArray, value->type); | 942 EXPECT_EQ(Dart_CObject::kArray, value->type); |
881 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 943 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
882 for (int i = 0; i < kArrayLength; i++) { | 944 for (int i = 0; i < kArrayLength; i++) { |
883 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[i]->type); | 945 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[i]->type); |
884 char buffer[3]; | 946 char buffer[3]; |
885 snprintf(buffer, sizeof(buffer), "%d", i); | 947 snprintf(buffer, sizeof(buffer), "%d", i); |
886 EXPECT_STREQ(buffer, value->value.as_array.values[i]->value.as_string); | 948 EXPECT_STREQ(buffer, value->value.as_array.values[i]->value.as_string); |
887 free(value->value.as_array.values[i]); | |
888 } | 949 } |
889 free(value); | |
890 } | 950 } |
891 { | 951 { |
892 // Generate a list of objects of different types from Dart code. | 952 // Generate a list of objects of different types from Dart code. |
893 Dart_CObject* value = GetDeserializedDartObject(lib, "getMixedList"); | 953 Zone zone(Isolate::Current()); |
| 954 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getMixedList"); |
| 955 Dart_CObject* value = cmessage->root; |
894 EXPECT_NOTNULL(value); | 956 EXPECT_NOTNULL(value); |
895 EXPECT_EQ(Dart_CObject::kArray, value->type); | 957 EXPECT_EQ(Dart_CObject::kArray, value->type); |
896 EXPECT_EQ(kArrayLength, value->value.as_array.length); | 958 EXPECT_EQ(kArrayLength, value->value.as_array.length); |
897 | 959 |
898 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[0]->type); | 960 EXPECT_EQ(Dart_CObject::kInt32, value->value.as_array.values[0]->type); |
899 EXPECT_EQ(0, value->value.as_array.values[0]->value.as_int32); | 961 EXPECT_EQ(0, value->value.as_array.values[0]->value.as_int32); |
900 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[1]->type); | 962 EXPECT_EQ(Dart_CObject::kString, value->value.as_array.values[1]->type); |
901 EXPECT_STREQ("1", value->value.as_array.values[1]->value.as_string); | 963 EXPECT_STREQ("1", value->value.as_array.values[1]->value.as_string); |
902 EXPECT_EQ(Dart_CObject::kDouble, value->value.as_array.values[2]->type); | 964 EXPECT_EQ(Dart_CObject::kDouble, value->value.as_array.values[2]->type); |
903 EXPECT_EQ(2.2, value->value.as_array.values[2]->value.as_double); | 965 EXPECT_EQ(2.2, value->value.as_array.values[2]->value.as_double); |
904 EXPECT_EQ(Dart_CObject::kBool, value->value.as_array.values[3]->type); | 966 EXPECT_EQ(Dart_CObject::kBool, value->value.as_array.values[3]->type); |
905 EXPECT_EQ(true, value->value.as_array.values[3]->value.as_bool); | 967 EXPECT_EQ(true, value->value.as_array.values[3]->value.as_bool); |
906 | 968 |
907 for (int i = 0; i < kArrayLength; i++) { | 969 for (int i = 0; i < kArrayLength; i++) { |
908 if (i > 3) { | 970 if (i > 3) { |
909 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); | 971 EXPECT_EQ(Dart_CObject::kNull, value->value.as_array.values[i]->type); |
910 } | 972 } |
911 free(value->value.as_array.values[i]); | |
912 } | 973 } |
913 free(value); | |
914 } | 974 } |
915 } | 975 } |
916 Dart_ExitScope(); | 976 Dart_ExitScope(); |
917 Dart_ShutdownIsolate(); | 977 Dart_ShutdownIsolate(); |
918 } | 978 } |
919 | 979 |
920 | 980 |
921 UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) { | 981 UNIT_TEST_CASE(DartGeneratedListMessagesWithBackref) { |
922 const int kArrayLength = 10; | 982 const int kArrayLength = 10; |
923 static const char* kScriptChars = | 983 static const char* kScriptChars = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
955 Dart_EnterScope(); | 1015 Dart_EnterScope(); |
956 | 1016 |
957 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 1017 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
958 EXPECT_VALID(lib); | 1018 EXPECT_VALID(lib); |
959 | 1019 |
960 { | 1020 { |
961 DARTSCOPE_NOCHECKS(isolate); | 1021 DARTSCOPE_NOCHECKS(isolate); |
962 | 1022 |
963 { | 1023 { |
964 // Generate a list of strings from Dart code. | 1024 // Generate a list of strings from Dart code. |
965 Dart_CObject* object = GetDeserializedDartObject(lib, "getStringList"); | 1025 Zone zone(Isolate::Current()); |
| 1026 Dart_CMessage* cmessage = |
| 1027 GetDeserializedDartMessage(lib, "getStringList"); |
| 1028 Dart_CObject* object = cmessage->root; |
966 EXPECT_NOTNULL(object); | 1029 EXPECT_NOTNULL(object); |
967 EXPECT_EQ(Dart_CObject::kArray, object->type); | 1030 EXPECT_EQ(Dart_CObject::kArray, object->type); |
968 EXPECT_EQ(kArrayLength, object->value.as_array.length); | 1031 EXPECT_EQ(kArrayLength, object->value.as_array.length); |
969 for (int i = 0; i < kArrayLength; i++) { | 1032 for (int i = 0; i < kArrayLength; i++) { |
970 Dart_CObject* element = object->value.as_array.values[i]; | 1033 Dart_CObject* element = object->value.as_array.values[i]; |
971 EXPECT_EQ(object->value.as_array.values[0], element); | 1034 EXPECT_EQ(object->value.as_array.values[0], element); |
972 EXPECT_EQ(Dart_CObject::kString, element->type); | 1035 EXPECT_EQ(Dart_CObject::kString, element->type); |
973 EXPECT_STREQ("Hello, world!", element->value.as_string); | 1036 EXPECT_STREQ("Hello, world!", element->value.as_string); |
974 } | 1037 } |
975 free(object->value.as_array.values[0]); | |
976 free(object); | |
977 } | 1038 } |
978 { | 1039 { |
979 // Generate a list of doubles from Dart code. | 1040 // Generate a list of doubles from Dart code. |
980 Dart_CObject* object = GetDeserializedDartObject(lib, "getDoubleList"); | 1041 Zone zone(Isolate::Current()); |
| 1042 Dart_CMessage* cmessage = |
| 1043 GetDeserializedDartMessage(lib, "getDoubleList"); |
| 1044 Dart_CObject* object = cmessage->root; |
981 EXPECT_NOTNULL(object); | 1045 EXPECT_NOTNULL(object); |
982 EXPECT_EQ(Dart_CObject::kArray, object->type); | 1046 EXPECT_EQ(Dart_CObject::kArray, object->type); |
983 EXPECT_EQ(kArrayLength, object->value.as_array.length); | 1047 EXPECT_EQ(kArrayLength, object->value.as_array.length); |
984 for (int i = 0; i < kArrayLength; i++) { | 1048 for (int i = 0; i < kArrayLength; i++) { |
985 Dart_CObject* element = object->value.as_array.values[i]; | 1049 Dart_CObject* element = object->value.as_array.values[i]; |
986 EXPECT_EQ(object->value.as_array.values[0], element); | 1050 EXPECT_EQ(object->value.as_array.values[0], element); |
987 EXPECT_EQ(Dart_CObject::kDouble, element->type); | 1051 EXPECT_EQ(Dart_CObject::kDouble, element->type); |
988 EXPECT_EQ(3.14, element->value.as_double); | 1052 EXPECT_EQ(3.14, element->value.as_double); |
989 } | 1053 } |
990 free(object->value.as_array.values[0]); | |
991 free(object); | |
992 } | 1054 } |
993 { | 1055 { |
994 // Generate a list of objects of different types from Dart code. | 1056 // Generate a list of objects of different types from Dart code. |
995 Dart_CObject* object = GetDeserializedDartObject(lib, "getMixedList"); | 1057 Zone zone(Isolate::Current()); |
| 1058 Dart_CMessage* cmessage = GetDeserializedDartMessage(lib, "getMixedList"); |
| 1059 Dart_CObject* object = cmessage->root; |
996 EXPECT_NOTNULL(object); | 1060 EXPECT_NOTNULL(object); |
997 EXPECT_EQ(Dart_CObject::kArray, object->type); | 1061 EXPECT_EQ(Dart_CObject::kArray, object->type); |
998 EXPECT_EQ(kArrayLength, object->value.as_array.length); | 1062 EXPECT_EQ(kArrayLength, object->value.as_array.length); |
999 for (int i = 0; i < kArrayLength; i++) { | 1063 for (int i = 0; i < kArrayLength; i++) { |
1000 Dart_CObject* element = object->value.as_array.values[i]; | 1064 Dart_CObject* element = object->value.as_array.values[i]; |
1001 if ((i % 2) == 0) { | 1065 if ((i % 2) == 0) { |
1002 EXPECT_EQ(object->value.as_array.values[0], element); | 1066 EXPECT_EQ(object->value.as_array.values[0], element); |
1003 EXPECT_EQ(Dart_CObject::kString, element->type); | 1067 EXPECT_EQ(Dart_CObject::kString, element->type); |
1004 EXPECT_STREQ("A", element->value.as_string); | 1068 EXPECT_STREQ("A", element->value.as_string); |
1005 } else { | 1069 } else { |
1006 EXPECT_EQ(object->value.as_array.values[1], element); | 1070 EXPECT_EQ(object->value.as_array.values[1], element); |
1007 EXPECT_EQ(Dart_CObject::kDouble, element->type); | 1071 EXPECT_EQ(Dart_CObject::kDouble, element->type); |
1008 EXPECT_STREQ(2.72, element->value.as_double); | 1072 EXPECT_STREQ(2.72, element->value.as_double); |
1009 } | 1073 } |
1010 } | 1074 } |
1011 free(object->value.as_array.values[0]); | |
1012 free(object->value.as_array.values[1]); | |
1013 free(object); | |
1014 } | 1075 } |
1015 { | 1076 { |
1016 // Generate a list of objects of different types from Dart code. | 1077 // Generate a list of objects of different types from Dart code. |
1017 Dart_CObject* object = GetDeserializedDartObject(lib, "getSelfRefList"); | 1078 Zone zone(Isolate::Current()); |
| 1079 Dart_CMessage* cmessage = |
| 1080 GetDeserializedDartMessage(lib, "getSelfRefList"); |
| 1081 Dart_CObject* object = cmessage->root; |
1018 EXPECT_NOTNULL(object); | 1082 EXPECT_NOTNULL(object); |
1019 EXPECT_EQ(Dart_CObject::kArray, object->type); | 1083 EXPECT_EQ(Dart_CObject::kArray, object->type); |
1020 EXPECT_EQ(kArrayLength, object->value.as_array.length); | 1084 EXPECT_EQ(kArrayLength, object->value.as_array.length); |
1021 for (int i = 0; i < kArrayLength; i++) { | 1085 for (int i = 0; i < kArrayLength; i++) { |
1022 Dart_CObject* element = object->value.as_array.values[i]; | 1086 Dart_CObject* element = object->value.as_array.values[i]; |
1023 EXPECT_EQ(Dart_CObject::kArray, element->type); | 1087 EXPECT_EQ(Dart_CObject::kArray, element->type); |
1024 EXPECT_EQ(object, element); | 1088 EXPECT_EQ(object, element); |
1025 } | 1089 } |
1026 free(object); | |
1027 } | 1090 } |
1028 } | 1091 } |
1029 Dart_ExitScope(); | 1092 Dart_ExitScope(); |
1030 Dart_ShutdownIsolate(); | 1093 Dart_ShutdownIsolate(); |
1031 } | 1094 } |
1032 | 1095 |
1033 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 1096 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
1034 | 1097 |
1035 } // namespace dart | 1098 } // namespace dart |
OLD | NEW |