Polyfill move_memory and set_memory and remove Heap

This commit is contained in:
dcodeIO
2018-01-14 02:30:20 +01:00
parent 2c009c67d3
commit ad469ca445
33 changed files with 8529 additions and 5057 deletions

View File

@ -8,7 +8,7 @@ export class Array<T> {
if (capacity < 0)
throw new RangeError("invalid array length");
this.__capacity = this.length = capacity;
this.__memory = capacity > 0 ? Heap.allocate(<usize>capacity * sizeof<T>()) : 0;
this.__memory = capacity > 0 ? allocate_memory(<usize>capacity * sizeof<T>()) : 0;
}
@operator("[]")
@ -34,19 +34,20 @@ export class Array<T> {
return -1;
}
private __grow(capacity: i32): void {
assert(capacity > this.__capacity);
var newMemory = Heap.allocate(<usize>(capacity * sizeof<T>()));
if (this.__memory)
Heap.copy(newMemory, this.__memory, this.__capacity * sizeof<T>());
Heap.dispose(this.__memory);
private __grow(newCapacity: i32): void {
assert(newCapacity > this.__capacity);
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
move_memory(newMemory, this.__memory, this.__capacity * sizeof<T>());
free_memory(this.__memory);
}
this.__memory = newMemory;
this.__capacity = capacity;
this.__capacity = newCapacity;
}
push(element: T): i32 {
if (<u32>this.length >= this.__capacity)
this.__grow(max(this.length + 1, this.__capacity * 2));
this.__grow(max(this.length + 1, this.__capacity << 1));
store<T>(this.__memory + <usize>this.length * sizeof<T>(), element);
return ++this.length;
}
@ -62,23 +63,22 @@ export class Array<T> {
if (this.length < 1 || <u32>this.length > this.__capacity)
throw new RangeError("index out of range");
var element = load<T>(this.__memory);
Heap.copy(this.__memory, this.__memory + sizeof<T>(), (this.__capacity - 1) * sizeof<T>());
Heap.fill(this.__memory + (this.__capacity - 1) * sizeof<T>(), 0, sizeof<T>());
move_memory(this.__memory, this.__memory + sizeof<T>(), (this.__capacity - 1) * sizeof<T>());
set_memory(this.__memory + (this.__capacity - 1) * sizeof<T>(), 0, sizeof<T>());
--this.length;
return element;
}
unshift(element: T): i32 {
var capacity = this.__capacity;
if (<u32>this.length >= capacity)
this.__grow(max(this.length + 1, capacity * 2));
var oldCapacity = this.__capacity;
if (<u32>this.length >= oldCapacity)
this.__grow(max(this.length + 1, oldCapacity * 2));
// FIXME: needs memmove (Heap.copy is just memcpy). it's also inefficient because
// __grow copies and then unshift copies again.
// Heap.copy(this.__memory + sizeof<T>(), this.__memory, capacity * sizeof<T>());
// FIXME: this is inefficient because of two copies, one in __grow and one here
// move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
if (capacity)
for (var index: usize = capacity; index > 0; --index)
if (oldCapacity)
for (var index: usize = oldCapacity; index > 0; --index)
store<T>(this.__memory + index * sizeof<T>(), load<T>(this.__memory + (index - 1) * sizeof<T>()));
store<T>(this.__memory, element);
@ -92,12 +92,16 @@ export class CArray<T> {
private constructor() {}
@operator("[]")
private __get(index: usize): T {
return load<T>(changetype<usize>(this) + index * sizeof<T>());
private __get(index: i32): T {
if (index < 0)
throw new RangeError("index out of range");
return load<T>(changetype<usize>(this) + <usize>index * sizeof<T>());
}
@operator("[]=")
private __set(index: usize, value: T): void {
store<T>(changetype<usize>(this) + index * sizeof<T>(), value);
private __set(index: i32, value: T): void {
if (index < 0)
throw new RangeError("index out of range");
store<T>(changetype<usize>(this) + <usize>index * sizeof<T>(), value);
}
}