NASHORN-434.js revision 2:da1e581c933b
154696Sshin/* 254696Sshin * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. 354696Sshin * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 454696Sshin * 554696Sshin * This code is free software; you can redistribute it and/or modify it 654696Sshin * under the terms of the GNU General Public License version 2 only, as 754696Sshin * published by the Free Software Foundation. 854696Sshin * 954696Sshin * This code is distributed in the hope that it will be useful, but WITHOUT 1054696Sshin * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1154696Sshin * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1254696Sshin * version 2 for more details (a copy is included in the LICENSE file that 1354696Sshin * accompanied this code). 1454696Sshin * 1554696Sshin * You should have received a copy of the GNU General Public License version 1654696Sshin * 2 along with this work; if not, write to the Free Software Foundation, 1754696Sshin * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1854696Sshin * 1954696Sshin * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2054696Sshin * or visit www.oracle.com if you need additional information or have any 2154696Sshin * questions. 2254696Sshin */ 2354696Sshin 2454696Sshin/** 2554696Sshin * NASHORN-434 : Callbacks to array iterator functions like forEach do not receive proper 'this' object 2654696Sshin * 2754696Sshin * @test 2854696Sshin * @run 2954696Sshin */ 3092986Sobrien 3192986Sobrienvar global = this; 3292986Sobrien 3354696Sshinfunction func(val, idx, obj) { 3454696Sshin if (this !== global) { 3554696Sshin fail("#1 callback got 'this' different from global"); 3654696Sshin } 3754696Sshin} 3854696Sshin 3954696Sshinfunction strictFunc(val, idx, obj) { 4054696Sshin 'use strict'; 4154696Sshin 4254696Sshin if (this === global) { 4354696Sshin fail("#2 callback got global as 'this'"); 4454696Sshin } 4554696Sshin 4654696Sshin if (this !== undefined) { 4754696Sshin fail("#3 callback 'this' is not undefined"); 4854696Sshin } 4954696Sshin} 5054696Sshin 5154696Sshinvar arr = [1]; 5254696Sshinarr.forEach(func); 5354696Sshinarr.forEach(strictFunc); 5454696Sshin 5554696Sshinvar callbackThis = {}; 5654696Sshinarr.forEach(function() { 5754696Sshin if (this !== callbackThis) { 5854696Sshin fail("#4 this !== callbackThis"); 5954696Sshin } 6054696Sshin}, callbackThis); 6154696Sshin 6254696Sshin