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" |
11 #include "vm/dart_api_state.h" | 11 #include "vm/dart_api_state.h" |
12 #include "vm/snapshot.h" | 12 #include "vm/snapshot.h" |
13 #include "vm/symbols.h" | 13 #include "vm/symbols.h" |
14 #include "vm/unicode.h" | |
14 #include "vm/unit_test.h" | 15 #include "vm/unit_test.h" |
15 | 16 |
16 namespace dart { | 17 namespace dart { |
17 | 18 |
18 // Check if serialized and deserialized objects are equal. | 19 // Check if serialized and deserialized objects are equal. |
19 static bool Equals(const Object& expected, const Object& actual) { | 20 static bool Equals(const Object& expected, const Object& actual) { |
20 if (expected.IsNull()) { | 21 if (expected.IsNull()) { |
21 return actual.IsNull(); | 22 return actual.IsNull(); |
22 } | 23 } |
23 if (expected.IsSmi()) { | 24 if (expected.IsSmi()) { |
(...skipping 466 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 EXPECT(Object::instructions_class() == reader.ReadObject()); | 491 EXPECT(Object::instructions_class() == reader.ReadObject()); |
491 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); | 492 EXPECT(Object::pc_descriptors_class() == reader.ReadObject()); |
492 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); | 493 EXPECT(Object::exception_handlers_class() == reader.ReadObject()); |
493 EXPECT(Object::context_class() == reader.ReadObject()); | 494 EXPECT(Object::context_class() == reader.ReadObject()); |
494 EXPECT(Object::context_scope_class() == reader.ReadObject()); | 495 EXPECT(Object::context_scope_class() == reader.ReadObject()); |
495 | 496 |
496 free(buffer); | 497 free(buffer); |
497 } | 498 } |
498 | 499 |
499 | 500 |
500 TEST_CASE(SerializeString) { | 501 static void TestString(const char* cstr) { |
501 StackZone zone(Isolate::Current()); | 502 StackZone zone(Isolate::Current()); |
502 | 503 |
503 // Write snapshot with object content. | 504 // Write snapshot with object content. |
504 uint8_t* buffer; | 505 uint8_t* buffer; |
505 MessageWriter writer(&buffer, &zone_allocator); | 506 MessageWriter writer(&buffer, &zone_allocator); |
506 static const char* cstr = "This string shall be serialized"; | |
507 String& str = String::Handle(String::New(cstr)); | 507 String& str = String::Handle(String::New(cstr)); |
508 writer.WriteMessage(str); | 508 writer.WriteMessage(str); |
509 intptr_t buffer_len = writer.BytesWritten(); | 509 intptr_t buffer_len = writer.BytesWritten(); |
510 | 510 |
511 // Read object back from the snapshot. | 511 // Read object back from the snapshot. |
512 SnapshotReader reader(buffer, buffer_len, | 512 SnapshotReader reader(buffer, buffer_len, |
513 Snapshot::kMessage, Isolate::Current()); | 513 Snapshot::kMessage, Isolate::Current()); |
514 String& serialized_str = String::Handle(); | 514 String& serialized_str = String::Handle(); |
515 serialized_str ^= reader.ReadObject(); | 515 serialized_str ^= reader.ReadObject(); |
516 EXPECT(str.Equals(serialized_str)); | 516 EXPECT(str.Equals(serialized_str)); |
517 | 517 |
518 // Read object back from the snapshot into a C structure. | 518 // Read object back from the snapshot into a C structure. |
519 ApiNativeScope scope; | 519 ApiNativeScope scope; |
520 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 520 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
521 Dart_CObject* root = api_reader.ReadMessage(); | 521 Dart_CObject* root = api_reader.ReadMessage(); |
522 EXPECT_EQ(Dart_CObject::kString, root->type); | 522 EXPECT_EQ(Dart_CObject::kString, root->type); |
523 EXPECT_STREQ(cstr, root->value.as_string); | 523 EXPECT_STREQ(cstr, root->value.as_string); |
524 CheckEncodeDecodeMessage(root); | 524 CheckEncodeDecodeMessage(root); |
525 } | 525 } |
526 | 526 |
527 | 527 |
528 TEST_CASE(SerializeString) { | |
529 TestString("This string shall be serialized"); | |
530 TestString("æøå"); // This file is UTF-8 encoded. | |
531 char data[] = {0x01, | |
532 0x7f, | |
533 0xc2, 0x80, // 0x80 | |
534 0xdf, 0xbf, // 0x7ff | |
535 0xe0, 0xa0, 0x80, // 0x800 | |
536 0xef, 0xbf, 0xbf}; // 0xffff | |
537 TestString(data); | |
538 // TODO(sgjesse): Add tests with non-BMP characters. | |
erikcorry
2012/11/12 11:44:07
You could add tests for isolated surrogates like 0
Søren Gjesse
2012/11/12 16:18:10
I can't do that. If I create a message with an val
| |
539 } | |
540 | |
541 | |
528 TEST_CASE(SerializeArray) { | 542 TEST_CASE(SerializeArray) { |
529 StackZone zone(Isolate::Current()); | 543 StackZone zone(Isolate::Current()); |
530 | 544 |
531 // Write snapshot with object content. | 545 // Write snapshot with object content. |
532 uint8_t* buffer; | 546 uint8_t* buffer; |
533 MessageWriter writer(&buffer, &zone_allocator); | 547 MessageWriter writer(&buffer, &zone_allocator); |
534 const int kArrayLength = 10; | 548 const int kArrayLength = 10; |
535 Array& array = Array::Handle(Array::New(kArrayLength)); | 549 Array& array = Array::Handle(Array::New(kArrayLength)); |
536 Smi& smi = Smi::Handle(); | 550 Smi& smi = Smi::Handle(); |
537 for (int i = 0; i < kArrayLength; i++) { | 551 for (int i = 0; i < kArrayLength; i++) { |
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1248 | 1262 |
1249 | 1263 |
1250 UNIT_TEST_CASE(DartGeneratedMessages) { | 1264 UNIT_TEST_CASE(DartGeneratedMessages) { |
1251 static const char* kCustomIsolateScriptChars = | 1265 static const char* kCustomIsolateScriptChars = |
1252 "getSmi() {\n" | 1266 "getSmi() {\n" |
1253 " return 42;\n" | 1267 " return 42;\n" |
1254 "}\n" | 1268 "}\n" |
1255 "getBigint() {\n" | 1269 "getBigint() {\n" |
1256 " return -0x424242424242424242424242424242424242;\n" | 1270 " return -0x424242424242424242424242424242424242;\n" |
1257 "}\n" | 1271 "}\n" |
1258 "getString() {\n" | 1272 "getAsciiString() {\n" |
1259 " return \"Hello, world!\";\n" | 1273 " return \"Hello, world!\";\n" |
1260 "}\n" | 1274 "}\n" |
1275 "getNonAsciiString() {\n" | |
1276 " return \"Blåbærgrød\";\n" | |
1277 "}\n" | |
1261 "getList() {\n" | 1278 "getList() {\n" |
1262 " return new List(kArrayLength);\n" | 1279 " return new List(kArrayLength);\n" |
1263 "}\n"; | 1280 "}\n"; |
1264 | 1281 |
1265 TestCase::CreateTestIsolate(); | 1282 TestCase::CreateTestIsolate(); |
1266 Isolate* isolate = Isolate::Current(); | 1283 Isolate* isolate = Isolate::Current(); |
1267 EXPECT(isolate != NULL); | 1284 EXPECT(isolate != NULL); |
1268 Dart_EnterScope(); | 1285 Dart_EnterScope(); |
1269 | 1286 |
1270 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 1287 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
1271 NULL); | 1288 NULL); |
1272 EXPECT_VALID(lib); | 1289 EXPECT_VALID(lib); |
1273 Dart_Handle smi_result; | 1290 Dart_Handle smi_result; |
1274 smi_result = Dart_Invoke(lib, NewString("getSmi"), 0, NULL); | 1291 smi_result = Dart_Invoke(lib, NewString("getSmi"), 0, NULL); |
1275 EXPECT_VALID(smi_result); | 1292 EXPECT_VALID(smi_result); |
1276 Dart_Handle bigint_result; | 1293 Dart_Handle bigint_result; |
1277 bigint_result = Dart_Invoke(lib, NewString("getBigint"), 0, NULL); | 1294 bigint_result = Dart_Invoke(lib, NewString("getBigint"), 0, NULL); |
1278 EXPECT_VALID(bigint_result); | 1295 EXPECT_VALID(bigint_result); |
1279 Dart_Handle string_result; | 1296 Dart_Handle ascii_string_result; |
1280 string_result = Dart_Invoke(lib, NewString("getString"), 0, NULL); | 1297 ascii_string_result = Dart_Invoke(lib, NewString("getAsciiString"), 0, NULL); |
1281 EXPECT_VALID(string_result); | 1298 EXPECT_VALID(ascii_string_result); |
1282 EXPECT(Dart_IsString(string_result)); | 1299 EXPECT(Dart_IsString(ascii_string_result)); |
1300 Dart_Handle non_ascii_string_result; | |
1301 non_ascii_string_result = | |
1302 Dart_Invoke(lib, NewString("getNonAsciiString"), 0, NULL); | |
1303 EXPECT_VALID(non_ascii_string_result); | |
1304 EXPECT(Dart_IsString(non_ascii_string_result)); | |
1283 | 1305 |
1284 { | 1306 { |
1285 DARTSCOPE_NOCHECKS(isolate); | 1307 DARTSCOPE_NOCHECKS(isolate); |
1286 | 1308 |
1287 { | 1309 { |
1288 StackZone zone(Isolate::Current()); | 1310 StackZone zone(Isolate::Current()); |
1289 uint8_t* buffer; | 1311 uint8_t* buffer; |
1290 MessageWriter writer(&buffer, &zone_allocator); | 1312 MessageWriter writer(&buffer, &zone_allocator); |
1291 Smi& smi = Smi::Handle(); | 1313 Smi& smi = Smi::Handle(); |
1292 smi ^= Api::UnwrapHandle(smi_result); | 1314 smi ^= Api::UnwrapHandle(smi_result); |
(...skipping 26 matching lines...) Expand all Loading... | |
1319 EXPECT_EQ(Dart_CObject::kBigint, root->type); | 1341 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
1320 EXPECT_STREQ("-424242424242424242424242424242424242", | 1342 EXPECT_STREQ("-424242424242424242424242424242424242", |
1321 root->value.as_bigint); | 1343 root->value.as_bigint); |
1322 CheckEncodeDecodeMessage(root); | 1344 CheckEncodeDecodeMessage(root); |
1323 } | 1345 } |
1324 { | 1346 { |
1325 StackZone zone(Isolate::Current()); | 1347 StackZone zone(Isolate::Current()); |
1326 uint8_t* buffer; | 1348 uint8_t* buffer; |
1327 MessageWriter writer(&buffer, &zone_allocator); | 1349 MessageWriter writer(&buffer, &zone_allocator); |
1328 String& str = String::Handle(); | 1350 String& str = String::Handle(); |
1329 str ^= Api::UnwrapHandle(string_result); | 1351 str ^= Api::UnwrapHandle(ascii_string_result); |
1330 writer.WriteMessage(str); | 1352 writer.WriteMessage(str); |
1331 intptr_t buffer_len = writer.BytesWritten(); | 1353 intptr_t buffer_len = writer.BytesWritten(); |
1332 | 1354 |
1333 // Read object back from the snapshot into a C structure. | 1355 // Read object back from the snapshot into a C structure. |
1334 ApiNativeScope scope; | 1356 ApiNativeScope scope; |
1335 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1357 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1336 Dart_CObject* root = api_reader.ReadMessage(); | 1358 Dart_CObject* root = api_reader.ReadMessage(); |
1337 EXPECT_NOTNULL(root); | 1359 EXPECT_NOTNULL(root); |
1338 EXPECT_EQ(Dart_CObject::kString, root->type); | 1360 EXPECT_EQ(Dart_CObject::kString, root->type); |
1339 EXPECT_STREQ("Hello, world!", root->value.as_string); | 1361 EXPECT_STREQ("Hello, world!", root->value.as_string); |
1340 CheckEncodeDecodeMessage(root); | 1362 CheckEncodeDecodeMessage(root); |
1341 } | 1363 } |
1364 { | |
1365 StackZone zone(Isolate::Current()); | |
1366 uint8_t* buffer; | |
1367 MessageWriter writer(&buffer, &zone_allocator); | |
1368 String& str = String::Handle(); | |
1369 str ^= Api::UnwrapHandle(non_ascii_string_result); | |
1370 writer.WriteMessage(str); | |
1371 intptr_t buffer_len = writer.BytesWritten(); | |
1372 | |
1373 // Read object back from the snapshot into a C structure. | |
1374 ApiNativeScope scope; | |
1375 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | |
1376 Dart_CObject* root = api_reader.ReadMessage(); | |
1377 EXPECT_NOTNULL(root); | |
1378 EXPECT_EQ(Dart_CObject::kString, root->type); | |
1379 EXPECT_STREQ("Blåbærgrød", root->value.as_string); | |
1380 CheckEncodeDecodeMessage(root); | |
1381 } | |
1342 } | 1382 } |
1343 Dart_ExitScope(); | 1383 Dart_ExitScope(); |
1344 Dart_ShutdownIsolate(); | 1384 Dart_ShutdownIsolate(); |
1345 } | 1385 } |
1346 | 1386 |
1347 | 1387 |
1348 UNIT_TEST_CASE(DartGeneratedListMessages) { | 1388 UNIT_TEST_CASE(DartGeneratedListMessages) { |
1349 const int kArrayLength = 10; | 1389 const int kArrayLength = 10; |
1350 static const char* kScriptChars = | 1390 static const char* kScriptChars = |
1351 "final int kArrayLength = 10;\n" | 1391 "final int kArrayLength = 10;\n" |
(...skipping 659 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2011 UNIT_TEST_CASE(PostCObject) { | 2051 UNIT_TEST_CASE(PostCObject) { |
2012 // Create a native port for posting from C to Dart | 2052 // Create a native port for posting from C to Dart |
2013 TestIsolateScope __test_isolate__; | 2053 TestIsolateScope __test_isolate__; |
2014 const char* kScriptChars = | 2054 const char* kScriptChars = |
2015 "#import('dart:isolate');\n" | 2055 "#import('dart:isolate');\n" |
2016 "main() {\n" | 2056 "main() {\n" |
2017 " var messageCount = 0;\n" | 2057 " var messageCount = 0;\n" |
2018 " var exception = '';\n" | 2058 " var exception = '';\n" |
2019 " var port = new ReceivePort();\n" | 2059 " var port = new ReceivePort();\n" |
2020 " port.receive((message, replyTo) {\n" | 2060 " port.receive((message, replyTo) {\n" |
2021 " if (messageCount < 7) {\n" | 2061 " if (messageCount < 8) {\n" |
2022 " exception = '$exception${message}';\n" | 2062 " exception = '$exception${message}';\n" |
2023 " } else {\n" | 2063 " } else {\n" |
2024 " exception = '$exception${message.length}';\n" | 2064 " exception = '$exception${message.length}';\n" |
2025 " for (int i = 0; i < message.length; i++) {\n" | 2065 " for (int i = 0; i < message.length; i++) {\n" |
2026 " exception = '$exception${message[i]}';\n" | 2066 " exception = '$exception${message[i]}';\n" |
2027 " }\n" | 2067 " }\n" |
2028 " }\n" | 2068 " }\n" |
2029 " messageCount++;\n" | 2069 " messageCount++;\n" |
2030 " if (messageCount == 8) throw new Exception(exception);\n" | 2070 " if (messageCount == 9) throw new Exception(exception);\n" |
2031 " });\n" | 2071 " });\n" |
2032 " return port.toSendPort();\n" | 2072 " return port.toSendPort();\n" |
2033 "}\n"; | 2073 "}\n"; |
2034 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 2074 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
2035 Dart_EnterScope(); | 2075 Dart_EnterScope(); |
2036 | 2076 |
2037 // xxx | 2077 // xxx |
2038 Dart_Handle send_port = Dart_Invoke(lib, NewString("main"), 0, NULL); | 2078 Dart_Handle send_port = Dart_Invoke(lib, NewString("main"), 0, NULL); |
2039 EXPECT_VALID(send_port); | 2079 EXPECT_VALID(send_port); |
2040 Dart_Handle result = Dart_GetField(send_port, NewString("_id")); | 2080 Dart_Handle result = Dart_GetField(send_port, NewString("_id")); |
(...skipping 18 matching lines...) Expand all Loading... | |
2059 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2099 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2060 | 2100 |
2061 object.type = Dart_CObject::kInt32; | 2101 object.type = Dart_CObject::kInt32; |
2062 object.value.as_int32 = 123; | 2102 object.value.as_int32 = 123; |
2063 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2103 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2064 | 2104 |
2065 object.type = Dart_CObject::kString; | 2105 object.type = Dart_CObject::kString; |
2066 object.value.as_string = const_cast<char*>("456"); | 2106 object.value.as_string = const_cast<char*>("456"); |
2067 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2107 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2068 | 2108 |
2109 object.type = Dart_CObject::kString; | |
2110 object.value.as_string = const_cast<char*>("æøå"); | |
2111 EXPECT(Dart_PostCObject(send_port_id, &object)); | |
2112 | |
2069 object.type = Dart_CObject::kDouble; | 2113 object.type = Dart_CObject::kDouble; |
2070 object.value.as_double = 3.14; | 2114 object.value.as_double = 3.14; |
2071 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2115 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2072 | 2116 |
2073 object.type = Dart_CObject::kArray; | 2117 object.type = Dart_CObject::kArray; |
2074 object.value.as_array.length = 0; | 2118 object.value.as_array.length = 0; |
2075 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2119 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2076 | 2120 |
2077 static const int kArrayLength = 10; | 2121 static const int kArrayLength = 10; |
2078 Dart_CObject* array = | 2122 Dart_CObject* array = |
(...skipping 10 matching lines...) Expand all Loading... | |
2089 Dart_ScopeAllocate(sizeof(Dart_CObject))); | 2133 Dart_ScopeAllocate(sizeof(Dart_CObject))); |
2090 element->type = Dart_CObject::kInt32; | 2134 element->type = Dart_CObject::kInt32; |
2091 element->value.as_int32 = i; | 2135 element->value.as_int32 = i; |
2092 array->value.as_array.values[i] = element; | 2136 array->value.as_array.values[i] = element; |
2093 } | 2137 } |
2094 EXPECT(Dart_PostCObject(send_port_id, array)); | 2138 EXPECT(Dart_PostCObject(send_port_id, array)); |
2095 | 2139 |
2096 result = Dart_RunLoop(); | 2140 result = Dart_RunLoop(); |
2097 EXPECT(Dart_IsError(result)); | 2141 EXPECT(Dart_IsError(result)); |
2098 EXPECT(Dart_ErrorHasException(result)); | 2142 EXPECT(Dart_ErrorHasException(result)); |
2099 EXPECT_SUBSTRING("Exception: nulltruefalse1234563.14[]100123456789\n", | 2143 EXPECT_SUBSTRING("Exception: nulltruefalse123456æøå3.14[]100123456789\n", |
2100 Dart_GetError(result)); | 2144 Dart_GetError(result)); |
2101 | 2145 |
2102 Dart_ExitScope(); | 2146 Dart_ExitScope(); |
2103 } | 2147 } |
2104 | 2148 |
2105 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 2149 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
2106 | 2150 |
2107 } // namespace dart | 2151 } // namespace dart |
OLD | NEW |