Home | History | Annotate | Download | only in mjsunit
      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 // Array's toString should call the object's own join method, if one exists and
     29 // is callable. Otherwise, just use the original Object.toString function.
     30 
     31 var success = "[test success]";
     32 var expectedThis;
     33 function testJoin() {
     34   assertEquals(0, arguments.length);
     35   assertSame(expectedThis, this);
     36   return success;
     37 }
     38 
     39 
     40 // On an Array object.
     41 
     42 // Default case.
     43 var a1 = [1, 2, 3];
     44 assertEquals(a1.join(), a1.toString());
     45 
     46 // Non-standard "join" function is called correctly.
     47 var a2 = [1, 2, 3];
     48 a2.join = testJoin;
     49 expectedThis = a2;
     50 assertEquals(success, a2.toString());
     51 
     52 // Non-callable join function is ignored and Object.prototype.toString is
     53 // used instead.
     54 var a3 = [1, 2, 3];
     55 a3.join = "not callable";
     56 assertEquals("[object Array]", a3.toString());
     57 
     58 // Non-existing join function is treated same as non-callable.
     59 var a4 = [1, 2, 3];
     60 a4.__proto__ = { toString: Array.prototype.toString };
     61 // No join on Array.
     62 assertEquals("[object Array]", a4.toString());
     63 
     64 
     65 // On a non-Array object.
     66 
     67 // Default looks-like-an-array case.
     68 var o1 = {length: 3, 0: 1, 1: 2, 2: 3,
     69           toString: Array.prototype.toString,
     70           join: Array.prototype.join};
     71 assertEquals(o1.join(), o1.toString());
     72 
     73 
     74 // Non-standard join is called correctly.
     75 // Check that we don't read, e.g., length before calling join.
     76 var o2 = {toString : Array.prototype.toString,
     77           join: testJoin,
     78           get length() { assertUnreachable(); },
     79           get 0() { assertUnreachable(); }};
     80 expectedThis = o2;
     81 assertEquals(success, o2.toString());
     82 
     83 // Non-standard join is called even if it looks like an array.
     84 var o3 = {length: 3, 0: 1, 1: 2, 2: 3,
     85           toString: Array.prototype.toString,
     86           join: testJoin};
     87 expectedThis = o3;
     88 assertEquals(success, o3.toString());
     89 
     90 // Non-callable join works same as for Array.
     91 var o4 = {length: 3, 0: 1, 1: 2, 2: 3,
     92           toString: Array.prototype.toString,
     93           join: "not callable"};
     94 assertEquals("[object Object]", o4.toString());
     95 
     96 
     97 // Non-existing join works same as for Array.
     98 var o5 = {length: 3, 0: 1, 1: 2, 2: 3,
     99           toString: Array.prototype.toString
    100           /* no join */};
    101 assertEquals("[object Object]", o5.toString());
    102 
    103 
    104 // Test that ToObject is called before getting "join", so the instance
    105 // that "join" is read from is the same one passed as receiver later.
    106 var called_before = false;
    107 expectedThis = null;
    108 Object.defineProperty(Number.prototype, "join", {get: function() {
    109             assertFalse(called_before);
    110             called_before = true;
    111             expectedThis = this;
    112             return testJoin;
    113         }});
    114 Number.prototype.arrayToString = Array.prototype.toString;
    115 assertEquals(success, (42).arrayToString());
    116 
    117 // ----------------------------------------------------------
    118 // Testing Array.prototype.toLocaleString
    119 
    120 // Ensure that it never uses Array.prototype.toString for anything.
    121 Array.prototype.toString = function() { assertUnreachable(); };
    122 
    123 // Default case.
    124 var la1 = [1, [2, 3], 4];
    125 assertEquals("1,2,3,4", la1.toLocaleString());
    126 
    127 // Used on a string (which looks like an array of characters).
    128 String.prototype.toLocaleString = Array.prototype.toLocaleString;
    129 assertEquals("1,2,3,4", "1234".toLocaleString());
    130 
    131 // If toLocaleString of element is not callable, throw a TypeError.
    132 var la2 = [1, {toLocaleString: "not callable"}, 3];
    133 assertThrows(function() { la2.toLocaleString(); }, TypeError);
    134 
    135 // If toLocaleString of element is callable, call it.
    136 var la3 = [1, {toLocaleString: function() { return "XX";}}, 3];
    137 assertEquals("1,XX,3", la3.toLocaleString());
    138 
    139 // Omitted elements, as well as undefined and null, become empty string.
    140 var la4 = [1, null, 3, undefined, 5,, 7];
    141 assertEquals("1,,3,,5,,7", la4.toLocaleString());
    142 
    143 
    144 // ToObject is called first and the same object is being used for the
    145 // rest of the operations.
    146 Object.defineProperty(Number.prototype, "length", {
    147     get: function() {
    148       exptectedThis = this;
    149       return 3;
    150     }});
    151 for (var i = 0; i < 3; i++) {
    152   Object.defineProperty(Number.prototype, i, {
    153       get: function() {
    154         assertEquals(expectedThis, this);
    155         return +this;
    156       }});
    157 }
    158 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString;
    159 assertEquals("42,42,42", (42).arrayToLocaleString());
    160