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