mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 22:41:27 +00:00
Correct TypedArray#byteOffset handling and fix TypedArray#subarray (#328)
This commit is contained in:
@ -32,15 +32,13 @@ export abstract class TypedArray<T,V> {
|
||||
|
||||
@inline
|
||||
get length(): i32 {
|
||||
return (this.byteLength - this.byteOffset) >> alignof<T>();
|
||||
return this.byteLength >>> alignof<T>();
|
||||
}
|
||||
|
||||
@operator("[]")
|
||||
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");
|
||||
return loadUnsafeWithOffset<T,T>(this.buffer, index, byteOffset);
|
||||
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
|
||||
return loadUnsafeWithOffset<T,T>(this.buffer, index, this.byteOffset);
|
||||
}
|
||||
|
||||
@inline @operator("{}")
|
||||
@ -50,10 +48,8 @@ export abstract class TypedArray<T,V> {
|
||||
|
||||
@operator("[]=")
|
||||
protected __set(index: i32, value: V): void {
|
||||
var byteOffset = this.byteOffset;
|
||||
var elementLength = (this.byteLength - byteOffset) >>> alignof<T>();
|
||||
if (<u32>index >= <u32>elementLength) throw new Error("Index out of bounds");
|
||||
storeUnsafeWithOffset<T,V>(this.buffer, index, value, byteOffset);
|
||||
if (<u32>index >= <u32>(this.byteLength >>> alignof<T>())) throw new Error("Index out of bounds");
|
||||
storeUnsafeWithOffset<T,V>(this.buffer, index, value, this.byteOffset);
|
||||
}
|
||||
|
||||
@inline @operator("{}=")
|
||||
@ -95,7 +91,7 @@ export abstract class TypedArray<T,V> {
|
||||
var slice = memory.allocate(offsetof<this>());
|
||||
store<usize>(slice, this.buffer, offsetof<this>("buffer"));
|
||||
store<i32>(slice, begin << alignof<T>(), offsetof<this>("byteOffset"));
|
||||
store<i32>(slice, end << alignof<T>(), offsetof<this>("byteLength"));
|
||||
store<i32>(slice, (end - begin) << alignof<T>(), offsetof<this>("byteLength"));
|
||||
return changetype<this>(slice);
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ export class Uint8Array extends TypedArray<u8,u32> {
|
||||
export class Uint8ClampedArray extends TypedArray<u8,u32> {
|
||||
static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
|
||||
|
||||
@operator("[]=")
|
||||
@inline @operator("[]=")
|
||||
protected __set(index: i32, value: i32): void {
|
||||
super.__set(index, max(min(value, 255), 0));
|
||||
}
|
||||
|
Reference in New Issue
Block a user