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