OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "include/dart_debugger_api.h" | 5 #include "include/dart_debugger_api.h" |
6 #include "platform/assert.h" | 6 #include "platform/assert.h" |
7 #include "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
10 #include "vm/dart_api_message.h" | 10 #include "vm/dart_api_message.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 break; | 76 break; |
77 case Dart_CObject::kBool: | 77 case Dart_CObject::kBool: |
78 EXPECT_EQ(first->value.as_bool, second->value.as_bool); | 78 EXPECT_EQ(first->value.as_bool, second->value.as_bool); |
79 break; | 79 break; |
80 case Dart_CObject::kInt32: | 80 case Dart_CObject::kInt32: |
81 EXPECT_EQ(first->value.as_int32, second->value.as_int32); | 81 EXPECT_EQ(first->value.as_int32, second->value.as_int32); |
82 break; | 82 break; |
83 case Dart_CObject::kInt64: | 83 case Dart_CObject::kInt64: |
84 EXPECT_EQ(first->value.as_int64, second->value.as_int64); | 84 EXPECT_EQ(first->value.as_int64, second->value.as_int64); |
85 break; | 85 break; |
| 86 case Dart_CObject::kBigint: |
| 87 EXPECT_STREQ(first->value.as_bigint, second->value.as_bigint); |
| 88 break; |
86 case Dart_CObject::kDouble: | 89 case Dart_CObject::kDouble: |
87 EXPECT_EQ(first->value.as_double, second->value.as_double); | 90 EXPECT_EQ(first->value.as_double, second->value.as_double); |
88 break; | 91 break; |
89 case Dart_CObject::kString: | 92 case Dart_CObject::kString: |
90 EXPECT_STREQ(first->value.as_string, second->value.as_string); | 93 EXPECT_STREQ(first->value.as_string, second->value.as_string); |
91 break; | 94 break; |
92 case Dart_CObject::kByteArray: | 95 case Dart_CObject::kByteArray: |
93 EXPECT_EQ(first->value.as_byte_array.length, | 96 EXPECT_EQ(first->value.as_byte_array.length, |
94 second->value.as_byte_array.length); | 97 second->value.as_byte_array.length); |
95 for (int i = 0; i < first->value.as_byte_array.length; i++) { | 98 for (int i = 0; i < first->value.as_byte_array.length; i++) { |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 384 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, |
382 writer.BytesWritten(), | 385 writer.BytesWritten(), |
383 &zone_allocator); | 386 &zone_allocator); |
384 EXPECT_NOTNULL(root); | 387 EXPECT_NOTNULL(root); |
385 EXPECT_EQ(Dart_CObject::kBool, root->type); | 388 EXPECT_EQ(Dart_CObject::kBool, root->type); |
386 EXPECT_EQ(false, root->value.as_bool); | 389 EXPECT_EQ(false, root->value.as_bool); |
387 CheckEncodeDecodeMessage(root); | 390 CheckEncodeDecodeMessage(root); |
388 } | 391 } |
389 | 392 |
390 | 393 |
| 394 static uword allocator(intptr_t size) { |
| 395 return reinterpret_cast<uword>(malloc(size)); |
| 396 } |
| 397 |
| 398 |
391 TEST_CASE(SerializeBigint) { | 399 TEST_CASE(SerializeBigint) { |
392 Zone zone(Isolate::Current()); | 400 Zone zone(Isolate::Current()); |
393 | 401 |
394 // Write snapshot with object content. | 402 // Write snapshot with object content. |
395 uint8_t* buffer; | 403 uint8_t* buffer; |
396 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 404 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
397 const Bigint& bigint = Bigint::Handle(Bigint::New(0xfffffffffLL)); | 405 const Bigint& bigint = Bigint::Handle(Bigint::New(DART_INT64_C(0xfffffffff))); |
398 writer.WriteObject(bigint.raw()); | 406 writer.WriteObject(bigint.raw()); |
399 writer.FinalizeBuffer(); | 407 writer.FinalizeBuffer(); |
400 | 408 |
401 // Create a snapshot object using the buffer. | 409 // Create a snapshot object using the buffer. |
402 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); | 410 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
403 | 411 |
404 // Read object back from the snapshot. | 412 // Read object back from the snapshot. |
405 SnapshotReader reader(snapshot, Isolate::Current()); | 413 SnapshotReader reader(snapshot, Isolate::Current()); |
406 Bigint& obj = Bigint::Handle(); | 414 Bigint& obj = Bigint::Handle(); |
407 obj ^= reader.ReadObject(); | 415 obj ^= reader.ReadObject(); |
408 OS::Print("%lld", BigintOperations::ToInt64(obj)); | 416 |
409 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); | 417 EXPECT_EQ(BigintOperations::ToInt64(bigint), BigintOperations::ToInt64(obj)); |
410 | 418 |
411 // Read object back from the snapshot into a C structure. | 419 // Read object back from the snapshot into a C structure. |
412 ApiNativeScope scope; | 420 ApiNativeScope scope; |
413 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 421 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, |
414 writer.BytesWritten(), | 422 writer.BytesWritten(), |
415 &zone_allocator); | 423 &zone_allocator); |
416 // Bigint not supported. | 424 // Bigint not supported. |
417 EXPECT(root == NULL); | 425 EXPECT_NOTNULL(root); |
| 426 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
| 427 EXPECT_STREQ("FFFFFFFFF", root->value.as_bigint); |
| 428 CheckEncodeDecodeMessage(root); |
418 } | 429 } |
419 | 430 |
420 | 431 |
| 432 Dart_CObject* SerializeAndDeserializeBigint(const Bigint& bigint) { |
| 433 // Write snapshot with object content. |
| 434 uint8_t* buffer; |
| 435 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
| 436 writer.WriteObject(bigint.raw()); |
| 437 writer.FinalizeBuffer(); |
| 438 |
| 439 // Create a snapshot object using the buffer. |
| 440 const Snapshot* snapshot = Snapshot::SetupFromBuffer(buffer); |
| 441 |
| 442 // Read object back from the snapshot. |
| 443 SnapshotReader reader(snapshot, Isolate::Current()); |
| 444 Bigint& serialized_bigint = Bigint::Handle(); |
| 445 serialized_bigint ^= reader.ReadObject(); |
| 446 const char *str1 = BigintOperations::ToDecCString(bigint, allocator); |
| 447 const char *str2 = |
| 448 BigintOperations::ToDecCString(serialized_bigint, allocator); |
| 449 EXPECT_STREQ(str1, str2); |
| 450 free(const_cast<char*>(str1)); |
| 451 free(const_cast<char*>(str2)); |
| 452 |
| 453 // Read object back from the snapshot into a C structure. |
| 454 ApiNativeScope scope; |
| 455 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, |
| 456 writer.BytesWritten(), |
| 457 &zone_allocator); |
| 458 // Bigint not supported. |
| 459 EXPECT_NOTNULL(root); |
| 460 CheckEncodeDecodeMessage(root); |
| 461 return root; |
| 462 } |
| 463 |
| 464 |
| 465 void CheckBigint(const char* bigint_value) { |
| 466 Zone zone(Isolate::Current()); |
| 467 |
| 468 Bigint& bigint = Bigint::Handle(); |
| 469 bigint ^= BigintOperations::NewFromCString(bigint_value); |
| 470 Dart_CObject* bigint_cobject = SerializeAndDeserializeBigint(bigint); |
| 471 EXPECT_EQ(Dart_CObject::kBigint, bigint_cobject->type); |
| 472 if (bigint_value[0] == '0') { |
| 473 EXPECT_STREQ(bigint_value + 2, bigint_cobject->value.as_bigint); |
| 474 } else { |
| 475 EXPECT_EQ('-', bigint_value[0]); |
| 476 EXPECT_EQ('-', bigint_cobject->value.as_bigint[0]); |
| 477 EXPECT_STREQ(bigint_value + 3, bigint_cobject->value.as_bigint + 1); |
| 478 } |
| 479 } |
| 480 |
| 481 |
| 482 TEST_CASE(SerializeBigint2) { |
| 483 CheckBigint("0x0"); |
| 484 CheckBigint("0x1"); |
| 485 CheckBigint("-0x1"); |
| 486 CheckBigint("0x11111111111111111111"); |
| 487 CheckBigint("-0x11111111111111111111"); |
| 488 CheckBigint("0x9876543210987654321098765432109876543210"); |
| 489 CheckBigint("-0x9876543210987654321098765432109876543210"); |
| 490 } |
| 491 |
| 492 |
421 TEST_CASE(SerializeSingletons) { | 493 TEST_CASE(SerializeSingletons) { |
422 // Write snapshot with object content. | 494 // Write snapshot with object content. |
423 uint8_t* buffer; | 495 uint8_t* buffer; |
424 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); | 496 SnapshotWriter writer(Snapshot::kMessage, &buffer, &malloc_allocator); |
425 writer.WriteObject(Object::class_class()); | 497 writer.WriteObject(Object::class_class()); |
426 writer.WriteObject(Object::null_class()); | 498 writer.WriteObject(Object::null_class()); |
427 writer.WriteObject(Object::type_class()); | 499 writer.WriteObject(Object::type_class()); |
428 writer.WriteObject(Object::type_parameter_class()); | 500 writer.WriteObject(Object::type_parameter_class()); |
429 writer.WriteObject(Object::instantiated_type_class()); | 501 writer.WriteObject(Object::instantiated_type_class()); |
430 writer.WriteObject(Object::abstract_type_arguments_class()); | 502 writer.WriteObject(Object::abstract_type_arguments_class()); |
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1004 writer.BytesWritten(), | 1076 writer.BytesWritten(), |
1005 &zone_allocator); | 1077 &zone_allocator); |
1006 } | 1078 } |
1007 | 1079 |
1008 | 1080 |
1009 UNIT_TEST_CASE(DartGeneratedMessages) { | 1081 UNIT_TEST_CASE(DartGeneratedMessages) { |
1010 static const char* kCustomIsolateScriptChars = | 1082 static const char* kCustomIsolateScriptChars = |
1011 "getSmi() {\n" | 1083 "getSmi() {\n" |
1012 " return 42;\n" | 1084 " return 42;\n" |
1013 "}\n" | 1085 "}\n" |
| 1086 "getBigint() {\n" |
| 1087 " return -0x424242424242424242424242424242424242;\n" |
| 1088 "}\n" |
1014 "getString() {\n" | 1089 "getString() {\n" |
1015 " return \"Hello, world!\";\n" | 1090 " return \"Hello, world!\";\n" |
1016 "}\n" | 1091 "}\n" |
1017 "getList() {\n" | 1092 "getList() {\n" |
1018 " return new List(kArrayLength);\n" | 1093 " return new List(kArrayLength);\n" |
1019 "}\n"; | 1094 "}\n"; |
1020 | 1095 |
1021 TestCase::CreateTestIsolate(); | 1096 TestCase::CreateTestIsolate(); |
1022 Isolate* isolate = Isolate::Current(); | 1097 Isolate* isolate = Isolate::Current(); |
1023 EXPECT(isolate != NULL); | 1098 EXPECT(isolate != NULL); |
1024 Dart_EnterScope(); | 1099 Dart_EnterScope(); |
1025 | 1100 |
1026 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 1101 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
1027 NULL); | 1102 NULL); |
1028 EXPECT_VALID(lib); | 1103 EXPECT_VALID(lib); |
1029 Dart_Handle smi_result; | 1104 Dart_Handle smi_result; |
1030 smi_result = Dart_InvokeStatic(lib, | 1105 smi_result = Dart_InvokeStatic(lib, |
1031 Dart_NewString(""), | 1106 Dart_NewString(""), |
1032 Dart_NewString("getSmi"), | 1107 Dart_NewString("getSmi"), |
1033 0, | 1108 0, |
1034 NULL); | 1109 NULL); |
1035 EXPECT_VALID(smi_result); | 1110 EXPECT_VALID(smi_result); |
| 1111 Dart_Handle bigint_result; |
| 1112 bigint_result = Dart_InvokeStatic(lib, |
| 1113 Dart_NewString(""), |
| 1114 Dart_NewString("getBigint"), |
| 1115 0, |
| 1116 NULL); |
| 1117 EXPECT_VALID(bigint_result); |
1036 Dart_Handle string_result; | 1118 Dart_Handle string_result; |
1037 string_result = Dart_InvokeStatic(lib, | 1119 string_result = Dart_InvokeStatic(lib, |
1038 Dart_NewString(""), | 1120 Dart_NewString(""), |
1039 Dart_NewString("getString"), | 1121 Dart_NewString("getString"), |
1040 0, | 1122 0, |
1041 NULL); | 1123 NULL); |
1042 EXPECT_VALID(string_result); | 1124 EXPECT_VALID(string_result); |
1043 EXPECT(Dart_IsString(string_result)); | 1125 EXPECT(Dart_IsString(string_result)); |
1044 | 1126 |
1045 { | 1127 { |
(...skipping 15 matching lines...) Expand all Loading... |
1061 &zone_allocator); | 1143 &zone_allocator); |
1062 EXPECT_NOTNULL(root); | 1144 EXPECT_NOTNULL(root); |
1063 EXPECT_EQ(Dart_CObject::kInt32, root->type); | 1145 EXPECT_EQ(Dart_CObject::kInt32, root->type); |
1064 EXPECT_EQ(42, root->value.as_int32); | 1146 EXPECT_EQ(42, root->value.as_int32); |
1065 CheckEncodeDecodeMessage(root); | 1147 CheckEncodeDecodeMessage(root); |
1066 } | 1148 } |
1067 { | 1149 { |
1068 Zone zone(Isolate::Current()); | 1150 Zone zone(Isolate::Current()); |
1069 uint8_t* buffer; | 1151 uint8_t* buffer; |
1070 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); | 1152 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
| 1153 Bigint& bigint = Bigint::Handle(); |
| 1154 bigint ^= Api::UnwrapHandle(bigint_result); |
| 1155 writer.WriteObject(bigint.raw()); |
| 1156 writer.FinalizeBuffer(); |
| 1157 |
| 1158 // Read object back from the snapshot into a C structure. |
| 1159 ApiNativeScope scope; |
| 1160 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, |
| 1161 writer.BytesWritten(), |
| 1162 &zone_allocator); |
| 1163 EXPECT_NOTNULL(root); |
| 1164 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
| 1165 EXPECT_STREQ("-424242424242424242424242424242424242", |
| 1166 root->value.as_bigint); |
| 1167 CheckEncodeDecodeMessage(root); |
| 1168 } |
| 1169 { |
| 1170 Zone zone(Isolate::Current()); |
| 1171 uint8_t* buffer; |
| 1172 SnapshotWriter writer(Snapshot::kMessage, &buffer, &zone_allocator); |
1071 String& str = String::Handle(); | 1173 String& str = String::Handle(); |
1072 str ^= Api::UnwrapHandle(string_result); | 1174 str ^= Api::UnwrapHandle(string_result); |
1073 writer.WriteObject(str.raw()); | 1175 writer.WriteObject(str.raw()); |
1074 writer.FinalizeBuffer(); | 1176 writer.FinalizeBuffer(); |
1075 | 1177 |
1076 // Read object back from the snapshot into a C structure. | 1178 // Read object back from the snapshot into a C structure. |
1077 ApiNativeScope scope; | 1179 ApiNativeScope scope; |
1078 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, | 1180 Dart_CObject* root = DecodeMessage(buffer + Snapshot::kHeaderSize, |
1079 writer.BytesWritten(), | 1181 writer.BytesWritten(), |
1080 &zone_allocator); | 1182 &zone_allocator); |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1390 EXPECT(Dart_ErrorHasException(result)); | 1492 EXPECT(Dart_ErrorHasException(result)); |
1391 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", | 1493 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]\n", |
1392 Dart_GetError(result)); | 1494 Dart_GetError(result)); |
1393 | 1495 |
1394 Dart_ExitScope(); | 1496 Dart_ExitScope(); |
1395 } | 1497 } |
1396 | 1498 |
1397 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 1499 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
1398 | 1500 |
1399 } // namespace dart | 1501 } // namespace dart |
OLD | NEW |