Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: runtime/vm/snapshot_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698