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

Side by Side Diff: test/cctest/test-api.cc

Issue 1148653007: Update UTF-8 decoder to detect more special cases. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: updates Created 5 years, 7 months 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
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 7189 matching lines...) Expand 10 before | Expand all | Expand 10 after
7200 Local<v8::String> string = 7200 Local<v8::String> string =
7201 Local<v8::String>::Cast(a->Get(i)); 7201 Local<v8::String>::Cast(a->Get(i));
7202 Local<v8::Number> expected_len = 7202 Local<v8::Number> expected_len =
7203 Local<v8::Number>::Cast(alens->Get(i)); 7203 Local<v8::Number>::Cast(alens->Get(i));
7204 int length = GetUtf8Length(string); 7204 int length = GetUtf8Length(string);
7205 CHECK_EQ(static_cast<int>(expected_len->Value()), length); 7205 CHECK_EQ(static_cast<int>(expected_len->Value()), length);
7206 } 7206 }
7207 } 7207 }
7208 7208
7209 7209
7210 static uint16_t StringGet(Handle<String> str, int index) {
7211 i::Handle<i::String> istring =
7212 v8::Utils::OpenHandle(String::Cast(*str));
7213 return istring->Get(index);
7214 }
7215
7216
7217 static void WriteUtf8Helper(
7218 LocalContext& context, // NOLINT
7219 const char* name,
7220 const char* lengths_name,
7221 int len) {
7222 Local<v8::Array> b =
7223 Local<v8::Array>::Cast(context->Global()->Get(v8_str(name)));
7224 Local<v8::Array> alens =
7225 Local<v8::Array>::Cast(context->Global()->Get(v8_str(lengths_name)));
7226 char buffer[1000];
7227 char buffer2[1000];
7228 for (int i = 0; i < len; i++) {
7229 Local<v8::String> string =
7230 Local<v8::String>::Cast(b->Get(i));
7231 Local<v8::Number> expected_len =
7232 Local<v8::Number>::Cast(alens->Get(i));
7233 int utf8_length = static_cast<int>(expected_len->Value());
7234 for (int j = utf8_length + 1; j >= 0; j--) {
7235 memset(reinterpret_cast<void*>(&buffer), 42, sizeof(buffer));
7236 memset(reinterpret_cast<void*>(&buffer2), 42, sizeof(buffer2));
7237 int nchars;
7238 int utf8_written =
7239 string->WriteUtf8(buffer, j, &nchars, String::NO_OPTIONS);
7240 int utf8_written2 =
7241 string->WriteUtf8(buffer2, j, &nchars, String::NO_NULL_TERMINATION);
7242 CHECK_GE(utf8_length + 1, utf8_written);
7243 CHECK_GE(utf8_length, utf8_written2);
7244 for (int k = 0; k < utf8_written2; k++) {
7245 CHECK_EQ(buffer[k], buffer2[k]);
7246 }
7247 CHECK(nchars * 3 >= utf8_written - 1);
7248 CHECK(nchars <= utf8_written);
7249 if (j == utf8_length + 1) {
7250 CHECK_EQ(utf8_written2, utf8_length);
7251 CHECK_EQ(utf8_written2 + 1, utf8_written);
7252 }
7253 CHECK_EQ(buffer[utf8_written], 42);
7254 if (j > utf8_length) {
7255 if (utf8_written != 0) CHECK_EQ(buffer[utf8_written - 1], 0);
7256 if (utf8_written > 1) CHECK_NE(buffer[utf8_written - 2], 42);
7257 Handle<String> roundtrip = v8_str(buffer);
7258 CHECK(roundtrip->Equals(string));
7259 } else {
7260 if (utf8_written != 0) CHECK_NE(buffer[utf8_written - 1], 42);
7261 }
7262 if (utf8_written2 != 0) CHECK_NE(buffer[utf8_written - 1], 42);
7263 if (nchars >= 2) {
7264 uint16_t trail = StringGet(string, nchars - 1);
7265 uint16_t lead = StringGet(string, nchars - 2);
7266 if (((lead & 0xfc00) == 0xd800) &&
7267 ((trail & 0xfc00) == 0xdc00)) {
7268 unsigned u1 = buffer2[utf8_written2 - 4];
7269 unsigned u2 = buffer2[utf8_written2 - 3];
7270 unsigned u3 = buffer2[utf8_written2 - 2];
7271 unsigned u4 = buffer2[utf8_written2 - 1];
7272 CHECK_EQ((u1 & 0xf8), 0xf0u);
7273 CHECK_EQ((u2 & 0xc0), 0x80u);
7274 CHECK_EQ((u3 & 0xc0), 0x80u);
7275 CHECK_EQ((u4 & 0xc0), 0x80u);
7276 uint32_t c = 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff);
7277 CHECK_EQ((u4 & 0x3f), (c & 0x3f));
7278 CHECK_EQ((u3 & 0x3f), ((c >> 6) & 0x3f));
7279 CHECK_EQ((u2 & 0x3f), ((c >> 12) & 0x3f));
7280 CHECK_EQ((u1 & 0x3), c >> 18);
7281 }
7282 }
7283 }
7284 }
7285 }
7286
7287
7288 THREADED_TEST(Utf16) { 7210 THREADED_TEST(Utf16) {
7289 LocalContext context; 7211 LocalContext context;
7290 v8::HandleScope scope(context->GetIsolate()); 7212 v8::HandleScope scope(context->GetIsolate());
7291 CompileRun( 7213 CompileRun(
7292 "var pad = '01234567890123456789';" 7214 "var pad = '01234567890123456789';"
7293 "var p = [];" 7215 "var p = [];"
7294 "var plens = [20, 3, 3];" 7216 "var plens = [20, 3, 3];"
7295 "p.push('01234567890123456789');" 7217 "p.push('01234567890123456789');"
7296 "var lead = 0xd800;" 7218 "var lead = 0xd800;"
7297 "var trail = 0xdc00;" 7219 "var trail = 0xdc00;"
(...skipping 26 matching lines...) Expand all
7324 " c2.push(newc.substring(1, newc.length - 1));" 7246 " c2.push(newc.substring(1, newc.length - 1));"
7325 " var utf = alens[m] + alens[n];" // And here. 7247 " var utf = alens[m] + alens[n];" // And here.
7326 // The 'n's that start with 0xdc.. are 6-8 7248 // The 'n's that start with 0xdc.. are 6-8
7327 // The 'm's that end with 0xd8.. are 1, 4 and 7 7249 // The 'm's that end with 0xd8.. are 1, 4 and 7
7328 " if ((m % 3) == 1 && n >= 6) utf -= 2;" 7250 " if ((m % 3) == 1 && n >= 6) utf -= 2;"
7329 " a2lens.push(utf);" 7251 " a2lens.push(utf);"
7330 " }" 7252 " }"
7331 "}"); 7253 "}");
7332 Utf16Helper(context, "a", "alens", 9); 7254 Utf16Helper(context, "a", "alens", 9);
7333 Utf16Helper(context, "a2", "a2lens", 81); 7255 Utf16Helper(context, "a2", "a2lens", 81);
7334 WriteUtf8Helper(context, "b", "alens", 9);
jochen (gone - plz use gerrit) 2015/05/22 12:38:12 these tests were testing what happens when convert
7335 WriteUtf8Helper(context, "b2", "a2lens", 81);
7336 WriteUtf8Helper(context, "c2", "a2lens", 81);
7337 } 7256 }
7338 7257
7339 7258
7340 static bool SameSymbol(Handle<String> s1, Handle<String> s2) { 7259 static bool SameSymbol(Handle<String> s1, Handle<String> s2) {
7341 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1)); 7260 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1));
7342 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2)); 7261 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2));
7343 return *is1 == *is2; 7262 return *is1 == *is2;
7344 } 7263 }
7345 7264
7346 static void SameSymbolHelper(v8::Isolate* isolate, const char* a,
7347 const char* b) {
7348 Handle<String> symbol1 =
7349 v8::String::NewFromUtf8(isolate, a, v8::String::kInternalizedString);
7350 Handle<String> symbol2 =
7351 v8::String::NewFromUtf8(isolate, b, v8::String::kInternalizedString);
7352 CHECK(SameSymbol(symbol1, symbol2));
7353 }
7354
7355 7265
7356 THREADED_TEST(Utf16Symbol) { 7266 THREADED_TEST(Utf16Symbol) {
7357 LocalContext context; 7267 LocalContext context;
7358 v8::HandleScope scope(context->GetIsolate()); 7268 v8::HandleScope scope(context->GetIsolate());
7359 7269
7360 Handle<String> symbol1 = v8::String::NewFromUtf8( 7270 Handle<String> symbol1 = v8::String::NewFromUtf8(
7361 context->GetIsolate(), "abc", v8::String::kInternalizedString); 7271 context->GetIsolate(), "abc", v8::String::kInternalizedString);
7362 Handle<String> symbol2 = v8::String::NewFromUtf8( 7272 Handle<String> symbol2 = v8::String::NewFromUtf8(
7363 context->GetIsolate(), "abc", v8::String::kInternalizedString); 7273 context->GetIsolate(), "abc", v8::String::kInternalizedString);
7364 CHECK(SameSymbol(symbol1, symbol2)); 7274 CHECK(SameSymbol(symbol1, symbol2));
7365 7275
7366 SameSymbolHelper(context->GetIsolate(),
7367 "\360\220\220\205", // 4 byte encoding.
7368 "\355\240\201\355\260\205"); // 2 3-byte surrogates.
jochen (gone - plz use gerrit) 2015/05/22 12:38:12 same here. if you convert the test to utf-16, it'
7369 SameSymbolHelper(context->GetIsolate(),
7370 "\355\240\201\355\260\206", // 2 3-byte surrogates.
7371 "\360\220\220\206"); // 4 byte encoding.
7372 SameSymbolHelper(context->GetIsolate(),
7373 "x\360\220\220\205", // 4 byte encoding.
7374 "x\355\240\201\355\260\205"); // 2 3-byte surrogates.
7375 SameSymbolHelper(context->GetIsolate(),
7376 "x\355\240\201\355\260\206", // 2 3-byte surrogates.
7377 "x\360\220\220\206"); // 4 byte encoding.
7378 CompileRun( 7276 CompileRun(
7379 "var sym0 = 'benedictus';" 7277 "var sym0 = 'benedictus';"
7380 "var sym0b = 'S\303\270ren';" 7278 "var sym0b = 'S\303\270ren';"
7381 "var sym1 = '\355\240\201\355\260\207';" 7279 "var sym1 = '\355\240\201\355\260\207';"
7382 "var sym2 = '\360\220\220\210';" 7280 "var sym2 = '\360\220\220\210';"
7383 "var sym3 = 'x\355\240\201\355\260\207';" 7281 "var sym3 = 'x\355\240\201\355\260\207';"
7384 "var sym4 = 'x\360\220\220\210';" 7282 "var sym4 = 'x\360\220\220\210';"
7385 "if (sym1.length != 2) throw sym1;" 7283 "if (sym1.length != 2) throw sym1;"
7386 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);" 7284 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);"
7387 "if (sym2.length != 2) throw sym2;" 7285 "if (sym2.length != 2) throw sym2;"
(...skipping 13729 matching lines...) Expand 10 before | Expand all | Expand 10 after
21117 // add the testExtraShouldReturnFive export 21015 // add the testExtraShouldReturnFive export
21118 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); 21016 v8::Local<v8::Object> exports = env->GetExtrasExportsObject();
21119 21017
21120 auto func = 21018 auto func =
21121 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); 21019 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>();
21122 auto undefined = v8::Undefined(isolate); 21020 auto undefined = v8::Undefined(isolate);
21123 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); 21021 auto result = func->Call(undefined, 0, {}).As<v8::Number>();
21124 21022
21125 CHECK(result->Value() == 5.0); 21023 CHECK(result->Value() == 5.0);
21126 } 21024 }
OLDNEW
« src/unicode.cc ('K') | « src/unicode.cc ('k') | test/cctest/test-parsing.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698