stdlib unlink wiring

This commit is contained in:
dcode 2019-03-20 17:12:33 +01:00
parent 9b664dc175
commit 952ac8627d
6 changed files with 65 additions and 59 deletions

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REALLOCATE, DISCARD, LINK, REGISTER, MAX_BYTELENGTH, ArrayBufferView } from "./runtime";
import { ALLOCATE, REALLOCATE, DISCARD, LINK, REGISTER, MAX_BYTELENGTH, ArrayBufferView, UNLINK } from "./runtime";
import { ArrayBuffer } from "./arraybuffer";
import { COMPARATOR, SORT } from "./util/sort";
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
@ -73,11 +73,14 @@ export class Array<T> extends ArrayBufferView {
@operator("[]=") // unchecked is built-in
private __set(index: i32, value: T): void {
ensureCapacity(this, index + 1, alignof<T>());
store<T>(this.dataStart + (<usize>index << alignof<T>()),
isManaged<T>()
? LINK<T,this>(value, this)
: value
);
if (isManaged<T>()) {
let offset = this.dataStart + (<usize>index << alignof<T>());
let oldValue = load<T>(offset);
store<T>(offset, LINK<T,this>(value, this));
UNLINK<T,this>(oldValue, this); // order is important
} else {
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
}
if (index >= this.length_) this.length_ = index + 1;
}
@ -324,20 +327,21 @@ export class Array<T> extends ArrayBufferView {
var length = this.length_;
start = start < 0 ? max<i32>(length + start, 0) : min<i32>(start, length);
deleteCount = max<i32>(min<i32>(deleteCount, length - start), 0);
var splice = new Array<T>(deleteCount);
var spliceStart = splice.dataStart;
var result = new Array<T>(deleteCount);
var resultStart = result.dataStart;
var thisStart = this.dataStart;
var thisBase = thisStart + (<usize>start << alignof<T>());
for (let i = 0; i < deleteCount; ++i) {
let element = load<T>(thisBase + (<usize>i << alignof<T>()));
store<T>(spliceStart + (<usize>i << alignof<T>()),
isManaged<T>()
? LINK<T,Array<T>>(element, splice)
: element
);
let deleted = load<T>(thisBase + (<usize>i << alignof<T>()));
if (isManaged<T>()) {
store<T>(resultStart + (<usize>i << alignof<T>()), LINK<T,Array<T>>(deleted, result));
UNLINK<T,this>(deleted, this); // order is important
} else {
store<T>(resultStart + (<usize>i << alignof<T>()), deleted);
}
}
memory.copy(
splice.dataStart,
result.dataStart,
thisBase,
<usize>deleteCount << alignof<T>()
);
@ -350,7 +354,7 @@ export class Array<T> extends ArrayBufferView {
);
}
this.length_ = length - deleteCount;
return splice;
return result;
}
reverse(): Array<T> {

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REGISTER, MAX_BYTELENGTH, HEADER, HEADER_SIZE, LINK } from "./runtime";
import { ALLOCATE, REGISTER, MAX_BYTELENGTH, HEADER, HEADER_SIZE, LINK, UNLINK } from "./runtime";
// NOTE: DO NOT USE YET!
@ -22,16 +22,12 @@ export class FixedArray<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>()));
return this.__unchecked_get(index);
}
@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
);
return this.__unchecked_set(index, value);
}
@operator("{}") private __unchecked_get(index: i32): T {
@ -39,10 +35,13 @@ export class FixedArray<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
);
if (isManaged<T>()) {
let offset = changetype<usize>(this) + (<usize>index << alignof<T>());
let oldValue = load<T>(offset);
store<T>(offset, LINK<T,this>(value, this));
UNLINK<T,this>(oldValue, this); // order is important
} else {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
}
}
}

View File

@ -1,4 +1,4 @@
import { LINK } from "./runtime";
import { LINK, UNLINK } from "./runtime";
import { HASH } from "./util/hash";
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@ -102,9 +102,15 @@ export class Map<K,V> {
set(key: K, value: V): void {
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode);
var entry = this.find(key, hashCode); // unmanaged!
if (entry) {
entry.value = value;
if (isManaged<V>()) {
let oldValue = entry.value;
entry.value = LINK<V,this>(value, this);
UNLINK<V,this>(oldValue, this); // order is important
} else {
entry.value = value;
}
} else {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
@ -119,13 +125,9 @@ export class Map<K,V> {
entry = changetype<MapEntry<K,V>>(
changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K,V>()
);
// link with the map (entry is unmanaged)
entry.key = isManaged<K>()
? LINK<K,this>(key, this)
: key;
entry.value = isManaged<V>()
? LINK<V,this>(value, this)
: value;
// link with the map
entry.key = isManaged<K>() ? LINK<K,this>(key, this) : key;
entry.value = isManaged<V>() ? LINK<V,this>(value, this) : value;
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
@ -137,6 +139,8 @@ export class Map<K,V> {
delete(key: K): bool {
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
if (isManaged<K>()) UNLINK<K,this>(entry.key, this);
if (isManaged<V>()) UNLINK<V,this>(entry.value, this);
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate

View File

@ -1,4 +1,4 @@
import { LINK } from "./runtime";
import { LINK, UNLINK } from "./runtime";
import { HASH } from "./util/hash";
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
@ -93,7 +93,7 @@ export class Set<K> {
add(key: K): void {
var hashCode = HASH<K>(key);
var entry = this.find(key, hashCode);
var entry = this.find(key, hashCode); // unmanaged!
if (!entry) {
// check if rehashing is necessary
if (this.entriesOffset == this.entriesCapacity) {
@ -108,10 +108,8 @@ export class Set<K> {
entry = changetype<SetEntry<K>>(
changetype<usize>(entries) + this.entriesOffset++ * ENTRY_SIZE<K>()
);
// link with the set itself (entry is unmanaged)
entry.key = isManaged<K>()
? LINK<K,this>(key, this)
: key;
// link with the set
entry.key = isManaged<K>() ? LINK<K,this>(key, this) : key;
++this.entriesCount;
// link with previous entry in bucket
let bucketPtrBase = changetype<usize>(this.buckets) + <usize>(hashCode & this.bucketsMask) * BUCKET_SIZE;
@ -123,6 +121,7 @@ export class Set<K> {
delete(key: K): bool {
var entry = this.find(key, HASH<K>(key));
if (!entry) return false;
if (isManaged<K>()) UNLINK<K,this>(entry.key, this);
entry.taggedNext |= EMPTY;
--this.entriesCount;
// check if rehashing is appropriate

View File

@ -2298,7 +2298,7 @@
if
i32.const 0
i32.const 208
i32.const 200
i32.const 203
i32.const 20
call $~lib/env/abort
unreachable
@ -2565,7 +2565,7 @@
if
i32.const 0
i32.const 208
i32.const 261
i32.const 264
i32.const 20
call $~lib/env/abort
unreachable
@ -4096,7 +4096,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -4592,7 +4592,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -5111,7 +5111,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -5436,7 +5436,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -2967,7 +2967,7 @@
if
i32.const 0
i32.const 208
i32.const 200
i32.const 203
i32.const 20
call $~lib/env/abort
unreachable
@ -3326,7 +3326,7 @@
if
i32.const 0
i32.const 208
i32.const 261
i32.const 264
i32.const 20
call $~lib/env/abort
unreachable
@ -5302,7 +5302,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -5912,7 +5912,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -6547,7 +6547,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -7051,7 +7051,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -7626,7 +7626,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -7990,7 +7990,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable
@ -8247,7 +8247,7 @@
if
i32.const 0
i32.const 208
i32.const 374
i32.const 378
i32.const 4
call $~lib/env/abort
unreachable