mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 15:01:28 +00:00
Implement Uint8ClampedArray (#82)
This commit is contained in:
@ -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");
|
||||
|
@ -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>();
|
||||
|
||||
|
Reference in New Issue
Block a user