blob: cc3d7bb582ff8108133a76b19fd7f2ac439ec808 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001# Copyright 2011 the V8 project authors. All rights reserved.
2# Redistribution and use in source and binary forms, with or without
3# modification, are permitted provided that the following conditions are
4# met:
5#
6# * Redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer.
8# * Redistributions in binary form must reproduce the above
9# copyright notice, this list of conditions and the following
10# disclaimer in the documentation and/or other materials provided
11# with the distribution.
12# * Neither the name of Google Inc. nor the names of its
13# contributors may be used to endorse or promote products derived
14# from this software without specific prior written permission.
15#
16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28# In strict mode, function declarations may only appear as source elements.
29
30# A template that performs the same strict-mode test in different
31# scopes (global scope, function scope, and nested function scope).
Emily Bernierd0a1eb72015-03-24 16:35:39 -040032def StrictTest(name, source, legacy):
33 if legacy:
34 extra_flags = [
35 "--noharmony-scoping",
36 "--noharmony-classes",
37 "--noharmony-object-literals"]
38 else:
39 extra_flags = []
40 Test(name, '"use strict";\n' + source, "strict_function",
41 extra_flags)
Ben Murdoch257744e2011-11-30 15:57:28 +000042 Test(name + '-infunc',
43 'function foo() {\n "use strict";\n' + source +'\n}\n',
Emily Bernierd0a1eb72015-03-24 16:35:39 -040044 "strict_function",
45 extra_flags)
Ben Murdoch257744e2011-11-30 15:57:28 +000046 Test(name + '-infunc2',
47 'function foo() {\n "use strict";\n function bar() {\n' +
48 source +'\n }\n}\n',
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 "strict_function",
50 extra_flags)
Ben Murdoch257744e2011-11-30 15:57:28 +000051
52# Not testing with-scope, since with is not allowed in strict mode at all.
53
54StrictTest("block", """
55 { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040056""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000057
58StrictTest("try-w-catch", """
59 try { function foo() { } } catch (e) { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000061
62StrictTest("try-w-finally", """
63 try { function foo() { } } finally { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040064""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000065
66StrictTest("catch", """
67 try { } catch (e) { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000069
70StrictTest("finally", """
71 try { } finally { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040072""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000073
74StrictTest("for", """
75 for (;;) { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040076""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000077
78StrictTest("while", """
79 while (true) { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040080""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000081
82StrictTest("do", """
83 do { function foo() { } } while (true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040084""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000085
86StrictTest("then", """
87 if (true) { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040088""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000089
90
91StrictTest("then-w-else", """
92 if (true) { function foo() { } } else { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040093""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000094
95
96StrictTest("else", """
97 if (true) { } else { function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040098""", True)
Ben Murdoch257744e2011-11-30 15:57:28 +000099
100StrictTest("switch-case", """
101 switch (true) { case true: function foo() { } }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400102""", False)
Ben Murdoch257744e2011-11-30 15:57:28 +0000103
104StrictTest("labeled", """
105 label: function foo() { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400106""", False)
Ben Murdoch257744e2011-11-30 15:57:28 +0000107
108
109