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
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.
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.
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.
31 var success = "[test success]";
34 assertEquals(0, arguments.length);
35 assertSame(expectedThis, this);
40 // On an Array object.
44 assertEquals(a1.join(), a1.toString());
46 // Non-standard "join" function is called correctly.
50 assertEquals(success, a2.toString());
52 // Non-callable join function is ignored and Object.prototype.toString is
55 a3.join = "not callable";
56 assertEquals("[object Array]", a3.toString());
58 // Non-existing join function is treated same as non-callable.
60 a4.__proto__ = { toString: Array.prototype.toString };
62 assertEquals("[object Array]", a4.toString());
65 // On a non-Array object.
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());
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,
78 get length() { assertUnreachable(); },
79 get 0() { assertUnreachable(); }};
81 assertEquals(success, o2.toString());
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,
88 assertEquals(success, o3.toString());
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());
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
101 assertEquals("[object Object]", o5.toString());
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;
108 Object.defineProperty(Number.prototype, "join", {get: function() {
109 assertFalse(called_before);
110 called_before = true;
114 Number.prototype.arrayToString = Array.prototype.toString;
115 assertEquals(success, (42).arrayToString());
117 // ----------------------------------------------------------
118 // Testing Array.prototype.toLocaleString
120 // Ensure that it never uses Array.prototype.toString for anything.
121 Array.prototype.toString = function() { assertUnreachable(); };
124 var la1 = [1, [2, 3], 4];
125 assertEquals("1,2,3,4", la1.toLocaleString());
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());
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);
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());
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());
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", {
148 exptectedThis = this;
151 for (var i = 0; i < 3; i++) {
152 Object.defineProperty(Number.prototype, i, {
154 assertEquals(expectedThis, this);
158 Number.prototype.arrayToLocaleString = Array.prototype.toLocaleString;
159 assertEquals("42,42,42", (42).arrayToLocaleString());