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 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 60 | // Delete on a parameter. |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 61 | function test3(value) { |
| 62 | var status; |
| 63 | with ({}) { status = delete value; } |
| 64 | return value + ":" + status; |
| 65 | } |
| 66 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 67 | assertEquals("3:false", test3(3), "test3"); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 68 | assertEquals(0, x, "test3"); // Global x is undisturbed. |
| 69 | |
| 70 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 71 | // Delete on a parameter found in an outer context. |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 72 | function test4(value) { |
| 73 | function f() { |
| 74 | with ({}) { return delete value; } |
| 75 | } |
| 76 | var status = f(); |
| 77 | return value + ":" + status; |
| 78 | } |
| 79 | |
| 80 | assertEquals("4:false", test4(4), "test4"); |
| 81 | assertEquals(0, x, "test4"); // Global x is undisturbed. |
| 82 | |
| 83 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 84 | // Delete on a parameter, arguments object should be unaffected. |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 85 | function test5(value) { |
| 86 | var status; |
| 87 | with ({}) { status = delete value; } |
| 88 | return arguments[0] + ":" + status; |
| 89 | } |
| 90 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 91 | assertEquals("5:false", test5(5), "test5"); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 92 | assertEquals(0, x, "test5"); // Global x is undisturbed. |
| 93 | |
| 94 | function test6(value) { |
| 95 | function f() { |
| 96 | with ({}) { return delete value; } |
| 97 | } |
| 98 | var status = f(); |
| 99 | return arguments[0] + ":" + status; |
| 100 | } |
| 101 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 102 | assertEquals("6:false", test6(6), "test6"); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 103 | assertEquals(0, x, "test6"); // Global x is undisturbed. |
| 104 | |
| 105 | |
| 106 | // Delete on a property found on 'with' object. |
| 107 | function test7(object) { |
| 108 | with (object) { return delete value; } |
| 109 | } |
| 110 | |
| 111 | var o = {value: 7}; |
| 112 | assertEquals(true, test7(o), "test7"); |
| 113 | assertEquals(void 0, o.value, "test7"); |
| 114 | assertEquals(0, x, "test7"); // Global x is undisturbed. |
| 115 | |
| 116 | |
| 117 | // Delete on a global property. |
| 118 | function test8() { |
| 119 | with ({}) { return delete x; } |
| 120 | } |
| 121 | |
| 122 | assertEquals(true, test8(), "test8"); |
| 123 | assertThrows("x", "test8"); // Global x should be deleted. |
| 124 | |
| 125 | |
| 126 | // Delete on a property that is not found anywhere. |
| 127 | function test9() { |
| 128 | with ({}) { return delete x; } |
| 129 | } |
| 130 | |
| 131 | assertThrows("x", "test9"); // Make sure it's not there. |
| 132 | assertEquals(true, test9(), "test9"); |
| 133 | |
| 134 | |
| 135 | // Delete on a DONT_DELETE property of the global object. |
| 136 | var y = 10; |
| 137 | function test10() { |
| 138 | with ({}) { return delete y; } |
| 139 | } |
| 140 | |
| 141 | assertEquals(false, test10(), "test10"); |
| 142 | assertEquals(10, y, "test10"); |