summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/trailing_comma_arguments.js
blob: 612fd46a130e7f5252f89b6575c334d54841bb98 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Trailing comma in Arguments production.

// 12.3 Left-Hand-Side Expressions
// Arguments[Yield]:
//   ()
//   ( ArgumentList[?Yield] )
//   ( ArgumentList[?Yield] , )


function argsLength() {
    return {value: arguments.length};
}
function sum(...rest) {
    return {value: rest.reduce((a, c) => a + c, 0)};
}

function call(f, argList) {
    return eval(`(${f}(${argList})).value`);
}

function newCall(F, argList) {
    return eval(`(new ${F}(${argList})).value`);
}

function superCall(superClass, argList) {
    return eval(`(new class extends ${superClass} {
        constructor() {
            super(${argList});
        }
    }).value`);
}

// Ensure the correct number of arguments is passed.
for (let type of [call, newCall, superCall]) {
    let test = type.bind(null, "argsLength");

    assertEq(test("10, "), 1);
    assertEq(test("10, 20, "), 2);
    assertEq(test("10, 20, 30, "), 3);
    assertEq(test("10, 20, 30, 40, "), 4);

    assertEq(test("...[10, 20], "), 2);
    assertEq(test("...[10, 20], 30, "), 3);
    assertEq(test("...[10, 20], ...[30], "), 3);
}

// Ensure the arguments themselves are passed correctly.
for (let type of [call, newCall, superCall]) {
    let test = type.bind(null, "sum");

    assertEq(test("10, "), 10);
    assertEq(test("10, 20, "), 30);
    assertEq(test("10, 20, 30, "), 60);
    assertEq(test("10, 20, 30, 40, "), 100);

    assertEq(test("...[10, 20], "), 30);
    assertEq(test("...[10, 20], 30, "), 60);
    assertEq(test("...[10, 20], ...[30], "), 60);
}

// Error cases.
for (let type of [call, newCall, superCall]) {
    let test = type.bind(null, "f");

    // Trailing comma in empty arguments list.
    assertThrowsInstanceOf(() => test(","), SyntaxError);

    // Leading comma.
    assertThrowsInstanceOf(() => test(", a"), SyntaxError);
    assertThrowsInstanceOf(() => test(", ...a"), SyntaxError);

    // Multiple trailing comma.
    assertThrowsInstanceOf(() => test("a, , "), SyntaxError);
    assertThrowsInstanceOf(() => test("...a, , "), SyntaxError);

    // Elision.
    assertThrowsInstanceOf(() => test("a, , b"), SyntaxError);
}

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