Home | History | Annotate | Download | only in js
      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 page tests for assertion failures in edge cases of property lookup on primitive values."
     26 );
     27 
     28 var didNotCrash = true;
     29 
     30 (function () {
     31     delete String.prototype.constructor;
     32     for (var i = 0; i < 3; ++i)
     33         "".replace;
     34 })();
     35 
     36 (function () {
     37     String.prototype.__proto__ = { x: 1, y: 1 };
     38     delete String.prototype.__proto__.x;
     39     for (var i = 0; i < 3; ++i)
     40         "".y;
     41 })();
     42 
     43 (function () {
     44     function f(x) {
     45         x.y;
     46     }
     47 
     48     String.prototype.x = 1;
     49     String.prototype.y = 1;
     50     delete String.prototype.x;
     51 
     52     Number.prototype.x = 1;
     53     Number.prototype.y = 1;
     54     delete Number.prototype.x;
     55 
     56     for (var i = 0; i < 3; ++i)
     57         f("");
     58 
     59     for (var i = 0; i < 3; ++i)
     60         f(.5);
     61 })();
     62 
     63 
     64 var checkOkay;
     65 
     66 function checkGet(x, constructor)
     67 {
     68     checkOkay = false;
     69     Object.defineProperty(constructor.prototype, "foo", { get: function() { checkOkay = typeof this === 'object'; }, configurable: true });
     70     x.foo;
     71     delete constructor.prototype.foo;
     72     return checkOkay;
     73 }
     74 
     75 function checkSet(x, constructor)
     76 {
     77     checkOkay = false;
     78     Object.defineProperty(constructor.prototype, "foo", { set: function() { checkOkay = typeof this === 'object'; }, configurable: true });
     79     x.foo = null;
     80     delete constructor.prototype.foo;
     81     return checkOkay;
     82 }
     83 
     84 function checkGetStrict(x, constructor)
     85 {
     86     checkOkay = false;
     87     Object.defineProperty(constructor.prototype, "foo", { get: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
     88     x.foo;
     89     delete constructor.prototype.foo;
     90     return checkOkay;
     91 }
     92 
     93 function checkSetStrict(x, constructor)
     94 {
     95     checkOkay = false;
     96     Object.defineProperty(constructor.prototype, "foo", { set: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
     97     x.foo = null;
     98     delete constructor.prototype.foo;
     99     return checkOkay;
    100 }
    101 
    102 shouldBeTrue("checkGet(1, Number)");
    103 shouldBeTrue("checkGet('hello', String)");
    104 shouldBeTrue("checkGet(true, Boolean)");
    105 shouldBeTrue("checkSet(1, Number)");
    106 shouldBeTrue("checkSet('hello', String)");
    107 shouldBeTrue("checkSet(true, Boolean)");
    108 shouldBeTrue("checkGetStrict(1, Number)");
    109 shouldBeTrue("checkGetStrict('hello', String)");
    110 shouldBeTrue("checkGetStrict(true, Boolean)");
    111 shouldBeTrue("checkSetStrict(1, Number)");
    112 shouldBeTrue("checkSetStrict('hello', String)");
    113 shouldBeTrue("checkSetStrict(true, Boolean)");
    114 
    115 function checkRead(x, constructor)
    116 {
    117     return x.foo === undefined;
    118 }
    119 
    120 function checkWrite(x, constructor)
    121 {
    122     x.foo = null;
    123     return x.foo === undefined;
    124 }
    125 
    126 function checkReadStrict(x, constructor)
    127 {
    128     "use strict";
    129     return x.foo === undefined;
    130 }
    131 
    132 function checkWriteStrict(x, constructor)
    133 {
    134     "use strict";
    135     x.foo = null;
    136     return x.foo === undefined;
    137 }
    138 
    139 shouldBeTrue("checkRead(1, Number)");
    140 shouldBeTrue("checkRead('hello', String)");
    141 shouldBeTrue("checkRead(true, Boolean)");
    142 shouldBeTrue("checkWrite(1, Number)");
    143 shouldBeTrue("checkWrite('hello', String)");
    144 shouldBeTrue("checkWrite(true, Boolean)");
    145 shouldBeTrue("checkReadStrict(1, Number)");
    146 shouldBeTrue("checkReadStrict('hello', String)");
    147 shouldBeTrue("checkReadStrict(true, Boolean)");
    148 shouldThrow("checkWriteStrict(1, Number)");
    149 shouldThrow("checkWriteStrict('hello', String)");
    150 shouldThrow("checkWriteStrict(true, Boolean)");
    151 
    152 function checkNumericGet(x, constructor)
    153 {
    154     checkOkay = false;
    155     Object.defineProperty(constructor.prototype, 42, { get: function() { checkOkay = typeof this === 'object'; }, configurable: true });
    156     x[42];
    157     delete constructor.prototype[42];
    158     return checkOkay;
    159 }
    160 
    161 function checkNumericSet(x, constructor)
    162 {
    163     checkOkay = false;
    164     Object.defineProperty(constructor.prototype, 42, { set: function() { checkOkay = typeof this === 'object'; }, configurable: true });
    165     x[42] = null;
    166     delete constructor.prototype[42];
    167     return checkOkay;
    168 }
    169 
    170 function checkNumericGetStrict(x, constructor)
    171 {
    172     checkOkay = false;
    173     Object.defineProperty(constructor.prototype, 42, { get: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
    174     x[42];
    175     delete constructor.prototype[42];
    176     return checkOkay;
    177 }
    178 
    179 function checkNumericSetStrict(x, constructor)
    180 {
    181     checkOkay = false;
    182     Object.defineProperty(constructor.prototype, 42, { set: function() { "use strict"; checkOkay = typeof this !== 'object'; }, configurable: true });
    183     x[42] = null;
    184     delete constructor.prototype[42];
    185     return checkOkay;
    186 }
    187 
    188 shouldBeTrue("checkNumericGet(1, Number)");
    189 shouldBeTrue("checkNumericGet('hello', String)");
    190 shouldBeTrue("checkNumericGet(true, Boolean)");
    191 shouldBeTrue("checkNumericSet(1, Number)");
    192 shouldBeTrue("checkNumericSet('hello', String)");
    193 shouldBeTrue("checkNumericSet(true, Boolean)");
    194 shouldBeTrue("checkNumericGetStrict(1, Number)");
    195 shouldBeTrue("checkNumericGetStrict('hello', String)");
    196 shouldBeTrue("checkNumericGetStrict(true, Boolean)");
    197 shouldBeTrue("checkNumericSetStrict(1, Number)");
    198 shouldBeTrue("checkNumericSetStrict('hello', String)");
    199 shouldBeTrue("checkNumericSetStrict(true, Boolean)");
    200 
    201 function checkNumericRead(x, constructor)
    202 {
    203     return x[42] === undefined;
    204 }
    205 
    206 function checkNumericWrite(x, constructor)
    207 {
    208     x[42] = null;
    209     return x[42] === undefined;
    210 }
    211 
    212 function checkNumericReadStrict(x, constructor)
    213 {
    214     "use strict";
    215     return x[42] === undefined;
    216 }
    217 
    218 function checkNumericWriteStrict(x, constructor)
    219 {
    220     "use strict";
    221     x[42] = null;
    222     return x[42] === undefined;
    223 }
    224 
    225 shouldBeTrue("checkNumericRead(1, Number)");
    226 shouldBeTrue("checkNumericRead('hello', String)");
    227 shouldBeTrue("checkNumericRead(true, Boolean)");
    228 shouldBeTrue("checkNumericWrite(1, Number)");
    229 shouldBeTrue("checkNumericWrite('hello', String)");
    230 shouldBeTrue("checkNumericWrite(true, Boolean)");
    231 shouldBeTrue("checkNumericReadStrict(1, Number)");
    232 shouldBeTrue("checkNumericReadStrict('hello', String)");
    233 shouldBeTrue("checkNumericReadStrict(true, Boolean)");
    234 shouldThrow("checkNumericWriteStrict(1, Number)");
    235 shouldThrow("checkNumericWriteStrict('hello', String)");
    236 shouldThrow("checkNumericWriteStrict(true, Boolean)");
    237 
    238 shouldBeTrue("didNotCrash");
    239