(createSourceBuffer, description)
| 5 | // Flags: --js-immutable-arraybuffer --allow-natives-syntax |
| 6 | |
| 7 | function TestImmutableBuffer(createSourceBuffer, description) { |
| 8 | const ab = createSourceBuffer(); |
| 9 | const immutable = ab.transferToImmutable(); |
| 10 | |
| 11 | assertTrue(immutable.immutable); |
| 12 | assertEquals(10, immutable.byteLength); |
| 13 | assertFalse(immutable.resizable, "Immutable buffer should not be resizable"); |
| 14 | assertEquals(immutable.byteLength, immutable.maxByteLength, "Immutable buffer maxByteLength should equal byteLength"); |
| 15 | |
| 16 | // 1. Test Detach throws |
| 17 | assertThrows(() => %ArrayBufferDetach(immutable), TypeError); |
| 18 | |
| 19 | // 2. Test transfer throws |
| 20 | assertThrows(() => immutable.transfer(), TypeError); |
| 21 | |
| 22 | // 3. Test resize throws |
| 23 | assertThrows(() => immutable.resize(20), TypeError); |
| 24 | |
| 25 | // 4. Test slice works (should return a new mutable buffer by default) |
| 26 | const sliced = immutable.slice(0, 5); |
| 27 | assertFalse(sliced.immutable); |
| 28 | assertEquals(5, sliced.byteLength); |
| 29 | |
| 30 | // 5. Test sliceToImmutable works |
| 31 | const slicedImmutable = immutable.sliceToImmutable(0, 5); |
| 32 | assertTrue(slicedImmutable.immutable); |
| 33 | assertEquals(5, slicedImmutable.byteLength); |
| 34 | |
| 35 | // 6. Test DataView set throws |
| 36 | const dv = new DataView(immutable); |
| 37 | assertThrows(() => dv.setUint8(0, 1), TypeError); |
| 38 | assertThrows(() => dv.setUint8(0, 255), TypeError); |
| 39 | assertThrows(() => dv.setInt8(0, -1), TypeError); |
| 40 | assertThrows(() => dv.setUint16(0, 0x1234), TypeError); |
| 41 | assertThrows(() => dv.setFloat64(0, 3.14), TypeError); |
| 42 | // 7. Test TypedArray set throws |
| 43 | const ta = new Uint8Array(immutable); |
| 44 | |
| 45 | // Sloppy mode assignment - silent failure |
| 46 | ta[0] = 1; |
| 47 | ta[0] = 1; |
| 48 | assertEquals(0, ta[0]); // Value should not change |
| 49 | |
| 50 | // Strict mode assignment - throws TypeError |
| 51 | assertThrows(() => { |
| 52 | "use strict"; |
| 53 | ta[0] = 1; |
| 54 | }, TypeError); |
| 55 | |
| 56 | assertThrows(() => ta.set([1]), TypeError); |
| 57 | assertThrows(() => ta.fill(1), TypeError); |
| 58 | |
| 59 | // 8. Test TypedArray.from/of with immutable buffer not directly applicable unless we construct one manually |
| 60 | // but we can test constructing a TypedArray from immutable buffer (Read-only) matches values |
| 61 | assertEquals(0, ta[0]); |
| 62 | |
| 63 | // 9. Test property descriptors of TypedArray backed by immutable buffer |
| 64 | const desc = Object.getOwnPropertyDescriptor(ta, 0); |
no test coverage detected