aboutsummaryrefslogtreecommitdiff
path: root/test/script/basic/NASHORN-207_2.js
blob: 681a3c28b72a9a3d73d081d92370fa3e1c236864 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 * 
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 * 
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 * 
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * NASHORN-207 : Implement strict mode.
 *
 * @test
 * @run
 */

// make sure that 'use strict' as first directive inside eval
// also works the same way (as eval called from strict mode caller).

try {
    eval("'use strict'; foo = 4;");
    fail("#1 should have thrown ReferenceError");
} catch (e) {
    if (! (e instanceof ReferenceError)) {
        fail("#2 expected ReferenceError but got " + e);
    }
}

if (typeof foo !== 'undefined') {
    fail("#3 strict mode eval defined var in global scope!");
}

try {
    eval("'use strict'; var let = 23;");
    fail("#4 should have thrown SyntaxError");
} catch (e) {
    if (! (e instanceof SyntaxError)) {
        fail("#5 SyntaxError expected but got " + e);
    }
}

// non-strict mode, some of the future reserved words can be used
// as identifiers. These include "let", "implements", "yield" etc.
var let = 30;
var implements = "hello";
function yield() {}
function public() {}
var private = false;
var protected = "hello";
var interface = "interface";
function f(package) {}
function static() {}

// in strict mode, arguments does not alias named access
function func(x, y) {
    'use strict';

    if (x !== arguments[0]) {
        fail("#6 arguments[0] !== x");
    }

    if (y !== arguments[1]) {
        fail("#7 arguments[1] !== y");
    }

    arguments[0] = 1;
    arguments[1] = 2;

    if (x === arguments[0]) {
        fail("#8 arguments[0] === x after assignment to it");
    }

    if (y === arguments[1]) {
        fail("#9 arguments[1] === y after assignment to it ");
    }
}

func();

// functions can not be declared everywhere!!
try {
    eval("'use strict'; if (true) { function func() {} }");
    fail("#10 should have thrown SyntaxError");
} catch (e) {
    if (! (e instanceof SyntaxError)) {
        fail("#11 SyntaxError expected got " + e);
    }
}

// arguments.caller and arguments.callee can't read or written in strict mode
function func2() {
    'use strict';

    try {
        print(arguments.callee);
        fail("#12 arguments.callee should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#13 TypeError expected, got " + e);
        }
    }

    try {
        print(arguments.caller);
        fail("#14 arguments.caller should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#15 TypeError expected, got " + e);
        }
    }

    try {
        arguments.caller = 10;
        fail("#16 arguments.caller assign should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#17 TypeError expected, got " + e);
        }
    }

    try {
        arguments.callee = true;
        fail("#18 arguments.callee assign should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#19 TypeError expected, got " + e);
        }
    }
}

func2();

// func.caller and func.arguments can't read or written in strict mode
function func3() {
    'use strict';

    try {
        print(func3.arguments);
        fail("#20 func.arguments should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#21 TypeError expected, got " + e);
        }
    }

    try {
        print(func3.caller);
        fail("#22 func3.caller should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#23 TypeError expected, got " + e);
        }
    }

    try {
        func3.arguments = 10;
        fail("#24 func3.arguments assign should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#25 TypeError expected, got " + e);
        }
    }

    try {
        func3.caller = true;
        fail("#26 func3.caller assign should have thrown TypeError");
    } catch (e) {
        if (! (e instanceof TypeError)) {
            fail("#27 TypeError expected, got " + e);
        }
    }
}

func3();

try {
    eval("function eval() { 'use strict'; }");
    fail("#28 should have thrown SyntaxError");
} catch (e) {
    if (! (e instanceof SyntaxError)) {
        fail("#29 SyntaxError expected, got " + e);
    }
}

function func4() {
  'use \
strict';

    // The use strict directive can't contain line continuation.
    // So this is not a strict mode function!!
    with({}) {}
}

func4();

function func5() {
   'use\u2028strict';

    // The use strict directive can't contain unicode whitespace escapes
    // So this is not a strict mode function!!
    with({}) {}
}

func5();

function func6() {
   'use\u2029strict';

    // The use strict directive can't contain unicode whitespace escapes
    // So this is not a strict mode function!!
    with({}) {}
}

func6();

try {
    eval("'bogus directive'; 'use strict'; eval = 10");
    fail("#30 SyntaxError expected from eval");
} catch (e) {
    if (! (e instanceof SyntaxError)) {
        fail("#31 SyntaxError expected but got " + e);
    }
}