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// We use the has() function to avoid relying on a functioning 29// implementation of 'in'. 30function has(o, k) { return typeof o[k] !== 'undefined'; } 31 32assertTrue(delete null); 33assertTrue(delete 2); 34assertTrue(delete 'foo'); 35assertTrue(delete Number(7)); 36assertTrue(delete new Number(8)); 37 38assertTrue(delete {}.x); 39assertTrue(delete {}.y); 40assertTrue(delete {}.toString); 41 42x = 42; 43assertEquals(42, x); 44assertTrue(delete x); 45assertTrue(typeof x === 'undefined', "x is gone"); 46 47var y = 87; // should have DontDelete attribute 48assertEquals(87, y); 49assertFalse(delete y, "don't delete"); 50assertFalse(typeof y === 'undefined'); 51assertEquals(87, y); 52 53var o = { x: 42, y: 87 }; 54assertTrue(has(o, 'x')); 55assertTrue(has(o, 'y')); 56assertTrue(delete o.x); 57assertFalse(has(o, 'x')); 58assertTrue(has(o, 'y')); 59assertTrue(delete o['y']); 60assertFalse(has(o, 'x')); 61assertFalse(has(o, 'y')); 62 63 64var o = {}; 65for (var i = 0x0020; i < 0x02ff; i+=2) { 66 o[String.fromCharCode(i)] = i; 67 o[String.fromCharCode(i+1)] = i+1; 68} 69for (var i = 0x0020; i < 0x02ff; i+=2) { 70 assertTrue(delete o[String.fromCharCode(i)]); 71} 72for (var i = 0x0020; i < 0x02ff; i+=2) { 73 assertFalse(has(o, String.fromCharCode(i)), "deleted (" + i + ")"); 74 assertTrue(has(o, String.fromCharCode(i+1)), "still here (" + i + ")"); 75} 76 77 78var a = [0,1,2]; 79assertTrue(has(a, 0)); 80assertTrue(delete a[0]); 81assertFalse(has(a, 0), "delete 0"); 82assertEquals(1, a[1]); 83assertEquals(2, a[2]); 84assertTrue(delete a[100], "delete 100"); 85assertTrue(delete a[Math.pow(2,31)-1], "delete 2^31-1"); 86assertFalse(has(a, 0), "delete 0"); 87assertEquals(1, a[1]); 88assertEquals(2, a[2]); 89 90 91var a = [0,1,2]; 92assertEquals(3, a.length); 93assertTrue(delete a[2]); 94assertEquals(3, a.length); 95assertTrue(delete a[0]); 96assertEquals(3, a.length); 97assertTrue(delete a[1]); 98assertEquals(3, a.length); 99 100 101var o = {}; 102o[Math.pow(2,30)-1] = 0; 103o[Math.pow(2,31)-1] = 0; 104o[1] = 0; 105assertTrue(delete o[0]); 106assertTrue(delete o[Math.pow(2,30)]); 107assertFalse(has(o, 0), "delete 0"); 108assertFalse(has(o, Math.pow(2,30))); 109assertTrue(has(o, 1)); 110assertTrue(has(o, Math.pow(2,30)-1)); 111assertTrue(has(o, Math.pow(2,31)-1)); 112 113assertTrue(delete o[Math.pow(2,30)-1]); 114assertTrue(has(o, 1)); 115assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 116assertTrue(has(o, Math.pow(2,31)-1)); 117 118assertTrue(delete o[1]); 119assertFalse(has(o, 1), "delete 1"); 120assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 121assertTrue(has(o, Math.pow(2,31)-1)); 122 123assertTrue(delete o[Math.pow(2,31)-1]); 124assertFalse(has(o, 1), "delete 1"); 125assertFalse(has(o, Math.pow(2,30)-1), "delete 2^30-1"); 126assertFalse(has(o, Math.pow(2,31)-1), "delete 2^31-1"); 127 128 129var a = []; 130a[Math.pow(2,30)-1] = 0; 131a[Math.pow(2,31)-1] = 0; 132a[1] = 0; 133assertTrue(delete a[0]); 134assertTrue(delete a[Math.pow(2,30)]); 135assertFalse(has(a, 0), "delete 0"); 136assertFalse(has(a, Math.pow(2,30)), "delete 2^30"); 137assertTrue(has(a, 1)); 138assertTrue(has(a, Math.pow(2,30)-1)); 139assertTrue(has(a, Math.pow(2,31)-1)); 140assertEquals(Math.pow(2,31), a.length); 141 142assertTrue(delete a[Math.pow(2,30)-1]); 143assertTrue(has(a, 1)); 144assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 145assertTrue(has(a, Math.pow(2,31)-1)); 146assertEquals(Math.pow(2,31), a.length); 147 148assertTrue(delete a[1]); 149assertFalse(has(a, 1), "delete 1"); 150assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 151assertTrue(has(a, Math.pow(2,31)-1)); 152assertEquals(Math.pow(2,31), a.length); 153 154assertTrue(delete a[Math.pow(2,31)-1]); 155assertFalse(has(a, 1), "delete 1"); 156assertFalse(has(a, Math.pow(2,30)-1), "delete 2^30-1"); 157assertFalse(has(a, Math.pow(2,31)-1), "delete 2^31-1"); 158assertEquals(Math.pow(2,31), a.length); 159 160// Check that a LoadIC for a dictionary field works, even 161// when the dictionary probe misses. 162function load_deleted_property_using_IC() { 163 var x = new Object(); 164 x.a = 3; 165 x.b = 4; 166 x.c = 5; 167 168 delete x.c; 169 assertEquals(3, load_a(x)); 170 assertEquals(3, load_a(x)); 171 delete x.a; 172 assertTrue(typeof load_a(x) === 'undefined', "x.a is gone"); 173 assertTrue(typeof load_a(x) === 'undefined', "x.a is gone"); 174} 175 176function load_a(x) { 177 return x.a; 178} 179 180load_deleted_property_using_IC(); 181