summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ion/object-prototype-tostring.js
blob: ad06dbde893c7e4842e2c1e9e551013835138f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var toString = Object.prototype.toString;
var iter = 500;

function testConstant() {
    for (var i = 0; i < iter; i++) {
        assertEq(({}).toString(), "[object Object]");
        assertEq(toString.call([]), "[object Array]");
        assertEq(toString.call(Math.abs), "[object Function]");
    }
}
testConstant();

function testOwnToStringTag() {
    var stringify = o => toString.call(o);
    var o = {};
    for (var i = 0; i < iter; i++)
        assertEq(stringify(o), "[object Object]");
    o[Symbol.toStringTag] = "foo";
    for (var i = 0; i < iter; i++)
        assertEq(stringify(o), "[object foo]");
}
testOwnToStringTag();

function testDynamic() {
    var arr = [{}, [], new Date, /a/];
    var expected = ["[object Object]", "[object Array]", "[object Date]", "[object RegExp]"];
    for (var i = 0; i < iter; i++) {
        for (var j = 0; j < arr.length; j++)
            assertEq(toString.call(arr[j]), expected[j]);
    }
}
testDynamic();

function testToStringTagProto() {
    var c = 0;
    Object.defineProperty(Date.prototype, Symbol.toStringTag, {get() { c++; return "evil"; }});
    var arr = [{}, [], new Date, /a/];
    var expected = ["[object Object]", "[object Array]", "[object evil]", "[object RegExp]"];
    for (var i = 0; i < iter; i++) {
        for (var j = 0; j < arr.length; j++)
            assertEq(toString.call(arr[j]), expected[j]);
    }
    assertEq(c, iter);
}
testToStringTagProto();