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 4653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4664 kAllowHarmonyComputedPropertyNames, | 4664 kAllowHarmonyComputedPropertyNames, |
4665 kAllowHarmonyObjectLiterals, | 4665 kAllowHarmonyObjectLiterals, |
4666 kAllowHarmonySloppy, | 4666 kAllowHarmonySloppy, |
4667 }; | 4667 }; |
4668 RunParserSyncTest(context_data, error_data, kError, NULL, 0, | 4668 RunParserSyncTest(context_data, error_data, kError, NULL, 0, |
4669 always_flags, arraysize(always_flags)); | 4669 always_flags, arraysize(always_flags)); |
4670 } | 4670 } |
4671 | 4671 |
4672 | 4672 |
4673 TEST(BasicImportExportParsing) { | 4673 TEST(BasicImportExportParsing) { |
4674 const char kSource[] = | 4674 const char* kSources[] = { |
4675 "export let x = 0;" | 4675 "export let x = 0;", |
4676 "import y from 'http://module.com/foo.js';" | 4676 "export var y = 0;", |
4677 "function f() {};" | 4677 "export const z = 0;", |
4678 "f();"; | 4678 "export function func() { };", |
| 4679 "export class C { };", |
| 4680 "export { };", |
| 4681 "function f() {}; f(); export { f };", |
| 4682 "var a, b, c; export { a, b as baz, c };", |
| 4683 "var d, e; export { d as dreary, e, };", |
| 4684 "import y from 'http://module.com/foo.js';", |
| 4685 "export default function f() {}", |
| 4686 "export default class C {}", |
| 4687 "export default 42", |
| 4688 "var x; export default x = 7", |
| 4689 "export { Q } from 'somemodule.js';", |
| 4690 "export * from 'somemodule.js';" |
| 4691 }; |
4679 | 4692 |
4680 i::Isolate* isolate = CcTest::i_isolate(); | 4693 i::Isolate* isolate = CcTest::i_isolate(); |
4681 i::Factory* factory = isolate->factory(); | 4694 i::Factory* factory = isolate->factory(); |
4682 | 4695 |
4683 v8::HandleScope handles(CcTest::isolate()); | 4696 v8::HandleScope handles(CcTest::isolate()); |
4684 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); | 4697 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
4685 v8::Context::Scope context_scope(context); | 4698 v8::Context::Scope context_scope(context); |
4686 | 4699 |
4687 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - | 4700 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
4688 128 * 1024); | 4701 128 * 1024); |
4689 | 4702 |
4690 int kProgramByteSize = i::StrLength(kSource); | 4703 for (unsigned i = 0; i < arraysize(kSources); ++i) { |
4691 i::ScopedVector<char> program(kProgramByteSize + 1); | 4704 int kProgramByteSize = i::StrLength(kSources[i]); |
4692 i::SNPrintF(program, "%s", kSource); | 4705 i::ScopedVector<char> program(kProgramByteSize + 1); |
4693 i::Handle<i::String> source = | 4706 i::SNPrintF(program, "%s", kSources[i]); |
4694 factory->NewStringFromUtf8(i::CStrVector(program.start())) | 4707 i::Handle<i::String> source = |
4695 .ToHandleChecked(); | 4708 factory->NewStringFromUtf8(i::CStrVector(program.start())) |
| 4709 .ToHandleChecked(); |
4696 | 4710 |
4697 // Show that parsing as a module works | 4711 // Show that parsing as a module works |
4698 { | 4712 { |
| 4713 i::Handle<i::Script> script = factory->NewScript(source); |
| 4714 i::CompilationInfoWithZone info(script); |
| 4715 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), |
| 4716 isolate->heap()->HashSeed(), |
| 4717 isolate->unicode_cache()}; |
| 4718 i::Parser parser(&info, &parse_info); |
| 4719 parser.set_allow_harmony_classes(true); |
| 4720 parser.set_allow_harmony_modules(true); |
| 4721 parser.set_allow_harmony_scoping(true); |
| 4722 info.MarkAsModule(); |
| 4723 CHECK(parser.Parse()); |
| 4724 } |
| 4725 |
| 4726 // And that parsing a script does not. |
| 4727 { |
| 4728 i::Handle<i::Script> script = factory->NewScript(source); |
| 4729 i::CompilationInfoWithZone info(script); |
| 4730 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), |
| 4731 isolate->heap()->HashSeed(), |
| 4732 isolate->unicode_cache()}; |
| 4733 i::Parser parser(&info, &parse_info); |
| 4734 parser.set_allow_harmony_classes(true); |
| 4735 parser.set_allow_harmony_modules(true); |
| 4736 parser.set_allow_harmony_scoping(true); |
| 4737 info.MarkAsGlobal(); |
| 4738 CHECK(!parser.Parse()); |
| 4739 } |
| 4740 } |
| 4741 } |
| 4742 |
| 4743 |
| 4744 TEST(ImportExportParsingErrors) { |
| 4745 const char* kErrorSources[] = { |
| 4746 "export {", |
| 4747 "var a; export { a", |
| 4748 "var a; export { a,", |
| 4749 "var a; export { a, ;", |
| 4750 "var a; export { a as };", |
| 4751 "var a, b; export { a as , b};", |
| 4752 "export }", |
| 4753 "var foo, bar; export { foo bar };", |
| 4754 "export { foo };", |
| 4755 "export { , };", |
| 4756 "export default;", |
| 4757 "export default var x = 7;", |
| 4758 "export default let x = 7;", |
| 4759 "export default const x = 7;", |
| 4760 "export *;", |
| 4761 "export * from;", |
| 4762 "export { Q } from;", |
| 4763 "export default from 'module.js';", |
| 4764 |
| 4765 // TODO(ES6): These two forms should be supported |
| 4766 "export default function() {};", |
| 4767 "export default class {};" |
| 4768 }; |
| 4769 |
| 4770 i::Isolate* isolate = CcTest::i_isolate(); |
| 4771 i::Factory* factory = isolate->factory(); |
| 4772 |
| 4773 v8::HandleScope handles(CcTest::isolate()); |
| 4774 v8::Handle<v8::Context> context = v8::Context::New(CcTest::isolate()); |
| 4775 v8::Context::Scope context_scope(context); |
| 4776 |
| 4777 isolate->stack_guard()->SetStackLimit(i::GetCurrentStackPosition() - |
| 4778 128 * 1024); |
| 4779 |
| 4780 for (unsigned i = 0; i < arraysize(kErrorSources); ++i) { |
| 4781 int kProgramByteSize = i::StrLength(kErrorSources[i]); |
| 4782 i::ScopedVector<char> program(kProgramByteSize + 1); |
| 4783 i::SNPrintF(program, "%s", kErrorSources[i]); |
| 4784 i::Handle<i::String> source = |
| 4785 factory->NewStringFromUtf8(i::CStrVector(program.start())) |
| 4786 .ToHandleChecked(); |
| 4787 |
4699 i::Handle<i::Script> script = factory->NewScript(source); | 4788 i::Handle<i::Script> script = factory->NewScript(source); |
4700 i::CompilationInfoWithZone info(script); | 4789 i::CompilationInfoWithZone info(script); |
4701 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), | 4790 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), |
4702 isolate->heap()->HashSeed(), | 4791 isolate->heap()->HashSeed(), |
4703 isolate->unicode_cache()}; | 4792 isolate->unicode_cache()}; |
4704 i::Parser parser(&info, &parse_info); | 4793 i::Parser parser(&info, &parse_info); |
| 4794 parser.set_allow_harmony_classes(true); |
4705 parser.set_allow_harmony_modules(true); | 4795 parser.set_allow_harmony_modules(true); |
4706 parser.set_allow_harmony_scoping(true); | 4796 parser.set_allow_harmony_scoping(true); |
4707 info.MarkAsModule(); | 4797 info.MarkAsModule(); |
4708 CHECK(parser.Parse()); | |
4709 } | |
4710 | |
4711 // And that parsing a script does not. | |
4712 { | |
4713 i::Handle<i::Script> script = factory->NewScript(source); | |
4714 i::CompilationInfoWithZone info(script); | |
4715 i::Parser::ParseInfo parse_info = {isolate->stack_guard()->real_climit(), | |
4716 isolate->heap()->HashSeed(), | |
4717 isolate->unicode_cache()}; | |
4718 i::Parser parser(&info, &parse_info); | |
4719 parser.set_allow_harmony_modules(true); | |
4720 parser.set_allow_harmony_scoping(true); | |
4721 info.MarkAsGlobal(); | |
4722 CHECK(!parser.Parse()); | 4798 CHECK(!parser.Parse()); |
4723 } | 4799 } |
4724 } | 4800 } |
OLD | NEW |