| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 i::UnicodeCache unicode_cache; | 64 i::UnicodeCache unicode_cache; |
| 65 i::byte buffer[32]; | 65 i::byte buffer[32]; |
| 66 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { | 66 for (int i = 0; (key_token = keywords[i]).keyword != NULL; i++) { |
| 67 const i::byte* keyword = | 67 const i::byte* keyword = |
| 68 reinterpret_cast<const i::byte*>(key_token.keyword); | 68 reinterpret_cast<const i::byte*>(key_token.keyword); |
| 69 int length = i::StrLength(key_token.keyword); | 69 int length = i::StrLength(key_token.keyword); |
| 70 CHECK(static_cast<int>(sizeof(buffer)) >= length); | 70 CHECK(static_cast<int>(sizeof(buffer)) >= length); |
| 71 { | 71 { |
| 72 i::Utf8ToUtf16CharacterStream stream(keyword, length); | 72 i::Utf8ToUtf16CharacterStream stream(keyword, length); |
| 73 i::Scanner scanner(&unicode_cache); | 73 i::Scanner scanner(&unicode_cache); |
| 74 // The scanner should parse Harmony keywords for this test. | |
| 75 scanner.SetHarmonyModules(true); | |
| 76 scanner.Initialize(&stream); | 74 scanner.Initialize(&stream); |
| 77 CHECK_EQ(key_token.token, scanner.Next()); | 75 CHECK_EQ(key_token.token, scanner.Next()); |
| 78 CHECK_EQ(i::Token::EOS, scanner.Next()); | 76 CHECK_EQ(i::Token::EOS, scanner.Next()); |
| 79 } | 77 } |
| 80 // Removing characters will make keyword matching fail. | 78 // Removing characters will make keyword matching fail. |
| 81 { | 79 { |
| 82 i::Utf8ToUtf16CharacterStream stream(keyword, length - 1); | 80 i::Utf8ToUtf16CharacterStream stream(keyword, length - 1); |
| 83 i::Scanner scanner(&unicode_cache); | 81 i::Scanner scanner(&unicode_cache); |
| 84 scanner.Initialize(&stream); | 82 scanner.Initialize(&stream); |
| 85 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); | 83 CHECK_EQ(i::Token::IDENTIFIER, scanner.Next()); |
| (...skipping 1334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1420 } | 1418 } |
| 1421 | 1419 |
| 1422 data.Dispose(); | 1420 data.Dispose(); |
| 1423 return i::MessageTemplate::FormatMessage(isolate, message, arg_object); | 1421 return i::MessageTemplate::FormatMessage(isolate, message, arg_object); |
| 1424 } | 1422 } |
| 1425 | 1423 |
| 1426 | 1424 |
| 1427 enum ParserFlag { | 1425 enum ParserFlag { |
| 1428 kAllowLazy, | 1426 kAllowLazy, |
| 1429 kAllowNatives, | 1427 kAllowNatives, |
| 1430 kAllowHarmonyModules, | |
| 1431 kAllowHarmonyArrowFunctions, | 1428 kAllowHarmonyArrowFunctions, |
| 1432 kAllowHarmonyRestParameters, | 1429 kAllowHarmonyRestParameters, |
| 1433 kAllowHarmonySloppy, | 1430 kAllowHarmonySloppy, |
| 1434 kAllowHarmonySloppyLet, | 1431 kAllowHarmonySloppyLet, |
| 1435 kAllowHarmonyUnicode, | 1432 kAllowHarmonyUnicode, |
| 1436 kAllowHarmonyComputedPropertyNames, | 1433 kAllowHarmonyComputedPropertyNames, |
| 1437 kAllowHarmonySpreadCalls, | 1434 kAllowHarmonySpreadCalls, |
| 1438 kAllowHarmonyDestructuring, | 1435 kAllowHarmonyDestructuring, |
| 1439 kAllowHarmonySpreadArrays, | 1436 kAllowHarmonySpreadArrays, |
| 1440 kAllowHarmonyNewTarget, | 1437 kAllowHarmonyNewTarget, |
| 1441 kAllowStrongMode, | 1438 kAllowStrongMode, |
| 1442 kNoLegacyConst | 1439 kNoLegacyConst |
| 1443 }; | 1440 }; |
| 1444 | 1441 |
| 1445 | 1442 |
| 1446 enum ParserSyncTestResult { | 1443 enum ParserSyncTestResult { |
| 1447 kSuccessOrError, | 1444 kSuccessOrError, |
| 1448 kSuccess, | 1445 kSuccess, |
| 1449 kError | 1446 kError |
| 1450 }; | 1447 }; |
| 1451 | 1448 |
| 1452 template <typename Traits> | 1449 template <typename Traits> |
| 1453 void SetParserFlags(i::ParserBase<Traits>* parser, | 1450 void SetParserFlags(i::ParserBase<Traits>* parser, |
| 1454 i::EnumSet<ParserFlag> flags) { | 1451 i::EnumSet<ParserFlag> flags) { |
| 1455 parser->set_allow_lazy(flags.Contains(kAllowLazy)); | 1452 parser->set_allow_lazy(flags.Contains(kAllowLazy)); |
| 1456 parser->set_allow_natives(flags.Contains(kAllowNatives)); | 1453 parser->set_allow_natives(flags.Contains(kAllowNatives)); |
| 1457 parser->set_allow_harmony_modules(flags.Contains(kAllowHarmonyModules)); | |
| 1458 parser->set_allow_harmony_arrow_functions( | 1454 parser->set_allow_harmony_arrow_functions( |
| 1459 flags.Contains(kAllowHarmonyArrowFunctions)); | 1455 flags.Contains(kAllowHarmonyArrowFunctions)); |
| 1460 parser->set_allow_harmony_rest_parameters( | 1456 parser->set_allow_harmony_rest_parameters( |
| 1461 flags.Contains(kAllowHarmonyRestParameters)); | 1457 flags.Contains(kAllowHarmonyRestParameters)); |
| 1462 parser->set_allow_harmony_spreadcalls( | 1458 parser->set_allow_harmony_spreadcalls( |
| 1463 flags.Contains(kAllowHarmonySpreadCalls)); | 1459 flags.Contains(kAllowHarmonySpreadCalls)); |
| 1464 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); | 1460 parser->set_allow_harmony_sloppy(flags.Contains(kAllowHarmonySloppy)); |
| 1465 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); | 1461 parser->set_allow_harmony_sloppy_let(flags.Contains(kAllowHarmonySloppyLet)); |
| 1466 parser->set_allow_harmony_unicode(flags.Contains(kAllowHarmonyUnicode)); | 1462 parser->set_allow_harmony_unicode(flags.Contains(kAllowHarmonyUnicode)); |
| 1467 parser->set_allow_harmony_computed_property_names( | 1463 parser->set_allow_harmony_computed_property_names( |
| (...skipping 3837 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5305 static const ParserFlag always_flags[] = { | 5301 static const ParserFlag always_flags[] = { |
| 5306 kAllowHarmonyComputedPropertyNames, | 5302 kAllowHarmonyComputedPropertyNames, |
| 5307 kAllowHarmonySloppy, | 5303 kAllowHarmonySloppy, |
| 5308 }; | 5304 }; |
| 5309 RunParserSyncTest(context_data, error_data, kError, NULL, 0, | 5305 RunParserSyncTest(context_data, error_data, kError, NULL, 0, |
| 5310 always_flags, arraysize(always_flags)); | 5306 always_flags, arraysize(always_flags)); |
| 5311 } | 5307 } |
| 5312 | 5308 |
| 5313 | 5309 |
| 5314 TEST(BasicImportExportParsing) { | 5310 TEST(BasicImportExportParsing) { |
| 5311 i::FLAG_harmony_modules = true; |
| 5312 |
| 5315 const char* kSources[] = { | 5313 const char* kSources[] = { |
| 5316 "export let x = 0;", | 5314 "export let x = 0;", |
| 5317 "export var y = 0;", | 5315 "export var y = 0;", |
| 5318 "export const z = 0;", | 5316 "export const z = 0;", |
| 5319 "export function func() { };", | 5317 "export function func() { };", |
| 5320 "export class C { };", | 5318 "export class C { };", |
| 5321 "export { };", | 5319 "export { };", |
| 5322 "function f() {}; f(); export { f };", | 5320 "function f() {}; f(); export { f };", |
| 5323 "var a, b, c; export { a, b as baz, c };", | 5321 "var a, b, c; export { a, b as baz, c };", |
| 5324 "var d, e; export { d as dreary, e, };", | 5322 "var d, e; export { d as dreary, e, };", |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5364 for (unsigned i = 0; i < arraysize(kSources); ++i) { | 5362 for (unsigned i = 0; i < arraysize(kSources); ++i) { |
| 5365 i::Handle<i::String> source = | 5363 i::Handle<i::String> source = |
| 5366 factory->NewStringFromAsciiChecked(kSources[i]); | 5364 factory->NewStringFromAsciiChecked(kSources[i]); |
| 5367 | 5365 |
| 5368 // Show that parsing as a module works | 5366 // Show that parsing as a module works |
| 5369 { | 5367 { |
| 5370 i::Handle<i::Script> script = factory->NewScript(source); | 5368 i::Handle<i::Script> script = factory->NewScript(source); |
| 5371 i::Zone zone; | 5369 i::Zone zone; |
| 5372 i::ParseInfo info(&zone, script); | 5370 i::ParseInfo info(&zone, script); |
| 5373 i::Parser parser(&info); | 5371 i::Parser parser(&info); |
| 5374 parser.set_allow_harmony_modules(true); | |
| 5375 info.set_module(); | 5372 info.set_module(); |
| 5376 if (!parser.Parse(&info)) { | 5373 if (!parser.Parse(&info)) { |
| 5377 i::Handle<i::JSObject> exception_handle( | 5374 i::Handle<i::JSObject> exception_handle( |
| 5378 i::JSObject::cast(isolate->pending_exception())); | 5375 i::JSObject::cast(isolate->pending_exception())); |
| 5379 i::Handle<i::String> message_string = | 5376 i::Handle<i::String> message_string = |
| 5380 i::Handle<i::String>::cast(i::Object::GetProperty( | 5377 i::Handle<i::String>::cast(i::Object::GetProperty( |
| 5381 isolate, exception_handle, "message").ToHandleChecked()); | 5378 isolate, exception_handle, "message").ToHandleChecked()); |
| 5382 | 5379 |
| 5383 v8::base::OS::Print( | 5380 v8::base::OS::Print( |
| 5384 "Parser failed on:\n" | 5381 "Parser failed on:\n" |
| 5385 "\t%s\n" | 5382 "\t%s\n" |
| 5386 "with error:\n" | 5383 "with error:\n" |
| 5387 "\t%s\n" | 5384 "\t%s\n" |
| 5388 "However, we expected no error.", | 5385 "However, we expected no error.", |
| 5389 source->ToCString().get(), message_string->ToCString().get()); | 5386 source->ToCString().get(), message_string->ToCString().get()); |
| 5390 CHECK(false); | 5387 CHECK(false); |
| 5391 } | 5388 } |
| 5392 } | 5389 } |
| 5393 | 5390 |
| 5394 // And that parsing a script does not. | 5391 // And that parsing a script does not. |
| 5395 { | 5392 { |
| 5396 i::Handle<i::Script> script = factory->NewScript(source); | 5393 i::Handle<i::Script> script = factory->NewScript(source); |
| 5397 i::Zone zone; | 5394 i::Zone zone; |
| 5398 i::ParseInfo info(&zone, script); | 5395 i::ParseInfo info(&zone, script); |
| 5399 i::Parser parser(&info); | 5396 i::Parser parser(&info); |
| 5400 parser.set_allow_harmony_modules(true); | |
| 5401 info.set_global(); | 5397 info.set_global(); |
| 5402 CHECK(!parser.Parse(&info)); | 5398 CHECK(!parser.Parse(&info)); |
| 5403 } | 5399 } |
| 5404 } | 5400 } |
| 5405 } | 5401 } |
| 5406 | 5402 |
| 5407 | 5403 |
| 5408 TEST(ImportExportParsingErrors) { | 5404 TEST(ImportExportParsingErrors) { |
| 5405 i::FLAG_harmony_modules = true; |
| 5406 |
| 5409 const char* kErrorSources[] = { | 5407 const char* kErrorSources[] = { |
| 5410 "export {", | 5408 "export {", |
| 5411 "var a; export { a", | 5409 "var a; export { a", |
| 5412 "var a; export { a,", | 5410 "var a; export { a,", |
| 5413 "var a; export { a, ;", | 5411 "var a; export { a, ;", |
| 5414 "var a; export { a as };", | 5412 "var a; export { a as };", |
| 5415 "var a, b; export { a as , b};", | 5413 "var a, b; export { a as , b};", |
| 5416 "export }", | 5414 "export }", |
| 5417 "var foo, bar; export { foo bar };", | 5415 "var foo, bar; export { foo bar };", |
| 5418 "export { foo };", | 5416 "export { foo };", |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5479 128 * 1024); | 5477 128 * 1024); |
| 5480 | 5478 |
| 5481 for (unsigned i = 0; i < arraysize(kErrorSources); ++i) { | 5479 for (unsigned i = 0; i < arraysize(kErrorSources); ++i) { |
| 5482 i::Handle<i::String> source = | 5480 i::Handle<i::String> source = |
| 5483 factory->NewStringFromAsciiChecked(kErrorSources[i]); | 5481 factory->NewStringFromAsciiChecked(kErrorSources[i]); |
| 5484 | 5482 |
| 5485 i::Handle<i::Script> script = factory->NewScript(source); | 5483 i::Handle<i::Script> script = factory->NewScript(source); |
| 5486 i::Zone zone; | 5484 i::Zone zone; |
| 5487 i::ParseInfo info(&zone, script); | 5485 i::ParseInfo info(&zone, script); |
| 5488 i::Parser parser(&info); | 5486 i::Parser parser(&info); |
| 5489 parser.set_allow_harmony_modules(true); | |
| 5490 info.set_module(); | 5487 info.set_module(); |
| 5491 CHECK(!parser.Parse(&info)); | 5488 CHECK(!parser.Parse(&info)); |
| 5492 } | 5489 } |
| 5493 } | 5490 } |
| 5494 | 5491 |
| 5495 | 5492 |
| 5496 TEST(ModuleParsingInternals) { | 5493 TEST(ModuleParsingInternals) { |
| 5497 i::FLAG_harmony_modules = true; | 5494 i::FLAG_harmony_modules = true; |
| 5498 | 5495 |
| 5499 i::Isolate* isolate = CcTest::i_isolate(); | 5496 i::Isolate* isolate = CcTest::i_isolate(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 5510 "import { q as z } from 'm.js';" | 5507 "import { q as z } from 'm.js';" |
| 5511 "import n from 'n.js';" | 5508 "import n from 'n.js';" |
| 5512 "export { a as b } from 'm.js';" | 5509 "export { a as b } from 'm.js';" |
| 5513 "export * from 'p.js';" | 5510 "export * from 'p.js';" |
| 5514 "import 'q.js'"; | 5511 "import 'q.js'"; |
| 5515 i::Handle<i::String> source = factory->NewStringFromAsciiChecked(kSource); | 5512 i::Handle<i::String> source = factory->NewStringFromAsciiChecked(kSource); |
| 5516 i::Handle<i::Script> script = factory->NewScript(source); | 5513 i::Handle<i::Script> script = factory->NewScript(source); |
| 5517 i::Zone zone; | 5514 i::Zone zone; |
| 5518 i::ParseInfo info(&zone, script); | 5515 i::ParseInfo info(&zone, script); |
| 5519 i::Parser parser(&info); | 5516 i::Parser parser(&info); |
| 5520 parser.set_allow_harmony_modules(true); | |
| 5521 info.set_module(); | 5517 info.set_module(); |
| 5522 CHECK(parser.Parse(&info)); | 5518 CHECK(parser.Parse(&info)); |
| 5523 CHECK(i::Compiler::Analyze(&info)); | 5519 CHECK(i::Compiler::Analyze(&info)); |
| 5524 i::FunctionLiteral* func = info.function(); | 5520 i::FunctionLiteral* func = info.function(); |
| 5525 i::Scope* module_scope = func->scope(); | 5521 i::Scope* module_scope = func->scope(); |
| 5526 i::Scope* outer_scope = module_scope->outer_scope(); | 5522 i::Scope* outer_scope = module_scope->outer_scope(); |
| 5527 CHECK(outer_scope->is_script_scope()); | 5523 CHECK(outer_scope->is_script_scope()); |
| 5528 CHECK_NULL(outer_scope->outer_scope()); | 5524 CHECK_NULL(outer_scope->outer_scope()); |
| 5529 CHECK_EQ(1, outer_scope->num_modules()); | 5525 CHECK_EQ(1, outer_scope->num_modules()); |
| 5530 CHECK(module_scope->is_module_scope()); | 5526 CHECK(module_scope->is_module_scope()); |
| (...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6841 "for (let x of []) {}", | 6837 "for (let x of []) {}", |
| 6842 NULL | 6838 NULL |
| 6843 }; | 6839 }; |
| 6844 // clang-format on | 6840 // clang-format on |
| 6845 | 6841 |
| 6846 static const ParserFlag always_flags[] = {kAllowHarmonySloppy, | 6842 static const ParserFlag always_flags[] = {kAllowHarmonySloppy, |
| 6847 kAllowHarmonySloppyLet}; | 6843 kAllowHarmonySloppyLet}; |
| 6848 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags, | 6844 RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags, |
| 6849 arraysize(always_flags)); | 6845 arraysize(always_flags)); |
| 6850 } | 6846 } |
| OLD | NEW |