Implement Uint8ClampedArray (#82)

This commit is contained in:
Max Graey
2018-04-24 01:33:21 +03:00
committed by Daniel Wirtz
parent 63aa648ace
commit ddde13a648
5 changed files with 942 additions and 373 deletions

View File

@ -33,7 +33,7 @@ export abstract class TypedArray<T> implements ArrayBufferView<T> {
}
@operator("[]")
private __get(index: i32): T {
protected __get(index: i32): T {
var byteOffset = this.byteOffset;
var elementLength = (this.byteLength - byteOffset) >>> alignof<T>();
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");
@ -41,7 +41,7 @@ export abstract class TypedArray<T> implements ArrayBufferView<T> {
}
@operator("[]=")
private __set(index: i32, value: T): void {
protected __set(index: i32, value: T): void {
var byteOffset = this.byteOffset;
var elementLength = (this.byteLength - byteOffset) >>> alignof<T>();
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");

View File

@ -2,6 +2,10 @@ import {
TypedArray
} from "./internal/typedarray";
import {
storeUnsafeWithOffset
} from "./internal/arraybuffer";
export class Int8Array extends TypedArray<i8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
@ -18,6 +22,23 @@ export class Uint8Array extends TypedArray<u8> {
}
}
export class Uint8ClampedArray extends TypedArray<u8> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
@operator("[]=")
protected __set(index: i32, value: i32): void {
var byteOffset = this.byteOffset;
var elementLength = (this.byteLength - byteOffset) >>> alignof<u8>();
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");
var clampedValue = <u8>max(0, min(0xFF, value));
storeUnsafeWithOffset<u8>(this.buffer, index, clampedValue, byteOffset);
}
subarray(begin: i32 = 0, end: i32 = this.length): Uint8ClampedArray {
return changetype<Uint8ClampedArray>(super.subarray(begin, end));
}
}
export class Int16Array extends TypedArray<i16> {
static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();