Closed Bug 1427812 Opened 8 years ago Closed 7 years ago

Incorrect Polyfill function ArrayBuffer.transfer()

Categories

(Developer Documentation Graveyard :: JavaScript, enhancement, P1)

All
Other
enhancement

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: Moritz.Hedtke, Assigned: fs)

References

()

Details

:: Developer Documentation Request Request Type: Correction Gecko Version: unspecified Technical Contact: :: Details if (!ArrayBuffer.transfer) { ArrayBuffer.transfer = function(source, length) { if (!(source instanceof ArrayBuffer)) throw new TypeError('Source must be an instance of ArrayBuffer'); if (length >= source.byteLength) return source.slice(0, length); var sourceView = new Uint8Array(source), destView = new Uint8Array(new ArrayBuffer(length)); destView.set(sourceView); return dest.buffer; }; } has the following 2 issues: * The if (length >= source.byteLength) check should be if (length <= source.byteLength) as source.slice(0, length) does not work if length is larger than the length of source. * The variable dest does not exist -> should be destView Working polyfill: if (!ArrayBuffer.transfer) { ArrayBuffer.transfer = function(source, length) { if (!(source instanceof ArrayBuffer)) throw new TypeError('Source must be an instance of ArrayBuffer'); if (length <= source.byteLength) return source.slice(0, length); var sourceView = new Uint8Array(source), destView = new Uint8Array(new ArrayBuffer(length)); destView.set(sourceView); return destView.buffer; }; } Html for reproduction: <!doctype html> <html> <head> <title></title> </head> <body> <script> if (!ArrayBuffer.transfer) { ArrayBuffer.transfer = function(source, length) { if (!(source instanceof ArrayBuffer)) throw new TypeError('Source must be an instance of ArrayBuffer'); if (length <= source.byteLength) return source.slice(0, length); var sourceView = new Uint8Array(source), destView = new Uint8Array(new ArrayBuffer(length)); destView.set(sourceView); return destView.buffer; }; } var buf1 = new ArrayBuffer(40); new Int32Array(buf1)[0] = 42; var buf2 = ArrayBuffer.transfer(buf1, 80); console.log('should be 40: ' + buf1.byteLength); // 0 but if you use the polyfill then the value is still 40 console.log('should be 80: ' + buf2.byteLength); // 80 console.log('should be 42: ' + new Int32Array(buf2)[0]); // 42 var buf3 = ArrayBuffer.transfer(buf2, 0); console.log('should be 80: ' + buf2.byteLength); // 0 but if you use the polyfill then the value is still 80 console.log('should be 0: ' + buf3.byteLength); // 0 </script> </body> </html>
Priority: P5 → P2
Assignee: nobody → fscholz
Priority: P2 → P1
Thanks, this is fixed now.
Status: UNCONFIRMED → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.