Add preliminary support for map keys/values, set values

Makes arrays instead of iterators for now
This commit is contained in:
dcode 2019-05-28 15:49:56 +02:00
parent fe70f1d863
commit d9fbf8a2dd
3 changed files with 49 additions and 4 deletions

View File

@ -1293,15 +1293,18 @@ declare class Map<K,V> {
get(key: K): V;
delete(key: K): bool;
clear(): void;
keys(): K[]; // preliminary
values(): V[]; // preliminary
toString(): string;
}
declare class Set<T> {
declare class Set<K> {
readonly size: i32;
has(value: T): bool;
add(value: T): void;
delete(value: T): bool;
has(value: K): bool;
add(value: K): void;
delete(value: K): bool;
clear(): void;
values(): K[]; // preliminary
toString(): string;
}

View File

@ -189,6 +189,34 @@ export class Map<K,V> {
this.entriesOffset = this.entriesCount;
}
keys(): K[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
var keys = Array.create<K>(size);
for (let i = 0; i < size; ++i) {
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
if (!(entry.taggedNext & EMPTY)) {
keys.push(entry.key);
}
}
return keys;
}
values(): V[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
var values = Array.create<V>(size);
for (let i = 0; i < size; ++i) {
let entry = changetype<MapEntry<K,V>>(start + <usize>i * ENTRY_SIZE<K,V>());
if (!(entry.taggedNext & EMPTY)) {
values.push(entry.value);
}
}
return values;
}
toString(): string {
return "[object Map]";
}

View File

@ -163,6 +163,20 @@ export class Set<K> {
this.entriesOffset = this.entriesCount;
}
values(): K[] {
// FIXME: this is preliminary, needs iterators/closures
var start = changetype<usize>(this.entries);
var size = this.entriesOffset;
var values = Array.create<K>(size);
for (let i = 0; i < size; ++i) {
let entry = changetype<SetEntry<K>>(start + <usize>i * ENTRY_SIZE<K>());
if (!(entry.taggedNext & EMPTY)) {
values.push(entry.key);
}
}
return values;
}
toString(): string {
return "[object Set]";
}