| 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 866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 877 CHECK_EQ(scope->inner_scopes()->length(), 1); | 877 CHECK_EQ(scope->inner_scopes()->length(), 1); |
| 878 | 878 |
| 879 i::Scope* inner_scope = scope->inner_scopes()->at(0); | 879 i::Scope* inner_scope = scope->inner_scopes()->at(0); |
| 880 CHECK_EQ(inner_scope->type(), source_data[i].scope_type); | 880 CHECK_EQ(inner_scope->type(), source_data[i].scope_type); |
| 881 CHECK_EQ(inner_scope->start_position(), kPrefixLen); | 881 CHECK_EQ(inner_scope->start_position(), kPrefixLen); |
| 882 // The end position of a token is one position after the last | 882 // The end position of a token is one position after the last |
| 883 // character belonging to that token. | 883 // character belonging to that token. |
| 884 CHECK_EQ(inner_scope->end_position(), kPrefixLen + kInnerLen); | 884 CHECK_EQ(inner_scope->end_position(), kPrefixLen + kInnerLen); |
| 885 } | 885 } |
| 886 } | 886 } |
| 887 |
| 888 |
| 889 void TestParserSync(i::Handle<i::String> source, int flags) { |
| 890 uintptr_t stack_limit = i::Isolate::Current()->stack_guard()->real_climit(); |
| 891 bool harmony_scoping = ((i::kLanguageModeMask & flags) == i::EXTENDED_MODE); |
| 892 |
| 893 // Preparse the data. |
| 894 i::CompleteParserRecorder log; |
| 895 i::Scanner scanner(i::Isolate::Current()->unicode_cache()); |
| 896 i::GenericStringUC16CharacterStream stream(source, 0, source->length()); |
| 897 scanner.SetHarmonyScoping(harmony_scoping); |
| 898 scanner.Initialize(&stream); |
| 899 v8::preparser::PreParser::PreParseResult result = |
| 900 v8::preparser::PreParser::PreParseProgram( |
| 901 &scanner, &log, flags, stack_limit); |
| 902 CHECK_EQ(v8::preparser::PreParser::kPreParseSuccess, result); |
| 903 i::ScriptDataImpl data(log.ExtractData()); |
| 904 |
| 905 // Parse the data |
| 906 i::Handle<i::Script> script = FACTORY->NewScript(source); |
| 907 bool save_harmony_scoping = i::FLAG_harmony_scoping; |
| 908 i::FLAG_harmony_scoping = harmony_scoping; |
| 909 i::Parser parser(script, flags, NULL, NULL); |
| 910 i::CompilationInfo info(script); |
| 911 info.MarkAsGlobal(); |
| 912 i::FunctionLiteral* function = parser.ParseProgram(&info); |
| 913 i::FLAG_harmony_scoping = save_harmony_scoping; |
| 914 |
| 915 i::String* type_string = NULL; |
| 916 if (function == NULL) { |
| 917 // Extract exception from the parser. |
| 918 i::Handle<i::String> type_symbol = FACTORY->LookupAsciiSymbol("type"); |
| 919 CHECK(i::Isolate::Current()->has_pending_exception()); |
| 920 i::MaybeObject* maybe_object = i::Isolate::Current()->pending_exception(); |
| 921 i::JSObject* exception = NULL; |
| 922 CHECK(maybe_object->To(&exception)); |
| 923 |
| 924 // Get the type string. |
| 925 maybe_object = exception->GetProperty(*type_symbol); |
| 926 CHECK(maybe_object->To(&type_string)); |
| 927 } |
| 928 |
| 929 // Check that preparsing fails iff parsing fails. |
| 930 if (data.has_error() && function != NULL) { |
| 931 i::OS::Print( |
| 932 "Preparser failed on:\n" |
| 933 "\t%s\n" |
| 934 "with error:\n" |
| 935 "\t%s\n" |
| 936 "However, the parser succeeded", |
| 937 *source->ToCString(), data.BuildMessage()); |
| 938 CHECK(false); |
| 939 } else if (!data.has_error() && function == NULL) { |
| 940 i::OS::Print( |
| 941 "Parser failed on:\n" |
| 942 "\t%s\n" |
| 943 "with error:\n" |
| 944 "\t%s\n" |
| 945 "However, the preparser succeeded", |
| 946 *source->ToCString(), *type_string->ToCString()); |
| 947 CHECK(false); |
| 948 } |
| 949 |
| 950 // Check that preparser and parser produce the same error. |
| 951 if (function == NULL) { |
| 952 if (!type_string->IsEqualTo(i::CStrVector(data.BuildMessage()))) { |
| 953 i::OS::Print( |
| 954 "Expected parser and preparser to produce the same error on:\n" |
| 955 "\t%s\n" |
| 956 "However, found the following error messages\n" |
| 957 "\tparser: %s\n" |
| 958 "\tpreparser: %s\n", |
| 959 *source->ToCString(), *type_string->ToCString(), data.BuildMessage()); |
| 960 CHECK(false); |
| 961 } |
| 962 } |
| 963 } |
| 964 |
| 965 |
| 966 void TestParserSyncWithFlags(i::Handle<i::String> source) { |
| 967 static const int kFlagsCount = 6; |
| 968 const int flags[kFlagsCount] = { |
| 969 i::kNoParsingFlags | i::CLASSIC_MODE, |
| 970 i::kNoParsingFlags | i::STRICT_MODE, |
| 971 i::kNoParsingFlags | i::EXTENDED_MODE, |
| 972 i::kAllowLazy | i::CLASSIC_MODE, |
| 973 i::kAllowLazy | i::STRICT_MODE, |
| 974 i::kAllowLazy | i::EXTENDED_MODE |
| 975 }; |
| 976 |
| 977 for (int k = 0; k < kFlagsCount; ++k) { |
| 978 TestParserSync(source, flags[k]); |
| 979 } |
| 980 } |
| 981 |
| 982 |
| 983 TEST(ParserSync) { |
| 984 const char* context_data[][2] = { |
| 985 { "", "" }, |
| 986 { "{", "}" }, |
| 987 { "if (true) ", " else {}" }, |
| 988 { "if (true) {} else ", "" }, |
| 989 { "if (true) ", "" }, |
| 990 { "do ", " while (false)" }, |
| 991 { "while (false) ", "" }, |
| 992 { "for (;;) ", "" }, |
| 993 { "with ({})", "" }, |
| 994 { "switch (12) { case 12: ", "}" }, |
| 995 { "switch (12) { default: ", "}" }, |
| 996 { "label2: ", "" }, |
| 997 { NULL, NULL } |
| 998 }; |
| 999 |
| 1000 const char* statement_data[] = { |
| 1001 "{}", |
| 1002 "var x", |
| 1003 "var x = 1", |
| 1004 "const x", |
| 1005 "const x = 1", |
| 1006 ";", |
| 1007 "12", |
| 1008 "if (false) {} else ;", |
| 1009 "if (false) {} else {}", |
| 1010 "if (false) {} else 12", |
| 1011 "if (false) ;" |
| 1012 "if (false) {}", |
| 1013 "if (false) 12", |
| 1014 "do {} while (false)", |
| 1015 "for (;;) ;", |
| 1016 "for (;;) {}", |
| 1017 "for (;;) 12", |
| 1018 "continue", |
| 1019 "continue label", |
| 1020 "continue\nlabel", |
| 1021 "break", |
| 1022 "break label", |
| 1023 "break\nlabel", |
| 1024 "return", |
| 1025 "return 12", |
| 1026 "return\n12", |
| 1027 "with ({}) ;", |
| 1028 "with ({}) {}", |
| 1029 "with ({}) 12", |
| 1030 "switch ({}) { default: }" |
| 1031 "label3: " |
| 1032 "throw", |
| 1033 "throw 12", |
| 1034 "throw\n12", |
| 1035 "try {} catch(e) {}", |
| 1036 "try {} finally {}", |
| 1037 "try {} catch(e) {} finally {}", |
| 1038 "debugger", |
| 1039 NULL |
| 1040 }; |
| 1041 |
| 1042 const char* termination_data[] = { |
| 1043 "", |
| 1044 ";", |
| 1045 "\n", |
| 1046 ";\n", |
| 1047 "\n;", |
| 1048 NULL |
| 1049 }; |
| 1050 |
| 1051 v8::HandleScope handles; |
| 1052 v8::Persistent<v8::Context> context = v8::Context::New(); |
| 1053 v8::Context::Scope context_scope(context); |
| 1054 |
| 1055 int marker; |
| 1056 i::Isolate::Current()->stack_guard()->SetStackLimit( |
| 1057 reinterpret_cast<uintptr_t>(&marker) - 128 * 1024); |
| 1058 |
| 1059 for (int i = 0; context_data[i][0] != NULL; ++i) { |
| 1060 for (int j = 0; statement_data[j] != NULL; ++j) { |
| 1061 for (int k = 0; termination_data[k] != NULL; ++k) { |
| 1062 int kPrefixLen = i::StrLength(context_data[i][0]); |
| 1063 int kStatementLen = i::StrLength(statement_data[j]); |
| 1064 int kTerminationLen = i::StrLength(termination_data[k]); |
| 1065 int kSuffixLen = i::StrLength(context_data[i][1]); |
| 1066 int kProgramSize = kPrefixLen + kStatementLen + kTerminationLen |
| 1067 + kSuffixLen + i::StrLength("label: for (;;) { }"); |
| 1068 |
| 1069 // Plug the source code pieces together. |
| 1070 i::Vector<char> program = i::Vector<char>::New(kProgramSize + 1); |
| 1071 int length = i::OS::SNPrintF(program, |
| 1072 "label: for (;;) { %s%s%s%s }", |
| 1073 context_data[i][0], |
| 1074 statement_data[j], |
| 1075 termination_data[k], |
| 1076 context_data[i][1]); |
| 1077 CHECK(length == kProgramSize); |
| 1078 i::Handle<i::String> source = |
| 1079 FACTORY->NewStringFromAscii(i::CStrVector(program.start())); |
| 1080 TestParserSyncWithFlags(source); |
| 1081 } |
| 1082 } |
| 1083 } |
| 1084 } |
| OLD | NEW |