NASHORN-275.js revision 6:5a1b0714df0e
1281494Sandrew/*
2281494Sandrew * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3281494Sandrew * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4281494Sandrew *
5281494Sandrew * This code is free software; you can redistribute it and/or modify it
6281494Sandrew * under the terms of the GNU General Public License version 2 only, as
7281494Sandrew * published by the Free Software Foundation.
8281494Sandrew *
9281494Sandrew * This code is distributed in the hope that it will be useful, but WITHOUT
10281494Sandrew * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11281494Sandrew * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12281494Sandrew * version 2 for more details (a copy is included in the LICENSE file that
13281494Sandrew * accompanied this code).
14281494Sandrew *
15281494Sandrew * You should have received a copy of the GNU General Public License version
16281494Sandrew * 2 along with this work; if not, write to the Free Software Foundation,
17281494Sandrew * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18281494Sandrew *
19281494Sandrew * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20281494Sandrew * or visit www.oracle.com if you need additional information or have any
21281494Sandrew * questions.
22281494Sandrew */
23281494Sandrew
24281494Sandrew/**
25281494Sandrew * NASHORN-275 : strict eval receives wrong 'this' object
26281494Sandrew *
27281494Sandrew * @test
28281494Sandrew * @run
29281494Sandrew */
30281494Sandrew
31281494Sandrewvar global = this;
32281494Sandrew
33281494Sandrewfunction func() {
34281494Sandrew    "use strict";
35281494Sandrew
36281494Sandrew    if (eval("this") === global) {
37281494Sandrew        fail("#1 strict eval gets 'global' as 'this'");
38281494Sandrew    }
39281494Sandrew
40281494Sandrew    if (eval ("typeof this") !== 'undefined') {
41281494Sandrew        fail("#2 typeof this is not undefined in strict eval");
42281494Sandrew    }
43281494Sandrew}
44281494Sandrew
45281494Sandrewfunc();
46281494Sandrew
47281494Sandrewvar global = this;
48281494Sandrewif (eval("\"use strict\";this") !== this) {
49281494Sandrew    fail("#3 strict mode eval receives wrong 'this'");
50281494Sandrew}
51281494Sandrew
52281494Sandrewvar obj = {
53281494Sandrew  func: function() {
54281494Sandrew      if (eval('"use strict"; this') !== obj) {
55281494Sandrew          fail("#4 strict mode eval receives wrong 'this'");
56281494Sandrew      }
57281494Sandrew  }
58281494Sandrew};
59281494Sandrew
60281494Sandrewobj.func();
61281494Sandrew
62281494Sandrewfunction func2() {
63281494Sandrew   'use strict';
64281494Sandrew   return eval('this');
65281494Sandrew}
66281494Sandrew
67281494Sandrewfunc2.call(null);
68281494Sandrewif (func2.call(null) !== null) {
69281494Sandrew    fail("#5 strict mode eval receives wrong 'this'");
70281494Sandrew}
71281494Sandrew
72281494Sandrewif (func2.call('hello') !== 'hello') {
73281494Sandrew    fail("#6 strict mode eval receives wrong 'this'");
74281494Sandrew}
75281494Sandrew
76281494Sandrew// indirect eval
77281494Sandrewvar my_eval = eval;
78281494Sandrewif (my_eval("'use strict'; this; ") !== this) {
79281494Sandrew    fail("#7 strict mode eval receives wrong 'this'");
80281494Sandrew}
81281494Sandrew
82281494Sandrew