mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 06:21:29 +00:00
Add offsetof<T> builtin; Put a function's trampoline into the function table if a it has optional parameters; TypedArray scaffolding
This commit is contained in:
@ -50,7 +50,9 @@ export declare function load<T>(offset: usize, constantOffset?: usize): T;
|
||||
|
||||
export declare function store<T>(offset: usize, value: void, constantOffset?: usize): T;
|
||||
|
||||
export declare function sizeof<T>(): usize;
|
||||
export declare function sizeof<T>(): usize; // | u32 / u64
|
||||
|
||||
export declare function offsetof<T>(fieldName?: string): usize; // | u32 / u64
|
||||
|
||||
export declare function select<T>(ifTrue: T, ifFalse: T, condition: bool): T;
|
||||
|
||||
|
78
std/assembly/typedarray.ts
Normal file
78
std/assembly/typedarray.ts
Normal file
@ -0,0 +1,78 @@
|
||||
class TypedArray<T> {
|
||||
|
||||
readonly buffer: ArrayBuffer;
|
||||
readonly byteOffset: i32;
|
||||
readonly byteLength: i32;
|
||||
get length(): i32 { return this.byteLength / sizeof<T>(); }
|
||||
|
||||
constructor(length: i32) {
|
||||
var byteLength = length * sizeof<T>();
|
||||
this.buffer = new ArrayBuffer(byteLength);
|
||||
this.byteOffset = 0;
|
||||
this.byteLength = byteLength;
|
||||
}
|
||||
|
||||
@operator("[]")
|
||||
private __get(index: i32): T {
|
||||
var offset = this.byteOffset;
|
||||
assert(<u32>index < <u32>this.byteLength / sizeof<T>());
|
||||
return load<T>(changetype<usize>(this.buffer) + (offset + index) * sizeof<T>(), 4);
|
||||
}
|
||||
|
||||
@operator("[]=")
|
||||
private __set(index: i32, value: T): void {
|
||||
var offset = this.byteOffset;
|
||||
assert(<u32>index < <u32>(this.byteLength / sizeof<T>()));
|
||||
store<T>(changetype<usize>(this.buffer) + (offset + index) * sizeof<T>(), value, 4);
|
||||
}
|
||||
}
|
||||
|
||||
// export class Int8Array extends TypedArray<i8> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
|
||||
// static readonly name: string = "Int8Array";
|
||||
// }
|
||||
|
||||
// export class Uint8Array extends TypedArray<u8> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
|
||||
// static readonly name: string = "Uint8Array";
|
||||
// }
|
||||
|
||||
// export class Int16Array extends TypedArray<i16> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
|
||||
// static readonly name: string = "Int16Array";
|
||||
// }
|
||||
|
||||
// export class Uint16Array extends TypedArray<u16> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
|
||||
// static readonly name: string = "Uint16Array";
|
||||
// }
|
||||
|
||||
// export class Int32Array extends TypedArray<i32> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
|
||||
// static readonly name: string = "Int32Array";
|
||||
// }
|
||||
|
||||
// export class Uint32Array extends TypedArray<u32> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
|
||||
// static readonly name: string = "Uint32Array";
|
||||
// }
|
||||
|
||||
// export class Int64Array extends TypedArray<i64> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
|
||||
// static readonly name: string = "Int64Array";
|
||||
// }
|
||||
|
||||
// export class Uint64Array extends TypedArray<u64> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
|
||||
// static readonly name: string = "Uint64Array";
|
||||
// }
|
||||
|
||||
// export class Float32Array extends TypedArray<f32> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
|
||||
// static readonly name: string = "Float32Array";
|
||||
// }
|
||||
|
||||
// export class Float64Array extends TypedArray<f64> {
|
||||
// static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
|
||||
// static readonly name: string = "Float64Array";
|
||||
// }
|
Reference in New Issue
Block a user