OLD | NEW |
| (Empty) |
1 // Copyright 2015 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --harmony-arrays | |
6 | |
7 // Array's toString should call the object's own join method, if one exists and | |
8 // is callable. Otherwise, just use the original Object.toString function. | |
9 | |
10 var typedArrayConstructors = [ | |
11 Uint8Array, | |
12 Int8Array, | |
13 Uint16Array, | |
14 Int16Array, | |
15 Uint32Array, | |
16 Int32Array, | |
17 Uint8ClampedArray, | |
18 Float32Array, | |
19 Float64Array | |
20 ]; | |
21 | |
22 for (var constructor of typedArrayConstructors) { | |
23 var success = "[test success]"; | |
24 var expectedThis; | |
25 function testJoin() { | |
26 assertEquals(0, arguments.length); | |
27 assertSame(expectedThis, this); | |
28 return success; | |
29 } | |
30 | |
31 | |
32 // On an Array object. | |
33 | |
34 // Default case. | |
35 var a1 = new constructor([1, 2, 3]); | |
36 assertEquals("1,2,3", a1.toString()); | |
37 assertEquals("1,2,3", a1.join()); | |
38 assertEquals("1,2,3", a1.toLocaleString()); | |
39 | |
40 // Non-standard "join" function is called correctly. | |
41 var a2 = new constructor([1, 2, 3]); | |
42 a2.join = testJoin; | |
43 expectedThis = a2; | |
44 assertEquals(success, a2.toString()); | |
45 assertEquals(success, a2.join()); | |
46 assertEquals("1,2,3", a2.toLocaleString()); | |
47 | |
48 // Non-callable join function is ignored and Object.prototype.toString is | |
49 // used instead. | |
50 var a3 = new constructor([1, 2, 3]); | |
51 a3.join = "not callable"; | |
52 assertEquals(0, a3.toString().search(/\[object .+Array\]/)); | |
53 | |
54 // Non-existing join function is treated same as non-callable. | |
55 var a4 = new constructor([1, 2, 3]); | |
56 a4.__proto__ = { toString: constructor.prototype.toString }; | |
57 // No join on Array. | |
58 assertEquals(0, a3.toString().search(/\[object .+Array\]/)); | |
59 | |
60 | |
61 // On a non-Array object, throws. | |
62 var o1 = {length: 3, 0: 1, 1: 2, 2: 3, | |
63 toString: constructor.prototype.toString, | |
64 join: constructor.prototype.join, | |
65 toLocaleString: constructor.prototype.toLocaleString}; | |
66 assertThrows(function() { o1.join() }, TypeError); | |
67 assertThrows(function() { o1.toString() }, TypeError); | |
68 assertThrows(function() { o1.toLocaleString() }, TypeError); | |
69 // toString is OK if join not from here: | |
70 o1.join = Array.prototype.join; | |
71 assertEquals("1,2,3", o1.join()); | |
72 assertEquals("1,2,3", o1.toString()); | |
73 assertThrows(function() { o1.toLocaleString() }, TypeError); | |
74 // TODO(littledan): Use the same function for TypedArray as for | |
75 // Array, as the spec says (but Firefox doesn't do either). | |
76 // Currently, using the same method leads to a bootstrap failure. | |
77 // assertEquals(o1.toString, Array.prototype.toString); | |
78 | |
79 // Redefining length does not change result | |
80 var a5 = new constructor([1, 2, 3]) | |
81 Object.defineProperty(a5, 'length', { value: 2 }); | |
82 assertEquals("1,2,3", a5.join()); | |
83 assertEquals("1,2,3", a5.toString()); | |
84 assertEquals("1,2,3", a5.toLocaleString()); | |
85 assertEquals("1,2", Array.prototype.join.call(a5)); | |
86 assertEquals("1,2,3", Array.prototype.toString.call(a5)); | |
87 assertEquals("1,2", Array.prototype.toLocaleString.call(a5)); | |
88 } | |
OLD | NEW |