FixedArray experimentation

This commit is contained in:
dcode
2019-03-19 15:43:05 +01:00
parent 74789c9c1e
commit 81039c4167
31 changed files with 762 additions and 855 deletions

View File

@@ -20,6 +20,7 @@ function ensureCapacity(array: ArrayBufferView, minCapacity: i32, alignLog2: u32
}
export class Array<T> extends ArrayBufferView {
[key: number]: T;
// Implementing ArrayBufferView isn't strictly necessary here but is done to allow glue code
// to work with typed and normal arrays interchangeably. Technically, normal arrays do not need
@@ -65,7 +66,7 @@ export class Array<T> extends ArrayBufferView {
@operator("[]") // unchecked is built-in
private __get(index: i32): T {
if (<u32>index >= <u32>this.dataLength >>> alignof<T>()) throw new Error("Offset out of bounds");
if (<u32>index >= <u32>this.dataLength >>> alignof<T>()) throw new RangeError("Offset out of bounds");
return load<T>(this.dataStart + (<usize>index << alignof<T>()));
}

View File

@@ -0,0 +1,48 @@
import { ALLOCATE, REGISTER, MAX_BYTELENGTH, HEADER, HEADER_SIZE, LINK } from "./runtime";
// NOTE: DO NOT USE YET!
// TODO: FixedArray<T,S> with S being the static size, i.e. `new FixedArray<i32,10>`.
// Then hard-wire this special type to the compiler and do static length checks instead :)
export class FixedArray<T> {
[key: number]: T;
constructor(length: i32) {
if (<u32>length > <u32>MAX_BYTELENGTH >>> alignof<T>()) throw new RangeError("Invalid length");
var outSize = <usize>length << alignof<T>();
var out = ALLOCATE(outSize);
memory.fill(out, 0, outSize);
return REGISTER<FixedArray<T>>(out);
}
get length(): i32 {
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize >>> alignof<T>();
}
@operator("[]") private __get(index: i32): T {
if (<u32>index >= <u32>this.length) throw new RangeError("Offset out of bounds");
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
}
@operator("[]=") private __set(index: i32, value: T): void {
if (<u32>index >= <u32>this.length) throw new RangeError("Offset out of bounds");
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()),
isManaged<T>()
? LINK<T,this>(value, this)
: value
);
}
@operator("{}") private __unchecked_get(index: i32): T {
return load<T>(changetype<usize>(this) + (<usize>index << alignof<T>()));
}
@operator("{}=") private __unchecked_set(index: i32, value: T): void {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()),
isManaged<T>()
? LINK<T,this>(value, this)
: value
);
}
}

View File

@@ -1205,6 +1205,13 @@ declare class Array<T> {
toString(): string;
}
/** Class representing a fixed sequence of values of type `T`. */
declare class FixedArray<T> {
[key: number]: T;
readonly length: i32;
constructor(capacity?: i32);
}
/** Class representing a sequence of characters. */
declare class String {

View File

@@ -216,7 +216,6 @@ export const MAX_BYTELENGTH: i32 = MAX_SIZE_32 - HEADER_SIZE;
/** Hard wired ArrayBufferView interface. */
export abstract class ArrayBufferView {
[key: number]: number;
// @ts-ignore: decorator
@unsafe

View File

@@ -2,6 +2,7 @@ import { ALLOCATE, REGISTER, ArrayBufferView } from "./runtime";
import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
export class Int8Array extends ArrayBufferView {
[key: number]: i8;
// @ts-ignore: decorator
@lazy
@@ -83,6 +84,7 @@ export class Int8Array extends ArrayBufferView {
}
export class Uint8Array extends ArrayBufferView {
[key: number]: u8;
// @ts-ignore: decorator
@lazy
@@ -164,6 +166,7 @@ export class Uint8Array extends ArrayBufferView {
}
export class Uint8ClampedArray extends ArrayBufferView {
[key: number]: u8;
// @ts-ignore: decorator
@lazy
@@ -245,6 +248,7 @@ export class Uint8ClampedArray extends ArrayBufferView {
}
export class Int16Array extends ArrayBufferView {
[key: number]: i16;
// @ts-ignore: decorator
@lazy
@@ -326,6 +330,7 @@ export class Int16Array extends ArrayBufferView {
}
export class Uint16Array extends ArrayBufferView {
[key: number]: u16;
// @ts-ignore: decorator
@lazy
@@ -407,6 +412,7 @@ export class Uint16Array extends ArrayBufferView {
}
export class Int32Array extends ArrayBufferView {
[key: number]: i32;
// @ts-ignore: decorator
@lazy
@@ -488,6 +494,7 @@ export class Int32Array extends ArrayBufferView {
}
export class Uint32Array extends ArrayBufferView {
[key: number]: u32;
// @ts-ignore: decorator
@lazy
@@ -569,6 +576,7 @@ export class Uint32Array extends ArrayBufferView {
}
export class Int64Array extends ArrayBufferView {
[key: number]: i64;
// @ts-ignore: decorator
@lazy
@@ -650,6 +658,7 @@ export class Int64Array extends ArrayBufferView {
}
export class Uint64Array extends ArrayBufferView {
[key: number]: u64;
// @ts-ignore: decorator
@lazy
@@ -731,6 +740,7 @@ export class Uint64Array extends ArrayBufferView {
}
export class Float32Array extends ArrayBufferView {
[key: number]: f32;
// @ts-ignore: decorator
@lazy
@@ -812,6 +822,7 @@ export class Float32Array extends ArrayBufferView {
}
export class Float64Array extends ArrayBufferView {
[key: number]: f64;
// @ts-ignore: decorator
@lazy