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" | |
| 9 #include "vm/dart_api_state.h" | |
| 8 #include "vm/snapshot.h" | 10 #include "vm/snapshot.h" |
| 9 #include "vm/unit_test.h" | 11 #include "vm/unit_test.h" |
| 10 | 12 |
| 11 namespace dart { | 13 namespace dart { |
| 12 | 14 |
| 13 // Check if serialized and deserialized objects are equal. | 15 // Check if serialized and deserialized objects are equal. |
| 14 static bool Equals(const Object& expected, const Object& actual) { | 16 static bool Equals(const Object& expected, const Object& actual) { |
| 15 if (expected.IsNull()) { | 17 if (expected.IsNull()) { |
| 16 return actual.IsNull(); | 18 return actual.IsNull(); |
| 17 } | 19 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 48 writer.WriteObject(null_object.raw()); | 50 writer.WriteObject(null_object.raw()); |
| 49 writer.FinalizeBuffer(); | 51 writer.FinalizeBuffer(); |
| 50 | 52 |
| 51 // Create a snapshot object using the buffer. | 53 // Create a snapshot object using the buffer. |
| 52 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 54 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 53 | 55 |
| 54 // Read object back from the snapshot. | 56 // Read object back from the snapshot. |
| 55 SnapshotReader reader(snapshot, Isolate::Current()); | 57 SnapshotReader reader(snapshot, Isolate::Current()); |
| 56 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 58 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 57 EXPECT(Equals(null_object, serialized_object)); | 59 EXPECT(Equals(null_object, serialized_object)); |
| 60 | |
| 61 // Read object back from the snapshot into a C structure. | |
| 62 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 63 writer.BytesWritten(), | |
| 64 &allocator); | |
| 65 Dart_CObject* cobject = mreader.ReadObject(); | |
| 66 EXPECT_EQ(Dart_CObject::kNull, cobject->type); | |
| 67 free(cobject); | |
| 58 } | 68 } |
| 59 | 69 |
| 60 | 70 |
| 61 TEST_CASE(SerializeSmi1) { | 71 TEST_CASE(SerializeSmi1) { |
| 62 // Write snapshot with object content. | 72 // Write snapshot with object content. |
| 63 uint8_t* buffer; | 73 uint8_t* buffer; |
| 64 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 74 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 65 const Smi& smi = Smi::Handle(Smi::New(124)); | 75 const Smi& smi = Smi::Handle(Smi::New(124)); |
| 66 writer.WriteObject(smi.raw()); | 76 writer.WriteObject(smi.raw()); |
| 67 writer.FinalizeBuffer(); | 77 writer.FinalizeBuffer(); |
| 68 | 78 |
| 69 // Create a snapshot object using the buffer. | 79 // Create a snapshot object using the buffer. |
| 70 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 80 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 71 | 81 |
| 72 // Read object back from the snapshot. | 82 // Read object back from the snapshot. |
| 73 SnapshotReader reader(snapshot, Isolate::Current()); | 83 SnapshotReader reader(snapshot, Isolate::Current()); |
| 74 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 84 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 75 EXPECT(Equals(smi, serialized_object)); | 85 EXPECT(Equals(smi, serialized_object)); |
| 86 | |
| 87 // Read object back from the snapshot into a C structure. | |
| 88 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 89 writer.BytesWritten(), | |
| 90 &allocator); | |
| 91 Dart_CObject* cobject = mreader.ReadObject(); | |
| 92 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | |
| 93 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | |
| 94 free(cobject); | |
| 76 } | 95 } |
| 77 | 96 |
| 78 | 97 |
| 79 TEST_CASE(SerializeSmi2) { | 98 TEST_CASE(SerializeSmi2) { |
| 80 // Write snapshot with object content. | 99 // Write snapshot with object content. |
| 81 uint8_t* buffer; | 100 uint8_t* buffer; |
| 82 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 101 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 83 const Smi& smi = Smi::Handle(Smi::New(-1)); | 102 const Smi& smi = Smi::Handle(Smi::New(-1)); |
| 84 writer.WriteObject(smi.raw()); | 103 writer.WriteObject(smi.raw()); |
| 85 writer.FinalizeBuffer(); | 104 writer.FinalizeBuffer(); |
| 86 | 105 |
| 87 // Create a snapshot object using the buffer. | 106 // Create a snapshot object using the buffer. |
| 88 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 107 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 89 | 108 |
| 90 // Read object back from the snapshot. | 109 // Read object back from the snapshot. |
| 91 SnapshotReader reader(snapshot, Isolate::Current()); | 110 SnapshotReader reader(snapshot, Isolate::Current()); |
| 92 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 111 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 93 EXPECT(Equals(smi, serialized_object)); | 112 EXPECT(Equals(smi, serialized_object)); |
| 113 | |
| 114 // Read object back from the snapshot into a C structure. | |
| 115 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 116 writer.BytesWritten(), | |
| 117 &allocator); | |
| 118 Dart_CObject* cobject = mreader.ReadObject(); | |
| 119 EXPECT_EQ(Dart_CObject::kInt32, cobject->type); | |
| 120 EXPECT_EQ(smi.Value(), cobject->value.as_int32); | |
| 121 free(cobject); | |
| 94 } | 122 } |
| 95 | 123 |
| 96 | 124 |
| 97 TEST_CASE(SerializeDouble) { | 125 TEST_CASE(SerializeDouble) { |
| 98 // Write snapshot with object content. | 126 // Write snapshot with object content. |
| 99 uint8_t* buffer; | 127 uint8_t* buffer; |
| 100 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 128 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 101 const Double& dbl = Double::Handle(Double::New(101.29)); | 129 const Double& dbl = Double::Handle(Double::New(101.29)); |
| 102 writer.WriteObject(dbl.raw()); | 130 writer.WriteObject(dbl.raw()); |
| 103 writer.FinalizeBuffer(); | 131 writer.FinalizeBuffer(); |
| 104 | 132 |
| 105 // Create a snapshot object using the buffer. | 133 // Create a snapshot object using the buffer. |
| 106 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 134 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 107 | 135 |
| 108 // Read object back from the snapshot. | 136 // Read object back from the snapshot. |
| 109 SnapshotReader reader(snapshot, Isolate::Current()); | 137 SnapshotReader reader(snapshot, Isolate::Current()); |
| 110 const Object& serialized_object = Object::Handle(reader.ReadObject()); | 138 const Object& serialized_object = Object::Handle(reader.ReadObject()); |
| 111 EXPECT(Equals(dbl, serialized_object)); | 139 EXPECT(Equals(dbl, serialized_object)); |
| 140 | |
| 141 // Read object back from the snapshot into a C structure. | |
| 142 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 143 writer.BytesWritten(), | |
| 144 &allocator); | |
| 145 Dart_CObject* cobject = mreader.ReadObject(); | |
| 146 EXPECT_EQ(Dart_CObject::kDouble, cobject->type); | |
| 147 EXPECT_EQ(dbl.value(), cobject->value.as_double); | |
| 148 free(cobject); | |
| 112 } | 149 } |
| 113 | 150 |
| 114 | 151 |
| 115 TEST_CASE(SerializeBool) { | 152 TEST_CASE(SerializeBool) { |
| 116 // Write snapshot with object content. | 153 // Write snapshot with object content. |
| 117 uint8_t* buffer; | 154 uint8_t* buffer; |
| 118 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 155 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 119 const Bool& bool1 = Bool::Handle(Bool::True()); | 156 const Bool& bool1 = Bool::Handle(Bool::True()); |
| 120 const Bool& bool2 = Bool::Handle(Bool::False()); | 157 const Bool& bool2 = Bool::Handle(Bool::False()); |
| 121 writer.WriteObject(bool1.raw()); | 158 writer.WriteObject(bool1.raw()); |
| 122 writer.WriteObject(bool2.raw()); | 159 writer.WriteObject(bool2.raw()); |
| 123 writer.FinalizeBuffer(); | 160 writer.FinalizeBuffer(); |
| 124 | 161 |
| 125 // Create a snapshot object using the buffer. | 162 // Create a snapshot object using the buffer. |
| 126 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 163 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 127 | 164 |
| 128 // Read object back from the snapshot. | 165 // Read object back from the snapshot. |
| 129 SnapshotReader reader(snapshot, Isolate::Current()); | 166 SnapshotReader reader(snapshot, Isolate::Current()); |
| 130 EXPECT(Bool::True() == reader.ReadObject()); | 167 EXPECT(Bool::True() == reader.ReadObject()); |
| 131 EXPECT(Bool::False() == reader.ReadObject()); | 168 EXPECT(Bool::False() == reader.ReadObject()); |
| 169 | |
| 170 // Read object back from the snapshot into a C structure. | |
| 171 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 172 writer.BytesWritten(), | |
| 173 &allocator); | |
| 174 Dart_CObject* cobject1 = mreader.ReadObject(); | |
| 175 EXPECT_EQ(Dart_CObject::kBool, cobject1->type); | |
| 176 EXPECT_EQ(true, cobject1->value.as_bool); | |
| 177 Dart_CObject* cobject2 = mreader.ReadObject(); | |
| 178 EXPECT_EQ(Dart_CObject::kBool, cobject2->type); | |
| 179 EXPECT_EQ(false, cobject2->value.as_bool); | |
| 180 free(cobject1); | |
| 181 free(cobject2); | |
| 132 } | 182 } |
| 133 | 183 |
| 134 | 184 |
| 135 TEST_CASE(SerializeBigint) { | 185 TEST_CASE(SerializeBigint) { |
| 136 // Write snapshot with object content. | 186 // Write snapshot with object content. |
| 137 uint8_t* buffer; | 187 uint8_t* buffer; |
| 138 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 188 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 139 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); | 189 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); |
| 140 writer.WriteObject(bigint.raw()); | 190 writer.WriteObject(bigint.raw()); |
| 141 writer.FinalizeBuffer(); | 191 writer.FinalizeBuffer(); |
| 142 | 192 |
| 143 // Create a snapshot object using the buffer. | 193 // Create a snapshot object using the buffer. |
| 144 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 194 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 145 | 195 |
| 146 // Read object back from the snapshot. | 196 // Read object back from the snapshot. |
| 147 SnapshotReader reader(snapshot, Isolate::Current()); | 197 SnapshotReader reader(snapshot, Isolate::Current()); |
| 148 Bigint& obj = Bigint::Handle(); | 198 Bigint& obj = Bigint::Handle(); |
| 149 obj ^= reader.ReadObject(); | 199 obj ^= reader.ReadObject(); |
| 150 OS::Print("%lld", BigintOperations::ToInt64(obj)); | 200 OS::Print("%lld", BigintOperations::ToInt64(obj)); |
| 151 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); | 201 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); |
| 202 | |
| 203 // Read object back from the snapshot into a C structure. | |
| 204 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 205 writer.BytesWritten(), | |
| 206 &allocator); | |
| 207 Dart_CObject* cobject = mreader.ReadObject(); | |
| 208 // Bigint not supported. | |
| 209 EXPECT(cobject == NULL); | |
| 152 } | 210 } |
| 153 | 211 |
| 154 | 212 |
| 155 TEST_CASE(SerializeSingletons) { | 213 TEST_CASE(SerializeSingletons) { |
| 156 // Write snapshot with object content. | 214 // Write snapshot with object content. |
| 157 uint8_t* buffer; | 215 uint8_t* buffer; |
| 158 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 216 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 159 writer.WriteObject(Object::class_class()); | 217 writer.WriteObject(Object::class_class()); |
| 160 writer.WriteObject(Object::null_class()); | 218 writer.WriteObject(Object::null_class()); |
| 161 writer.WriteObject(Object::type_class()); | 219 writer.WriteObject(Object::type_class()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); | 259 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); |
| 202 EXPECT(Object::context_class() == reader.ReadObject()); | 260 EXPECT(Object::context_class() == reader.ReadObject()); |
| 203 EXPECT(Object::context_scope_class() == reader.ReadObject()); | 261 EXPECT(Object::context_scope_class() == reader.ReadObject()); |
| 204 } | 262 } |
| 205 | 263 |
| 206 | 264 |
| 207 TEST_CASE(SerializeString) { | 265 TEST_CASE(SerializeString) { |
| 208 // Write snapshot with object content. | 266 // Write snapshot with object content. |
| 209 uint8_t* buffer; | 267 uint8_t* buffer; |
| 210 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 268 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 211 String& str = String::Handle(String::New("This string shall be serialized")); | 269 static const char* cstr = "This string shall be serialized"; |
| 270 String& str = String::Handle(String::New(cstr)); | |
| 212 writer.WriteObject(str.raw()); | 271 writer.WriteObject(str.raw()); |
| 213 writer.FinalizeBuffer(); | 272 writer.FinalizeBuffer(); |
| 214 | 273 |
| 215 // Create a snapshot object using the buffer. | 274 // Create a snapshot object using the buffer. |
| 216 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 275 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 217 | 276 |
| 218 // Read object back from the snapshot. | 277 // Read object back from the snapshot. |
| 219 SnapshotReader reader(snapshot, Isolate::Current()); | 278 SnapshotReader reader(snapshot, Isolate::Current()); |
| 220 String& serialized_str = String::Handle(); | 279 String& serialized_str = String::Handle(); |
| 221 serialized_str ^= reader.ReadObject(); | 280 serialized_str ^= reader.ReadObject(); |
| 222 EXPECT(str.Equals(serialized_str)); | 281 EXPECT(str.Equals(serialized_str)); |
| 282 | |
| 283 // Read object back from the snapshot into a C structure. | |
| 284 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 285 writer.BytesWritten(), | |
| 286 &allocator); | |
| 287 Dart_CObject* cobject = mreader.ReadObject(); | |
| 288 EXPECT_EQ(Dart_CObject::kString, cobject->type); | |
| 289 EXPECT_STREQ(cstr, cobject->value.as_string); | |
| 290 free(cobject); | |
| 223 } | 291 } |
| 224 | 292 |
| 225 | 293 |
| 226 TEST_CASE(SerializeArray) { | 294 TEST_CASE(SerializeArray) { |
| 227 // Write snapshot with object content. | 295 // Write snapshot with object content. |
| 228 uint8_t* buffer; | 296 uint8_t* buffer; |
| 229 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | 297 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); |
| 230 const int kArrayLength = 10; | 298 const int kArrayLength = 10; |
| 231 Array& array = Array::Handle(Array::New(kArrayLength)); | 299 Array& array = Array::Handle(Array::New(kArrayLength)); |
| 232 Smi& smi = Smi::Handle(); | 300 Smi& smi = Smi::Handle(); |
| 233 for (int i = 0; i < kArrayLength; i++) { | 301 for (int i = 0; i < kArrayLength; i++) { |
| 234 smi ^= Smi::New(i); | 302 smi ^= Smi::New(i); |
| 235 array.SetAt(i, smi); | 303 array.SetAt(i, smi); |
| 236 } | 304 } |
| 237 writer.WriteObject(array.raw()); | 305 writer.WriteObject(array.raw()); |
| 238 writer.FinalizeBuffer(); | 306 writer.FinalizeBuffer(); |
| 239 | 307 |
| 240 // Create a snapshot object using the buffer. | 308 // Create a snapshot object using the buffer. |
| 241 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 309 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 242 | 310 |
| 243 // Read object back from the snapshot. | 311 // Read object back from the snapshot. |
| 244 SnapshotReader reader(snapshot, Isolate::Current()); | 312 SnapshotReader reader(snapshot, Isolate::Current()); |
| 245 Array& serialized_array = Array::Handle(); | 313 Array& serialized_array = Array::Handle(); |
| 246 serialized_array ^= reader.ReadObject(); | 314 serialized_array ^= reader.ReadObject(); |
| 247 EXPECT(array.Equals(serialized_array)); | 315 EXPECT(array.Equals(serialized_array)); |
| 316 | |
| 317 // Read object back from the snapshot into a C structure. | |
| 318 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 319 writer.BytesWritten(), | |
| 320 &allocator); | |
| 321 Dart_CObject* cobject = mreader.ReadObject(); | |
| 322 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | |
| 323 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | |
| 324 for (int i = 0; i < kArrayLength; i++) { | |
| 325 Dart_CObject* element = cobject->value.as_array.values[i]; | |
| 326 EXPECT_EQ(Dart_CObject::kInt32, element->type); | |
| 327 EXPECT_EQ(i, element->value.as_int32); | |
| 328 free(element); | |
| 329 } | |
| 330 free(cobject); | |
| 248 } | 331 } |
| 249 | 332 |
| 250 | 333 |
| 334 TEST_CASE(SerializeEmptyArray) { | |
| 335 // Write snapshot with object content. | |
| 336 uint8_t* buffer; | |
| 337 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | |
| 338 const int kArrayLength = 0; | |
| 339 Array& array = Array::Handle(Array::New(kArrayLength)); | |
| 340 writer.WriteObject(array.raw()); | |
| 341 writer.FinalizeBuffer(); | |
| 342 | |
| 343 // Create a snapshot object using the buffer. | |
| 344 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | |
| 345 | |
| 346 // Read object back from the snapshot. | |
| 347 SnapshotReader reader(snapshot, Isolate::Current()); | |
| 348 Array& serialized_array = Array::Handle(); | |
| 349 serialized_array ^= reader.ReadObject(); | |
| 350 EXPECT(array.Equals(serialized_array)); | |
| 351 | |
| 352 // Read object back from the snapshot into a C structure. | |
| 353 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 354 writer.BytesWritten(), | |
| 355 &allocator); | |
| 356 Dart_CObject* cobject = mreader.ReadObject(); | |
| 357 EXPECT_EQ(Dart_CObject::kArray, cobject->type); | |
| 358 EXPECT_EQ(kArrayLength, cobject->value.as_array.length); | |
| 359 EXPECT(cobject->value.as_array.values == NULL); | |
| 360 free(cobject); | |
| 361 } | |
| 362 | |
| 363 | |
| 251 TEST_CASE(SerializeScript) { | 364 TEST_CASE(SerializeScript) { |
| 252 const char* kScriptChars = | 365 const char* kScriptChars = |
| 253 "class A {\n" | 366 "class A {\n" |
| 254 " static bar() { return 42; }\n" | 367 " static bar() { return 42; }\n" |
| 255 " static fly() { return 5; }\n" | 368 " static fly() { return 5; }\n" |
| 256 "}\n"; | 369 "}\n"; |
| 257 | 370 |
| 258 String& url = String::Handle(String::New("dart-test:SerializeScript")); | 371 String& url = String::Handle(String::New("dart-test:SerializeScript")); |
| 259 String& source = String::Handle(String::New(kScriptChars)); | 372 String& source = String::Handle(String::New(kScriptChars)); |
| 260 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); | 373 Script& script = Script::Handle(Script::New(url, source, RawScript::kSource)); |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 517 0, | 630 0, |
| 518 NULL); | 631 NULL); |
| 519 EXPECT_VALID(result); | 632 EXPECT_VALID(result); |
| 520 Dart_ExitScope(); | 633 Dart_ExitScope(); |
| 521 } | 634 } |
| 522 Dart_ShutdownIsolate(); | 635 Dart_ShutdownIsolate(); |
| 523 free(full_snapshot); | 636 free(full_snapshot); |
| 524 free(script_snapshot); | 637 free(script_snapshot); |
| 525 } | 638 } |
| 526 | 639 |
| 640 | |
| 641 TEST_CASE(IntArrayMessage) { | |
| 642 uint8_t* buffer = NULL; | |
| 643 MessageWriter writer(&buffer, &allocator); | |
| 644 | |
| 645 static const int kArrayLength = 2; | |
| 646 intptr_t data[kArrayLength] = {1, 2}; | |
| 647 int len = kArrayLength; | |
| 648 writer.WriteMessage(len, data); | |
| 649 | |
| 650 // Read object back from the snapshot into a C structure. | |
| 651 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 652 writer.BytesWritten(), | |
| 653 &allocator); | |
| 654 Dart_CObject* value = mreader.ReadObject(); | |
| 655 EXPECT_EQ(Dart_CObject::kArray, value->type); | |
| 656 EXPECT_EQ(kArrayLength, value->value.as_array.length); | |
| 657 for (int i = 0; i < kArrayLength; i++) { | |
| 658 Dart_CObject* element = value->value.as_array.values[i]; | |
| 659 EXPECT_EQ(Dart_CObject::kInt32, element->type); | |
| 660 EXPECT_EQ(i + 1, element->value.as_int32); | |
| 661 free(element); | |
| 662 } | |
| 663 free(value); | |
| 664 } | |
| 665 | |
| 666 | |
| 667 // Unit test for creating and deleting persistent handles. | |
|
siva
2012/01/27 18:34:54
The comment for this tests seems off.
Søren Gjesse
2012/01/30 08:26:03
Absolutely, removed.
| |
| 668 UNIT_TEST_CASE(DartGeneratedMessages) { | |
| 669 const int kArrayLength = 10; | |
| 670 static const char* kCustomIsolateScriptChars = | |
| 671 "getSmi() {\n" | |
| 672 " return 42;\n" | |
| 673 "}\n" | |
| 674 "getString() {\n" | |
| 675 " return \"Hello, world!\";\n" | |
| 676 "}\n" | |
| 677 "getList() {\n" | |
| 678 " return [1,2,3,4,5,6,7,8,9,10];\n" | |
| 679 "}\n"; | |
| 680 | |
| 681 TestCase::CreateTestIsolate(); | |
| 682 Isolate* isolate = Isolate::Current(); | |
| 683 EXPECT(isolate != NULL); | |
| 684 Dart_EnterScope(); | |
| 685 | |
| 686 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | |
| 687 NULL); | |
| 688 EXPECT_VALID(lib); | |
| 689 Dart_Handle smi_result; | |
| 690 smi_result = Dart_InvokeStatic(lib, | |
| 691 Dart_NewString(""), | |
| 692 Dart_NewString("getSmi"), | |
| 693 0, | |
| 694 NULL); | |
| 695 EXPECT_VALID(smi_result); | |
| 696 Dart_Handle string_result; | |
| 697 string_result = Dart_InvokeStatic(lib, | |
| 698 Dart_NewString(""), | |
| 699 Dart_NewString("getString"), | |
| 700 0, | |
| 701 NULL); | |
| 702 EXPECT_VALID(string_result); | |
| 703 EXPECT(Dart_IsString(string_result)); | |
| 704 Dart_Handle list_result; | |
| 705 list_result = Dart_InvokeStatic(lib, | |
| 706 Dart_NewString(""), | |
| 707 Dart_NewString("getList"), | |
| 708 0, | |
| 709 NULL); | |
| 710 EXPECT_VALID(list_result); | |
| 711 EXPECT(Dart_IsList(list_result)); | |
| 712 intptr_t result_len = 0; | |
| 713 EXPECT_VALID(Dart_ListLength(list_result, &result_len)); | |
| 714 EXPECT_EQ(kArrayLength, result_len); | |
| 715 { | |
| 716 DARTSCOPE_NOCHECKS(isolate); | |
| 717 | |
| 718 { | |
| 719 uint8_t* buffer; | |
| 720 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | |
| 721 Smi& smi = Smi::Handle(); | |
| 722 smi ^= Api::UnwrapHandle(smi_result); | |
| 723 writer.WriteObject(smi.raw()); | |
| 724 writer.FinalizeBuffer(); | |
| 725 | |
| 726 // Read object back from the snapshot into a C structure. | |
| 727 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 728 writer.BytesWritten(), | |
| 729 &allocator); | |
| 730 Dart_CObject* value = mreader.ReadObject(); | |
| 731 EXPECT_EQ(Dart_CObject::kInt32, value->type); | |
| 732 EXPECT_EQ(42, value->value.as_int32); | |
| 733 free(value); | |
| 734 free(buffer); | |
| 735 } | |
| 736 { | |
| 737 uint8_t* buffer; | |
| 738 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | |
| 739 String& str = String::Handle(); | |
| 740 str ^= Api::UnwrapHandle(string_result); | |
| 741 writer.WriteObject(str.raw()); | |
| 742 writer.FinalizeBuffer(); | |
| 743 | |
| 744 // Read object back from the snapshot into a C structure. | |
| 745 CMessageReader mreader(buffer + Snapshot::kHeaderSize, | |
| 746 writer.BytesWritten(), | |
| 747 &allocator); | |
| 748 Dart_CObject* value = mreader.ReadObject(); | |
| 749 EXPECT_EQ(Dart_CObject::kString, value->type); | |
| 750 EXPECT_STREQ("Hello, world!", value->value.as_string); | |
| 751 free(value); | |
| 752 free(buffer); | |
| 753 } | |
| 754 { | |
| 755 uint8_t* buffer; | |
| 756 SnapshotWriter writer(Snapshot::kMessage, &buffer, &allocator); | |
| 757 const Object& list = Object::Handle(Api::UnwrapHandle(list_result)); | |
| 758 writer.WriteObject(list.raw()); | |
| 759 writer.FinalizeBuffer(); | |
| 760 // TODO(sgjesse): Make this work! | |
| 761 free(buffer); | |
| 762 } | |
| 763 } | |
| 764 Dart_ExitScope(); | |
| 765 Dart_ShutdownIsolate(); | |
| 766 } | |
| 767 | |
| 527 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 768 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
| 528 | 769 |
| 529 } // namespace dart | 770 } // namespace dart |
| OLD | NEW |