blob: 8e1ea6b3bb3e7cd9ab8e3545d13f42c2bc26d06e [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions
6// are met:
7// 1. Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// 2. Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16// DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
24description(
25"This tests that a call to array/string prototype methods pass the correct this value (undefined) to strict callees."
26);
27
28var undefinedString = String(undefined);
29var globalObjectString = String(this);
30
31function strictThrowThisString()
32{
33 "use strict";
34 throw String(this);
35}
36
37function nonstrictThrowThisString()
38{
39 throw String(this);
40}
41
42function testArrayPrototypeSort(callback)
43{
44 try {
45 [1,2].sort(callback);
46 } catch (e) {
47 return e;
48 }
49 return "FAILED";
50}
51
52function testArrayPrototypeFilter(callback)
53{
54 try {
55 [1,2].filter(callback);
56 } catch (e) {
57 return e;
58 }
59 return "FAILED";
60}
61
62function testArrayPrototypeMap(callback)
63{
64 try {
65 [1,2].map(callback);
66 } catch (e) {
67 return e;
68 }
69 return "FAILED";
70}
71
72function testArrayPrototypeEvery(callback)
73{
74 try {
75 [1,2].every(callback);
76 } catch (e) {
77 return e;
78 }
79 return "FAILED";
80}
81
82function testArrayPrototypeForEach(callback)
83{
84 try {
85 [1,2].forEach(callback);
86 } catch (e) {
87 return e;
88 }
89 return "FAILED";
90}
91
92function testArrayPrototypeSome(callback)
93{
94 try {
95 [1,2].some(callback);
96 } catch (e) {
97 return e;
98 }
99 return "FAILED";
100}
101
102function testStringPrototypeReplace(callback)
103{
104 try {
105 "1,2".replace('1', callback);
106 } catch (e) {
107 return e;
108 }
109 return "FAILED";
110}
111
112shouldBe('testArrayPrototypeSort(strictThrowThisString)', 'undefinedString');
113shouldBe('testArrayPrototypeFilter(strictThrowThisString)', 'undefinedString');
114shouldBe('testArrayPrototypeMap(strictThrowThisString)', 'undefinedString');
115shouldBe('testArrayPrototypeEvery(strictThrowThisString)', 'undefinedString');
116shouldBe('testArrayPrototypeForEach(strictThrowThisString)', 'undefinedString');
117shouldBe('testArrayPrototypeSome(strictThrowThisString)', 'undefinedString');
118shouldBe('testStringPrototypeReplace(strictThrowThisString)', 'undefinedString');
119
120shouldBe('testArrayPrototypeSort(nonstrictThrowThisString)', 'globalObjectString');
121shouldBe('testArrayPrototypeFilter(nonstrictThrowThisString)', 'globalObjectString');
122shouldBe('testArrayPrototypeMap(nonstrictThrowThisString)', 'globalObjectString');
123shouldBe('testArrayPrototypeEvery(nonstrictThrowThisString)', 'globalObjectString');
124shouldBe('testArrayPrototypeForEach(nonstrictThrowThisString)', 'globalObjectString');
125shouldBe('testArrayPrototypeSome(nonstrictThrowThisString)', 'globalObjectString');
126shouldBe('testStringPrototypeReplace(nonstrictThrowThisString)', 'globalObjectString');