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 5620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5631 "(a/*\n*/=> a)(1)", | 5631 "(a/*\n*/=> a)(1)", |
5632 "((a)\n=> a)(1)", | 5632 "((a)\n=> a)(1)", |
5633 "((a)/*\n*/=> a)(1)", | 5633 "((a)/*\n*/=> a)(1)", |
5634 "((a, b)\n=> a + b)(1, 2)", | 5634 "((a, b)\n=> a + b)(1, 2)", |
5635 "((a, b)/*\n*/=> a + b)(1, 2)", | 5635 "((a, b)/*\n*/=> a + b)(1, 2)", |
5636 NULL}; | 5636 NULL}; |
5637 static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions}; | 5637 static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions}; |
5638 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, | 5638 RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags, |
5639 arraysize(always_flags)); | 5639 arraysize(always_flags)); |
5640 } | 5640 } |
| 5641 |
| 5642 |
| 5643 TEST(StrongModeFreeVariablesDeclaredByPreviousScript) { |
| 5644 i::FLAG_strong_mode = true; |
| 5645 v8::V8::Initialize(); |
| 5646 v8::HandleScope scope(CcTest::isolate()); |
| 5647 v8::Context::Scope context_scope(v8::Context::New(CcTest::isolate())); |
| 5648 v8::TryCatch try_catch; |
| 5649 |
| 5650 // Introduce a bunch of variables, in all language modes. |
| 5651 const char* script1 = |
| 5652 "var my_var1 = 0; \n" |
| 5653 "function my_func1() { } \n" |
| 5654 "const my_const1 = 0; \n"; |
| 5655 CompileRun(v8_str(script1)); |
| 5656 CHECK(!try_catch.HasCaught()); |
| 5657 |
| 5658 const char* script2 = |
| 5659 "\"use strict\"; \n" |
| 5660 "let my_var2 = 0; \n" |
| 5661 "function my_func2() { } \n" |
| 5662 "const my_const2 = 0 \n"; |
| 5663 CompileRun(v8_str(script2)); |
| 5664 CHECK(!try_catch.HasCaught()); |
| 5665 |
| 5666 const char* script3 = |
| 5667 "\"use strong\"; \n" |
| 5668 "let my_var3 = 0; \n" |
| 5669 "function my_func3() { } \n" |
| 5670 "const my_const3 = 0; \n"; |
| 5671 CompileRun(v8_str(script3)); |
| 5672 CHECK(!try_catch.HasCaught()); |
| 5673 |
| 5674 // Sloppy eval introduces variables in the surrounding scope. |
| 5675 const char* script4 = |
| 5676 "eval('var my_var4 = 0;') \n" |
| 5677 "eval('function my_func4() { }') \n" |
| 5678 "eval('const my_const4 = 0;') \n"; |
| 5679 CompileRun(v8_str(script4)); |
| 5680 CHECK(!try_catch.HasCaught()); |
| 5681 |
| 5682 // Test that referencing these variables work. |
| 5683 const char* script5 = |
| 5684 "\"use strong\"; \n" |
| 5685 "my_var1; \n" |
| 5686 "my_func1; \n" |
| 5687 "my_const1; \n" |
| 5688 "my_var2; \n" |
| 5689 "my_func2; \n" |
| 5690 "my_const2; \n" |
| 5691 "my_var3; \n" |
| 5692 "my_func3; \n" |
| 5693 "my_const3; \n" |
| 5694 "my_var4; \n" |
| 5695 "my_func4; \n" |
| 5696 "my_const4; \n"; |
| 5697 CompileRun(v8_str(script5)); |
| 5698 CHECK(!try_catch.HasCaught()); |
| 5699 } |
| 5700 |
| 5701 |
| 5702 TEST(StrongModeFreeVariablesDeclaredByLanguage) { |
| 5703 i::FLAG_strong_mode = true; |
| 5704 v8::V8::Initialize(); |
| 5705 v8::HandleScope scope(CcTest::isolate()); |
| 5706 v8::Context::Scope context_scope(v8::Context::New(CcTest::isolate())); |
| 5707 v8::TryCatch try_catch; |
| 5708 |
| 5709 const char* script1 = |
| 5710 "\"use strong\"; \n" |
| 5711 "Math; \n" |
| 5712 "RegExp; \n"; |
| 5713 CompileRun(v8_str(script1)); |
| 5714 CHECK(!try_catch.HasCaught()); |
| 5715 } |
| 5716 |
| 5717 |
| 5718 TEST(StrongModeFreeVariablesDeclaredInGlobalPrototype) { |
| 5719 i::FLAG_strong_mode = true; |
| 5720 v8::V8::Initialize(); |
| 5721 v8::HandleScope scope(CcTest::isolate()); |
| 5722 v8::Context::Scope context_scope(v8::Context::New(CcTest::isolate())); |
| 5723 v8::TryCatch try_catch; |
| 5724 |
| 5725 const char* script1 = "this.__proto__.my_var = 0;\n"; |
| 5726 CompileRun(v8_str(script1)); |
| 5727 CHECK(!try_catch.HasCaught()); |
| 5728 |
| 5729 const char* script2 = |
| 5730 "\"use strong\"; \n" |
| 5731 "my_var; \n"; |
| 5732 CompileRun(v8_str(script2)); |
| 5733 CHECK(!try_catch.HasCaught()); |
| 5734 } |
| 5735 |
| 5736 |
| 5737 TEST(StrongModeFreeVariablesNotDeclared) { |
| 5738 i::FLAG_strong_mode = true; |
| 5739 v8::V8::Initialize(); |
| 5740 v8::HandleScope scope(CcTest::isolate()); |
| 5741 v8::Context::Scope context_scope(v8::Context::New(CcTest::isolate())); |
| 5742 v8::TryCatch try_catch; |
| 5743 |
| 5744 // Test that referencing unintroduced variables in sloppy mode is ok. |
| 5745 const char* script1 = |
| 5746 "if (false) { \n" |
| 5747 " not_there1; \n" |
| 5748 "} \n"; |
| 5749 CompileRun(v8_str(script1)); |
| 5750 CHECK(!try_catch.HasCaught()); |
| 5751 |
| 5752 // But not in strong mode. |
| 5753 { |
| 5754 const char* script2 = |
| 5755 "\"use strong\"; \n" |
| 5756 "if (false) { \n" |
| 5757 " not_there2; \n" |
| 5758 "} \n"; |
| 5759 v8::TryCatch try_catch2; |
| 5760 v8::Script::Compile(v8_str(script2)); |
| 5761 CHECK(try_catch2.HasCaught()); |
| 5762 v8::String::Utf8Value exception(try_catch2.Exception()); |
| 5763 CHECK_EQ(0, |
| 5764 strcmp( |
| 5765 "ReferenceError: In strong mode, using an undeclared global " |
| 5766 "variable 'not_there2' is not allowed", |
| 5767 *exception)); |
| 5768 } |
| 5769 |
| 5770 // Check that the variable reference is detected inside a strong function too, |
| 5771 // even if the script scope is not strong. |
| 5772 { |
| 5773 const char* script3 = |
| 5774 "(function not_lazy() { \n" |
| 5775 " \"use strong\"; \n" |
| 5776 " if (false) { \n" |
| 5777 " not_there3; \n" |
| 5778 " } \n" |
| 5779 "})(); \n"; |
| 5780 v8::TryCatch try_catch2; |
| 5781 v8::Script::Compile(v8_str(script3)); |
| 5782 CHECK(try_catch2.HasCaught()); |
| 5783 v8::String::Utf8Value exception(try_catch2.Exception()); |
| 5784 CHECK_EQ(0, |
| 5785 strcmp( |
| 5786 "ReferenceError: In strong mode, using an undeclared global " |
| 5787 "variable 'not_there3' is not allowed", |
| 5788 *exception)); |
| 5789 } |
| 5790 } |
OLD | NEW |