-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
array-buffer-byte-length.js
66 lines (56 loc) · 2.74 KB
/
array-buffer-byte-length.js
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
//@ skip
function shouldBe(actual, expected)
{
if (actual !== expected)
throw new Error(`bad value: ${String(actual)}`);
}
function shouldThrow(func, errorMessage)
{
var errorThrown = false;
var error = null;
try {
func();
} catch (e) {
errorThrown = true;
error = e;
}
if (!errorThrown)
throw new Error('not thrown');
if (String(error) !== errorMessage)
throw new Error(`bad error: ${String(error)}`);
}
{
let arrayBuffer = new ArrayBuffer(42);
let sharedArrayBuffer = new SharedArrayBuffer(500);
shouldBe(arrayBuffer.byteLength, 42);
shouldBe(sharedArrayBuffer.byteLength, 500);
shouldBe(ArrayBuffer.prototype.hasOwnProperty('byteLength'), true);
shouldBe(SharedArrayBuffer.prototype.hasOwnProperty('byteLength'), true);
shouldBe(arrayBuffer.hasOwnProperty('byteLength'), false);
shouldBe(sharedArrayBuffer.hasOwnProperty('byteLength'), false);
shouldBe(!!Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get, true);
shouldBe(!!Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get, true);
shouldBe(!!Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').set, false);
shouldBe(!!Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').set, false);
shouldBe(Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get !== Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get, true);
shouldThrow(() => {
Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get.call(sharedArrayBuffer);
}, `TypeError: Receiver should not be a shared array buffer`);
shouldThrow(() => {
Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get.call(arrayBuffer);
}, `TypeError: Receiver should be a shared array buffer`);
for (let value of [ 0, true, "Cocoa", null, undefined, Symbol("Cappuccino") ]) {
shouldThrow(() => {
Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get.call(value);
}, `TypeError: Receiver should be an array buffer but was not an object`);
shouldThrow(() => {
Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get.call(value);
}, `TypeError: Receiver should be an array buffer but was not an object`);
}
shouldThrow(() => {
Object.getOwnPropertyDescriptor(ArrayBuffer.prototype, 'byteLength').get.call({});
}, `TypeError: Receiver should be an array buffer`);
shouldThrow(() => {
Object.getOwnPropertyDescriptor(SharedArrayBuffer.prototype, 'byteLength').get.call({});
}, `TypeError: Receiver should be an array buffer`);
}