summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/argumentsLengthOpt.js
blob: 16385fa5a4962883afaa2994f0092b3830b957a0 (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
// Test cases for arguments.length optimization.

function f1() {
    return arguments.length;
}

function f2(a, b, c) {
    return arguments.length;
}

// arrow functions don't have their own arguments, and so capture the enclosing
// scope.
function f3(a, b, c, d) {
    return (() => arguments.length)();
}

// Test a function which mutates arguments.length
function f4(a, b, c, d) {
    arguments.length = 42;
    return arguments.length;
}

// Manually read out arguments; should disable the length opt
function f5() {
    for (var i = 0; i < arguments.length; i++) {
        if (arguments[i] == 10) { return true }
    }
    return false;
}

function f6() {
    function inner() {
        return arguments.length;
    }
    return inner(1, 2, 3);
}

// edge cases of the arguments bindings:
function f7() {
    var arguments = 42;
    return arguments;
}

function f8() {
    var arguments = [1, 2];
    return arguments.length;
}

function f9() {
    eval("arguments.length = 42");
    return arguments.length;
}

function test() {
    assertEq(f1(), 0);
    assertEq(f1(1), 1);
    assertEq(f1(1, 2), 2);
    assertEq(f1(1, 2, 3), 3);

    assertEq(f2(), 0);
    assertEq(f2(1, 2, 3), 3);

    assertEq(f3(), 0);
    assertEq(f3(1, 2, 3), 3);

    assertEq(f4(), 42);
    assertEq(f4(1, 2, 3), 42);

    assertEq(f5(), false);
    assertEq(f5(1, 2, 3, 10), true);
    assertEq(f5(1, 2, 3, 10, 20), true);
    assertEq(f5(1, 2, 3, 9, 20, 30), false);

    assertEq(f6(), 3)
    assertEq(f6(1, 2, 3, 4), 3)

    assertEq(f7(), 42);

    assertEq(f8(), 2);

    assertEq(f9(), 42);
}

test();

if (typeof reportCompare === "function")
    reportCompare(0, 0, "ok");