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 1239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1250 const Object& list = Object::Handle(Api::UnwrapHandle(result)); | 1250 const Object& list = Object::Handle(Api::UnwrapHandle(result)); |
1251 writer.WriteMessage(list); | 1251 writer.WriteMessage(list); |
1252 intptr_t buffer_len = writer.BytesWritten(); | 1252 intptr_t buffer_len = writer.BytesWritten(); |
1253 | 1253 |
1254 // Read object back from the snapshot into a C structure. | 1254 // Read object back from the snapshot into a C structure. |
1255 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1255 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1256 return api_reader.ReadMessage(); | 1256 return api_reader.ReadMessage(); |
1257 } | 1257 } |
1258 | 1258 |
1259 | 1259 |
| 1260 static void CheckString(Dart_Handle string, const char* expected) { |
| 1261 StackZone zone(Isolate::Current()); |
| 1262 uint8_t* buffer; |
| 1263 MessageWriter writer(&buffer, &zone_allocator); |
| 1264 String& str = String::Handle(); |
| 1265 str ^= Api::UnwrapHandle(string); |
| 1266 writer.WriteMessage(str); |
| 1267 intptr_t buffer_len = writer.BytesWritten(); |
| 1268 |
| 1269 // Read object back from the snapshot into a C structure. |
| 1270 ApiNativeScope scope; |
| 1271 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 1272 Dart_CObject* root = api_reader.ReadMessage(); |
| 1273 EXPECT_NOTNULL(root); |
| 1274 EXPECT_EQ(Dart_CObject::kString, root->type); |
| 1275 EXPECT_STREQ(expected, root->value.as_string); |
| 1276 CheckEncodeDecodeMessage(root); |
| 1277 } |
| 1278 |
| 1279 |
| 1280 static void CheckStringInvalid(Dart_Handle string) { |
| 1281 StackZone zone(Isolate::Current()); |
| 1282 uint8_t* buffer; |
| 1283 MessageWriter writer(&buffer, &zone_allocator); |
| 1284 String& str = String::Handle(); |
| 1285 str ^= Api::UnwrapHandle(string); |
| 1286 writer.WriteMessage(str); |
| 1287 intptr_t buffer_len = writer.BytesWritten(); |
| 1288 |
| 1289 // Read object back from the snapshot into a C structure. |
| 1290 ApiNativeScope scope; |
| 1291 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
| 1292 Dart_CObject* root = api_reader.ReadMessage(); |
| 1293 EXPECT_NOTNULL(root); |
| 1294 EXPECT_EQ(Dart_CObject::kUnsupported, root->type); |
| 1295 } |
| 1296 |
| 1297 |
1260 UNIT_TEST_CASE(DartGeneratedMessages) { | 1298 UNIT_TEST_CASE(DartGeneratedMessages) { |
1261 static const char* kCustomIsolateScriptChars = | 1299 static const char* kCustomIsolateScriptChars = |
1262 "getSmi() {\n" | 1300 "getSmi() {\n" |
1263 " return 42;\n" | 1301 " return 42;\n" |
1264 "}\n" | 1302 "}\n" |
1265 "getBigint() {\n" | 1303 "getBigint() {\n" |
1266 " return -0x424242424242424242424242424242424242;\n" | 1304 " return -0x424242424242424242424242424242424242;\n" |
1267 "}\n" | 1305 "}\n" |
1268 "getAsciiString() {\n" | 1306 "getAsciiString() {\n" |
1269 " return \"Hello, world!\";\n" | 1307 " return \"Hello, world!\";\n" |
1270 "}\n" | 1308 "}\n" |
1271 "getNonAsciiString() {\n" | 1309 "getNonAsciiString() {\n" |
1272 " return \"Blåbærgrød\";\n" | 1310 " return \"Blåbærgrød\";\n" |
1273 "}\n" | 1311 "}\n" |
| 1312 "getNonBMPString() {\n" |
| 1313 " return \"\\u{10000}\\u{1F601}\\u{1F637}\\u{20000}\";\n" |
| 1314 "}\n" |
| 1315 "getLeadSurrogateString() {\n" |
| 1316 " return new String.fromCharCodes([0xd800]);\n" |
| 1317 "}\n" |
| 1318 "getTrailSurrogateString() {\n" |
| 1319 " return \"\\u{10000}\".substring(1);\n" |
| 1320 "}\n" |
| 1321 "getSurrogatesString() {\n" |
| 1322 " return new String.fromCharCodes([0xdc00, 0xdc00, 0xd800, 0xd800]);\n" |
| 1323 "}\n" |
| 1324 "getCrappyString() {\n" |
| 1325 " return new String.fromCharCodes([0xd800, 32, 0xdc00, 32]);\n" |
| 1326 "}\n" |
1274 "getList() {\n" | 1327 "getList() {\n" |
1275 " return new List(kArrayLength);\n" | 1328 " return new List(kArrayLength);\n" |
1276 "}\n"; | 1329 "}\n"; |
1277 | 1330 |
1278 TestCase::CreateTestIsolate(); | 1331 TestCase::CreateTestIsolate(); |
1279 Isolate* isolate = Isolate::Current(); | 1332 Isolate* isolate = Isolate::Current(); |
1280 EXPECT(isolate != NULL); | 1333 EXPECT(isolate != NULL); |
1281 Dart_EnterScope(); | 1334 Dart_EnterScope(); |
1282 | 1335 |
1283 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, | 1336 Dart_Handle lib = TestCase::LoadTestScript(kCustomIsolateScriptChars, |
1284 NULL); | 1337 NULL); |
1285 EXPECT_VALID(lib); | 1338 EXPECT_VALID(lib); |
1286 Dart_Handle smi_result; | 1339 Dart_Handle smi_result; |
1287 smi_result = Dart_Invoke(lib, NewString("getSmi"), 0, NULL); | 1340 smi_result = Dart_Invoke(lib, NewString("getSmi"), 0, NULL); |
1288 EXPECT_VALID(smi_result); | 1341 EXPECT_VALID(smi_result); |
1289 Dart_Handle bigint_result; | 1342 Dart_Handle bigint_result; |
1290 bigint_result = Dart_Invoke(lib, NewString("getBigint"), 0, NULL); | 1343 bigint_result = Dart_Invoke(lib, NewString("getBigint"), 0, NULL); |
1291 EXPECT_VALID(bigint_result); | 1344 EXPECT_VALID(bigint_result); |
| 1345 |
1292 Dart_Handle ascii_string_result; | 1346 Dart_Handle ascii_string_result; |
1293 ascii_string_result = Dart_Invoke(lib, NewString("getAsciiString"), 0, NULL); | 1347 ascii_string_result = Dart_Invoke(lib, NewString("getAsciiString"), 0, NULL); |
1294 EXPECT_VALID(ascii_string_result); | 1348 EXPECT_VALID(ascii_string_result); |
1295 EXPECT(Dart_IsString(ascii_string_result)); | 1349 EXPECT(Dart_IsString(ascii_string_result)); |
| 1350 |
1296 Dart_Handle non_ascii_string_result; | 1351 Dart_Handle non_ascii_string_result; |
1297 non_ascii_string_result = | 1352 non_ascii_string_result = |
1298 Dart_Invoke(lib, NewString("getNonAsciiString"), 0, NULL); | 1353 Dart_Invoke(lib, NewString("getNonAsciiString"), 0, NULL); |
1299 EXPECT_VALID(non_ascii_string_result); | 1354 EXPECT_VALID(non_ascii_string_result); |
1300 EXPECT(Dart_IsString(non_ascii_string_result)); | 1355 EXPECT(Dart_IsString(non_ascii_string_result)); |
1301 | 1356 |
| 1357 Dart_Handle non_bmp_string_result; |
| 1358 non_bmp_string_result = |
| 1359 Dart_Invoke(lib, NewString("getNonBMPString"), 0, NULL); |
| 1360 EXPECT_VALID(non_bmp_string_result); |
| 1361 EXPECT(Dart_IsString(non_bmp_string_result)); |
| 1362 |
| 1363 Dart_Handle lead_surrogate_string_result; |
| 1364 lead_surrogate_string_result = |
| 1365 Dart_Invoke(lib, NewString("getLeadSurrogateString"), 0, NULL); |
| 1366 EXPECT_VALID(lead_surrogate_string_result); |
| 1367 EXPECT(Dart_IsString(lead_surrogate_string_result)); |
| 1368 |
| 1369 Dart_Handle trail_surrogate_string_result; |
| 1370 trail_surrogate_string_result = |
| 1371 Dart_Invoke(lib, NewString("getTrailSurrogateString"), 0, NULL); |
| 1372 EXPECT_VALID(trail_surrogate_string_result); |
| 1373 EXPECT(Dart_IsString(trail_surrogate_string_result)); |
| 1374 |
| 1375 Dart_Handle surrogates_string_result; |
| 1376 surrogates_string_result = |
| 1377 Dart_Invoke(lib, NewString("getSurrogatesString"), 0, NULL); |
| 1378 EXPECT_VALID(surrogates_string_result); |
| 1379 EXPECT(Dart_IsString(surrogates_string_result)); |
| 1380 |
| 1381 Dart_Handle crappy_string_result; |
| 1382 crappy_string_result = |
| 1383 Dart_Invoke(lib, NewString("getCrappyString"), 0, NULL); |
| 1384 EXPECT_VALID(crappy_string_result); |
| 1385 EXPECT(Dart_IsString(crappy_string_result)); |
| 1386 |
1302 { | 1387 { |
1303 DARTSCOPE_NOCHECKS(isolate); | 1388 DARTSCOPE_NOCHECKS(isolate); |
1304 | 1389 |
1305 { | 1390 { |
1306 StackZone zone(Isolate::Current()); | 1391 StackZone zone(Isolate::Current()); |
1307 uint8_t* buffer; | 1392 uint8_t* buffer; |
1308 MessageWriter writer(&buffer, &zone_allocator); | 1393 MessageWriter writer(&buffer, &zone_allocator); |
1309 Smi& smi = Smi::Handle(); | 1394 Smi& smi = Smi::Handle(); |
1310 smi ^= Api::UnwrapHandle(smi_result); | 1395 smi ^= Api::UnwrapHandle(smi_result); |
1311 writer.WriteMessage(smi); | 1396 writer.WriteMessage(smi); |
(...skipping 20 matching lines...) Expand all Loading... |
1332 // Read object back from the snapshot into a C structure. | 1417 // Read object back from the snapshot into a C structure. |
1333 ApiNativeScope scope; | 1418 ApiNativeScope scope; |
1334 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | 1419 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); |
1335 Dart_CObject* root = api_reader.ReadMessage(); | 1420 Dart_CObject* root = api_reader.ReadMessage(); |
1336 EXPECT_NOTNULL(root); | 1421 EXPECT_NOTNULL(root); |
1337 EXPECT_EQ(Dart_CObject::kBigint, root->type); | 1422 EXPECT_EQ(Dart_CObject::kBigint, root->type); |
1338 EXPECT_STREQ("-424242424242424242424242424242424242", | 1423 EXPECT_STREQ("-424242424242424242424242424242424242", |
1339 root->value.as_bigint); | 1424 root->value.as_bigint); |
1340 CheckEncodeDecodeMessage(root); | 1425 CheckEncodeDecodeMessage(root); |
1341 } | 1426 } |
1342 { | 1427 CheckString(ascii_string_result, "Hello, world!"); |
1343 StackZone zone(Isolate::Current()); | 1428 CheckString(non_ascii_string_result, "Blåbærgrød"); |
1344 uint8_t* buffer; | 1429 CheckString(non_bmp_string_result, |
1345 MessageWriter writer(&buffer, &zone_allocator); | 1430 "\xf0\x90\x80\x80" |
1346 String& str = String::Handle(); | 1431 "\xf0\x9f\x98\x81" |
1347 str ^= Api::UnwrapHandle(ascii_string_result); | 1432 "\xf0\x9f\x98\xb7" |
1348 writer.WriteMessage(str); | 1433 "\xf0\xa0\x80\x80"); |
1349 intptr_t buffer_len = writer.BytesWritten(); | 1434 CheckStringInvalid(lead_surrogate_string_result); |
1350 | 1435 CheckStringInvalid(trail_surrogate_string_result); |
1351 // Read object back from the snapshot into a C structure. | 1436 CheckStringInvalid(crappy_string_result); |
1352 ApiNativeScope scope; | 1437 CheckStringInvalid(surrogates_string_result); |
1353 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | |
1354 Dart_CObject* root = api_reader.ReadMessage(); | |
1355 EXPECT_NOTNULL(root); | |
1356 EXPECT_EQ(Dart_CObject::kString, root->type); | |
1357 EXPECT_STREQ("Hello, world!", root->value.as_string); | |
1358 CheckEncodeDecodeMessage(root); | |
1359 } | |
1360 { | |
1361 StackZone zone(Isolate::Current()); | |
1362 uint8_t* buffer; | |
1363 MessageWriter writer(&buffer, &zone_allocator); | |
1364 String& str = String::Handle(); | |
1365 str ^= Api::UnwrapHandle(non_ascii_string_result); | |
1366 writer.WriteMessage(str); | |
1367 intptr_t buffer_len = writer.BytesWritten(); | |
1368 | |
1369 // Read object back from the snapshot into a C structure. | |
1370 ApiNativeScope scope; | |
1371 ApiMessageReader api_reader(buffer, buffer_len, &zone_allocator); | |
1372 Dart_CObject* root = api_reader.ReadMessage(); | |
1373 EXPECT_NOTNULL(root); | |
1374 EXPECT_EQ(Dart_CObject::kString, root->type); | |
1375 EXPECT_STREQ("Blåbærgrød", root->value.as_string); | |
1376 CheckEncodeDecodeMessage(root); | |
1377 } | |
1378 } | 1438 } |
1379 Dart_ExitScope(); | 1439 Dart_ExitScope(); |
1380 Dart_ShutdownIsolate(); | 1440 Dart_ShutdownIsolate(); |
1381 } | 1441 } |
1382 | 1442 |
1383 | 1443 |
1384 UNIT_TEST_CASE(DartGeneratedListMessages) { | 1444 UNIT_TEST_CASE(DartGeneratedListMessages) { |
1385 const int kArrayLength = 10; | 1445 const int kArrayLength = 10; |
1386 static const char* kScriptChars = | 1446 static const char* kScriptChars = |
1387 "final int kArrayLength = 10;\n" | 1447 "final int kArrayLength = 10;\n" |
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2063 " }\n" | 2123 " }\n" |
2064 " }\n" | 2124 " }\n" |
2065 " messageCount++;\n" | 2125 " messageCount++;\n" |
2066 " if (messageCount == 9) throw new Exception(exception);\n" | 2126 " if (messageCount == 9) throw new Exception(exception);\n" |
2067 " });\n" | 2127 " });\n" |
2068 " return port.toSendPort();\n" | 2128 " return port.toSendPort();\n" |
2069 "}\n"; | 2129 "}\n"; |
2070 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 2130 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
2071 Dart_EnterScope(); | 2131 Dart_EnterScope(); |
2072 | 2132 |
2073 // xxx | |
2074 Dart_Handle send_port = Dart_Invoke(lib, NewString("main"), 0, NULL); | 2133 Dart_Handle send_port = Dart_Invoke(lib, NewString("main"), 0, NULL); |
2075 EXPECT_VALID(send_port); | 2134 EXPECT_VALID(send_port); |
2076 Dart_Handle result = Dart_GetField(send_port, NewString("_id")); | 2135 Dart_Handle result = Dart_GetField(send_port, NewString("_id")); |
2077 ASSERT(!Dart_IsError(result)); | 2136 ASSERT(!Dart_IsError(result)); |
2078 ASSERT(Dart_IsInteger(result)); | 2137 ASSERT(Dart_IsInteger(result)); |
2079 int64_t send_port_id; | 2138 int64_t send_port_id; |
2080 Dart_Handle result2 = Dart_IntegerToInt64(result, &send_port_id); | 2139 Dart_Handle result2 = Dart_IntegerToInt64(result, &send_port_id); |
2081 ASSERT(!Dart_IsError(result2)); | 2140 ASSERT(!Dart_IsError(result2)); |
2082 | 2141 |
2083 // Setup single object message. | 2142 // Setup single object message. |
(...skipping 15 matching lines...) Expand all Loading... |
2099 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2158 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2100 | 2159 |
2101 object.type = Dart_CObject::kString; | 2160 object.type = Dart_CObject::kString; |
2102 object.value.as_string = const_cast<char*>("456"); | 2161 object.value.as_string = const_cast<char*>("456"); |
2103 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2162 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2104 | 2163 |
2105 object.type = Dart_CObject::kString; | 2164 object.type = Dart_CObject::kString; |
2106 object.value.as_string = const_cast<char*>("æøå"); | 2165 object.value.as_string = const_cast<char*>("æøå"); |
2107 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2166 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2108 | 2167 |
2109 // Try to post an invalid UTF-8 sequence (lead surrogate). | |
2110 const char* data = "\xED\xA0\x80"; // U+D800 | |
2111 object.type = Dart_CObject::kString; | |
2112 object.value.as_string = const_cast<char*>(data); | |
2113 EXPECT(!Dart_PostCObject(send_port_id, &object)); | |
2114 | |
2115 object.type = Dart_CObject::kDouble; | 2168 object.type = Dart_CObject::kDouble; |
2116 object.value.as_double = 3.14; | 2169 object.value.as_double = 3.14; |
2117 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2170 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2118 | 2171 |
2119 object.type = Dart_CObject::kArray; | 2172 object.type = Dart_CObject::kArray; |
2120 object.value.as_array.length = 0; | 2173 object.value.as_array.length = 0; |
2121 EXPECT(Dart_PostCObject(send_port_id, &object)); | 2174 EXPECT(Dart_PostCObject(send_port_id, &object)); |
2122 | 2175 |
2123 static const int kArrayLength = 10; | 2176 static const int kArrayLength = 10; |
2124 Dart_CObject* array = | 2177 Dart_CObject* array = |
(...skipping 19 matching lines...) Expand all Loading... |
2144 EXPECT(Dart_ErrorHasException(result)); | 2197 EXPECT(Dart_ErrorHasException(result)); |
2145 EXPECT_SUBSTRING("Exception: nulltruefalse123456æøå3.14[]100123456789\n", | 2198 EXPECT_SUBSTRING("Exception: nulltruefalse123456æøå3.14[]100123456789\n", |
2146 Dart_GetError(result)); | 2199 Dart_GetError(result)); |
2147 | 2200 |
2148 Dart_ExitScope(); | 2201 Dart_ExitScope(); |
2149 } | 2202 } |
2150 | 2203 |
2151 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). | 2204 #endif // defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64). |
2152 | 2205 |
2153 } // namespace dart | 2206 } // namespace dart |
OLD | NEW |