summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/ion/bug977966.js
blob: 6d80c5c376bb4b1b10d77b71dd51a75e3ce1b840 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// |jit-test| --ion-eager

function join_check() {
    var lengthWasCalled = false;
    var obj = {"0": "", "1": ""};
    Object.defineProperty(obj, "length", {
        get : function(){ lengthWasCalled = true; return 2; },
        enumerable : true,
        configurable : true
    });

    var res = Array.prototype.join.call(obj, { toString: function () {
        if (lengthWasCalled)
            return "good";
        else
            return "bad";
    }})

    assertEq(res, "good");
}
function split(i) {
    var x = (i + "->" + i).split("->");
    assertEq(x[0], "" + i);
    return i;
}

function join(i) {
    var x = [i, i].join("->");
    assertEq(x, i + "->" + i);
    return i;
}

function split_join(i) {
    var x = (i + "-" + i).split("-").join("->");
    assertEq(x, i + "->" + i);
    return i;
}

function split_join_2(i) {
    var x = (i + "-" + i).split("-");
    x.push("" + i);
    var res = x.join("->");
    assertEq(res, i + "->" + i + "->" + i);
    return i;
}

function resumeHere() { bailout(); }

function split_join_3(i) {
    var x = (i + "-" + i).split("-");
    resumeHere();
    var res = x.join("->");
    assertEq(res, i + "->" + i);
    return i;
}

function trip(i) {
    if (i == 99)
        assertEq(myjoin.arguments[1][0], "" + i)
}

function myjoin(i, x) {
    trip(i);
    return x.join("->");
}

function split_join_4(i) {
    var x = (i + "-" + i).split("-");
    var res = myjoin(i, x);
    assertEq(res, i + "->" + i);
    return i;
}

function split_join_5(i) {
    var s = "abca";
    assertEq(s.split("a").join("") + i, "bc" + i);
}

function split_join_two_byte_char(i) {
    var s1 = "ab";
    assertEq(s1.split("").join("\u03c0"), "a\u03c0b");
    var s2 = i + "\u03c0" + i;
    assertEq(s2.split("\u03c0").join("-"), i + "-" + i);
}

function split_join_underflow(i)
{
    var s = "";
    assertEq(s.split("").join("x" + i), "");
}

// Check that we do not consider the string argument of join as a replacement
// pattern, as the string replace primitive is supposed to do.
function split_join_pattern(i) {
    var s = i + "-" + i;
    assertEq(s.split("-").join("$`$&$'"), i + "$`$&$'" + i);
    assertEq(s.replace("-", "$`$&$'"), "" + i + i + "-" + i + i);
}

// Check that, as opposed to String.replace, we are doing a global replacement
// as String.split does.
function split_join_multiple(i) {
    var s1 = i + "-\n-" + i + "-\n-" + i;
    assertEq(s1.split("-\n-").join("-")  , i + "-" + i + "-" + i);
    assertEq(s1.replace("-\n-", "-")     , i + "-" + i + "-\n-" + i);

    var s2 = "abc";
    assertEq(s2.split("").join("" + i)   , "a" + i + "b" + i + "c");
    assertEq(s2.replace("", "" + i)      , i + "abc");
}

for (var i = 0; i < 1000; ++i) {
    join_check(i);
    split(i);
    join(i);
    split_join(i);
    split_join_2(i);
    split_join_3(i);
    split_join_4(i);
    split_join_5(i);
    split_join_pattern(i);
    split_join_multiple(i);
    split_join_two_byte_char(i);
    split_join_underflow(i);
}