| 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 const char* source = | 204 const char* source = |
| 205 "var x = 42;" | 205 "var x = 42;" |
| 206 "function foo(a) { return function nolazy(b) { return a + b; } }" | 206 "function foo(a) { return function nolazy(b) { return a + b; } }" |
| 207 "function bar(a) { if (a) return function lazy(b) { return b; } }" | 207 "function bar(a) { if (a) return function lazy(b) { return b; } }" |
| 208 "var z = {'string': 'string literal', bareword: 'propertyName', " | 208 "var z = {'string': 'string literal', bareword: 'propertyName', " |
| 209 " 42: 'number literal', for: 'keyword as propertyName', " | 209 " 42: 'number literal', for: 'keyword as propertyName', " |
| 210 " f\\u006fr: 'keyword propertyname with escape'};" | 210 " f\\u006fr: 'keyword propertyname with escape'};" |
| 211 "var v = /RegExp Literal/;" | 211 "var v = /RegExp Literal/;" |
| 212 "var w = /RegExp Literal\\u0020With Escape/gin;" | 212 "var w = /RegExp Literal\\u0020With Escape/gin;" |
| 213 "var y = { get getter() { return 42; }, " | 213 "var y = { get getter() { return 42; }, " |
| 214 " set setter(v) { this.value = v; }};"; | 214 " set setter(v) { this.value = v; }};" |
| 215 "var f = a => function (b) { return a + b; };" |
| 216 "var g = a => b => a + b;"; |
| 215 int source_length = i::StrLength(source); | 217 int source_length = i::StrLength(source); |
| 216 | 218 |
| 217 // ScriptResource will be deleted when the corresponding String is GCd. | 219 // ScriptResource will be deleted when the corresponding String is GCd. |
| 218 v8::ScriptCompiler::Source script_source(v8::String::NewExternal( | 220 v8::ScriptCompiler::Source script_source(v8::String::NewExternal( |
| 219 isolate, new ScriptResource(source, source_length))); | 221 isolate, new ScriptResource(source, source_length))); |
| 222 i::FLAG_harmony_arrow_functions = true; |
| 220 i::FLAG_min_preparse_length = 0; | 223 i::FLAG_min_preparse_length = 0; |
| 221 v8::ScriptCompiler::Compile(isolate, &script_source, | 224 v8::ScriptCompiler::Compile(isolate, &script_source, |
| 222 v8::ScriptCompiler::kProduceParserCache); | 225 v8::ScriptCompiler::kProduceParserCache); |
| 223 CHECK(script_source.GetCachedData()); | 226 CHECK(script_source.GetCachedData()); |
| 224 | 227 |
| 225 // Compile the script again, using the cached data. | 228 // Compile the script again, using the cached data. |
| 226 bool lazy_flag = i::FLAG_lazy; | 229 bool lazy_flag = i::FLAG_lazy; |
| 227 i::FLAG_lazy = true; | 230 i::FLAG_lazy = true; |
| 228 v8::ScriptCompiler::Compile(isolate, &script_source, | 231 v8::ScriptCompiler::Compile(isolate, &script_source, |
| 229 v8::ScriptCompiler::kConsumeParserCache); | 232 v8::ScriptCompiler::kConsumeParserCache); |
| 230 i::FLAG_lazy = false; | 233 i::FLAG_lazy = false; |
| 231 v8::ScriptCompiler::CompileUnbound(isolate, &script_source, | 234 v8::ScriptCompiler::CompileUnbound(isolate, &script_source, |
| 232 v8::ScriptCompiler::kConsumeParserCache); | 235 v8::ScriptCompiler::kConsumeParserCache); |
| 233 i::FLAG_lazy = lazy_flag; | 236 i::FLAG_lazy = lazy_flag; |
| 234 } | 237 } |
| 235 | 238 |
| 236 | 239 |
| 237 TEST(PreparseFunctionDataIsUsed) { | 240 TEST(PreparseFunctionDataIsUsed) { |
| 238 // This tests that we actually do use the function data generated by the | 241 // This tests that we actually do use the function data generated by the |
| 239 // preparser. | 242 // preparser. |
| 240 | 243 |
| 241 // Make preparsing work for short scripts. | 244 // Make preparsing work for short scripts. |
| 242 i::FLAG_min_preparse_length = 0; | 245 i::FLAG_min_preparse_length = 0; |
| 246 i::FLAG_harmony_arrow_functions = true; |
| 243 | 247 |
| 244 v8::Isolate* isolate = CcTest::isolate(); | 248 v8::Isolate* isolate = CcTest::isolate(); |
| 245 v8::HandleScope handles(isolate); | 249 v8::HandleScope handles(isolate); |
| 246 v8::Local<v8::Context> context = v8::Context::New(isolate); | 250 v8::Local<v8::Context> context = v8::Context::New(isolate); |
| 247 v8::Context::Scope context_scope(context); | 251 v8::Context::Scope context_scope(context); |
| 248 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - | 252 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - |
| 249 128 * 1024); | 253 128 * 1024); |
| 250 | 254 |
| 251 const char* good_code = | 255 const char* good_code[] = { |
| 252 "function this_is_lazy() { var a; } function foo() { return 25; } foo();"; | 256 "function this_is_lazy() { var a; } function foo() { return 25; } foo();", |
| 257 "var this_is_lazy = () => { var a; }; var foo = () => 25; foo();", |
| 258 }; |
| 253 | 259 |
| 254 // Insert a syntax error inside the lazy function. | 260 // Insert a syntax error inside the lazy function. |
| 255 const char* bad_code = | 261 const char* bad_code[] = { |
| 256 "function this_is_lazy() { if ( } function foo() { return 25; } foo();"; | 262 "function this_is_lazy() { if ( } function foo() { return 25; } foo();", |
| 263 "var this_is_lazy = () => { if ( }; var foo = () => 25; foo();", |
| 264 }; |
| 257 | 265 |
| 258 v8::ScriptCompiler::Source good_source(v8_str(good_code)); | 266 for (unsigned i = 0; i < ARRAY_SIZE(good_code); i++) { |
| 259 v8::ScriptCompiler::Compile(isolate, &good_source, | 267 v8::ScriptCompiler::Source good_source(v8_str(good_code[i])); |
| 260 v8::ScriptCompiler::kProduceParserCache); | 268 v8::ScriptCompiler::Compile(isolate, &good_source, |
| 269 v8::ScriptCompiler::kProduceDataToCache); |
| 261 | 270 |
| 262 const v8::ScriptCompiler::CachedData* cached_data = | 271 const v8::ScriptCompiler::CachedData* cached_data = |
| 263 good_source.GetCachedData(); | 272 good_source.GetCachedData(); |
| 264 CHECK(cached_data->data != NULL); | 273 CHECK(cached_data->data != NULL); |
| 265 CHECK_GT(cached_data->length, 0); | 274 CHECK_GT(cached_data->length, 0); |
| 266 | 275 |
| 267 // Now compile the erroneous code with the good preparse data. If the preparse | 276 // Now compile the erroneous code with the good preparse data. If the |
| 268 // data is used, the lazy function is skipped and it should compile fine. | 277 // preparse data is used, the lazy function is skipped and it should |
| 269 v8::ScriptCompiler::Source bad_source( | 278 // compile fine. |
| 270 v8_str(bad_code), new v8::ScriptCompiler::CachedData( | 279 v8::ScriptCompiler::Source bad_source( |
| 271 cached_data->data, cached_data->length)); | 280 v8_str(bad_code[i]), new v8::ScriptCompiler::CachedData( |
| 272 v8::Local<v8::Value> result = | 281 cached_data->data, cached_data->length)); |
| 273 v8::ScriptCompiler::Compile( | 282 v8::Local<v8::Value> result = |
| 274 isolate, &bad_source, v8::ScriptCompiler::kConsumeParserCache)->Run(); | 283 v8::ScriptCompiler::Compile(isolate, &bad_source)->Run(); |
| 275 CHECK(result->IsInt32()); | 284 CHECK(result->IsInt32()); |
| 276 CHECK_EQ(25, result->Int32Value()); | 285 CHECK_EQ(25, result->Int32Value()); |
| 286 } |
| 277 } | 287 } |
| 278 | 288 |
| 279 | 289 |
| 280 TEST(StandAlonePreParser) { | 290 TEST(StandAlonePreParser) { |
| 281 v8::V8::Initialize(); | 291 v8::V8::Initialize(); |
| 282 | 292 |
| 283 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - | 293 CcTest::i_isolate()->stack_guard()->SetStackLimit(GetCurrentStackPosition() - |
| 284 128 * 1024); | 294 128 * 1024); |
| 285 | 295 |
| 286 const char* programs[] = { | 296 const char* programs[] = { |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 | 485 |
| 476 i::Utf8ToUtf16CharacterStream stream( | 486 i::Utf8ToUtf16CharacterStream stream( |
| 477 reinterpret_cast<const i::byte*>(program.get()), | 487 reinterpret_cast<const i::byte*>(program.get()), |
| 478 static_cast<unsigned>(kProgramSize)); | 488 static_cast<unsigned>(kProgramSize)); |
| 479 i::CompleteParserRecorder log; | 489 i::CompleteParserRecorder log; |
| 480 i::Scanner scanner(CcTest::i_isolate()->unicode_cache()); | 490 i::Scanner scanner(CcTest::i_isolate()->unicode_cache()); |
| 481 scanner.Initialize(&stream); | 491 scanner.Initialize(&stream); |
| 482 | 492 |
| 483 i::PreParser preparser(&scanner, &log, stack_limit); | 493 i::PreParser preparser(&scanner, &log, stack_limit); |
| 484 preparser.set_allow_lazy(true); | 494 preparser.set_allow_lazy(true); |
| 495 preparser.set_allow_arrow_functions(true); |
| 485 i::PreParser::PreParseResult result = preparser.PreParseProgram(); | 496 i::PreParser::PreParseResult result = preparser.PreParseProgram(); |
| 486 CHECK_EQ(i::PreParser::kPreParseStackOverflow, result); | 497 CHECK_EQ(i::PreParser::kPreParseStackOverflow, result); |
| 487 } | 498 } |
| 488 | 499 |
| 489 | 500 |
| 490 class TestExternalResource: public v8::String::ExternalStringResource { | 501 class TestExternalResource: public v8::String::ExternalStringResource { |
| 491 public: | 502 public: |
| 492 explicit TestExternalResource(uint16_t* data, int length) | 503 explicit TestExternalResource(uint16_t* data, int length) |
| 493 : data_(data), length_(static_cast<size_t>(length)) { } | 504 : data_(data), length_(static_cast<size_t>(length)) { } |
| 494 | 505 |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 952 " }", "\n" | 963 " }", "\n" |
| 953 " more;", i::BLOCK_SCOPE, i::STRICT }, | 964 " more;", i::BLOCK_SCOPE, i::STRICT }, |
| 954 { " start;\n" | 965 { " start;\n" |
| 955 " function fun", "(a,b) { infunction; }", " more;", | 966 " function fun", "(a,b) { infunction; }", " more;", |
| 956 i::FUNCTION_SCOPE, i::SLOPPY }, | 967 i::FUNCTION_SCOPE, i::SLOPPY }, |
| 957 { " start;\n" | 968 { " start;\n" |
| 958 " function fun", "(a,b) {\n" | 969 " function fun", "(a,b) {\n" |
| 959 " infunction;\n" | 970 " infunction;\n" |
| 960 " }", "\n" | 971 " }", "\n" |
| 961 " more;", i::FUNCTION_SCOPE, i::SLOPPY }, | 972 " more;", i::FUNCTION_SCOPE, i::SLOPPY }, |
| 962 { " (function fun", "(a,b) { infunction; }", ")();", | 973 // TODO(aperez): Change to use i::ARROW_SCOPE when implemented |
| 974 { " start;\n", "(a,b) => a + b", "; more;", |
| 975 i::FUNCTION_SCOPE, i::SLOPPY }, |
| 976 { " start;\n", "(a,b) => { return a+b; }", "\nmore;", |
| 977 i::FUNCTION_SCOPE, i::SLOPPY }, |
| 978 { " start;\n" |
| 979 " (function fun", "(a,b) { infunction; }", ")();", |
| 963 i::FUNCTION_SCOPE, i::SLOPPY }, | 980 i::FUNCTION_SCOPE, i::SLOPPY }, |
| 964 { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", " more;", | 981 { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", " more;", |
| 965 i::BLOCK_SCOPE, i::STRICT }, | 982 i::BLOCK_SCOPE, i::STRICT }, |
| 966 { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", "; more;", | 983 { " for ", "(let x = 1 ; x < 10; ++ x) { block; }", "; more;", |
| 967 i::BLOCK_SCOPE, i::STRICT }, | 984 i::BLOCK_SCOPE, i::STRICT }, |
| 968 { " for ", "(let x = 1 ; x < 10; ++ x) {\n" | 985 { " for ", "(let x = 1 ; x < 10; ++ x) {\n" |
| 969 " block;\n" | 986 " block;\n" |
| 970 " }", "\n" | 987 " }", "\n" |
| 971 " more;", i::BLOCK_SCOPE, i::STRICT }, | 988 " more;", i::BLOCK_SCOPE, i::STRICT }, |
| 972 { " for ", "(let x = 1 ; x < 10; ++ x) statement;", " more;", | 989 { " for ", "(let x = 1 ; x < 10; ++ x) statement;", " more;", |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1105 | 1122 |
| 1106 // Parse program source. | 1123 // Parse program source. |
| 1107 i::Handle<i::String> source = factory->NewStringFromUtf8( | 1124 i::Handle<i::String> source = factory->NewStringFromUtf8( |
| 1108 i::CStrVector(program.start())).ToHandleChecked(); | 1125 i::CStrVector(program.start())).ToHandleChecked(); |
| 1109 CHECK_EQ(source->length(), kProgramSize); | 1126 CHECK_EQ(source->length(), kProgramSize); |
| 1110 i::Handle<i::Script> script = factory->NewScript(source); | 1127 i::Handle<i::Script> script = factory->NewScript(source); |
| 1111 i::CompilationInfoWithZone info(script); | 1128 i::CompilationInfoWithZone info(script); |
| 1112 i::Parser parser(&info); | 1129 i::Parser parser(&info); |
| 1113 parser.set_allow_lazy(true); | 1130 parser.set_allow_lazy(true); |
| 1114 parser.set_allow_harmony_scoping(true); | 1131 parser.set_allow_harmony_scoping(true); |
| 1132 parser.set_allow_arrow_functions(true); |
| 1115 info.MarkAsGlobal(); | 1133 info.MarkAsGlobal(); |
| 1116 info.SetStrictMode(source_data[i].strict_mode); | 1134 info.SetStrictMode(source_data[i].strict_mode); |
| 1117 parser.Parse(); | 1135 parser.Parse(); |
| 1118 CHECK(info.function() != NULL); | 1136 CHECK(info.function() != NULL); |
| 1119 | 1137 |
| 1120 // Check scope types and positions. | 1138 // Check scope types and positions. |
| 1121 i::Scope* scope = info.function()->scope(); | 1139 i::Scope* scope = info.function()->scope(); |
| 1122 CHECK(scope->is_global_scope()); | 1140 CHECK(scope->is_global_scope()); |
| 1123 CHECK_EQ(scope->start_position(), 0); | 1141 CHECK_EQ(scope->start_position(), 0); |
| 1124 CHECK_EQ(scope->end_position(), kProgramSize); | 1142 CHECK_EQ(scope->end_position(), kProgramSize); |
| (...skipping 2019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3144 | 3162 |
| 3145 // Arrow has more precedence, this is the same as: foo ? bar : (baz = {}) | 3163 // Arrow has more precedence, this is the same as: foo ? bar : (baz = {}) |
| 3146 "foo ? bar : baz => {}", | 3164 "foo ? bar : baz => {}", |
| 3147 NULL | 3165 NULL |
| 3148 }; | 3166 }; |
| 3149 | 3167 |
| 3150 static const ParserFlag always_flags[] = {kAllowArrowFunctions}; | 3168 static const ParserFlag always_flags[] = {kAllowArrowFunctions}; |
| 3151 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, | 3169 RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0, |
| 3152 always_flags, ARRAY_SIZE(always_flags)); | 3170 always_flags, ARRAY_SIZE(always_flags)); |
| 3153 } | 3171 } |
| OLD | NEW |