OLD | NEW |
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2009 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 4326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4337 CHECK_NE(obj, context->Global()->Get(v8_str("o"))); | 4337 CHECK_NE(obj, context->Global()->Get(v8_str("o"))); |
4338 context->Global()->Set(v8_str("o2"), obj); | 4338 context->Global()->Set(v8_str("o2"), obj); |
4339 v8::Handle<Value> value = | 4339 v8::Handle<Value> value = |
4340 Script::Compile(v8_str("o.__proto__ === o2.__proto__"))->Run(); | 4340 Script::Compile(v8_str("o.__proto__ === o2.__proto__"))->Run(); |
4341 CHECK_EQ(v8::True(), value); | 4341 CHECK_EQ(v8::True(), value); |
4342 context->Global()->Set(v8_str("o"), obj); | 4342 context->Global()->Set(v8_str("o"), obj); |
4343 } | 4343 } |
4344 } | 4344 } |
4345 | 4345 |
4346 | 4346 |
| 4347 static int StrCmp16(uint16_t* a, uint16_t* b) { |
| 4348 while (true) { |
| 4349 if (*a == 0 && *b == 0) return 0; |
| 4350 if (*a != *b) return 0 + *a - *b; |
| 4351 a++; |
| 4352 b++; |
| 4353 } |
| 4354 } |
| 4355 |
| 4356 |
| 4357 static int StrNCmp16(uint16_t* a, uint16_t* b, int n) { |
| 4358 while (true) { |
| 4359 if (n-- == 0) return 0; |
| 4360 if (*a == 0 && *b == 0) return 0; |
| 4361 if (*a != *b) return 0 + *a - *b; |
| 4362 a++; |
| 4363 b++; |
| 4364 } |
| 4365 } |
| 4366 |
| 4367 |
4347 THREADED_TEST(StringWrite) { | 4368 THREADED_TEST(StringWrite) { |
4348 v8::HandleScope scope; | 4369 v8::HandleScope scope; |
4349 v8::Handle<String> str = v8_str("abcde"); | 4370 v8::Handle<String> str = v8_str("abcde"); |
| 4371 // abc<Icelandic eth><Unicode snowman>. |
| 4372 v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203"); |
| 4373 |
| 4374 CHECK_EQ(5, str2->Length()); |
4350 | 4375 |
4351 char buf[100]; | 4376 char buf[100]; |
| 4377 char utf8buf[100]; |
| 4378 uint16_t wbuf[100]; |
4352 int len; | 4379 int len; |
| 4380 int charlen; |
| 4381 |
| 4382 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4383 len = str2->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen); |
| 4384 CHECK_EQ(len, 9); |
| 4385 CHECK_EQ(charlen, 5); |
| 4386 CHECK_EQ(strcmp(utf8buf, "abc\303\260\342\230\203"), 0); |
| 4387 |
| 4388 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4389 len = str2->WriteUtf8(utf8buf, 8, &charlen); |
| 4390 CHECK_EQ(len, 8); |
| 4391 CHECK_EQ(charlen, 5); |
| 4392 CHECK_EQ(strncmp(utf8buf, "abc\303\260\342\230\203\1", 9), 0); |
| 4393 |
| 4394 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4395 len = str2->WriteUtf8(utf8buf, 7, &charlen); |
| 4396 CHECK_EQ(len, 5); |
| 4397 CHECK_EQ(charlen, 4); |
| 4398 CHECK_EQ(strncmp(utf8buf, "abc\303\260\1", 5), 0); |
| 4399 |
| 4400 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4401 len = str2->WriteUtf8(utf8buf, 6, &charlen); |
| 4402 CHECK_EQ(len, 5); |
| 4403 CHECK_EQ(charlen, 4); |
| 4404 CHECK_EQ(strncmp(utf8buf, "abc\303\260\1", 5), 0); |
| 4405 |
| 4406 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4407 len = str2->WriteUtf8(utf8buf, 5, &charlen); |
| 4408 CHECK_EQ(len, 5); |
| 4409 CHECK_EQ(charlen, 4); |
| 4410 CHECK_EQ(strncmp(utf8buf, "abc\303\260\1", 5), 0); |
| 4411 |
| 4412 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4413 len = str2->WriteUtf8(utf8buf, 4, &charlen); |
| 4414 CHECK_EQ(len, 3); |
| 4415 CHECK_EQ(charlen, 3); |
| 4416 CHECK_EQ(strncmp(utf8buf, "abc\1", 4), 0); |
| 4417 |
| 4418 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4419 len = str2->WriteUtf8(utf8buf, 3, &charlen); |
| 4420 CHECK_EQ(len, 3); |
| 4421 CHECK_EQ(charlen, 3); |
| 4422 CHECK_EQ(strncmp(utf8buf, "abc\1", 4), 0); |
| 4423 |
| 4424 memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 4425 len = str2->WriteUtf8(utf8buf, 2, &charlen); |
| 4426 CHECK_EQ(len, 2); |
| 4427 CHECK_EQ(charlen, 2); |
| 4428 CHECK_EQ(strncmp(utf8buf, "ab\1", 3), 0); |
4353 | 4429 |
4354 memset(buf, 0x1, sizeof(buf)); | 4430 memset(buf, 0x1, sizeof(buf)); |
| 4431 memset(wbuf, 0x1, sizeof(wbuf)); |
4355 len = str->WriteAscii(buf); | 4432 len = str->WriteAscii(buf); |
4356 CHECK_EQ(len, 5); | 4433 CHECK_EQ(len, 5); |
4357 CHECK_EQ(strncmp("abcde\0", buf, 6), 0); | 4434 len = str->Write(wbuf); |
| 4435 CHECK_EQ(len, 5); |
| 4436 CHECK_EQ(strcmp("abcde", buf), 0); |
| 4437 uint16_t answer1[] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
| 4438 CHECK_EQ(StrCmp16(answer1, wbuf), 0); |
4358 | 4439 |
4359 memset(buf, 0x1, sizeof(buf)); | 4440 memset(buf, 0x1, sizeof(buf)); |
| 4441 memset(wbuf, 0x1, sizeof(wbuf)); |
4360 len = str->WriteAscii(buf, 0, 4); | 4442 len = str->WriteAscii(buf, 0, 4); |
4361 CHECK_EQ(len, 4); | 4443 CHECK_EQ(len, 4); |
| 4444 len = str->Write(wbuf, 0, 4); |
| 4445 CHECK_EQ(len, 4); |
4362 CHECK_EQ(strncmp("abcd\1", buf, 5), 0); | 4446 CHECK_EQ(strncmp("abcd\1", buf, 5), 0); |
| 4447 uint16_t answer2[] = {'a', 'b', 'c', 'd', 0x101}; |
| 4448 CHECK_EQ(StrNCmp16(answer2, wbuf, 5), 0); |
4363 | 4449 |
4364 memset(buf, 0x1, sizeof(buf)); | 4450 memset(buf, 0x1, sizeof(buf)); |
| 4451 memset(wbuf, 0x1, sizeof(wbuf)); |
4365 len = str->WriteAscii(buf, 0, 5); | 4452 len = str->WriteAscii(buf, 0, 5); |
4366 CHECK_EQ(len, 5); | 4453 CHECK_EQ(len, 5); |
| 4454 len = str->Write(wbuf, 0, 5); |
| 4455 CHECK_EQ(len, 5); |
4367 CHECK_EQ(strncmp("abcde\1", buf, 6), 0); | 4456 CHECK_EQ(strncmp("abcde\1", buf, 6), 0); |
| 4457 uint16_t answer3[] = {'a', 'b', 'c', 'd', 'e', 0x101}; |
| 4458 CHECK_EQ(StrNCmp16(answer3, wbuf, 6), 0); |
4368 | 4459 |
4369 memset(buf, 0x1, sizeof(buf)); | 4460 memset(buf, 0x1, sizeof(buf)); |
| 4461 memset(wbuf, 0x1, sizeof(wbuf)); |
4370 len = str->WriteAscii(buf, 0, 6); | 4462 len = str->WriteAscii(buf, 0, 6); |
4371 CHECK_EQ(len, 5); | 4463 CHECK_EQ(len, 5); |
4372 CHECK_EQ(strncmp("abcde\0", buf, 6), 0); | 4464 len = str->Write(wbuf, 0, 6); |
| 4465 CHECK_EQ(len, 5); |
| 4466 CHECK_EQ(strcmp("abcde", buf), 0); |
| 4467 uint16_t answer4[] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
| 4468 CHECK_EQ(StrCmp16(answer4, wbuf), 0); |
4373 | 4469 |
4374 memset(buf, 0x1, sizeof(buf)); | 4470 memset(buf, 0x1, sizeof(buf)); |
| 4471 memset(wbuf, 0x1, sizeof(wbuf)); |
4375 len = str->WriteAscii(buf, 4, -1); | 4472 len = str->WriteAscii(buf, 4, -1); |
4376 CHECK_EQ(len, 1); | 4473 CHECK_EQ(len, 1); |
4377 CHECK_EQ(strncmp("e\0", buf, 2), 0); | 4474 len = str->Write(wbuf, 4, -1); |
| 4475 CHECK_EQ(len, 1); |
| 4476 CHECK_EQ(strcmp("e", buf), 0); |
| 4477 uint16_t answer5[] = {'e', '\0'}; |
| 4478 CHECK_EQ(StrCmp16(answer5, wbuf), 0); |
4378 | 4479 |
4379 memset(buf, 0x1, sizeof(buf)); | 4480 memset(buf, 0x1, sizeof(buf)); |
| 4481 memset(wbuf, 0x1, sizeof(wbuf)); |
4380 len = str->WriteAscii(buf, 4, 6); | 4482 len = str->WriteAscii(buf, 4, 6); |
4381 CHECK_EQ(len, 1); | 4483 CHECK_EQ(len, 1); |
4382 CHECK_EQ(strncmp("e\0", buf, 2), 0); | 4484 len = str->Write(wbuf, 4, 6); |
| 4485 CHECK_EQ(len, 1); |
| 4486 CHECK_EQ(strcmp("e", buf), 0); |
| 4487 CHECK_EQ(StrCmp16(answer5, wbuf), 0); |
4383 | 4488 |
4384 memset(buf, 0x1, sizeof(buf)); | 4489 memset(buf, 0x1, sizeof(buf)); |
| 4490 memset(wbuf, 0x1, sizeof(wbuf)); |
4385 len = str->WriteAscii(buf, 4, 1); | 4491 len = str->WriteAscii(buf, 4, 1); |
4386 CHECK_EQ(len, 1); | 4492 CHECK_EQ(len, 1); |
| 4493 len = str->Write(wbuf, 4, 1); |
| 4494 CHECK_EQ(len, 1); |
4387 CHECK_EQ(strncmp("e\1", buf, 2), 0); | 4495 CHECK_EQ(strncmp("e\1", buf, 2), 0); |
| 4496 uint16_t answer6[] = {'e', 0x101}; |
| 4497 CHECK_EQ(StrNCmp16(answer6, wbuf, 2), 0); |
| 4498 |
| 4499 memset(buf, 0x1, sizeof(buf)); |
| 4500 memset(wbuf, 0x1, sizeof(wbuf)); |
| 4501 len = str->WriteAscii(buf, 3, 1); |
| 4502 CHECK_EQ(len, 1); |
| 4503 len = str->Write(wbuf, 3, 1); |
| 4504 CHECK_EQ(len, 1); |
| 4505 CHECK_EQ(strncmp("d\1", buf, 2), 0); |
| 4506 uint16_t answer7[] = {'d', 0x101}; |
| 4507 CHECK_EQ(StrNCmp16(answer7, wbuf, 2), 0); |
4388 } | 4508 } |
4389 | 4509 |
4390 | 4510 |
4391 THREADED_TEST(ToArrayIndex) { | 4511 THREADED_TEST(ToArrayIndex) { |
4392 v8::HandleScope scope; | 4512 v8::HandleScope scope; |
4393 LocalContext context; | 4513 LocalContext context; |
4394 | 4514 |
4395 v8::Handle<String> str = v8_str("42"); | 4515 v8::Handle<String> str = v8_str("42"); |
4396 v8::Handle<v8::Uint32> index = str->ToArrayIndex(); | 4516 v8::Handle<v8::Uint32> index = str->ToArrayIndex(); |
4397 CHECK(!index.IsEmpty()); | 4517 CHECK(!index.IsEmpty()); |
(...skipping 7555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
11953 v8::Context::Scope context_scope(context.local()); | 12073 v8::Context::Scope context_scope(context.local()); |
11954 | 12074 |
11955 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); | 12075 v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
11956 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); | 12076 tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
11957 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); | 12077 context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
11958 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( | 12078 v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
11959 "var result = []; for (var k in o) result.push(k); result")); | 12079 "var result = []; for (var k in o) result.push(k); result")); |
11960 CHECK_EQ(1, result->Length()); | 12080 CHECK_EQ(1, result->Length()); |
11961 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); | 12081 CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
11962 } | 12082 } |
OLD | NEW |