patched-global.any.js (1901B)
1 // META: global=window,worker,shadowrealm 2 // META: script=../resources/test-utils.js 3 'use strict'; 4 5 // Tests which patch the global environment are kept separate to avoid 6 // interfering with other tests. 7 8 promise_test(async (t) => { 9 let controller; 10 const rs = new ReadableStream({ 11 type: 'bytes', 12 start(c) { 13 controller = c; 14 } 15 }); 16 const reader = rs.getReader({mode: 'byob'}); 17 18 const length = 0x4000; 19 const buffer = new ArrayBuffer(length); 20 const bigArray = new BigUint64Array(buffer, length - 8, 1); 21 22 const read1 = reader.read(new Uint8Array(new ArrayBuffer(0x100))); 23 const read2 = reader.read(bigArray); 24 25 let flag = false; 26 Object.defineProperty(Object.prototype, 'then', { 27 get: t.step_func(() => { 28 if (!flag) { 29 flag = true; 30 assert_equals(controller.byobRequest, null, 'byobRequest should be null after filling both views'); 31 } 32 }), 33 configurable: true 34 }); 35 t.add_cleanup(() => { 36 delete Object.prototype.then; 37 }); 38 39 controller.enqueue(new Uint8Array(0x110).fill(0x42)); 40 assert_true(flag, 'patched then() should be called'); 41 42 // The first read() is filled entirely with 0x100 bytes 43 const result1 = await read1; 44 assert_false(result1.done, 'result1.done'); 45 assert_typed_array_equals(result1.value, new Uint8Array(0x100).fill(0x42), 'result1.value'); 46 47 // The second read() is filled with the remaining 0x10 bytes 48 const result2 = await read2; 49 assert_false(result2.done, 'result2.done'); 50 assert_equals(result2.value.constructor, BigUint64Array, 'result2.value constructor'); 51 assert_equals(result2.value.byteOffset, length - 8, 'result2.value byteOffset'); 52 assert_equals(result2.value.length, 1, 'result2.value length'); 53 assert_array_equals([...result2.value], [0x42424242_42424242n], 'result2.value contents'); 54 }, 'Patched then() sees byobRequest after filling all pending pull-into descriptors');