From 0d8e58a5cd25041121aae0af0906e6f64cfb2792 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Thu, 30 Jun 2016 00:46:43 +0300 Subject: [PATCH 1/2] fix(copy): fix typed subarrays handling Previously, it would return copy of the whole original typed array, not its slice. Now buffer offset and length are also saved. Closes #14842 --- src/Angular.js | 2 +- test/AngularSpec.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Angular.js b/src/Angular.js index a5180712dfa1..3457420ca93a 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -901,7 +901,7 @@ function copy(source, destination) { case '[object Uint8ClampedArray]': case '[object Uint16Array]': case '[object Uint32Array]': - return new source.constructor(copyElement(source.buffer)); + return new source.constructor(copyElement(source.buffer), source.byteOffset, source.length); case '[object ArrayBuffer]': //Support: IE10 diff --git a/test/AngularSpec.js b/test/AngularSpec.js index ec334690019d..4ef16c5e497b 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -240,6 +240,20 @@ describe('angular', function() { } }); + it("should handle Uint8Array subarray", function() { + if (typeof Uint8Array !== 'undefined') { + var arr = new Uint8Array(4); + arr[1] = 1; + var src = arr.subarray(1, 2); + var dst = copy(src); + expect(dst instanceof Uint8Array).toBeTruthy(); + expect(dst.length).toEqual(1); + expect(dst[0]).toEqual(1); + expect(dst).not.toBe(src); + expect(dst.buffer).not.toBe(src.buffer); + } + }); + it("should throw an exception if a Uint8Array is the destination", function() { if (typeof Uint8Array !== 'undefined') { var src = new Uint8Array(); From aa2eea30f0b0b79cf534e8a2347fe6ed895e356a Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Mon, 4 Jul 2016 15:56:14 +0300 Subject: [PATCH 2/2] Change type to Uint16 to demo byteLength vs length --- test/AngularSpec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 4ef16c5e497b..b7e4d821d743 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -240,13 +240,13 @@ describe('angular', function() { } }); - it("should handle Uint8Array subarray", function() { - if (typeof Uint8Array !== 'undefined') { - var arr = new Uint8Array(4); + it("should handle Uint16Array subarray", function() { + if (typeof Uint16Array !== 'undefined') { + var arr = new Uint16Array(4); arr[1] = 1; var src = arr.subarray(1, 2); var dst = copy(src); - expect(dst instanceof Uint8Array).toBeTruthy(); + expect(dst instanceof Uint16Array).toBeTruthy(); expect(dst.length).toEqual(1); expect(dst[0]).toEqual(1); expect(dst).not.toBe(src);