Initial GC integration (#196)

This commit is contained in:
Daniel Wirtz
2018-08-02 18:23:02 +02:00
committed by GitHub
parent 671121bf70
commit dc0f271fc2
139 changed files with 7370 additions and 5016 deletions

View File

@ -1,8 +1,8 @@
import {
MAX_BLENGTH,
HEADER_SIZE as HEADER_SIZE_AB,
allocUnsafe,
reallocUnsafe,
HEADER_SIZE,
allocateUnsafe,
reallocateUnsafe,
loadUnsafe,
storeUnsafe
} from "./internal/arraybuffer";
@ -22,11 +22,11 @@ export class Array<T> {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
var byteLength = length << alignof<T>();
var buffer = allocUnsafe(byteLength);
var buffer = allocateUnsafe(byteLength);
this.buffer_ = buffer;
this.length_ = length;
memory.fill(
changetype<usize>(buffer) + HEADER_SIZE_AB,
changetype<usize>(buffer) + HEADER_SIZE,
0,
<usize>byteLength
);
@ -42,7 +42,7 @@ export class Array<T> {
if (<u32>length > <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
buffer = reallocUnsafe(buffer, length << alignof<T>());
buffer = reallocateUnsafe(buffer, length << alignof<T>());
this.buffer_ = buffer;
}
this.length_ = length;
@ -84,16 +84,18 @@ export class Array<T> {
if (<u32>index >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>index >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocUnsafe(buffer, (index + 1) << alignof<T>());
buffer = reallocateUnsafe(buffer, (index + 1) << alignof<T>());
this.buffer_ = buffer;
this.length_ = index + 1;
}
storeUnsafe<T,T>(buffer, index, value);
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(value)); // tslint:disable-line
}
@operator("{}=")
private __unchecked_set(index: i32, value: T): void {
storeUnsafe<T,T>(this.buffer_, index, value);
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(value)); // tslint:disable-line
}
includes(searchElement: T, fromIndex: i32 = 0): bool {
@ -141,11 +143,12 @@ export class Array<T> {
if (<u32>length >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocUnsafe(buffer, newLength << alignof<T>());
buffer = reallocateUnsafe(buffer, newLength << alignof<T>());
this.buffer_ = buffer;
}
this.length_ = newLength;
storeUnsafe<T,T>(buffer, length, element);
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(element)); // tslint:disable-line
return newLength;
}
@ -217,8 +220,8 @@ export class Array<T> {
var element = loadUnsafe<T,T>(buffer, 0);
var lastIndex = length - 1;
memory.copy(
changetype<usize>(buffer) + HEADER_SIZE_AB,
changetype<usize>(buffer) + HEADER_SIZE_AB + sizeof<T>(),
changetype<usize>(buffer) + HEADER_SIZE,
changetype<usize>(buffer) + HEADER_SIZE + sizeof<T>(),
<usize>lastIndex << alignof<T>()
);
storeUnsafe<T,T>(buffer, lastIndex, <T>null);
@ -242,17 +245,18 @@ export class Array<T> {
if (<u32>length >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocUnsafe(buffer, newLength << alignof<T>());
buffer = reallocateUnsafe(buffer, newLength << alignof<T>());
capacity = buffer.byteLength >>> alignof<T>();
this.buffer_ = buffer;
}
memory.copy(
changetype<usize>(buffer) + HEADER_SIZE_AB + sizeof<T>(),
changetype<usize>(buffer) + HEADER_SIZE_AB,
changetype<usize>(buffer) + HEADER_SIZE + sizeof<T>(),
changetype<usize>(buffer) + HEADER_SIZE,
<usize>(capacity - 1) << alignof<T>()
);
storeUnsafe<T,T>(buffer, 0, element);
this.length_ = newLength;
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(element)); // tslint:disable-line
return newLength;
}
@ -268,8 +272,8 @@ export class Array<T> {
var sliced = new Array<T>(newLength);
if (newLength) {
memory.copy(
changetype<usize>(sliced.buffer_) + HEADER_SIZE_AB,
changetype<usize>(this.buffer_) + HEADER_SIZE_AB + (<usize>begin << alignof<T>()),
changetype<usize>(sliced.buffer_) + HEADER_SIZE,
changetype<usize>(this.buffer_) + HEADER_SIZE + (<usize>begin << alignof<T>()),
<usize>newLength << alignof<T>()
);
}
@ -284,8 +288,8 @@ export class Array<T> {
deleteCount = min(deleteCount, length - start);
var buffer = this.buffer_;
memory.copy(
changetype<usize>(buffer) + HEADER_SIZE_AB + (<usize>start << alignof<T>()),
changetype<usize>(buffer) + HEADER_SIZE_AB + (<usize>(start + deleteCount) << alignof<T>()),
changetype<usize>(buffer) + HEADER_SIZE + (<usize>start << alignof<T>()),
changetype<usize>(buffer) + HEADER_SIZE + (<usize>(start + deleteCount) << alignof<T>()),
<usize>deleteCount << alignof<T>()
);
this.length_ = length - deleteCount;
@ -328,4 +332,16 @@ export class Array<T> {
);
}
}
private __gc(): void {
if (isManaged<T>()) {
let buffer = this.buffer_;
let offset: usize = 0;
let end = <usize>this.length_ << alignof<usize>();
while (offset < end) {
__gc_mark(load<usize>(changetype<usize>(buffer) + offset, HEADER_SIZE)); // tslint:disable-line
offset += sizeof<usize>();
}
}
}
}