OLD | NEW |
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 7108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7119 Local<v8::String> string = | 7119 Local<v8::String> string = |
7120 Local<v8::String>::Cast(a->Get(i)); | 7120 Local<v8::String>::Cast(a->Get(i)); |
7121 Local<v8::Number> expected_len = | 7121 Local<v8::Number> expected_len = |
7122 Local<v8::Number>::Cast(alens->Get(i)); | 7122 Local<v8::Number>::Cast(alens->Get(i)); |
7123 int length = GetUtf8Length(string); | 7123 int length = GetUtf8Length(string); |
7124 CHECK_EQ(static_cast<int>(expected_len->Value()), length); | 7124 CHECK_EQ(static_cast<int>(expected_len->Value()), length); |
7125 } | 7125 } |
7126 } | 7126 } |
7127 | 7127 |
7128 | 7128 |
7129 static uint16_t StringGet(Handle<String> str, int index) { | |
7130 i::Handle<i::String> istring = | |
7131 v8::Utils::OpenHandle(String::Cast(*str)); | |
7132 return istring->Get(index); | |
7133 } | |
7134 | |
7135 | |
7136 static void WriteUtf8Helper( | |
7137 LocalContext& context, // NOLINT | |
7138 const char* name, | |
7139 const char* lengths_name, | |
7140 int len) { | |
7141 Local<v8::Array> b = | |
7142 Local<v8::Array>::Cast(context->Global()->Get(v8_str(name))); | |
7143 Local<v8::Array> alens = | |
7144 Local<v8::Array>::Cast(context->Global()->Get(v8_str(lengths_name))); | |
7145 char buffer[1000]; | |
7146 char buffer2[1000]; | |
7147 for (int i = 0; i < len; i++) { | |
7148 Local<v8::String> string = | |
7149 Local<v8::String>::Cast(b->Get(i)); | |
7150 Local<v8::Number> expected_len = | |
7151 Local<v8::Number>::Cast(alens->Get(i)); | |
7152 int utf8_length = static_cast<int>(expected_len->Value()); | |
7153 for (int j = utf8_length + 1; j >= 0; j--) { | |
7154 memset(reinterpret_cast<void*>(&buffer), 42, sizeof(buffer)); | |
7155 memset(reinterpret_cast<void*>(&buffer2), 42, sizeof(buffer2)); | |
7156 int nchars; | |
7157 int utf8_written = | |
7158 string->WriteUtf8(buffer, j, &nchars, String::NO_OPTIONS); | |
7159 int utf8_written2 = | |
7160 string->WriteUtf8(buffer2, j, &nchars, String::NO_NULL_TERMINATION); | |
7161 CHECK_GE(utf8_length + 1, utf8_written); | |
7162 CHECK_GE(utf8_length, utf8_written2); | |
7163 for (int k = 0; k < utf8_written2; k++) { | |
7164 CHECK_EQ(buffer[k], buffer2[k]); | |
7165 } | |
7166 CHECK(nchars * 3 >= utf8_written - 1); | |
7167 CHECK(nchars <= utf8_written); | |
7168 if (j == utf8_length + 1) { | |
7169 CHECK_EQ(utf8_written2, utf8_length); | |
7170 CHECK_EQ(utf8_written2 + 1, utf8_written); | |
7171 } | |
7172 CHECK_EQ(buffer[utf8_written], 42); | |
7173 if (j > utf8_length) { | |
7174 if (utf8_written != 0) CHECK_EQ(buffer[utf8_written - 1], 0); | |
7175 if (utf8_written > 1) CHECK_NE(buffer[utf8_written - 2], 42); | |
7176 Handle<String> roundtrip = v8_str(buffer); | |
7177 CHECK(roundtrip->Equals(string)); | |
7178 } else { | |
7179 if (utf8_written != 0) CHECK_NE(buffer[utf8_written - 1], 42); | |
7180 } | |
7181 if (utf8_written2 != 0) CHECK_NE(buffer[utf8_written - 1], 42); | |
7182 if (nchars >= 2) { | |
7183 uint16_t trail = StringGet(string, nchars - 1); | |
7184 uint16_t lead = StringGet(string, nchars - 2); | |
7185 if (((lead & 0xfc00) == 0xd800) && | |
7186 ((trail & 0xfc00) == 0xdc00)) { | |
7187 unsigned u1 = buffer2[utf8_written2 - 4]; | |
7188 unsigned u2 = buffer2[utf8_written2 - 3]; | |
7189 unsigned u3 = buffer2[utf8_written2 - 2]; | |
7190 unsigned u4 = buffer2[utf8_written2 - 1]; | |
7191 CHECK_EQ((u1 & 0xf8), 0xf0u); | |
7192 CHECK_EQ((u2 & 0xc0), 0x80u); | |
7193 CHECK_EQ((u3 & 0xc0), 0x80u); | |
7194 CHECK_EQ((u4 & 0xc0), 0x80u); | |
7195 uint32_t c = 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff); | |
7196 CHECK_EQ((u4 & 0x3f), (c & 0x3f)); | |
7197 CHECK_EQ((u3 & 0x3f), ((c >> 6) & 0x3f)); | |
7198 CHECK_EQ((u2 & 0x3f), ((c >> 12) & 0x3f)); | |
7199 CHECK_EQ((u1 & 0x3), c >> 18); | |
7200 } | |
7201 } | |
7202 } | |
7203 } | |
7204 } | |
7205 | |
7206 | |
7207 THREADED_TEST(Utf16) { | 7129 THREADED_TEST(Utf16) { |
7208 LocalContext context; | 7130 LocalContext context; |
7209 v8::HandleScope scope(context->GetIsolate()); | 7131 v8::HandleScope scope(context->GetIsolate()); |
7210 CompileRun( | 7132 CompileRun( |
7211 "var pad = '01234567890123456789';" | 7133 "var pad = '01234567890123456789';" |
7212 "var p = [];" | 7134 "var p = [];" |
7213 "var plens = [20, 3, 3];" | 7135 "var plens = [20, 3, 3];" |
7214 "p.push('01234567890123456789');" | 7136 "p.push('01234567890123456789');" |
7215 "var lead = 0xd800;" | 7137 "var lead = 0xd800;" |
7216 "var trail = 0xdc00;" | 7138 "var trail = 0xdc00;" |
(...skipping 26 matching lines...) Expand all Loading... |
7243 " c2.push(newc.substring(1, newc.length - 1));" | 7165 " c2.push(newc.substring(1, newc.length - 1));" |
7244 " var utf = alens[m] + alens[n];" // And here. | 7166 " var utf = alens[m] + alens[n];" // And here. |
7245 // The 'n's that start with 0xdc.. are 6-8 | 7167 // The 'n's that start with 0xdc.. are 6-8 |
7246 // The 'm's that end with 0xd8.. are 1, 4 and 7 | 7168 // The 'm's that end with 0xd8.. are 1, 4 and 7 |
7247 " if ((m % 3) == 1 && n >= 6) utf -= 2;" | 7169 " if ((m % 3) == 1 && n >= 6) utf -= 2;" |
7248 " a2lens.push(utf);" | 7170 " a2lens.push(utf);" |
7249 " }" | 7171 " }" |
7250 "}"); | 7172 "}"); |
7251 Utf16Helper(context, "a", "alens", 9); | 7173 Utf16Helper(context, "a", "alens", 9); |
7252 Utf16Helper(context, "a2", "a2lens", 81); | 7174 Utf16Helper(context, "a2", "a2lens", 81); |
7253 WriteUtf8Helper(context, "b", "alens", 9); | |
7254 WriteUtf8Helper(context, "b2", "a2lens", 81); | |
7255 WriteUtf8Helper(context, "c2", "a2lens", 81); | |
7256 } | 7175 } |
7257 | 7176 |
7258 | 7177 |
7259 static bool SameSymbol(Handle<String> s1, Handle<String> s2) { | 7178 static bool SameSymbol(Handle<String> s1, Handle<String> s2) { |
7260 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1)); | 7179 i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1)); |
7261 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2)); | 7180 i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2)); |
7262 return *is1 == *is2; | 7181 return *is1 == *is2; |
7263 } | 7182 } |
7264 | 7183 |
7265 static void SameSymbolHelper(v8::Isolate* isolate, const char* a, | |
7266 const char* b) { | |
7267 Handle<String> symbol1 = | |
7268 v8::String::NewFromUtf8(isolate, a, v8::String::kInternalizedString); | |
7269 Handle<String> symbol2 = | |
7270 v8::String::NewFromUtf8(isolate, b, v8::String::kInternalizedString); | |
7271 CHECK(SameSymbol(symbol1, symbol2)); | |
7272 } | |
7273 | |
7274 | 7184 |
7275 THREADED_TEST(Utf16Symbol) { | 7185 THREADED_TEST(Utf16Symbol) { |
7276 LocalContext context; | 7186 LocalContext context; |
7277 v8::HandleScope scope(context->GetIsolate()); | 7187 v8::HandleScope scope(context->GetIsolate()); |
7278 | 7188 |
7279 Handle<String> symbol1 = v8::String::NewFromUtf8( | 7189 Handle<String> symbol1 = v8::String::NewFromUtf8( |
7280 context->GetIsolate(), "abc", v8::String::kInternalizedString); | 7190 context->GetIsolate(), "abc", v8::String::kInternalizedString); |
7281 Handle<String> symbol2 = v8::String::NewFromUtf8( | 7191 Handle<String> symbol2 = v8::String::NewFromUtf8( |
7282 context->GetIsolate(), "abc", v8::String::kInternalizedString); | 7192 context->GetIsolate(), "abc", v8::String::kInternalizedString); |
7283 CHECK(SameSymbol(symbol1, symbol2)); | 7193 CHECK(SameSymbol(symbol1, symbol2)); |
7284 | 7194 |
7285 SameSymbolHelper(context->GetIsolate(), | |
7286 "\360\220\220\205", // 4 byte encoding. | |
7287 "\355\240\201\355\260\205"); // 2 3-byte surrogates. | |
7288 SameSymbolHelper(context->GetIsolate(), | |
7289 "\355\240\201\355\260\206", // 2 3-byte surrogates. | |
7290 "\360\220\220\206"); // 4 byte encoding. | |
7291 SameSymbolHelper(context->GetIsolate(), | |
7292 "x\360\220\220\205", // 4 byte encoding. | |
7293 "x\355\240\201\355\260\205"); // 2 3-byte surrogates. | |
7294 SameSymbolHelper(context->GetIsolate(), | |
7295 "x\355\240\201\355\260\206", // 2 3-byte surrogates. | |
7296 "x\360\220\220\206"); // 4 byte encoding. | |
7297 CompileRun( | 7195 CompileRun( |
7298 "var sym0 = 'benedictus';" | 7196 "var sym0 = 'benedictus';" |
7299 "var sym0b = 'S\303\270ren';" | 7197 "var sym0b = 'S\303\270ren';" |
7300 "var sym1 = '\355\240\201\355\260\207';" | 7198 "var sym1 = '\355\240\201\355\260\207';" |
7301 "var sym2 = '\360\220\220\210';" | 7199 "var sym2 = '\360\220\220\210';" |
7302 "var sym3 = 'x\355\240\201\355\260\207';" | 7200 "var sym3 = 'x\355\240\201\355\260\207';" |
7303 "var sym4 = 'x\360\220\220\210';" | 7201 "var sym4 = 'x\360\220\220\210';" |
7304 "if (sym1.length != 2) throw sym1;" | 7202 "if (sym1.length != 2) throw sym1;" |
7305 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);" | 7203 "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);" |
7306 "if (sym2.length != 2) throw sym2;" | 7204 "if (sym2.length != 2) throw sym2;" |
(...skipping 13729 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
21036 // add the testExtraShouldReturnFive export | 20934 // add the testExtraShouldReturnFive export |
21037 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); | 20935 v8::Local<v8::Object> exports = env->GetExtrasExportsObject(); |
21038 | 20936 |
21039 auto func = | 20937 auto func = |
21040 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); | 20938 exports->Get(v8_str("testExtraShouldReturnFive")).As<v8::Function>(); |
21041 auto undefined = v8::Undefined(isolate); | 20939 auto undefined = v8::Undefined(isolate); |
21042 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); | 20940 auto result = func->Call(undefined, 0, {}).As<v8::Number>(); |
21043 | 20941 |
21044 CHECK(result->Value() == 5.0); | 20942 CHECK(result->Value() == 5.0); |
21045 } | 20943 } |
OLD | NEW |