Implement optional type parameters (#360)

* Add a NATIVE<T> macro type to simplify use of a native WebAssembly type
* Add default type parameters for internal helpers for explicit loads and stores
* Unify loadUnsafe/loadUnsafeWithOffset etc. into one
* Renamed loadUnsafe etc. into just LOAD, like a macro
* Implement parsing of index signatures, but ignore them, for properly linting code
* Refactor TypedArray<T> to use macros
This commit is contained in:
Daniel Wirtz
2018-12-07 14:33:32 +01:00
committed by GitHub
parent d7f4874650
commit ebae7cbd73
39 changed files with 4698 additions and 4128 deletions

View File

@ -1,52 +1,77 @@
import {
TypedArray
TypedArray,
FILL,
SORT,
SUBARRAY,
REDUCE,
REDUCE_RIGHT
} from "./internal/typedarray";
export class Int8Array extends TypedArray<i8,i32> {
import {
defaultComparator
} from "./internal/array";
export class Int8Array extends TypedArray<i8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
return FILL<Int8Array, i8>(this, value, start, end);
}
sort(comparator: (a: i8, b: i8) => i32 = defaultComparator<i8>()): Int8Array {
return SORT<Int8Array, i8>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int8Array {
return changetype<Int8Array>(super.subarray(begin, end));
return SUBARRAY<Int8Array, i8>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: i8, index: i32, array: Int8Array) => T,
initialValue: T,
): T {
return REDUCE<Int8Array, i8, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i8, index: i32, array: Int8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int8Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: i8, index: i32, array: Int8Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Int8Array, i8, T>(this, callbackfn, initialValue);
}
}
export class Uint8Array extends TypedArray<u8,u32> {
export class Uint8Array extends TypedArray<u8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint8Array {
return FILL<Uint8Array, u8>(this, value, start, end);
}
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8Array {
return SORT<Uint8Array, u8>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8Array {
return changetype<Uint8Array>(super.subarray(begin, end));
return SUBARRAY<Uint8Array, u8>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: u8, index: i32, array: Uint8Array) => T,
initialValue: T,
): T {
return REDUCE<Uint8Array, u8, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: u8, index: i32, array: Uint8Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Uint8Array, u8, T>(this, callbackfn, initialValue);
}
}
export class Uint8ClampedArray extends TypedArray<u8,u32> {
export class Uint8ClampedArray extends Uint8Array {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
@inline @operator("[]=")
@ -58,202 +83,244 @@ export class Uint8ClampedArray extends TypedArray<u8,u32> {
protected __unchecked_set(index: i32, value: i32): void {
super.__unchecked_set(index, max(min(value, 255), 0));
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.subarray(begin, end));
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u8, index: i32, array: Uint8ClampedArray) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint8ClampedArray, ReturnType>(callbackfn, this, initialValue, true);
}
}
export class Int16Array extends TypedArray<i16,i32> {
export class Int16Array extends TypedArray<i16> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
return FILL<Int16Array, i16>(this, value, start, end);
}
sort(comparator: (a: i16, b: i16) => i32 = defaultComparator<i16>()): Int16Array {
return SORT<Int16Array, i16>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int16Array {
return changetype<Int16Array>(super.subarray(begin, end));
return SUBARRAY<Int16Array, i16>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: i16, index: i32, array: Int16Array) => T,
initialValue: T,
): T {
return REDUCE<Int16Array, i16, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i16, index: i32, array: Int16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int16Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: i16, index: i32, array: Int16Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Int16Array, i16, T>(this, callbackfn, initialValue);
}
}
export class Uint16Array extends TypedArray<u16,u32> {
export class Uint16Array extends TypedArray<u16> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
return FILL<Uint16Array, u16>(this, value, start, end);
}
sort(comparator: (a: u16, b: u16) => i32 = defaultComparator<u16>()): Uint16Array {
return SORT<Uint16Array, u16>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint16Array {
return changetype<Uint16Array>(super.subarray(begin, end));
return SUBARRAY<Uint16Array, u16>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: u16, index: i32, array: Uint16Array) => T,
initialValue: T,
): T {
return REDUCE<Uint16Array, u16, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u16, index: i32, array: Uint16Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint16Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: u16, index: i32, array: Uint16Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Uint16Array, u16, T>(this, callbackfn, initialValue);
}
}
export class Int32Array extends TypedArray<i32,i32> {
export class Int32Array extends TypedArray<i32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
return FILL<Int32Array, i32>(this, value, start, end);
}
sort(comparator: (a: i32, b: i32) => i32 = defaultComparator<i32>()): Int32Array {
return SORT<Int32Array, i32>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int32Array {
return changetype<Int32Array>(super.subarray(begin, end));
return SUBARRAY<Int32Array, i32>(this, begin, end);
}
/**
* @param callbackfn {function} - a function that reduces each value to a ReturnType
* @param initialValue {ReturnType} - the initial ReturnType value to be passed to the callbackfn
*/
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: i32, index: i32, array: Int32Array) => T,
initialValue: T,
): T {
return REDUCE<Int32Array, i32, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i32, index: i32, array: Int32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int32Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: i32, index: i32, array: Int32Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Int32Array, i32, T>(this, callbackfn, initialValue);
}
}
export class Uint32Array extends TypedArray<u32,u32> {
export class Uint32Array extends TypedArray<u32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
return FILL<Uint32Array, u32>(this, value, start, end);
}
sort(comparator: (a: u32, b: u32) => i32 = defaultComparator<u32>()): Uint32Array {
return SORT<Uint32Array, u32>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint32Array {
return changetype<Uint32Array>(super.subarray(begin, end));
return SUBARRAY<Uint32Array, u32>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: u32, index: i32, array: Uint32Array) => T,
initialValue: T,
): T {
return REDUCE<Uint32Array, u32, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u32, index: i32, array: Uint32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint32Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: u32, index: i32, array: Uint32Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Uint32Array, u32, T>(this, callbackfn, initialValue);
}
}
export class Int64Array extends TypedArray<i64,i64> {
export class Int64Array extends TypedArray<i64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
fill(value: i64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
return FILL<Int64Array, i64>(this, value, start, end);
}
sort(comparator: (a: i64, b: i64) => i32 = defaultComparator<i64>()): Int64Array {
return SORT<Int64Array, i64>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Int64Array {
return changetype<Int64Array>(super.subarray(begin, end));
return SUBARRAY<Int64Array, i64>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: i64, index: i32, array: Int64Array) => T,
initialValue: T,
): T {
return REDUCE<Int64Array, i64, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: i64, index: i32, array: Int64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Int64Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: i64, index: i32, array: Int64Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Int64Array, i64, T>(this, callbackfn, initialValue);
}
}
export class Uint64Array extends TypedArray<u64,u64> {
export class Uint64Array extends TypedArray<u64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
fill(value: u64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
return FILL<Uint64Array, u64>(this, value, start, end);
}
sort(comparator: (a: u64, b: u64) => i32 = defaultComparator<u64>()): Uint64Array {
return SORT<Uint64Array, u64>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Uint64Array {
return changetype<Uint64Array>(super.subarray(begin, end));
return SUBARRAY<Uint64Array, u64>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: u64, index: i32, array: Uint64Array) => T,
initialValue: T,
): T {
return REDUCE<Uint64Array, u64, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: u64, index: i32, array: Uint64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Uint64Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: u64, index: i32, array: Uint64Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Uint64Array, u64, T>(this, callbackfn, initialValue);
}
}
export class Float32Array extends TypedArray<f32,f32> {
export class Float32Array extends TypedArray<f32> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
fill(value: f32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
return FILL<Float32Array, f32>(this, value, start, end);
}
sort(comparator: (a: f32, b: f32) => i32 = defaultComparator<f32>()): Float32Array {
return SORT<Float32Array, f32>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float32Array {
return changetype<Float32Array>(super.subarray(begin, end));
return SUBARRAY<Float32Array, f32>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: f32, index: i32, array: Float32Array) => T,
initialValue: T,
): T {
return REDUCE<Float32Array, f32, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f32, index: i32, array: Float32Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float32Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: f32, index: i32, array: Float32Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Float32Array, f32, T>(this, callbackfn, initialValue);
}
}
export class Float64Array extends TypedArray<f64,f64> {
export class Float64Array extends TypedArray<f64> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
fill(value: f64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
return FILL<Float64Array, f64>(this, value, start, end);
}
sort(comparator: (a: f64, b: f64) => i32 = defaultComparator<f64>()): Float64Array {
return SORT<Float64Array, f64>(this, comparator);
}
subarray(begin: i32 = 0, end: i32 = 0x7fffffff): Float64Array {
return changetype<Float64Array>(super.subarray(begin, end));
return SUBARRAY<Float64Array, f64>(this, begin, end);
}
reduce<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue);
reduce<T>(
callbackfn: (accumulator: T, value: f64, index: i32, array: Float64Array) => T,
initialValue: T,
): T {
return REDUCE<Float64Array, f64, T>(this, callbackfn, initialValue);
}
reduceRight<ReturnType>(
callbackfn: (accumulator: ReturnType, value: f64, index: i32, array: Float64Array) => ReturnType,
initialValue: ReturnType,
): ReturnType {
return super.reduce_internal<Float64Array, ReturnType>(callbackfn, this, initialValue, true);
reduceRight<T>(
callbackfn: (accumulator: T, value: f64, index: i32, array: Float64Array) => T,
initialValue: T,
): T {
return REDUCE_RIGHT<Float64Array, f64, T>(this, callbackfn, initialValue);
}
}