From 266ea38d91c74389ba68a1630fa0d392722592ab Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 23 Nov 2018 07:35:55 -0800 Subject: [PATCH 1/4] Improve types of fill polyfill and test fallback The polyfill will now return the T instead of the TypedArray union type. Tests now target the fallback only so engines that support fill will run tests against the fallback code. --- src/core/TypedArrayUtils.test.ts | 54 ++++++++++++++++---------------- src/core/TypedArrayUtils.ts | 8 +++-- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/core/TypedArrayUtils.test.ts b/src/core/TypedArrayUtils.test.ts index ef86d314..1df06f4c 100644 --- a/src/core/TypedArrayUtils.test.ts +++ b/src/core/TypedArrayUtils.test.ts @@ -3,7 +3,7 @@ * @license MIT */ import { assert } from 'chai'; -import { fill } from './TypedArrayUtils'; +import { fillFallback } from './TypedArrayUtils'; type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array @@ -42,42 +42,42 @@ describe('polyfill conformance tests', function(): void { it('should work with all typed array types', function(): void { const u81 = new Uint8Array(5); const u82 = new Uint8Array(5); - deepEquals(fill(u81, 2), u82.fill(2)); - deepEquals(fill(u81, -1), u82.fill(-1)); + deepEquals(fillFallback(u81, 2), u82.fill(2)); + deepEquals(fillFallback(u81, -1), u82.fill(-1)); const u161 = new Uint16Array(5); const u162 = new Uint16Array(5); - deepEquals(fill(u161, 2), u162.fill(2)); - deepEquals(fill(u161, 65535), u162.fill(65535)); - deepEquals(fill(u161, -1), u162.fill(-1)); + deepEquals(fillFallback(u161, 2), u162.fill(2)); + deepEquals(fillFallback(u161, 65535), u162.fill(65535)); + deepEquals(fillFallback(u161, -1), u162.fill(-1)); const u321 = new Uint32Array(5); const u322 = new Uint32Array(5); - deepEquals(fill(u321, 2), u322.fill(2)); - deepEquals(fill(u321, 65537), u322.fill(65537)); - deepEquals(fill(u321, -1), u322.fill(-1)); + deepEquals(fillFallback(u321, 2), u322.fill(2)); + deepEquals(fillFallback(u321, 65537), u322.fill(65537)); + deepEquals(fillFallback(u321, -1), u322.fill(-1)); const i81 = new Int8Array(5); const i82 = new Int8Array(5); - deepEquals(fill(i81, 2), i82.fill(2)); - deepEquals(fill(i81, -1), i82.fill(-1)); + deepEquals(fillFallback(i81, 2), i82.fill(2)); + deepEquals(fillFallback(i81, -1), i82.fill(-1)); const i161 = new Int16Array(5); const i162 = new Int16Array(5); - deepEquals(fill(i161, 2), i162.fill(2)); - deepEquals(fill(i161, 65535), i162.fill(65535)); - deepEquals(fill(i161, -1), i162.fill(-1)); + deepEquals(fillFallback(i161, 2), i162.fill(2)); + deepEquals(fillFallback(i161, 65535), i162.fill(65535)); + deepEquals(fillFallback(i161, -1), i162.fill(-1)); const i321 = new Int32Array(5); const i322 = new Int32Array(5); - deepEquals(fill(i321, 2), i322.fill(2)); - deepEquals(fill(i321, 65537), i322.fill(65537)); - deepEquals(fill(i321, -1), i322.fill(-1)); + deepEquals(fillFallback(i321, 2), i322.fill(2)); + deepEquals(fillFallback(i321, 65537), i322.fill(65537)); + deepEquals(fillFallback(i321, -1), i322.fill(-1)); const f321 = new Float32Array(5); const f322 = new Float32Array(5); - deepEquals(fill(f321, 1.2345), f322.fill(1.2345)); + deepEquals(fillFallback(f321, 1.2345), f322.fill(1.2345)); const f641 = new Float64Array(5); const f642 = new Float64Array(5); - deepEquals(fill(f641, 1.2345), f642.fill(1.2345)); + deepEquals(fillFallback(f641, 1.2345), f642.fill(1.2345)); const u8Clamped1 = new Uint8ClampedArray(5); const u8Clamped2 = new Uint8ClampedArray(5); - deepEquals(fill(u8Clamped1, 2), u8Clamped2.fill(2)); - deepEquals(fill(u8Clamped1, 257), u8Clamped2.fill(257)); + deepEquals(fillFallback(u8Clamped1, 2), u8Clamped2.fill(2)); + deepEquals(fillFallback(u8Clamped1, 257), u8Clamped2.fill(257)); }); it('should work with all typed array types - explicit looping', function(): void { const u81 = new Uint8Array(5); @@ -124,8 +124,8 @@ describe('polyfill conformance tests', function(): void { const u81 = new Uint8Array(5); const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); - deepEquals(fill(u81, 2, i), u83.fill(2, i)); - deepEquals(fill(u81, -1, i), u83.fill(-1, i)); + deepEquals(fillFallback(u81, 2, i), u83.fill(2, i)); + deepEquals(fillFallback(u81, -1, i), u83.fill(-1, i)); deepEquals(loopFill(u82, 2, i), u83.fill(2, i)); deepEquals(loopFill(u82, -1, i), u83.fill(-1, i)); } @@ -135,8 +135,8 @@ describe('polyfill conformance tests', function(): void { const u81 = new Uint8Array(5); const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); - deepEquals(fill(u81, 2, 0, i), u83.fill(2, 0, i)); - deepEquals(fill(u81, -1, 0, i), u83.fill(-1, 0, i)); + deepEquals(fillFallback(u81, 2, 0, i), u83.fill(2, 0, i)); + deepEquals(fillFallback(u81, -1, 0, i), u83.fill(-1, 0, i)); deepEquals(loopFill(u82, 2, 0, i), u83.fill(2, 0, i)); deepEquals(loopFill(u82, -1, 0, i), u83.fill(-1, 0, i)); } @@ -147,8 +147,8 @@ describe('polyfill conformance tests', function(): void { const u81 = new Uint8Array(5); const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); - deepEquals(fill(u81, 2, i, j), u83.fill(2, i, j)); - deepEquals(fill(u81, -1, i, j), u83.fill(-1, i, j)); + deepEquals(fillFallback(u81, 2, i, j), u83.fill(2, i, j)); + deepEquals(fillFallback(u81, -1, i, j), u83.fill(-1, i, j)); deepEquals(loopFill(u82, 2, i, j), u83.fill(2, i, j)); deepEquals(loopFill(u82, -1, i, j), u83.fill(-1, i, j)); } diff --git a/src/core/TypedArrayUtils.ts b/src/core/TypedArrayUtils.ts index 56e9d7b0..2e85400c 100644 --- a/src/core/TypedArrayUtils.ts +++ b/src/core/TypedArrayUtils.ts @@ -12,11 +12,15 @@ type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; -export function fill(array: TypedArray, value: number, start: number = 0, end?: number | undefined): TypedArray { +export function fill(array: T, value: number, start: number = 0, end?: number | undefined): T { // all modern engines that support .fill if (array.fill) { - return array.fill(value, start, end); + return array.fill(value, start, end) as T; } + return fillFallback(array, value, start, end); +} + +export function fillFallback(array: T, value: number, start: number = 0, end?: number | undefined): T { // safari and IE 11 // since IE 11 does not support Array.prototype.fill either // we cannot use the suggested polyfill from MDN From 4f18717940e5be633228c1dabd81d0dd78f19a82 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 23 Nov 2018 07:49:35 -0800 Subject: [PATCH 2/4] Improve handling of default values --- src/core/TypedArrayUtils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/core/TypedArrayUtils.ts b/src/core/TypedArrayUtils.ts index 2e85400c..6e1a3630 100644 --- a/src/core/TypedArrayUtils.ts +++ b/src/core/TypedArrayUtils.ts @@ -12,7 +12,7 @@ type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; -export function fill(array: T, value: number, start: number = 0, end?: number | undefined): T { +export function fill(array: T, value: number, start?: number, end?: number): T { // all modern engines that support .fill if (array.fill) { return array.fill(value, start, end) as T; @@ -20,7 +20,7 @@ export function fill(array: T, value: number, start: numbe return fillFallback(array, value, start, end); } -export function fillFallback(array: T, value: number, start: number = 0, end?: number | undefined): T { +export function fillFallback(array: T, value: number, start: number = 0, end: number = array.length): T { // safari and IE 11 // since IE 11 does not support Array.prototype.fill either // we cannot use the suggested polyfill from MDN @@ -29,9 +29,6 @@ export function fillFallback(array: T, value: number, star return array; } start = (array.length + start) % array.length; - if (end === undefined) { - end = array.length; - } if (end >= array.length) { end = array.length; } else { From 1b5ef5d93a047b346b20beafd68545ea6295d20e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 23 Nov 2018 08:08:30 -0800 Subject: [PATCH 3/4] Move TypedArrayUtils to common It belongs here since the renderers will use it --- src/CharWidth.ts | 2 +- src/{core => common}/TypedArrayUtils.test.ts | 0 src/{core => common}/TypedArrayUtils.ts | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename src/{core => common}/TypedArrayUtils.test.ts (100%) rename src/{core => common}/TypedArrayUtils.ts (100%) diff --git a/src/CharWidth.ts b/src/CharWidth.ts index fd6ac55f..43cd948e 100644 --- a/src/CharWidth.ts +++ b/src/CharWidth.ts @@ -3,7 +3,7 @@ * @license MIT */ -import { fill } from './core/TypedArrayUtils'; +import { fill } from './common/TypedArrayUtils'; export const wcwidth = (function(opts: {nul: number, control: number}): (ucs: number) => number { // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c diff --git a/src/core/TypedArrayUtils.test.ts b/src/common/TypedArrayUtils.test.ts similarity index 100% rename from src/core/TypedArrayUtils.test.ts rename to src/common/TypedArrayUtils.test.ts diff --git a/src/core/TypedArrayUtils.ts b/src/common/TypedArrayUtils.ts similarity index 100% rename from src/core/TypedArrayUtils.ts rename to src/common/TypedArrayUtils.ts From 41ba20f7499356f8ad45cb9526ed6e84c93f6267 Mon Sep 17 00:00:00 2001 From: jerch Date: Fri, 23 Nov 2018 18:40:52 +0100 Subject: [PATCH 4/4] remove not longer needed test cases Removed the `loopFill` test method as it is not needed anymore with the separation of the looping variant in `fillFallback`. --- src/common/TypedArrayUtils.test.ts | 69 ------------------------------ 1 file changed, 69 deletions(-) diff --git a/src/common/TypedArrayUtils.test.ts b/src/common/TypedArrayUtils.test.ts index 1df06f4c..69a62abc 100644 --- a/src/common/TypedArrayUtils.test.ts +++ b/src/common/TypedArrayUtils.test.ts @@ -9,26 +9,6 @@ type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array; -// we explicitly test against the looping version in the test cases -function loopFill(array: TypedArray, value: number, start: number = 0, end?: number | undefined): TypedArray { - if (start >= array.length) { - return array; - } - start = (array.length + start) % array.length; - if (end === undefined) { - end = array.length; - } - if (end >= array.length) { - end = array.length; - } else { - end = (array.length + end) % array.length; - } - for (let i = start; i < end; ++i) { - array[i] = value; - } - return array; -} - describe('polyfill conformance tests', function(): void { function deepEquals(a: TypedArray, b: TypedArray): void { @@ -79,78 +59,29 @@ describe('polyfill conformance tests', function(): void { deepEquals(fillFallback(u8Clamped1, 2), u8Clamped2.fill(2)); deepEquals(fillFallback(u8Clamped1, 257), u8Clamped2.fill(257)); }); - it('should work with all typed array types - explicit looping', function(): void { - const u81 = new Uint8Array(5); - const u82 = new Uint8Array(5); - deepEquals(loopFill(u81, 2), u82.fill(2)); - deepEquals(loopFill(u81, -1), u82.fill(-1)); - const u161 = new Uint16Array(5); - const u162 = new Uint16Array(5); - deepEquals(loopFill(u161, 2), u162.fill(2)); - deepEquals(loopFill(u161, 65535), u162.fill(65535)); - deepEquals(loopFill(u161, -1), u162.fill(-1)); - const u321 = new Uint32Array(5); - const u322 = new Uint32Array(5); - deepEquals(loopFill(u321, 2), u322.fill(2)); - deepEquals(loopFill(u321, 65537), u322.fill(65537)); - deepEquals(loopFill(u321, -1), u322.fill(-1)); - const i81 = new Int8Array(5); - const i82 = new Int8Array(5); - deepEquals(loopFill(i81, 2), i82.fill(2)); - deepEquals(loopFill(i81, -1), i82.fill(-1)); - const i161 = new Int16Array(5); - const i162 = new Int16Array(5); - deepEquals(loopFill(i161, 2), i162.fill(2)); - deepEquals(loopFill(i161, 65535), i162.fill(65535)); - deepEquals(loopFill(i161, -1), i162.fill(-1)); - const i321 = new Int32Array(5); - const i322 = new Int32Array(5); - deepEquals(loopFill(i321, 2), i322.fill(2)); - deepEquals(loopFill(i321, 65537), i322.fill(65537)); - deepEquals(loopFill(i321, -1), i322.fill(-1)); - const f321 = new Float32Array(5); - const f322 = new Float32Array(5); - deepEquals(loopFill(f321, 1.2345), f322.fill(1.2345)); - const f641 = new Float64Array(5); - const f642 = new Float64Array(5); - deepEquals(loopFill(f641, 1.2345), f642.fill(1.2345)); - const u8Clamped1 = new Uint8ClampedArray(5); - const u8Clamped2 = new Uint8ClampedArray(5); - deepEquals(loopFill(u8Clamped1, 2), u8Clamped2.fill(2)); - deepEquals(loopFill(u8Clamped1, 257), u8Clamped2.fill(257)); - }); it('start offset', function(): void { for (let i = -2; i < 10; ++i) { const u81 = new Uint8Array(5); - const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); deepEquals(fillFallback(u81, 2, i), u83.fill(2, i)); deepEquals(fillFallback(u81, -1, i), u83.fill(-1, i)); - deepEquals(loopFill(u82, 2, i), u83.fill(2, i)); - deepEquals(loopFill(u82, -1, i), u83.fill(-1, i)); } }); it('end offset', function(): void { for (let i = -2; i < 10; ++i) { const u81 = new Uint8Array(5); - const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); deepEquals(fillFallback(u81, 2, 0, i), u83.fill(2, 0, i)); deepEquals(fillFallback(u81, -1, 0, i), u83.fill(-1, 0, i)); - deepEquals(loopFill(u82, 2, 0, i), u83.fill(2, 0, i)); - deepEquals(loopFill(u82, -1, 0, i), u83.fill(-1, 0, i)); } }); it('start/end offset', function(): void { for (let i = -2; i < 10; ++i) { for (let j = -2; j < 10; ++j) { const u81 = new Uint8Array(5); - const u82 = new Uint8Array(5); const u83 = new Uint8Array(5); deepEquals(fillFallback(u81, 2, i, j), u83.fill(2, i, j)); deepEquals(fillFallback(u81, -1, i, j), u83.fill(-1, i, j)); - deepEquals(loopFill(u82, 2, i, j), u83.fill(2, i, j)); - deepEquals(loopFill(u82, -1, i, j), u83.fill(-1, i, j)); } } });