| OLD | NEW |
| 1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-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 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 "var x = 42;", | 254 "var x = 42;", |
| 255 "function foo(x, y) { return x + y; }", | 255 "function foo(x, y) { return x + y; }", |
| 256 "native function foo(); return %ArgleBargle(glop);", | 256 "native function foo(); return %ArgleBargle(glop);", |
| 257 "var x = new new Function('this.x = 42');", | 257 "var x = new new Function('this.x = 42');", |
| 258 NULL | 258 NULL |
| 259 }; | 259 }; |
| 260 | 260 |
| 261 uintptr_t stack_limit = ISOLATE->stack_guard()->real_climit(); | 261 uintptr_t stack_limit = ISOLATE->stack_guard()->real_climit(); |
| 262 for (int i = 0; programs[i]; i++) { | 262 for (int i = 0; programs[i]; i++) { |
| 263 const char* program = programs[i]; | 263 const char* program = programs[i]; |
| 264 unibrow::Utf8InputBuffer<256> stream(program, strlen(program)); | 264 i::Utf8ToUC16CharacterStream stream( |
| 265 reinterpret_cast<const i::byte*>(program), |
| 266 static_cast<unsigned>(strlen(program))); |
| 265 i::CompleteParserRecorder log; | 267 i::CompleteParserRecorder log; |
| 266 i::V8JavaScriptScanner scanner(ISOLATE); | 268 i::V8JavaScriptScanner scanner(ISOLATE); |
| 267 scanner.Initialize(i::Handle<i::String>::null(), &stream); | 269 scanner.Initialize(&stream); |
| 268 | 270 |
| 269 v8::preparser::PreParser::PreParseResult result = | 271 v8::preparser::PreParser::PreParseResult result = |
| 270 v8::preparser::PreParser::PreParseProgram(&scanner, | 272 v8::preparser::PreParser::PreParseProgram(&scanner, |
| 271 &log, | 273 &log, |
| 272 true, | 274 true, |
| 273 stack_limit); | 275 stack_limit); |
| 274 CHECK_EQ(v8::preparser::PreParser::kPreParseSuccess, result); | 276 CHECK_EQ(v8::preparser::PreParser::kPreParseSuccess, result); |
| 275 i::ScriptDataImpl data(log.ExtractData()); | 277 i::ScriptDataImpl data(log.ExtractData()); |
| 276 CHECK(!data.has_error()); | 278 CHECK(!data.has_error()); |
| 277 } | 279 } |
| 278 } | 280 } |
| 279 | 281 |
| 280 | 282 |
| 281 TEST(RegressChromium62639) { | 283 TEST(RegressChromium62639) { |
| 282 int marker; | 284 int marker; |
| 283 ISOLATE->stack_guard()->SetStackLimit( | 285 ISOLATE->stack_guard()->SetStackLimit( |
| 284 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | 286 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 285 | 287 |
| 286 const char* program = "var x = 'something';\n" | 288 const char* program = "var x = 'something';\n" |
| 287 "escape: function() {}"; | 289 "escape: function() {}"; |
| 288 // Fails parsing expecting an identifier after "function". | 290 // Fails parsing expecting an identifier after "function". |
| 289 // Before fix, didn't check *ok after Expect(Token::Identifier, ok), | 291 // Before fix, didn't check *ok after Expect(Token::Identifier, ok), |
| 290 // and then used the invalid currently scanned literal. This always | 292 // and then used the invalid currently scanned literal. This always |
| 291 // failed in debug mode, and sometimes crashed in release mode. | 293 // failed in debug mode, and sometimes crashed in release mode. |
| 292 | 294 |
| 293 unibrow::Utf8InputBuffer<256> stream(program, strlen(program)); | 295 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), |
| 296 static_cast<unsigned>(strlen(program))); |
| 294 i::ScriptDataImpl* data = | 297 i::ScriptDataImpl* data = |
| 295 i::ParserApi::PreParse(i::Handle<i::String>::null(), &stream, NULL); | 298 i::ParserApi::PreParse(&stream, NULL); |
| 296 CHECK(data->HasError()); | 299 CHECK(data->HasError()); |
| 297 delete data; | 300 delete data; |
| 298 } | 301 } |
| 299 | 302 |
| 300 | 303 |
| 301 TEST(Regress928) { | 304 TEST(Regress928) { |
| 302 // Preparsing didn't consider the catch clause of a try statement | 305 // Preparsing didn't consider the catch clause of a try statement |
| 303 // as with-content, which made it assume that a function inside | 306 // as with-content, which made it assume that a function inside |
| 304 // the block could be lazily compiled, and an extra, unexpected, | 307 // the block could be lazily compiled, and an extra, unexpected, |
| 305 // entry was added to the data. | 308 // entry was added to the data. |
| 306 int marker; | 309 int marker; |
| 307 ISOLATE->stack_guard()->SetStackLimit( | 310 ISOLATE->stack_guard()->SetStackLimit( |
| 308 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | 311 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 309 | 312 |
| 310 const char* program = | 313 const char* program = |
| 311 "try { } catch (e) { var foo = function () { /* first */ } }" | 314 "try { } catch (e) { var foo = function () { /* first */ } }" |
| 312 "var bar = function () { /* second */ }"; | 315 "var bar = function () { /* second */ }"; |
| 313 | 316 |
| 314 unibrow::Utf8InputBuffer<256> stream(program, strlen(program)); | 317 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), |
| 318 static_cast<unsigned>(strlen(program))); |
| 315 i::ScriptDataImpl* data = | 319 i::ScriptDataImpl* data = |
| 316 i::ParserApi::PartialPreParse(i::Handle<i::String>::null(), | 320 i::ParserApi::PartialPreParse(&stream, NULL); |
| 317 &stream, NULL); | |
| 318 CHECK(!data->HasError()); | 321 CHECK(!data->HasError()); |
| 319 | 322 |
| 320 data->Initialize(); | 323 data->Initialize(); |
| 321 | 324 |
| 322 int first_function = strstr(program, "function") - program; | 325 int first_function = strstr(program, "function") - program; |
| 323 int first_lbrace = first_function + strlen("function () "); | 326 int first_lbrace = first_function + strlen("function () "); |
| 324 CHECK_EQ('{', program[first_lbrace]); | 327 CHECK_EQ('{', program[first_lbrace]); |
| 325 i::FunctionEntry entry1 = data->GetFunctionEntry(first_lbrace); | 328 i::FunctionEntry entry1 = data->GetFunctionEntry(first_lbrace); |
| 326 CHECK(!entry1.is_valid()); | 329 CHECK(!entry1.is_valid()); |
| 327 | 330 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 341 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | 344 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 342 | 345 |
| 343 size_t kProgramSize = 1024 * 1024; | 346 size_t kProgramSize = 1024 * 1024; |
| 344 i::SmartPointer<char> program( | 347 i::SmartPointer<char> program( |
| 345 reinterpret_cast<char*>(malloc(kProgramSize + 1))); | 348 reinterpret_cast<char*>(malloc(kProgramSize + 1))); |
| 346 memset(*program, '(', kProgramSize); | 349 memset(*program, '(', kProgramSize); |
| 347 program[kProgramSize] = '\0'; | 350 program[kProgramSize] = '\0'; |
| 348 | 351 |
| 349 uintptr_t stack_limit = ISOLATE->stack_guard()->real_climit(); | 352 uintptr_t stack_limit = ISOLATE->stack_guard()->real_climit(); |
| 350 | 353 |
| 351 unibrow::Utf8InputBuffer<256> stream(*program, strlen(*program)); | 354 i::Utf8ToUC16CharacterStream stream( |
| 355 reinterpret_cast<const i::byte*>(*program), |
| 356 static_cast<unsigned>(kProgramSize)); |
| 352 i::CompleteParserRecorder log; | 357 i::CompleteParserRecorder log; |
| 353 i::V8JavaScriptScanner scanner(ISOLATE); | 358 i::V8JavaScriptScanner scanner(ISOLATE); |
| 354 scanner.Initialize(i::Handle<i::String>::null(), &stream); | 359 scanner.Initialize(&stream); |
| 355 | 360 |
| 356 | 361 |
| 357 v8::preparser::PreParser::PreParseResult result = | 362 v8::preparser::PreParser::PreParseResult result = |
| 358 v8::preparser::PreParser::PreParseProgram(&scanner, | 363 v8::preparser::PreParser::PreParseProgram(&scanner, |
| 359 &log, | 364 &log, |
| 360 true, | 365 true, |
| 361 stack_limit); | 366 stack_limit); |
| 362 CHECK_EQ(v8::preparser::PreParser::kPreParseStackOverflow, result); | 367 CHECK_EQ(v8::preparser::PreParser::kPreParseStackOverflow, result); |
| 363 } | 368 } |
| 369 |
| 370 |
| 371 class TestExternalResource: public v8::String::ExternalStringResource { |
| 372 public: |
| 373 explicit TestExternalResource(uint16_t* data, int length) |
| 374 : data_(data), length_(static_cast<size_t>(length)) { } |
| 375 |
| 376 ~TestExternalResource() { } |
| 377 |
| 378 const uint16_t* data() const { |
| 379 return data_; |
| 380 } |
| 381 |
| 382 size_t length() const { |
| 383 return length_; |
| 384 } |
| 385 private: |
| 386 uint16_t* data_; |
| 387 size_t length_; |
| 388 }; |
| 389 |
| 390 |
| 391 #define CHECK_EQU(v1, v2) CHECK_EQ(static_cast<int>(v1), static_cast<int>(v2)) |
| 392 |
| 393 void TestCharacterStream(const char* ascii_source, |
| 394 unsigned length, |
| 395 unsigned start = 0, |
| 396 unsigned end = 0) { |
| 397 if (end == 0) end = length; |
| 398 unsigned sub_length = end - start; |
| 399 i::HandleScope test_scope; |
| 400 i::SmartPointer<i::uc16> uc16_buffer(new i::uc16[length]); |
| 401 for (unsigned i = 0; i < length; i++) { |
| 402 uc16_buffer[i] = static_cast<i::uc16>(ascii_source[i]); |
| 403 } |
| 404 i::Vector<const char> ascii_vector(ascii_source, static_cast<int>(length)); |
| 405 i::Handle<i::String> ascii_string( |
| 406 FACTORY->NewStringFromAscii(ascii_vector)); |
| 407 TestExternalResource resource(*uc16_buffer, length); |
| 408 i::Handle<i::String> uc16_string( |
| 409 FACTORY->NewExternalStringFromTwoByte(&resource)); |
| 410 |
| 411 i::ExternalTwoByteStringUC16CharacterStream uc16_stream( |
| 412 i::Handle<i::ExternalTwoByteString>::cast(uc16_string), start, end); |
| 413 i::GenericStringUC16CharacterStream string_stream(ascii_string, start, end); |
| 414 i::Utf8ToUC16CharacterStream utf8_stream( |
| 415 reinterpret_cast<const i::byte*>(ascii_source), end); |
| 416 utf8_stream.SeekForward(start); |
| 417 |
| 418 unsigned i = start; |
| 419 while (i < end) { |
| 420 // Read streams one char at a time |
| 421 CHECK_EQU(i, uc16_stream.pos()); |
| 422 CHECK_EQU(i, string_stream.pos()); |
| 423 CHECK_EQU(i, utf8_stream.pos()); |
| 424 int32_t c0 = ascii_source[i]; |
| 425 int32_t c1 = uc16_stream.Advance(); |
| 426 int32_t c2 = string_stream.Advance(); |
| 427 int32_t c3 = utf8_stream.Advance(); |
| 428 i++; |
| 429 CHECK_EQ(c0, c1); |
| 430 CHECK_EQ(c0, c2); |
| 431 CHECK_EQ(c0, c3); |
| 432 CHECK_EQU(i, uc16_stream.pos()); |
| 433 CHECK_EQU(i, string_stream.pos()); |
| 434 CHECK_EQU(i, utf8_stream.pos()); |
| 435 } |
| 436 while (i > start + sub_length / 4) { |
| 437 // Pushback, re-read, pushback again. |
| 438 int32_t c0 = ascii_source[i - 1]; |
| 439 CHECK_EQU(i, uc16_stream.pos()); |
| 440 CHECK_EQU(i, string_stream.pos()); |
| 441 CHECK_EQU(i, utf8_stream.pos()); |
| 442 uc16_stream.PushBack(c0); |
| 443 string_stream.PushBack(c0); |
| 444 utf8_stream.PushBack(c0); |
| 445 i--; |
| 446 CHECK_EQU(i, uc16_stream.pos()); |
| 447 CHECK_EQU(i, string_stream.pos()); |
| 448 CHECK_EQU(i, utf8_stream.pos()); |
| 449 int32_t c1 = uc16_stream.Advance(); |
| 450 int32_t c2 = string_stream.Advance(); |
| 451 int32_t c3 = utf8_stream.Advance(); |
| 452 i++; |
| 453 CHECK_EQU(i, uc16_stream.pos()); |
| 454 CHECK_EQU(i, string_stream.pos()); |
| 455 CHECK_EQU(i, utf8_stream.pos()); |
| 456 CHECK_EQ(c0, c1); |
| 457 CHECK_EQ(c0, c2); |
| 458 CHECK_EQ(c0, c3); |
| 459 uc16_stream.PushBack(c0); |
| 460 string_stream.PushBack(c0); |
| 461 utf8_stream.PushBack(c0); |
| 462 i--; |
| 463 CHECK_EQU(i, uc16_stream.pos()); |
| 464 CHECK_EQU(i, string_stream.pos()); |
| 465 CHECK_EQU(i, utf8_stream.pos()); |
| 466 } |
| 467 unsigned halfway = start + sub_length / 2; |
| 468 uc16_stream.SeekForward(halfway - i); |
| 469 string_stream.SeekForward(halfway - i); |
| 470 utf8_stream.SeekForward(halfway - i); |
| 471 i = halfway; |
| 472 CHECK_EQU(i, uc16_stream.pos()); |
| 473 CHECK_EQU(i, string_stream.pos()); |
| 474 CHECK_EQU(i, utf8_stream.pos()); |
| 475 |
| 476 while (i < end) { |
| 477 // Read streams one char at a time |
| 478 CHECK_EQU(i, uc16_stream.pos()); |
| 479 CHECK_EQU(i, string_stream.pos()); |
| 480 CHECK_EQU(i, utf8_stream.pos()); |
| 481 int32_t c0 = ascii_source[i]; |
| 482 int32_t c1 = uc16_stream.Advance(); |
| 483 int32_t c2 = string_stream.Advance(); |
| 484 int32_t c3 = utf8_stream.Advance(); |
| 485 i++; |
| 486 CHECK_EQ(c0, c1); |
| 487 CHECK_EQ(c0, c2); |
| 488 CHECK_EQ(c0, c3); |
| 489 CHECK_EQU(i, uc16_stream.pos()); |
| 490 CHECK_EQU(i, string_stream.pos()); |
| 491 CHECK_EQU(i, utf8_stream.pos()); |
| 492 } |
| 493 |
| 494 int32_t c1 = uc16_stream.Advance(); |
| 495 int32_t c2 = string_stream.Advance(); |
| 496 int32_t c3 = utf8_stream.Advance(); |
| 497 CHECK_LT(c1, 0); |
| 498 CHECK_LT(c2, 0); |
| 499 CHECK_LT(c3, 0); |
| 500 } |
| 501 |
| 502 |
| 503 TEST(CharacterStreams) { |
| 504 v8::HandleScope handles; |
| 505 v8::Persistent<v8::Context> context = v8::Context::New(); |
| 506 v8::Context::Scope context_scope(context); |
| 507 |
| 508 TestCharacterStream("abc\0\n\r\x7f", 7); |
| 509 static const unsigned kBigStringSize = 4096; |
| 510 char buffer[kBigStringSize + 1]; |
| 511 for (unsigned i = 0; i < kBigStringSize; i++) { |
| 512 buffer[i] = static_cast<char>(i & 0x7f); |
| 513 } |
| 514 TestCharacterStream(buffer, kBigStringSize); |
| 515 |
| 516 TestCharacterStream(buffer, kBigStringSize, 576, 3298); |
| 517 |
| 518 TestCharacterStream("\0", 1); |
| 519 TestCharacterStream("", 0); |
| 520 } |
| 521 |
| 522 |
| 523 TEST(Utf8CharacterStream) { |
| 524 static const unsigned kMaxUC16CharU = unibrow::Utf8::kMaxThreeByteChar; |
| 525 static const int kMaxUC16Char = static_cast<int>(kMaxUC16CharU); |
| 526 |
| 527 static const int kAllUtf8CharsSize = |
| 528 (unibrow::Utf8::kMaxOneByteChar + 1) + |
| 529 (unibrow::Utf8::kMaxTwoByteChar - unibrow::Utf8::kMaxOneByteChar) * 2 + |
| 530 (unibrow::Utf8::kMaxThreeByteChar - unibrow::Utf8::kMaxTwoByteChar) * 3; |
| 531 static const unsigned kAllUtf8CharsSizeU = |
| 532 static_cast<unsigned>(kAllUtf8CharsSize); |
| 533 |
| 534 char buffer[kAllUtf8CharsSizeU]; |
| 535 unsigned cursor = 0; |
| 536 for (int i = 0; i <= kMaxUC16Char; i++) { |
| 537 cursor += unibrow::Utf8::Encode(buffer + cursor, i); |
| 538 } |
| 539 ASSERT(cursor == kAllUtf8CharsSizeU); |
| 540 |
| 541 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(buffer), |
| 542 kAllUtf8CharsSizeU); |
| 543 for (int i = 0; i <= kMaxUC16Char; i++) { |
| 544 CHECK_EQU(i, stream.pos()); |
| 545 int32_t c = stream.Advance(); |
| 546 CHECK_EQ(i, c); |
| 547 CHECK_EQU(i + 1, stream.pos()); |
| 548 } |
| 549 for (int i = kMaxUC16Char; i >= 0; i--) { |
| 550 CHECK_EQU(i + 1, stream.pos()); |
| 551 stream.PushBack(i); |
| 552 CHECK_EQU(i, stream.pos()); |
| 553 } |
| 554 int i = 0; |
| 555 while (stream.pos() < kMaxUC16CharU) { |
| 556 CHECK_EQU(i, stream.pos()); |
| 557 unsigned progress = stream.SeekForward(12); |
| 558 i += progress; |
| 559 int32_t c = stream.Advance(); |
| 560 if (i <= kMaxUC16Char) { |
| 561 CHECK_EQ(i, c); |
| 562 } else { |
| 563 CHECK_EQ(-1, c); |
| 564 } |
| 565 i += 1; |
| 566 CHECK_EQU(i, stream.pos()); |
| 567 } |
| 568 } |
| 569 |
| 570 #undef CHECK_EQU |
| 571 |
| 572 void TestStreamScanner(i::UC16CharacterStream* stream, |
| 573 i::Token::Value* expected_tokens, |
| 574 int skip_pos = 0, // Zero means not skipping. |
| 575 int skip_to = 0) { |
| 576 i::V8JavaScriptScanner scanner(ISOLATE); |
| 577 scanner.Initialize(stream, i::JavaScriptScanner::kAllLiterals); |
| 578 |
| 579 int i = 0; |
| 580 do { |
| 581 i::Token::Value expected = expected_tokens[i]; |
| 582 i::Token::Value actual = scanner.Next(); |
| 583 CHECK_EQ(i::Token::String(expected), i::Token::String(actual)); |
| 584 if (scanner.location().end_pos == skip_pos) { |
| 585 scanner.SeekForward(skip_to); |
| 586 } |
| 587 i++; |
| 588 } while (expected_tokens[i] != i::Token::ILLEGAL); |
| 589 } |
| 590 |
| 591 TEST(StreamScanner) { |
| 592 const char* str1 = "{ foo get for : */ <- \n\n /*foo*/ bib"; |
| 593 i::Utf8ToUC16CharacterStream stream1(reinterpret_cast<const i::byte*>(str1), |
| 594 static_cast<unsigned>(strlen(str1))); |
| 595 i::Token::Value expectations1[] = { |
| 596 i::Token::LBRACE, |
| 597 i::Token::IDENTIFIER, |
| 598 i::Token::IDENTIFIER, |
| 599 i::Token::FOR, |
| 600 i::Token::COLON, |
| 601 i::Token::MUL, |
| 602 i::Token::DIV, |
| 603 i::Token::LT, |
| 604 i::Token::SUB, |
| 605 i::Token::IDENTIFIER, |
| 606 i::Token::EOS, |
| 607 i::Token::ILLEGAL |
| 608 }; |
| 609 TestStreamScanner(&stream1, expectations1, 0, 0); |
| 610 |
| 611 const char* str2 = "case default const {THIS\nPART\nSKIPPED} do"; |
| 612 i::Utf8ToUC16CharacterStream stream2(reinterpret_cast<const i::byte*>(str2), |
| 613 static_cast<unsigned>(strlen(str2))); |
| 614 i::Token::Value expectations2[] = { |
| 615 i::Token::CASE, |
| 616 i::Token::DEFAULT, |
| 617 i::Token::CONST, |
| 618 i::Token::LBRACE, |
| 619 // Skipped part here |
| 620 i::Token::RBRACE, |
| 621 i::Token::DO, |
| 622 i::Token::EOS, |
| 623 i::Token::ILLEGAL |
| 624 }; |
| 625 ASSERT_EQ('{', str2[19]); |
| 626 ASSERT_EQ('}', str2[37]); |
| 627 TestStreamScanner(&stream2, expectations2, 20, 37); |
| 628 |
| 629 const char* str3 = "{}}}}"; |
| 630 i::Token::Value expectations3[] = { |
| 631 i::Token::LBRACE, |
| 632 i::Token::RBRACE, |
| 633 i::Token::RBRACE, |
| 634 i::Token::RBRACE, |
| 635 i::Token::RBRACE, |
| 636 i::Token::EOS, |
| 637 i::Token::ILLEGAL |
| 638 }; |
| 639 // Skip zero-four RBRACEs. |
| 640 for (int i = 0; i <= 4; i++) { |
| 641 expectations3[6 - i] = i::Token::ILLEGAL; |
| 642 expectations3[5 - i] = i::Token::EOS; |
| 643 i::Utf8ToUC16CharacterStream stream3( |
| 644 reinterpret_cast<const i::byte*>(str3), |
| 645 static_cast<unsigned>(strlen(str3))); |
| 646 TestStreamScanner(&stream3, expectations3, 1, 1 + i); |
| 647 } |
| 648 } |
| OLD | NEW |