| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 22 matching lines...) Expand all Loading... |
| 33 | 33 |
| 34 #include "isolate.h" | 34 #include "isolate.h" |
| 35 #include "token.h" | 35 #include "token.h" |
| 36 #include "scanner.h" | 36 #include "scanner.h" |
| 37 #include "parser.h" | 37 #include "parser.h" |
| 38 #include "utils.h" | 38 #include "utils.h" |
| 39 #include "execution.h" | 39 #include "execution.h" |
| 40 #include "preparser.h" | 40 #include "preparser.h" |
| 41 #include "cctest.h" | 41 #include "cctest.h" |
| 42 | 42 |
| 43 namespace i = ::v8::internal; | 43 TEST(ScanKeywords) { |
| 44 | |
| 45 TEST(KeywordMatcher) { | |
| 46 struct KeywordToken { | 44 struct KeywordToken { |
| 47 const char* keyword; | 45 const char* keyword; |
| 48 i::Token::Value token; | 46 i::Token::Value token; |
| 49 }; | 47 }; |
| 50 | 48 |
| 51 static const KeywordToken keywords[] = { | 49 static const KeywordToken keywords[] = { |
| 52 #define KEYWORD(t, s, d) { s, i::Token::t }, | 50 #define KEYWORD(t, s, d) { s, i::Token::t }, |
| 53 #define IGNORE(t, s, d) /* */ | 51 TOKEN_LIST(IGNORE_TOKEN, KEYWORD) |
| 54 TOKEN_LIST(IGNORE, KEYWORD, IGNORE) | |
| 55 #undef KEYWORD | 52 #undef KEYWORD |
| 56 { NULL, i::Token::IDENTIFIER } | 53 { NULL, i::Token::IDENTIFIER } |
| 57 }; | 54 }; |
| 58 | 55 |
| 59 static const char* future_keywords[] = { | |
| 60 #define FUTURE(t, s, d) s, | |
| 61 TOKEN_LIST(IGNORE, IGNORE, FUTURE) | |
| 62 #undef FUTURE | |
| 63 #undef IGNORE | |
| 64 NULL | |
| 65 }; | |
| 66 | |
| 67 KeywordToken key_token; | 56 KeywordToken key_token; |
| 57 i::UnicodeCache unicode_cache; |
| 58 i::byte buffer[32]; |
| 68 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { | 59 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { |
| 69 i::KeywordMatcher matcher; | 60 const i::byte* keyword = |
| 70 const char* keyword = key_token.keyword; | 61 reinterpret_cast<const i::byte*>(key_token.keyword); |
| 71 int length = i::StrLength(keyword); | 62 int length = i::StrLength(key_token.keyword); |
| 72 for (int j = 0; j < length; j++) { | 63 CHECK(static_cast<int>(sizeof(buffer)) >= length); |
| 73 if (key_token.token == i::Token::INSTANCEOF && j == 2) { | 64 { |
| 74 // "in" is a prefix of "instanceof". It's the only keyword | 65 i::Utf8ToUC16CharacterStream stream(keyword, length); |
| 75 // that is a prefix of another. | 66 i::JavaScriptScanner scanner(&unicode_cache); |
| 76 CHECK_EQ(i::Token::IN, matcher.token()); | 67 // The scanner should parse 'let' as Token::LET for this test. |
| 77 } else { | 68 scanner.SetHarmonyBlockScoping(true); |
| 78 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 69 scanner.Initialize(&stream); |
| 79 } | 70 CHECK_EQ(key_token.token, scanner.Next()); |
| 80 matcher.AddChar(keyword[j]); | 71 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 81 } | 72 } |
| 82 CHECK_EQ(key_token.token, matcher.token()); | 73 // Removing characters will make keyword matching fail. |
| 83 // Adding more characters will make keyword matching fail. | 74 { |
| 84 matcher.AddChar('z'); | 75 i::Utf8ToUC16CharacterStream stream(keyword, length - 1); |
| 85 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 76 i::JavaScriptScanner scanner(&unicode_cache); |
| 86 // Adding a keyword later will not make it match again. | 77 scanner.Initialize(&stream); |
| 87 matcher.AddChar('i'); | 78 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| 88 matcher.AddChar('f'); | 79 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 89 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | 80 } |
| 81 // Adding characters will make keyword matching fail. |
| 82 static const char chars_to_append[] = { 'z', '0', '_' }; |
| 83 for (int j = 0; j < static_cast<int>(ARRAY_SIZE(chars_to_append)); ++j) { |
| 84 memmove(buffer, keyword, length); |
| 85 buffer[length] = chars_to_append[j]; |
| 86 i::Utf8ToUC16CharacterStream stream(buffer, length + 1); |
| 87 i::JavaScriptScanner scanner(&unicode_cache); |
| 88 scanner.Initialize(&stream); |
| 89 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| 90 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 91 } |
| 92 // Replacing characters will make keyword matching fail. |
| 93 { |
| 94 memmove(buffer, keyword, length); |
| 95 buffer[length - 1] = '_'; |
| 96 i::Utf8ToUC16CharacterStream stream(buffer, length); |
| 97 i::JavaScriptScanner scanner(&unicode_cache); |
| 98 scanner.Initialize(&stream); |
| 99 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| 100 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 101 } |
| 90 } | 102 } |
| 91 | |
| 92 // Future keywords are not recognized. | |
| 93 const char* future_keyword; | |
| 94 for (int i = 0; (future_keyword = future_keywords[i]) != NULL; i++) { | |
| 95 i::KeywordMatcher matcher; | |
| 96 int length = i::StrLength(future_keyword); | |
| 97 for (int j = 0; j < length; j++) { | |
| 98 matcher.AddChar(future_keyword[j]); | |
| 99 } | |
| 100 CHECK_EQ(i::Token::IDENTIFIER, matcher.token()); | |
| 101 } | |
| 102 | |
| 103 // Zero isn't ignored at first. | |
| 104 i::KeywordMatcher bad_start; | |
| 105 bad_start.AddChar(0); | |
| 106 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); | |
| 107 bad_start.AddChar('i'); | |
| 108 bad_start.AddChar('f'); | |
| 109 CHECK_EQ(i::Token::IDENTIFIER, bad_start.token()); | |
| 110 | |
| 111 // Zero isn't ignored at end. | |
| 112 i::KeywordMatcher bad_end; | |
| 113 bad_end.AddChar('i'); | |
| 114 bad_end.AddChar('f'); | |
| 115 CHECK_EQ(i::Token::IF, bad_end.token()); | |
| 116 bad_end.AddChar(0); | |
| 117 CHECK_EQ(i::Token::IDENTIFIER, bad_end.token()); | |
| 118 | |
| 119 // Case isn't ignored. | |
| 120 i::KeywordMatcher bad_case; | |
| 121 bad_case.AddChar('i'); | |
| 122 bad_case.AddChar('F'); | |
| 123 CHECK_EQ(i::Token::IDENTIFIER, bad_case.token()); | |
| 124 | |
| 125 // If we mark it as failure, continuing won't help. | |
| 126 i::KeywordMatcher full_stop; | |
| 127 full_stop.AddChar('i'); | |
| 128 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
| 129 full_stop.Fail(); | |
| 130 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
| 131 full_stop.AddChar('f'); | |
| 132 CHECK_EQ(i::Token::IDENTIFIER, full_stop.token()); | |
| 133 } | 103 } |
| 134 | 104 |
| 135 | 105 |
| 136 TEST(ScanHTMLEndComments) { | 106 TEST(ScanHTMLEndComments) { |
| 137 v8::V8::Initialize(); | 107 v8::V8::Initialize(); |
| 138 | 108 |
| 139 // Regression test. See: | 109 // Regression test. See: |
| 140 // http://code.google.com/p/chromium/issues/detail?id=53548 | 110 // http://code.google.com/p/chromium/issues/detail?id=53548 |
| 141 // Tests that --> is correctly interpreted as comment-to-end-of-line if there | 111 // Tests that --> is correctly interpreted as comment-to-end-of-line if there |
| 142 // is only whitespace before it on the line (with comments considered as | 112 // is only whitespace before it on the line (with comments considered as |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 const char* program = "var x = 'something';\n" | 282 const char* program = "var x = 'something';\n" |
| 313 "escape: function() {}"; | 283 "escape: function() {}"; |
| 314 // Fails parsing expecting an identifier after "function". | 284 // Fails parsing expecting an identifier after "function". |
| 315 // Before fix, didn't check *ok after Expect(Token::Identifier, ok), | 285 // Before fix, didn't check *ok after Expect(Token::Identifier, ok), |
| 316 // and then used the invalid currently scanned literal. This always | 286 // and then used the invalid currently scanned literal. This always |
| 317 // failed in debug mode, and sometimes crashed in release mode. | 287 // failed in debug mode, and sometimes crashed in release mode. |
| 318 | 288 |
| 319 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), | 289 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), |
| 320 static_cast<unsigned>(strlen(program))); | 290 static_cast<unsigned>(strlen(program))); |
| 321 i::ScriptDataImpl* data = | 291 i::ScriptDataImpl* data = |
| 322 i::ParserApi::PreParse(&stream, NULL); | 292 i::ParserApi::PreParse(&stream, NULL, false); |
| 323 CHECK(data->HasError()); | 293 CHECK(data->HasError()); |
| 324 delete data; | 294 delete data; |
| 325 } | 295 } |
| 326 | 296 |
| 327 | 297 |
| 328 TEST(Regress928) { | 298 TEST(Regress928) { |
| 329 v8::V8::Initialize(); | 299 v8::V8::Initialize(); |
| 330 | 300 |
| 331 // Preparsing didn't consider the catch clause of a try statement | 301 // Preparsing didn't consider the catch clause of a try statement |
| 332 // as with-content, which made it assume that a function inside | 302 // as with-content, which made it assume that a function inside |
| 333 // the block could be lazily compiled, and an extra, unexpected, | 303 // the block could be lazily compiled, and an extra, unexpected, |
| 334 // entry was added to the data. | 304 // entry was added to the data. |
| 335 int marker; | 305 int marker; |
| 336 i::Isolate::Current()->stack_guard()->SetStackLimit( | 306 i::Isolate::Current()->stack_guard()->SetStackLimit( |
| 337 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); | 307 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 338 | 308 |
| 339 const char* program = | 309 const char* program = |
| 340 "try { } catch (e) { var foo = function () { /* first */ } }" | 310 "try { } catch (e) { var foo = function () { /* first */ } }" |
| 341 "var bar = function () { /* second */ }"; | 311 "var bar = function () { /* second */ }"; |
| 342 | 312 |
| 343 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), | 313 i::Utf8ToUC16CharacterStream stream(reinterpret_cast<const i::byte*>(program), |
| 344 static_cast<unsigned>(strlen(program))); | 314 static_cast<unsigned>(strlen(program))); |
| 345 i::ScriptDataImpl* data = | 315 i::ScriptDataImpl* data = |
| 346 i::ParserApi::PartialPreParse(&stream, NULL); | 316 i::ParserApi::PartialPreParse(&stream, NULL, false); |
| 347 CHECK(!data->HasError()); | 317 CHECK(!data->HasError()); |
| 348 | 318 |
| 349 data->Initialize(); | 319 data->Initialize(); |
| 350 | 320 |
| 351 int first_function = | 321 int first_function = |
| 352 static_cast<int>(strstr(program, "function") - program); | 322 static_cast<int>(strstr(program, "function") - program); |
| 353 int first_lbrace = first_function + static_cast<int>(strlen("function () ")); | 323 int first_lbrace = first_function + static_cast<int>(strlen("function () ")); |
| 354 CHECK_EQ('{', program[first_lbrace]); | 324 CHECK_EQ('{', program[first_lbrace]); |
| 355 i::FunctionEntry entry1 = data->GetFunctionEntry(first_lbrace); | 325 i::FunctionEntry entry1 = data->GetFunctionEntry(first_lbrace); |
| 356 CHECK(!entry1.is_valid()); | 326 CHECK(!entry1.is_valid()); |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 TestScanRegExp("/[\\u12]/flipperwald", "[\\u12]"); | 699 TestScanRegExp("/[\\u12]/flipperwald", "[\\u12]"); |
| 730 TestScanRegExp("/[\\u123]/flipperwald", "[\\u123]"); | 700 TestScanRegExp("/[\\u123]/flipperwald", "[\\u123]"); |
| 731 // Escaped ']'s wont end the character class. | 701 // Escaped ']'s wont end the character class. |
| 732 TestScanRegExp("/[\\]/]/flipperwald", "[\\]/]"); | 702 TestScanRegExp("/[\\]/]/flipperwald", "[\\]/]"); |
| 733 // Escaped slashes are not terminating. | 703 // Escaped slashes are not terminating. |
| 734 TestScanRegExp("/\\//flipperwald", "\\/"); | 704 TestScanRegExp("/\\//flipperwald", "\\/"); |
| 735 // Starting with '=' works too. | 705 // Starting with '=' works too. |
| 736 TestScanRegExp("/=/", "="); | 706 TestScanRegExp("/=/", "="); |
| 737 TestScanRegExp("/=?/", "=?"); | 707 TestScanRegExp("/=?/", "=?"); |
| 738 } | 708 } |
| OLD | NEW |