Skip to content

Commit d93ca84

Browse files
MaxGraeydcodeIO
authored andcommittedNov 8, 2018
Correct TypedArray#byteOffset handling and fix TypedArray#subarray (#328)
1 parent 2ecec66 commit d93ca84

File tree

5 files changed

+361
-418
lines changed

5 files changed

+361
-418
lines changed
 

‎std/assembly/internal/typedarray.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,13 @@ export abstract class TypedArray<T,V> {
3232

3333
@inline
3434
get length(): i32 {
35-
return (this.byteLength - this.byteOffset) >> alignof<T>();
35+
return this.byteLength >>> alignof<T>();
3636
}
3737

3838
@operator("[]")
3939
protected __get(index: i32): T {
40-
var byteOffset = this.byteOffset;
41-
var elementLength = (this.byteLength - byteOffset) >>> alignof<T>();
42-
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");
43-
return loadUnsafeWithOffset<T,T>(this.buffer, index, byteOffset);
40+
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
41+
return loadUnsafeWithOffset<T,T>(this.buffer, index, this.byteOffset);
4442
}
4543

4644
@inline @operator("{}")
@@ -50,10 +48,8 @@ export abstract class TypedArray<T,V> {
5048

5149
@operator("[]=")
5250
protected __set(index: i32, value: V): void {
53-
var byteOffset = this.byteOffset;
54-
var elementLength = (this.byteLength - byteOffset) >>> alignof<T>();
55-
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");
56-
storeUnsafeWithOffset<T,V>(this.buffer, index, value, byteOffset);
51+
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
52+
storeUnsafeWithOffset<T,V>(this.buffer, index, value, this.byteOffset);
5753
}
5854

5955
@inline @operator("{}=")
@@ -95,7 +91,7 @@ export abstract class TypedArray<T,V> {
9591
var slice = memory.allocate(offsetof<this>());
9692
store<usize>(slice, this.buffer, offsetof<this>("buffer"));
9793
store<i32>(slice, begin << alignof<T>(), offsetof<this>("byteOffset"));
98-
store<i32>(slice, end << alignof<T>(), offsetof<this>("byteLength"));
94+
store<i32>(slice, (end - begin) << alignof<T>(), offsetof<this>("byteLength"));
9995
return changetype<this>(slice);
10096
}
10197

‎std/assembly/typedarray.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Uint8Array extends TypedArray<u8,u32> {
2121
export class Uint8ClampedArray extends TypedArray<u8,u32> {
2222
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
2323

24-
@operator("[]=")
24+
@inline @operator("[]=")
2525
protected __set(index: i32, value: i32): void {
2626
super.__set(index, max(min(value, 255), 0));
2727
}

0 commit comments

Comments
 (0)