summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/TypedArray/of.js
blob: af9c1120235160cab0921270b78e7ee34a7461f0 (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
for (var constructor of anyTypedArrayConstructors) {
    assertEq(constructor.of.length, 0);

    assertDeepEq(Object.getOwnPropertyDescriptor(constructor.__proto__, "of"), {
        value: constructor.of,
        writable: true,
        enumerable: false,
        configurable: true
    });

    // Basic tests.
    assertEq(constructor.of().constructor, constructor);
    assertEq(constructor.of() instanceof constructor, true);
    assertDeepEq(constructor.of(10), new constructor([10]));
    assertDeepEq(constructor.of(1, 2, 3), new constructor([1, 2, 3]));
    assertDeepEq(constructor.of("1", "2", "3"), new constructor([1, 2, 3]));

    // This method can't be transplanted to other constructors.
    assertThrowsInstanceOf(() => constructor.of.call(Array), TypeError);
    assertThrowsInstanceOf(() => constructor.of.call(Array, 1, 2, 3), TypeError);

    var hits = 0;
    assertDeepEq(constructor.of.call(function(len) {
        assertEq(arguments.length, 1);
        assertEq(len, 3);
        hits++;
        return new constructor(len);
    }, 10, 20, 30), new constructor([10, 20, 30]));
    assertEq(hits, 1);

    // Behavior across compartments.
    if (typeof newGlobal === "function") {
        var newC = newGlobal()[constructor.name];
        assertEq(newC.of() instanceof newC, true);
        assertEq(newC.of() instanceof constructor, false);
        assertEq(newC.of.call(constructor) instanceof constructor, true);
    }

    // Throws if `this` isn't a constructor.
    var invalidConstructors = [undefined, null, 1, false, "", Symbol(), [], {}, /./,
                               constructor.of, () => {}];
    invalidConstructors.forEach(C => {
        assertThrowsInstanceOf(() => {
            constructor.of.call(C);
        }, TypeError);
    });

    // Throw if `this` is a method definition or a getter/setter function.
    assertThrowsInstanceOf(() => {
        constructor.of.call({method() {}}.method);
    }, TypeError);
    assertThrowsInstanceOf(() => {
        constructor.of.call(Object.getOwnPropertyDescriptor({get getter() {}}, "getter").get);
    }, TypeError);

    // Generators are not legal constructors.
    assertThrowsInstanceOf(() => {
      constructor.of.call(function*(len) {
        return len;
      }, "a")
    }, TypeError);

    // An exception might be thrown in a strict assignment to the new object's indexed properties.
    assertThrowsInstanceOf(() => {
        constructor.of.call(function() {
            return {get 0() {}};
        }, "a");
    }, TypeError);

    assertThrowsInstanceOf(() => {
        constructor.of.call(function() {
            return Object("1");
        }, "a");
    }, TypeError);

    assertThrowsInstanceOf(() => {
        constructor.of.call(function() {
            return Object.create({
                set 0(v) {
                    throw new TypeError;
                }
            });
        }, "a");
    }, TypeError);
}

for (let constructor of anyTypedArrayConstructors.filter(isFloatConstructor)) {
    assertDeepEq(constructor.of(0.1, null, undefined, NaN), new constructor([0.1, 0, NaN, NaN]));
}

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