NASHORN-275.js revision 6:5a1b0714df0e
12786Ssos/*
22786Ssos * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
32786Ssos * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
42786Ssos *
52786Ssos * This code is free software; you can redistribute it and/or modify it
62786Ssos * under the terms of the GNU General Public License version 2 only, as
72786Ssos * published by the Free Software Foundation.
82786Ssos *
92786Ssos * This code is distributed in the hope that it will be useful, but WITHOUT
102786Ssos * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
112786Ssos * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
122786Ssos * version 2 for more details (a copy is included in the LICENSE file that
132786Ssos * accompanied this code).
142786Ssos *
152786Ssos * You should have received a copy of the GNU General Public License version
162786Ssos * 2 along with this work; if not, write to the Free Software Foundation,
172786Ssos * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
182786Ssos *
192786Ssos * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202786Ssos * or visit www.oracle.com if you need additional information or have any
212786Ssos * questions.
222786Ssos */
232786Ssos
242786Ssos/**
252786Ssos * NASHORN-275 : strict eval receives wrong 'this' object
262786Ssos *
272786Ssos * @test
282786Ssos * @run
292786Ssos */
302786Ssos
312786Ssosvar global = this;
322786Ssos
332786Ssosfunction func() {
342786Ssos    "use strict";
352786Ssos
362786Ssos    if (eval("this") === global) {
372786Ssos        fail("#1 strict eval gets 'global' as 'this'");
382786Ssos    }
392786Ssos
402786Ssos    if (eval ("typeof this") !== 'undefined') {
412786Ssos        fail("#2 typeof this is not undefined in strict eval");
422786Ssos    }
432786Ssos}
442786Ssos
452786Ssosfunc();
462786Ssos
472786Ssosvar global = this;
482786Ssosif (eval("\"use strict\";this") !== this) {
492786Ssos    fail("#3 strict mode eval receives wrong 'this'");
502786Ssos}
512786Ssos
522786Ssosvar obj = {
532786Ssos  func: function() {
542786Ssos      if (eval('"use strict"; this') !== obj) {
552786Ssos          fail("#4 strict mode eval receives wrong 'this'");
562786Ssos      }
572786Ssos  }
582786Ssos};
592786Ssos
602786Ssosobj.func();
612786Ssos
622786Ssosfunction func2() {
632786Ssos   'use strict';
642786Ssos   return eval('this');
652786Ssos}
662786Ssos
672786Ssosfunc2.call(null);
682786Ssosif (func2.call(null) !== null) {
692786Ssos    fail("#5 strict mode eval receives wrong 'this'");
702786Ssos}
712786Ssos
722786Ssosif (func2.call('hello') !== 'hello') {
732786Ssos    fail("#6 strict mode eval receives wrong 'this'");
742786Ssos}
752786Ssos
762786Ssos// indirect eval
772786Ssosvar my_eval = eval;
782786Ssosif (my_eval("'use strict'; this; ") !== this) {
792786Ssos    fail("#7 strict mode eval receives wrong 'this'");
802786Ssos}
815994Ssos
822786Ssos