Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | // When calling user-defined functions on strings, booleans or |
| 29 | // numbers, we should create a wrapper object. |
| 30 | |
| 31 | function RunTests() { |
| 32 | for (var i = 0; i < 10; i++) { |
| 33 | assertEquals('object', 'xxx'.TypeOfThis()); |
| 34 | assertEquals('object', true.TypeOfThis(2,3)); |
| 35 | assertEquals('object', false.TypeOfThis()); |
| 36 | assertEquals('object', (42).TypeOfThis()); |
| 37 | assertEquals('object', (3.14).TypeOfThis()); |
| 38 | } |
| 39 | |
| 40 | for (var i = 0; i < 10; i++) { |
| 41 | assertEquals('object', 'xxx'['TypeOfThis']()); |
| 42 | assertEquals('object', true['TypeOfThis']()); |
| 43 | assertEquals('object', false['TypeOfThis']()); |
| 44 | assertEquals('object', (42)['TypeOfThis']()); |
| 45 | assertEquals('object', (3.14)['TypeOfThis']()); |
| 46 | } |
| 47 | |
| 48 | function CallTypeOfThis(obj) { |
| 49 | assertEquals('object', obj.TypeOfThis()); |
| 50 | } |
| 51 | |
| 52 | for (var i = 0; i < 10; i++) { |
| 53 | CallTypeOfThis('xxx'); |
| 54 | CallTypeOfThis(true); |
| 55 | CallTypeOfThis(false); |
| 56 | CallTypeOfThis(42); |
| 57 | CallTypeOfThis(3.14); |
| 58 | } |
| 59 | |
| 60 | function TestWithWith(obj) { |
| 61 | with (obj) { |
| 62 | for (var i = 0; i < 10; i++) { |
| 63 | assertEquals('object', TypeOfThis()); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | TestWithWith('xxx'); |
| 69 | TestWithWith(true); |
| 70 | TestWithWith(false); |
| 71 | TestWithWith(42); |
| 72 | TestWithWith(3.14); |
| 73 | |
| 74 | for (var i = 0; i < 10; i++) { |
| 75 | assertEquals('object', true[7]()); |
| 76 | assertEquals('object', false[7]()); |
| 77 | assertEquals('object', (42)[7]()); |
| 78 | assertEquals('object', (3.14)[7]()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | function TypeOfThis() { return typeof this; } |
| 83 | |
| 84 | // Test with normal setup of prototype. |
| 85 | String.prototype.TypeOfThis = TypeOfThis; |
| 86 | Boolean.prototype.TypeOfThis = TypeOfThis; |
| 87 | Number.prototype.TypeOfThis = TypeOfThis; |
| 88 | Boolean.prototype[7] = TypeOfThis; |
| 89 | Number.prototype[7] = TypeOfThis; |
Leon Clarke | eab96aa | 2010-01-27 16:31:12 +0000 | [diff] [blame^] | 90 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 91 | |
| 92 | RunTests(); |
| 93 | |
| 94 | // Run test after properties have been set to a different value. |
| 95 | String.prototype.TypeOfThis = 'x'; |
| 96 | Boolean.prototype.TypeOfThis = 'x'; |
| 97 | Number.prototype.TypeOfThis = 'x'; |
| 98 | Boolean.prototype[7] = 'x'; |
| 99 | Number.prototype[7] = 'x'; |
| 100 | |
| 101 | String.prototype.TypeOfThis = TypeOfThis; |
| 102 | Boolean.prototype.TypeOfThis = TypeOfThis; |
| 103 | Number.prototype.TypeOfThis = TypeOfThis; |
| 104 | Boolean.prototype[7] = TypeOfThis; |
| 105 | Number.prototype[7] = TypeOfThis; |
| 106 | |
| 107 | RunTests(); |
| 108 | |
| 109 | // Force the prototype into slow case and run the test again. |
| 110 | delete String.prototype.TypeOfThis; |
| 111 | delete Boolean.prototype.TypeOfThis; |
| 112 | delete Number.prototype.TypeOfThis; |
| 113 | Boolean.prototype[7]; |
| 114 | Number.prototype[7]; |
| 115 | |
| 116 | String.prototype.TypeOfThis = TypeOfThis; |
| 117 | Boolean.prototype.TypeOfThis = TypeOfThis; |
| 118 | Number.prototype.TypeOfThis = TypeOfThis; |
| 119 | Boolean.prototype[7] = TypeOfThis; |
| 120 | Number.prototype[7] = TypeOfThis; |
| 121 | |
| 122 | RunTests(); |
| 123 | |
| 124 | // According to ES3 15.3.4.3 the this value passed to Function.prototyle.apply |
| 125 | // should wrapped. According to ES5 it should not. |
| 126 | assertEquals('object', TypeOfThis.apply('xxx', [])); |
| 127 | assertEquals('object', TypeOfThis.apply(true, [])); |
| 128 | assertEquals('object', TypeOfThis.apply(false, [])); |
| 129 | assertEquals('object', TypeOfThis.apply(42, [])); |
| 130 | assertEquals('object', TypeOfThis.apply(3.14, [])); |
| 131 | |
| 132 | // According to ES3 15.3.4.3 the this value passed to Function.prototyle.call |
| 133 | // should wrapped. According to ES5 it should not. |
| 134 | assertEquals('object', TypeOfThis.call('xxx')); |
| 135 | assertEquals('object', TypeOfThis.call(true)); |
| 136 | assertEquals('object', TypeOfThis.call(false)); |
| 137 | assertEquals('object', TypeOfThis.call(42)); |
| 138 | assertEquals('object', TypeOfThis.call(3.14)); |