summaryrefslogtreecommitdiff
path: root/test/built-ins/TypedArrays/ctors-bigint/typedarray-arg/typedarray-arg-detached-when-species-retrieved-different-type.js
blob: c5de19602e878dc05d896a3c402c1b087fd3165b (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
// Copyright (C) 2017 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-typedarray-typedarray
description: >
    When a TypedArray is created from another TypedArray with a different element-type
    and SpeciesConstructor detaches the source buffer, AllocateArrayBuffer is still
    executed.
info: |
    22.2.4.3 TypedArray ( typedArray )

    ...
    16. If IsSharedArrayBuffer(srcData) is false, then
        a. Let bufferConstructor be ? SpeciesConstructor(srcData, %ArrayBuffer%).
    ...
    18. If SameValue(elementType, srcType) is true, then
        ...
    19. Else,
        a. Let data be ? AllocateArrayBuffer(bufferConstructor, byteLength).
        b. If IsDetachedBuffer(srcData) is true, throw a TypeError exception.
    ...

    24.1.1.1 AllocateArrayBuffer ( constructor, byteLength )

    1. Let obj be ? OrdinaryCreateFromConstructor(constructor, "%ArrayBufferPrototype%",
       « [[ArrayBufferData]], [[ArrayBufferByteLength]] »).
    ...
includes: [testBigIntTypedArray.js, detachArrayBuffer.js]
features: [BigInt, TypedArray, Symbol.species]
---*/

testWithBigIntTypedArrayConstructors(function(TA) {
    var speciesCallCount = 0;
    var bufferConstructor = Object.defineProperty({}, Symbol.species, {
        get: function() {
            speciesCallCount += 1;
            $DETACHBUFFER(ta.buffer);
            return speciesConstructor;
        }
    });

    var prototypeCallCount = 0;
    var speciesConstructor = Object.defineProperty(function(){}.bind(), "prototype", {
        get: function() {
            prototypeCallCount += 1;
            return null;
        }
    });

    var ta = new TA(0);
    ta.buffer.constructor = bufferConstructor;

    assert.throws(TypeError, function() {
        var targetType = TA !== Int32Array ? Int32Array : Uint32Array;
        new targetType(ta);
    }, "TypeError thrown for detached source buffer");

    assert.sameValue(speciesCallCount, 1, "@@species getter called once");
    assert.sameValue(prototypeCallCount, 1, "prototype getter called once");
});