| 1 |
// Any copyright is dedicated to the Public Domain. |
| 2 |
// http://creativecommons.org/licenses/publicdomain/ |
| 3 |
|
| 4 |
load(libdir + "asserts.js") |
| 5 |
|
| 6 |
const constructors = [ |
| 7 |
Int8Array, |
| 8 |
Uint8Array, |
| 9 |
Uint8ClampedArray, |
| 10 |
Int16Array, |
| 11 |
Uint16Array, |
| 12 |
Int32Array, |
| 13 |
Uint32Array, |
| 14 |
Float16Array, |
| 15 |
Float32Array, |
| 16 |
Float64Array |
| 17 |
]; |
| 18 |
|
| 19 |
for (constructor of constructors) { |
| 20 |
print("testing non-empty " + constructor.name); |
| 21 |
let a = new constructor(10); |
| 22 |
assertEq(Object.isExtensible(a), true); |
| 23 |
assertEq(Object.isSealed(a), false); |
| 24 |
assertEq(Object.isFrozen(a), false); |
| 25 |
|
| 26 |
// Should complain that it can't change attributes of indexed typed array properties. |
| 27 |
assertThrowsInstanceOf(() => Object.seal(a), TypeError); |
| 28 |
|
| 29 |
// Should complain that it can't change attributes of indexed typed array properties. |
| 30 |
assertThrowsInstanceOf(() => Object.freeze(a), TypeError); |
| 31 |
} |
| 32 |
|
| 33 |
print(); |
| 34 |
|
| 35 |
for (constructor of constructors) { |
| 36 |
print("testing zero-length " + constructor.name); |
| 37 |
let a = new constructor(0); |
| 38 |
assertEq(Object.isExtensible(a), true); |
| 39 |
assertEq(Object.isSealed(a), false); |
| 40 |
assertEq(Object.isFrozen(a), false); |
| 41 |
|
| 42 |
// Should not throw. |
| 43 |
Object.seal(a); |
| 44 |
Object.freeze(a); |
| 45 |
} |
| 46 |
|
| 47 |
// isSealed and isFrozen should not try to build an array of all the |
| 48 |
// property names of a typed array, since they're often especially large. |
| 49 |
// This should not throw an allocation error. |
| 50 |
let a = new Uint8Array(1 << 24); |
| 51 |
Object.isSealed(a); |
| 52 |
Object.isFrozen(a); |
| 53 |
|
| 54 |
for (constructor of constructors) { |
| 55 |
print("testing extensibility " + constructor.name); |
| 56 |
let a = new constructor(10); |
| 57 |
|
| 58 |
// New named properties should show up on typed arrays. |
| 59 |
a.foo = "twelve"; |
| 60 |
assertEq(a.foo, "twelve"); |
| 61 |
|
| 62 |
// New indexed properties should not show up on typed arrays. |
| 63 |
a[20] = "twelve"; |
| 64 |
assertEq(a[20], undefined); |
| 65 |
|
| 66 |
// Watch for especially large indexed properties. |
| 67 |
a[-10 >>> 0] = "twelve"; |
| 68 |
assertEq(a[-10 >>> 0], undefined); |
| 69 |
|
| 70 |
// Watch for really large indexed properties too. |
| 71 |
a[Math.pow(2, 53)] = "twelve"; |
| 72 |
assertEq(a[Math.pow(2, 53)], undefined); |
| 73 |
|
| 74 |
// Don't define old properties. |
| 75 |
Object.defineProperty(a, 5, {value: 3}); |
| 76 |
assertEq(a[5], 3); |
| 77 |
|
| 78 |
// Don't define new properties. |
| 79 |
assertThrowsInstanceOf(() => Object.defineProperty(a, 20, {value: 3}), TypeError); |
| 80 |
assertEq(a[20], undefined); |
| 81 |
|
| 82 |
// Don't delete indexed properties. |
| 83 |
a[3] = 3; |
| 84 |
delete a[3]; |
| 85 |
assertEq(a[3], 3); |
| 86 |
} |