Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame^] | 1 | // 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 | |
| 24 | description( |
| 25 | "This tests that a call to array/string prototype methods pass the correct this value (undefined) to strict callees." |
| 26 | ); |
| 27 | |
| 28 | var undefinedString = String(undefined); |
| 29 | var globalObjectString = String(this); |
| 30 | |
| 31 | function strictThrowThisString() |
| 32 | { |
| 33 | "use strict"; |
| 34 | throw String(this); |
| 35 | } |
| 36 | |
| 37 | function nonstrictThrowThisString() |
| 38 | { |
| 39 | throw String(this); |
| 40 | } |
| 41 | |
| 42 | function testArrayPrototypeSort(callback) |
| 43 | { |
| 44 | try { |
| 45 | [1,2].sort(callback); |
| 46 | } catch (e) { |
| 47 | return e; |
| 48 | } |
| 49 | return "FAILED"; |
| 50 | } |
| 51 | |
| 52 | function testArrayPrototypeFilter(callback) |
| 53 | { |
| 54 | try { |
| 55 | [1,2].filter(callback); |
| 56 | } catch (e) { |
| 57 | return e; |
| 58 | } |
| 59 | return "FAILED"; |
| 60 | } |
| 61 | |
| 62 | function testArrayPrototypeMap(callback) |
| 63 | { |
| 64 | try { |
| 65 | [1,2].map(callback); |
| 66 | } catch (e) { |
| 67 | return e; |
| 68 | } |
| 69 | return "FAILED"; |
| 70 | } |
| 71 | |
| 72 | function testArrayPrototypeEvery(callback) |
| 73 | { |
| 74 | try { |
| 75 | [1,2].every(callback); |
| 76 | } catch (e) { |
| 77 | return e; |
| 78 | } |
| 79 | return "FAILED"; |
| 80 | } |
| 81 | |
| 82 | function testArrayPrototypeForEach(callback) |
| 83 | { |
| 84 | try { |
| 85 | [1,2].forEach(callback); |
| 86 | } catch (e) { |
| 87 | return e; |
| 88 | } |
| 89 | return "FAILED"; |
| 90 | } |
| 91 | |
| 92 | function testArrayPrototypeSome(callback) |
| 93 | { |
| 94 | try { |
| 95 | [1,2].some(callback); |
| 96 | } catch (e) { |
| 97 | return e; |
| 98 | } |
| 99 | return "FAILED"; |
| 100 | } |
| 101 | |
| 102 | function testStringPrototypeReplace(callback) |
| 103 | { |
| 104 | try { |
| 105 | "1,2".replace('1', callback); |
| 106 | } catch (e) { |
| 107 | return e; |
| 108 | } |
| 109 | return "FAILED"; |
| 110 | } |
| 111 | |
| 112 | shouldBe('testArrayPrototypeSort(strictThrowThisString)', 'undefinedString'); |
| 113 | shouldBe('testArrayPrototypeFilter(strictThrowThisString)', 'undefinedString'); |
| 114 | shouldBe('testArrayPrototypeMap(strictThrowThisString)', 'undefinedString'); |
| 115 | shouldBe('testArrayPrototypeEvery(strictThrowThisString)', 'undefinedString'); |
| 116 | shouldBe('testArrayPrototypeForEach(strictThrowThisString)', 'undefinedString'); |
| 117 | shouldBe('testArrayPrototypeSome(strictThrowThisString)', 'undefinedString'); |
| 118 | shouldBe('testStringPrototypeReplace(strictThrowThisString)', 'undefinedString'); |
| 119 | |
| 120 | shouldBe('testArrayPrototypeSort(nonstrictThrowThisString)', 'globalObjectString'); |
| 121 | shouldBe('testArrayPrototypeFilter(nonstrictThrowThisString)', 'globalObjectString'); |
| 122 | shouldBe('testArrayPrototypeMap(nonstrictThrowThisString)', 'globalObjectString'); |
| 123 | shouldBe('testArrayPrototypeEvery(nonstrictThrowThisString)', 'globalObjectString'); |
| 124 | shouldBe('testArrayPrototypeForEach(nonstrictThrowThisString)', 'globalObjectString'); |
| 125 | shouldBe('testArrayPrototypeSome(nonstrictThrowThisString)', 'globalObjectString'); |
| 126 | shouldBe('testStringPrototypeReplace(nonstrictThrowThisString)', 'globalObjectString'); |