|
| 1 | +import test from 'ava'; |
| 2 | +import '../index.js'; |
| 3 | + |
| 4 | +const { isFrozen, getPrototypeOf } = Object; |
| 5 | + |
| 6 | +lockdown(); |
| 7 | + |
| 8 | +// TODO Need to treat the hidden prototype as a hidden instrinsic so it |
| 9 | +// gets hardened when the rest do. |
| 10 | +test.failing('Immutable ArrayBuffer installed and hardened', t => { |
| 11 | + const ab1 = new ArrayBuffer(2); |
| 12 | + const iab = ab1.transferToImmutable(); |
| 13 | + const iabProto = getPrototypeOf(iab); |
| 14 | + t.true(isFrozen(iabProto)); |
| 15 | + t.true(isFrozen(iabProto.slice)); |
| 16 | +}); |
| 17 | + |
| 18 | +test('Immutable ArrayBuffer ops', t => { |
| 19 | + // Absent on Node <= 18 |
| 20 | + const canResize = 'maxByteLength' in ArrayBuffer.prototype; |
| 21 | + const canDetach = 'detached' in ArrayBuffer.prototype; |
| 22 | + |
| 23 | + const ab1 = new ArrayBuffer(2, { maxByteLength: 7 }); |
| 24 | + const ta1 = new Uint8Array(ab1); |
| 25 | + ta1[0] = 3; |
| 26 | + ta1[1] = 4; |
| 27 | + const iab = ab1.transferToImmutable(); |
| 28 | + t.true(iab instanceof ArrayBuffer); |
| 29 | + ta1[1] = 5; |
| 30 | + const ab2 = iab.slice(0); |
| 31 | + const ta2 = new Uint8Array(ab2); |
| 32 | + t.is(ta1[1], canDetach ? undefined : 5); |
| 33 | + t.is(ta2[1], 4); |
| 34 | + ta2[1] = 6; |
| 35 | + |
| 36 | + const ab3 = iab.slice(0); |
| 37 | + t.true(ab3 instanceof ArrayBuffer); |
| 38 | + |
| 39 | + const ta3 = new Uint8Array(ab3); |
| 40 | + t.is(ta1[1], canDetach ? undefined : 5); |
| 41 | + t.is(ta2[1], 6); |
| 42 | + t.is(ta3[1], 4); |
| 43 | + |
| 44 | + t.is(ab1.byteLength, canDetach ? 0 : 2); |
| 45 | + t.is(iab.byteLength, 2); |
| 46 | + t.is(ab2.byteLength, 2); |
| 47 | + |
| 48 | + t.is(iab.maxByteLength, 2); |
| 49 | + if (canResize) { |
| 50 | + t.is(ab1.maxByteLength, canDetach? 0 : 7); |
| 51 | + t.is(ab2.maxByteLength, 2); |
| 52 | + } |
| 53 | + |
| 54 | + if (canDetach) { |
| 55 | + t.true(ab1.detached); |
| 56 | + t.false(ab2.detached); |
| 57 | + t.false(ab3.detached); |
| 58 | + } |
| 59 | + t.false(iab.detached); |
| 60 | + t.false(iab.resizable); |
| 61 | +}); |
| 62 | + |
| 63 | +// This could have been written as a test.failing as compared to |
| 64 | +// the immutable ArrayBuffer we'll propose. However, I'd rather test what |
| 65 | +// the shim purposely does instead. |
| 66 | +test('Immutable ArrayBuffer shim limitations', t => { |
| 67 | + const ab1 = new ArrayBuffer(2); |
| 68 | + const dv1 = new DataView(ab1); |
| 69 | + t.is(dv1.buffer, ab1); |
| 70 | + t.is(dv1.byteLength, 2); |
| 71 | + const ta1 = new Uint8Array(ab1); |
| 72 | + ta1[0] = 3; |
| 73 | + ta1[1] = 4; |
| 74 | + t.is(ta1.byteLength, 2); |
| 75 | + |
| 76 | + t.throws(() => new DataView({}), { instanceOf: TypeError }); |
| 77 | + // Unfortutanely, calling a TypeArray constructor with an object that |
| 78 | + // is not a TypeArray, ArrayBuffer, or Iterable just creates a useless |
| 79 | + // empty TypedArray, rather than throwing. |
| 80 | + const ta2 = new Uint8Array({}); |
| 81 | + t.is(ta2.byteLength, 0); |
| 82 | + |
| 83 | + const iab = ab1.transferToImmutable(); |
| 84 | + t.throws(() => new DataView(iab), { |
| 85 | + instanceOf: TypeError, |
| 86 | + }); |
| 87 | + // Unfortunately, unlike the immutable ArrayBuffer to be proposed, |
| 88 | + // calling a TypedArray constructor with the shim implementation of |
| 89 | + // an immutable ArrayBuffer as argument treats it as an unrecognized object, |
| 90 | + // rather than throwing an error. |
| 91 | + t.is(iab.byteLength, 2); |
| 92 | + const ta3 = new Uint8Array(iab); |
| 93 | + t.is(ta3.byteLength, 0); |
| 94 | +}); |
0 commit comments