assemblyscript/std/assembly/fixedarray.ts

81 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-05-12 13:50:28 +02:00
/// <reference path="./rt/index.d.ts" />
import { BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "./rt/common";
import { idof } from "./builtins";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
2019-03-19 15:43:05 +01:00
// 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) {
2019-05-12 13:50:28 +02:00
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
if (isReference<T>()) {
if (!isNullable<T>()) {
if (length) throw new Error(E_HOLEYARRAY);
}
}
2019-03-19 15:43:05 +01:00
var outSize = <usize>length << alignof<T>();
2019-05-12 13:50:28 +02:00
var out = __alloc(outSize, idof<FixedArray<T>>());
2019-03-19 15:43:05 +01:00
memory.fill(out, 0, outSize);
2019-05-12 13:50:28 +02:00
return changetype<FixedArray<T>>(out); // retains
2019-03-19 15:43:05 +01:00
}
get length(): i32 {
2019-05-12 13:50:28 +02:00
return changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtSize >>> alignof<T>();
2019-03-19 15:43:05 +01:00
}
@operator("[]") private __get(index: i32): T {
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
2019-03-20 17:12:33 +01:00
return this.__unchecked_get(index);
2019-03-19 15:43:05 +01:00
}
@operator("[]=") private __set(index: i32, value: T): void {
if (<u32>index >= <u32>this.length) throw new RangeError(E_INDEXOUTOFRANGE);
2019-03-20 17:12:33 +01:00
return this.__unchecked_set(index, value);
2019-03-19 15:43:05 +01:00
}
@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 {
2019-03-20 17:12:33 +01:00
if (isManaged<T>()) {
let offset = changetype<usize>(this) + (<usize>index << alignof<T>());
2019-05-12 13:50:28 +02:00
let oldValue = load<usize>(offset);
if (changetype<usize>(value) != oldValue) {
store<usize>(offset, __retain(changetype<usize>(value)));
__release(changetype<usize>(oldValue));
2019-03-21 17:34:51 +01:00
}
2019-03-20 17:12:33 +01:00
} else {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
}
2019-03-19 15:43:05 +01:00
}
2019-03-21 10:44:14 +01:00
// GC integration
2019-05-12 13:50:28 +02:00
@unsafe private __traverse(cookie: u32): void {
2019-03-21 10:44:14 +01:00
if (isManaged<T>()) {
let cur = changetype<usize>(this);
2019-05-12 13:50:28 +02:00
let end = cur + changetype<BLOCK>(changetype<usize>(this) - BLOCK_OVERHEAD).rtSize;
2019-03-21 10:44:14 +01:00
while (cur < end) {
2019-03-30 13:58:20 +01:00
let val = load<usize>(cur);
if (isNullable<T>()) {
if (val) {
2019-05-12 13:50:28 +02:00
__visit(val, cookie);
__visit_members(val, cookie);
2019-03-30 13:58:20 +01:00
}
} else {
2019-05-12 13:50:28 +02:00
__visit(val, cookie);
__visit_members(val, cookie);
2019-03-30 13:58:20 +01:00
}
2019-03-21 10:44:14 +01:00
cur += sizeof<usize>();
}
}
}
2019-03-19 15:43:05 +01:00
}