From 4432178dedee66ec73010c02e034d212948a69c9 Mon Sep 17 00:00:00 2001 From: Cayman Date: Fri, 13 Mar 2020 21:50:16 +0100 Subject: [PATCH] Add valueOf declarations for TypedArrays (#36668) --- src/lib/es2020.bigint.d.ts | 6 ++ src/lib/es5.d.ts | 27 ++++++ .../baselines/reference/valueOfTypedArray.js | 26 ++++++ .../reference/valueOfTypedArray.symbols | 72 +++++++++++++++ .../reference/valueOfTypedArray.types | 92 +++++++++++++++++++ tests/cases/compiler/valueOfTypedArray.ts | 13 +++ 6 files changed, 236 insertions(+) create mode 100644 tests/baselines/reference/valueOfTypedArray.js create mode 100644 tests/baselines/reference/valueOfTypedArray.symbols create mode 100644 tests/baselines/reference/valueOfTypedArray.types create mode 100644 tests/cases/compiler/valueOfTypedArray.ts diff --git a/src/lib/es2020.bigint.d.ts b/src/lib/es2020.bigint.d.ts index 332b654fc97..d39759b8189 100644 --- a/src/lib/es2020.bigint.d.ts +++ b/src/lib/es2020.bigint.d.ts @@ -268,6 +268,9 @@ interface BigInt64Array { /** Returns a string representation of the array. */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): BigInt64Array; + /** Yields each value in the array. */ values(): IterableIterator; @@ -537,6 +540,9 @@ interface BigUint64Array { /** Returns a string representation of the array. */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): BigUint64Array; + /** Yields each value in the array. */ values(): IterableIterator; diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index afdd6ad1795..9b981d69fc0 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -1920,6 +1920,9 @@ interface Int8Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Int8Array; + [index: number]: number; } interface Int8ArrayConstructor { @@ -2199,6 +2202,9 @@ interface Uint8Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Uint8Array; + [index: number]: number; } @@ -2478,6 +2484,9 @@ interface Uint8ClampedArray { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Uint8ClampedArray; + [index: number]: number; } @@ -2755,6 +2764,9 @@ interface Int16Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Int16Array; + [index: number]: number; } @@ -3035,6 +3047,9 @@ interface Uint16Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Uint16Array; + [index: number]: number; } @@ -3314,6 +3329,9 @@ interface Int32Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Int32Array; + [index: number]: number; } @@ -3592,6 +3610,9 @@ interface Uint32Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Uint32Array; + [index: number]: number; } @@ -3871,6 +3892,9 @@ interface Float32Array { */ toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Float32Array; + [index: number]: number; } @@ -4142,6 +4166,9 @@ interface Float64Array { toString(): string; + /** Returns the primitive value of the specified object. */ + valueOf(): Float64Array; + [index: number]: number; } diff --git a/tests/baselines/reference/valueOfTypedArray.js b/tests/baselines/reference/valueOfTypedArray.js new file mode 100644 index 00000000000..ea00d2fc243 --- /dev/null +++ b/tests/baselines/reference/valueOfTypedArray.js @@ -0,0 +1,26 @@ +//// [valueOfTypedArray.ts] +// All declarations should pass, as valueOf has been specialized for all TypedArrays +const typedArray0: Int8Array = (new Int8Array()).valueOf(); +const typedArray1: Uint8Array = (new Uint8Array()).valueOf(); +const typedArray2: Int16Array = (new Int16Array()).valueOf(); +const typedArray3: Uint16Array = (new Uint16Array()).valueOf(); +const typedArray4: Int32Array = (new Int32Array()).valueOf(); +const typedArray5: Uint32Array = (new Uint32Array()).valueOf(); +const typedArray6: Float32Array = (new Float32Array()).valueOf(); +const typedArray7: Float64Array = (new Float64Array()).valueOf(); +const typedArray8: BigInt64Array = (new BigInt64Array()).valueOf(); +const typedArray9: BigUint64Array = (new BigUint64Array()).valueOf(); + + +//// [valueOfTypedArray.js] +// All declarations should pass, as valueOf has been specialized for all TypedArrays +const typedArray0 = (new Int8Array()).valueOf(); +const typedArray1 = (new Uint8Array()).valueOf(); +const typedArray2 = (new Int16Array()).valueOf(); +const typedArray3 = (new Uint16Array()).valueOf(); +const typedArray4 = (new Int32Array()).valueOf(); +const typedArray5 = (new Uint32Array()).valueOf(); +const typedArray6 = (new Float32Array()).valueOf(); +const typedArray7 = (new Float64Array()).valueOf(); +const typedArray8 = (new BigInt64Array()).valueOf(); +const typedArray9 = (new BigUint64Array()).valueOf(); diff --git a/tests/baselines/reference/valueOfTypedArray.symbols b/tests/baselines/reference/valueOfTypedArray.symbols new file mode 100644 index 00000000000..533eb7a3f0f --- /dev/null +++ b/tests/baselines/reference/valueOfTypedArray.symbols @@ -0,0 +1,72 @@ +=== tests/cases/compiler/valueOfTypedArray.ts === +// All declarations should pass, as valueOf has been specialized for all TypedArrays +const typedArray0: Int8Array = (new Int8Array()).valueOf(); +>typedArray0 : Symbol(typedArray0, Decl(valueOfTypedArray.ts, 1, 5)) +>Int8Array : Symbol(Int8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Int8Array()).valueOf : Symbol(Int8Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Int8Array : Symbol(Int8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Int8Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray1: Uint8Array = (new Uint8Array()).valueOf(); +>typedArray1 : Symbol(typedArray1, Decl(valueOfTypedArray.ts, 2, 5)) +>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Uint8Array()).valueOf : Symbol(Uint8Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Uint8Array : Symbol(Uint8Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Uint8Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray2: Int16Array = (new Int16Array()).valueOf(); +>typedArray2 : Symbol(typedArray2, Decl(valueOfTypedArray.ts, 3, 5)) +>Int16Array : Symbol(Int16Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Int16Array()).valueOf : Symbol(Int16Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Int16Array : Symbol(Int16Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Int16Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray3: Uint16Array = (new Uint16Array()).valueOf(); +>typedArray3 : Symbol(typedArray3, Decl(valueOfTypedArray.ts, 4, 5)) +>Uint16Array : Symbol(Uint16Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Uint16Array()).valueOf : Symbol(Uint16Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Uint16Array : Symbol(Uint16Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Uint16Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray4: Int32Array = (new Int32Array()).valueOf(); +>typedArray4 : Symbol(typedArray4, Decl(valueOfTypedArray.ts, 5, 5)) +>Int32Array : Symbol(Int32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Int32Array()).valueOf : Symbol(Int32Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Int32Array : Symbol(Int32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Int32Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray5: Uint32Array = (new Uint32Array()).valueOf(); +>typedArray5 : Symbol(typedArray5, Decl(valueOfTypedArray.ts, 6, 5)) +>Uint32Array : Symbol(Uint32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Uint32Array()).valueOf : Symbol(Uint32Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Uint32Array : Symbol(Uint32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Uint32Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray6: Float32Array = (new Float32Array()).valueOf(); +>typedArray6 : Symbol(typedArray6, Decl(valueOfTypedArray.ts, 7, 5)) +>Float32Array : Symbol(Float32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Float32Array()).valueOf : Symbol(Float32Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Float32Array : Symbol(Float32Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Float32Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray7: Float64Array = (new Float64Array()).valueOf(); +>typedArray7 : Symbol(typedArray7, Decl(valueOfTypedArray.ts, 8, 5)) +>Float64Array : Symbol(Float64Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>(new Float64Array()).valueOf : Symbol(Float64Array.valueOf, Decl(lib.es5.d.ts, --, --)) +>Float64Array : Symbol(Float64Array, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2016.array.include.d.ts, --, --)) +>valueOf : Symbol(Float64Array.valueOf, Decl(lib.es5.d.ts, --, --)) + +const typedArray8: BigInt64Array = (new BigInt64Array()).valueOf(); +>typedArray8 : Symbol(typedArray8, Decl(valueOfTypedArray.ts, 9, 5)) +>BigInt64Array : Symbol(BigInt64Array, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --)) +>(new BigInt64Array()).valueOf : Symbol(BigInt64Array.valueOf, Decl(lib.es2020.bigint.d.ts, --, --)) +>BigInt64Array : Symbol(BigInt64Array, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --)) +>valueOf : Symbol(BigInt64Array.valueOf, Decl(lib.es2020.bigint.d.ts, --, --)) + +const typedArray9: BigUint64Array = (new BigUint64Array()).valueOf(); +>typedArray9 : Symbol(typedArray9, Decl(valueOfTypedArray.ts, 10, 5)) +>BigUint64Array : Symbol(BigUint64Array, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --)) +>(new BigUint64Array()).valueOf : Symbol(BigUint64Array.valueOf, Decl(lib.es2020.bigint.d.ts, --, --)) +>BigUint64Array : Symbol(BigUint64Array, Decl(lib.es2020.bigint.d.ts, --, --), Decl(lib.es2020.bigint.d.ts, --, --)) +>valueOf : Symbol(BigUint64Array.valueOf, Decl(lib.es2020.bigint.d.ts, --, --)) + diff --git a/tests/baselines/reference/valueOfTypedArray.types b/tests/baselines/reference/valueOfTypedArray.types new file mode 100644 index 00000000000..937efd903d9 --- /dev/null +++ b/tests/baselines/reference/valueOfTypedArray.types @@ -0,0 +1,92 @@ +=== tests/cases/compiler/valueOfTypedArray.ts === +// All declarations should pass, as valueOf has been specialized for all TypedArrays +const typedArray0: Int8Array = (new Int8Array()).valueOf(); +>typedArray0 : Int8Array +>(new Int8Array()).valueOf() : Int8Array +>(new Int8Array()).valueOf : () => Int8Array +>(new Int8Array()) : Int8Array +>new Int8Array() : Int8Array +>Int8Array : Int8ArrayConstructor +>valueOf : () => Int8Array + +const typedArray1: Uint8Array = (new Uint8Array()).valueOf(); +>typedArray1 : Uint8Array +>(new Uint8Array()).valueOf() : Uint8Array +>(new Uint8Array()).valueOf : () => Uint8Array +>(new Uint8Array()) : Uint8Array +>new Uint8Array() : Uint8Array +>Uint8Array : Uint8ArrayConstructor +>valueOf : () => Uint8Array + +const typedArray2: Int16Array = (new Int16Array()).valueOf(); +>typedArray2 : Int16Array +>(new Int16Array()).valueOf() : Int16Array +>(new Int16Array()).valueOf : () => Int16Array +>(new Int16Array()) : Int16Array +>new Int16Array() : Int16Array +>Int16Array : Int16ArrayConstructor +>valueOf : () => Int16Array + +const typedArray3: Uint16Array = (new Uint16Array()).valueOf(); +>typedArray3 : Uint16Array +>(new Uint16Array()).valueOf() : Uint16Array +>(new Uint16Array()).valueOf : () => Uint16Array +>(new Uint16Array()) : Uint16Array +>new Uint16Array() : Uint16Array +>Uint16Array : Uint16ArrayConstructor +>valueOf : () => Uint16Array + +const typedArray4: Int32Array = (new Int32Array()).valueOf(); +>typedArray4 : Int32Array +>(new Int32Array()).valueOf() : Int32Array +>(new Int32Array()).valueOf : () => Int32Array +>(new Int32Array()) : Int32Array +>new Int32Array() : Int32Array +>Int32Array : Int32ArrayConstructor +>valueOf : () => Int32Array + +const typedArray5: Uint32Array = (new Uint32Array()).valueOf(); +>typedArray5 : Uint32Array +>(new Uint32Array()).valueOf() : Uint32Array +>(new Uint32Array()).valueOf : () => Uint32Array +>(new Uint32Array()) : Uint32Array +>new Uint32Array() : Uint32Array +>Uint32Array : Uint32ArrayConstructor +>valueOf : () => Uint32Array + +const typedArray6: Float32Array = (new Float32Array()).valueOf(); +>typedArray6 : Float32Array +>(new Float32Array()).valueOf() : Float32Array +>(new Float32Array()).valueOf : () => Float32Array +>(new Float32Array()) : Float32Array +>new Float32Array() : Float32Array +>Float32Array : Float32ArrayConstructor +>valueOf : () => Float32Array + +const typedArray7: Float64Array = (new Float64Array()).valueOf(); +>typedArray7 : Float64Array +>(new Float64Array()).valueOf() : Float64Array +>(new Float64Array()).valueOf : () => Float64Array +>(new Float64Array()) : Float64Array +>new Float64Array() : Float64Array +>Float64Array : Float64ArrayConstructor +>valueOf : () => Float64Array + +const typedArray8: BigInt64Array = (new BigInt64Array()).valueOf(); +>typedArray8 : BigInt64Array +>(new BigInt64Array()).valueOf() : BigInt64Array +>(new BigInt64Array()).valueOf : () => BigInt64Array +>(new BigInt64Array()) : BigInt64Array +>new BigInt64Array() : BigInt64Array +>BigInt64Array : BigInt64ArrayConstructor +>valueOf : () => BigInt64Array + +const typedArray9: BigUint64Array = (new BigUint64Array()).valueOf(); +>typedArray9 : BigUint64Array +>(new BigUint64Array()).valueOf() : BigUint64Array +>(new BigUint64Array()).valueOf : () => BigUint64Array +>(new BigUint64Array()) : BigUint64Array +>new BigUint64Array() : BigUint64Array +>BigUint64Array : BigUint64ArrayConstructor +>valueOf : () => BigUint64Array + diff --git a/tests/cases/compiler/valueOfTypedArray.ts b/tests/cases/compiler/valueOfTypedArray.ts new file mode 100644 index 00000000000..310a039d85f --- /dev/null +++ b/tests/cases/compiler/valueOfTypedArray.ts @@ -0,0 +1,13 @@ +// @target: es2020 + +// All declarations should pass, as valueOf has been specialized for all TypedArrays +const typedArray0: Int8Array = (new Int8Array()).valueOf(); +const typedArray1: Uint8Array = (new Uint8Array()).valueOf(); +const typedArray2: Int16Array = (new Int16Array()).valueOf(); +const typedArray3: Uint16Array = (new Uint16Array()).valueOf(); +const typedArray4: Int32Array = (new Int32Array()).valueOf(); +const typedArray5: Uint32Array = (new Uint32Array()).valueOf(); +const typedArray6: Float32Array = (new Float32Array()).valueOf(); +const typedArray7: Float64Array = (new Float64Array()).valueOf(); +const typedArray8: BigInt64Array = (new BigInt64Array()).valueOf(); +const typedArray9: BigUint64Array = (new BigUint64Array()).valueOf();