ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 1 | // Copyright 2011 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 | // Regression test for Chromium issue 70066. Delete should work properly |
| 29 | // from inside 'with' scopes. |
| 30 | // http://code.google.com/p/chromium/issues/detail?id=70066 |
| 31 | |
| 32 | x = 0; |
| 33 | |
| 34 | // Delete on a slot from a function's own context. |
| 35 | function test1() { |
| 36 | var value = 1; |
| 37 | var status; |
| 38 | with ({}) { status = delete value; } |
| 39 | return value + ":" + status; |
| 40 | } |
| 41 | |
| 42 | assertEquals("1:false", test1(), "test1"); |
| 43 | assertEquals(0, x, "test1"); // Global x is undisturbed. |
| 44 | |
| 45 | |
| 46 | // Delete on a slot from an outer context. |
| 47 | function test2() { |
| 48 | function f() { |
| 49 | with ({}) { return delete value; } |
| 50 | } |
| 51 | var value = 2; |
| 52 | var status = f(); |
| 53 | return value + ":" + status; |
| 54 | } |
| 55 | |
| 56 | assertEquals("2:false", test2(), "test2"); |
| 57 | assertEquals(0, x, "test2"); // Global x is undisturbed. |
| 58 | |
| 59 | |
| 60 | // Delete on an argument. This hits the same code paths as test5 because |
| 61 | // 'with' forces all parameters to be indirected through the arguments |
| 62 | // object. |
| 63 | function test3(value) { |
| 64 | var status; |
| 65 | with ({}) { status = delete value; } |
| 66 | return value + ":" + status; |
| 67 | } |
| 68 | |
| 69 | assertEquals("undefined:true", test3(3), "test3"); |
| 70 | assertEquals(0, x, "test3"); // Global x is undisturbed. |
| 71 | |
| 72 | |
| 73 | // Delete on an argument from an outer context. This hits the same code |
| 74 | // path as test2. |
| 75 | function test4(value) { |
| 76 | function f() { |
| 77 | with ({}) { return delete value; } |
| 78 | } |
| 79 | var status = f(); |
| 80 | return value + ":" + status; |
| 81 | } |
| 82 | |
| 83 | assertEquals("4:false", test4(4), "test4"); |
| 84 | assertEquals(0, x, "test4"); // Global x is undisturbed. |
| 85 | |
| 86 | |
| 87 | // Delete on an argument found in the arguments object. Such properties are |
| 88 | // normally DONT_DELETE in JavaScript but deletion is allowed by V8. |
| 89 | function test5(value) { |
| 90 | var status; |
| 91 | with ({}) { status = delete value; } |
| 92 | return arguments[0] + ":" + status; |
| 93 | } |
| 94 | |
| 95 | assertEquals("undefined:true", test5(5), "test5"); |
| 96 | assertEquals(0, x, "test5"); // Global x is undisturbed. |
| 97 | |
| 98 | function test6(value) { |
| 99 | function f() { |
| 100 | with ({}) { return delete value; } |
| 101 | } |
| 102 | var status = f(); |
| 103 | return arguments[0] + ":" + status; |
| 104 | } |
| 105 | |
| 106 | assertEquals("undefined:true", test6(6), "test6"); |
| 107 | assertEquals(0, x, "test6"); // Global x is undisturbed. |
| 108 | |
| 109 | |
| 110 | // Delete on a property found on 'with' object. |
| 111 | function test7(object) { |
| 112 | with (object) { return delete value; } |
| 113 | } |
| 114 | |
| 115 | var o = {value: 7}; |
| 116 | assertEquals(true, test7(o), "test7"); |
| 117 | assertEquals(void 0, o.value, "test7"); |
| 118 | assertEquals(0, x, "test7"); // Global x is undisturbed. |
| 119 | |
| 120 | |
| 121 | // Delete on a global property. |
| 122 | function test8() { |
| 123 | with ({}) { return delete x; } |
| 124 | } |
| 125 | |
| 126 | assertEquals(true, test8(), "test8"); |
| 127 | assertThrows("x", "test8"); // Global x should be deleted. |
| 128 | |
| 129 | |
| 130 | // Delete on a property that is not found anywhere. |
| 131 | function test9() { |
| 132 | with ({}) { return delete x; } |
| 133 | } |
| 134 | |
| 135 | assertThrows("x", "test9"); // Make sure it's not there. |
| 136 | assertEquals(true, test9(), "test9"); |
| 137 | |
| 138 | |
| 139 | // Delete on a DONT_DELETE property of the global object. |
| 140 | var y = 10; |
| 141 | function test10() { |
| 142 | with ({}) { return delete y; } |
| 143 | } |
| 144 | |
| 145 | assertEquals(false, test10(), "test10"); |
| 146 | assertEquals(10, y, "test10"); |