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>();

View File

@ -10,11 +10,13 @@
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
(global $~lib/allocator/arena/offset (mut i32) (i32.const 0))
(global $std/typedarray/arr (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 164))
(global $std/typedarray/clampedArr (mut i32) (i32.const 0))
(global $HEAP_BASE i32 (i32.const 204))
(memory $0 1)
(data (i32.const 4) "\11\00\00\00s\00t\00d\00/\00t\00y\00p\00e\00d\00a\00r\00r\00a\00y\00.\00t\00s")
(data (i32.const 44) "\1b\00\00\00~\00l\00i\00b\00/\00i\00n\00t\00e\00r\00n\00a\00l\00/\00t\00y\00p\00e\00d\00a\00r\00r\00a\00y\00.\00t\00s")
(data (i32.const 104) "\1c\00\00\00~\00l\00i\00b\00/\00i\00n\00t\00e\00r\00n\00a\00l\00/\00a\00r\00r\00a\00y\00b\00u\00f\00f\00e\00r\00.\00t\00s")
(data (i32.const 164) "\12\00\00\00~\00l\00i\00b\00/\00t\00y\00p\00e\00d\00a\00r\00r\00a\00y\00.\00t\00s")
(export "memory" (memory $0))
(start $start)
(func $~lib/internal/arraybuffer/computeSize (; 1 ;) (type $ii) (param $0 i32) (result i32)
@ -821,7 +823,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 17)
(i32.const 18)
(i32.const 2)
)
(unreachable)
@ -838,7 +840,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 18)
(i32.const 19)
(i32.const 2)
)
(unreachable)
@ -855,7 +857,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 19)
(i32.const 20)
(i32.const 2)
)
(unreachable)
@ -874,7 +876,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 22)
(i32.const 23)
(i32.const 2)
)
(unreachable)
@ -891,7 +893,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 23)
(i32.const 24)
(i32.const 2)
)
(unreachable)
@ -908,7 +910,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 24)
(i32.const 25)
(i32.const 2)
)
(unreachable)
@ -917,32 +919,12 @@
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i16>#constructor
(call $~lib/internal/typedarray/TypedArray<i8>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 27)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 1)
)
)
(block
(call $abort
(i32.const 0)
@ -955,7 +937,7 @@
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i16>#get:length
(i32.load offset=8
(get_local $1)
)
(get_local $0)
@ -970,6 +952,23 @@
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i8>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 30)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
@ -983,7 +982,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 32)
(i32.const 33)
(i32.const 2)
)
(unreachable)
@ -1003,7 +1002,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 33)
(i32.const 34)
(i32.const 2)
)
(unreachable)
@ -1020,7 +1019,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 34)
(i32.const 35)
(i32.const 2)
)
(unreachable)
@ -1029,32 +1028,12 @@
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i32>#constructor
(call $~lib/internal/typedarray/TypedArray<i16>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 37)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
(block
(call $abort
(i32.const 0)
@ -1067,10 +1046,13 @@
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i32>#get:length
(i32.load offset=8
(get_local $1)
)
(get_local $0)
(i32.shl
(get_local $0)
(i32.const 1)
)
)
(block
(call $abort
@ -1083,32 +1065,29 @@
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i32>#constructor
(i32.const 0)
(get_local $0)
)
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i16>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 42)
(i32.const 40)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 2)
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i32>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
@ -1123,10 +1102,13 @@
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i32>#get:length
(i32.load offset=8
(get_local $1)
)
(get_local $0)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
(block
(call $abort
@ -1138,48 +1120,9 @@
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i64>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 47)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 3)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 48)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i64>#get:length
(call $~lib/internal/typedarray/TypedArray<i32>#get:length
(get_local $1)
)
(get_local $0)
@ -1188,63 +1131,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 49)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i64>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 52)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 3)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 53)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i64>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 54)
(i32.const 45)
(i32.const 2)
)
(unreachable)
@ -1263,7 +1150,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 57)
(i32.const 48)
(i32.const 2)
)
(unreachable)
@ -1283,7 +1170,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 58)
(i32.const 49)
(i32.const 2)
)
(unreachable)
@ -1300,7 +1187,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 59)
(i32.const 50)
(i32.const 2)
)
(unreachable)
@ -1319,7 +1206,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 62)
(i32.const 53)
(i32.const 2)
)
(unreachable)
@ -1339,7 +1226,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 63)
(i32.const 54)
(i32.const 2)
)
(unreachable)
@ -1352,6 +1239,101 @@
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 55)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i64>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 58)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 3)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 59)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i64>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 60)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i32>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 63)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
(block
(call $abort
(i32.const 0)
@ -1362,6 +1344,79 @@
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i32>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 65)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.load offset=4
(tee_local $1
(call $~lib/internal/typedarray/TypedArray<i64>#constructor
(i32.const 0)
(get_local $0)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 68)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(i32.load offset=8
(get_local $1)
)
(i32.shl
(get_local $0)
(i32.const 3)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 69)
(i32.const 2)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<i64>#get:length
(get_local $1)
)
(get_local $0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 70)
(i32.const 2)
)
(unreachable)
)
)
)
(func $~lib/internal/typedarray/TypedArray<i32>#__set (; 14 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
@ -1573,7 +1628,94 @@
)
(get_local $2)
)
(func $start (; 17 ;) (type $v)
(func $~lib/typedarray/Uint8ClampedArray#__set (; 17 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i32)
(if
(i32.ge_u
(get_local $1)
(i32.sub
(i32.load offset=8
(get_local $0)
)
(tee_local $3
(i32.load offset=4
(get_local $0)
)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 164)
(i32.const 32)
(i32.const 42)
)
(unreachable)
)
)
(i32.store8 offset=8
(i32.add
(i32.add
(i32.load
(get_local $0)
)
(get_local $3)
)
(get_local $1)
)
(i32.trunc_u/f64
(f64.max
(f64.const 0)
(f64.min
(f64.const 255)
(f64.convert_s/i32
(get_local $2)
)
)
)
)
)
)
(func $~lib/internal/typedarray/TypedArray<u8>#__get (; 18 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(if
(i32.ge_u
(get_local $1)
(i32.sub
(i32.load offset=8
(get_local $0)
)
(tee_local $2
(i32.load offset=4
(get_local $0)
)
)
)
)
(block
(call $abort
(i32.const 0)
(i32.const 44)
(i32.const 39)
(i32.const 42)
)
(unreachable)
)
)
(i32.load8_u offset=8
(i32.add
(i32.add
(i32.load
(get_local $0)
)
(get_local $2)
)
(get_local $1)
)
)
)
(func $start (; 19 ;) (type $v)
(set_global $~lib/allocator/arena/startOffset
(i32.and
(i32.add
@ -1624,7 +1766,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 74)
(i32.const 80)
(i32.const 0)
)
(unreachable)
@ -1638,7 +1780,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 75)
(i32.const 81)
(i32.const 0)
)
(unreachable)
@ -1655,7 +1797,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 76)
(i32.const 82)
(i32.const 0)
)
(unreachable)
@ -1673,7 +1815,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 77)
(i32.const 83)
(i32.const 0)
)
(unreachable)
@ -1691,7 +1833,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 78)
(i32.const 84)
(i32.const 0)
)
(unreachable)
@ -1709,7 +1851,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 79)
(i32.const 85)
(i32.const 0)
)
(unreachable)
@ -1733,7 +1875,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 82)
(i32.const 88)
(i32.const 0)
)
(unreachable)
@ -1750,7 +1892,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 83)
(i32.const 89)
(i32.const 0)
)
(unreachable)
@ -1767,7 +1909,7 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 84)
(i32.const 90)
(i32.const 0)
)
(unreachable)
@ -1785,7 +1927,79 @@
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 85)
(i32.const 91)
(i32.const 0)
)
(unreachable)
)
)
(set_global $std/typedarray/clampedArr
(call $~lib/internal/typedarray/TypedArray<i8>#constructor
(i32.const 0)
(i32.const 3)
)
)
(call $~lib/typedarray/Uint8ClampedArray#__set
(get_global $std/typedarray/clampedArr)
(i32.const 0)
(i32.const -32)
)
(call $~lib/typedarray/Uint8ClampedArray#__set
(get_global $std/typedarray/clampedArr)
(i32.const 1)
(i32.const 2)
)
(call $~lib/typedarray/Uint8ClampedArray#__set
(get_global $std/typedarray/clampedArr)
(i32.const 2)
(i32.const 256)
)
(if
(call $~lib/internal/typedarray/TypedArray<u8>#__get
(get_global $std/typedarray/clampedArr)
(i32.const 0)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 98)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<u8>#__get
(get_global $std/typedarray/clampedArr)
(i32.const 1)
)
(i32.const 2)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 99)
(i32.const 0)
)
(unreachable)
)
)
(if
(i32.ne
(call $~lib/internal/typedarray/TypedArray<u8>#__get
(get_global $std/typedarray/clampedArr)
(i32.const 2)
)
(i32.const 255)
)
(block
(call $abort
(i32.const 0)
(i32.const 4)
(i32.const 100)
(i32.const 0)
)
(unreachable)

View File

@ -1,5 +1,6 @@
assert(Int8Array.BYTES_PER_ELEMENT == 1);
assert(Uint8Array.BYTES_PER_ELEMENT == 1);
assert(Uint8ClampedArray.BYTES_PER_ELEMENT == 1);
assert(Int16Array.BYTES_PER_ELEMENT == 2);
assert(Uint16Array.BYTES_PER_ELEMENT == 2);
assert(Int32Array.BYTES_PER_ELEMENT == 4);
@ -23,6 +24,11 @@ function testInstantiate(len: i32): void {
assert(u8a.byteLength == len * Uint8Array.BYTES_PER_ELEMENT);
assert(u8a.length == len);
var c8a = new Uint8ClampedArray(len);
assert(c8a.byteOffset == 0);
assert(c8a.byteLength == len * Uint8Array.BYTES_PER_ELEMENT);
assert(c8a.length == len);
var i16a = new Int16Array(len);
assert(i16a.byteOffset == 0);
assert(i16a.byteLength == len * Int16Array.BYTES_PER_ELEMENT);
@ -84,6 +90,15 @@ assert(arr.byteOffset == 1 * sizeof<i32>());
assert(arr.byteLength == 2 * sizeof<i32>());
assert(arr[0] == 2);
var clampedArr = new Uint8ClampedArray(3);
clampedArr[0] = -32;
clampedArr[1] = 2;
clampedArr[2] = 256;
assert(clampedArr[0] == 0);
assert(clampedArr[1] == 2);
assert(clampedArr[2] == 255);
import { MAX_BLENGTH } from "internal/arraybuffer";
const MAX_F64LENGTH = <u32>MAX_BLENGTH >> alignof<f64>();

File diff suppressed because it is too large Load Diff