Add initial std Symbol; Fix some type inference issues

This commit is contained in:
dcodeIO
2018-06-21 19:42:18 +02:00
parent 1626e50b0f
commit c74eed2bd8
15 changed files with 4257 additions and 47 deletions

14
std/assembly.d.ts vendored
View File

@ -451,6 +451,14 @@ interface Number {}
interface Object {}
interface RegExp {}
declare class Map<K,V> {
readonly size: i32;
has(key: K): bool;
set(key: K, value: V): void;
delete(key: K): bool;
clear(): void;
}
declare class Set<T> {
readonly size: i32;
has(value: T): bool;
@ -459,6 +467,12 @@ declare class Set<T> {
clear(): void;
}
declare class Symbol {
constructor(description?: string | null);
static for(key: string): Symbol;
static keyFor(sym: Symbol): string | null;
}
interface IMath<T> {
/** The base of natural logarithms, e, approximately 2.718. */
readonly E: T;

27
std/assembly/symbol.ts Normal file
View File

@ -0,0 +1,27 @@
import { Map } from "./map";
var nextId: usize = 1;
var stringToId: Map<string, usize>;
var idToString: Map<usize, string>;
export class Symbol {
static for(key: string): Symbol {
if (!stringToId) { stringToId = new Map(); idToString = new Map(); }
else if (stringToId.has(key)) return changetype<Symbol>(stringToId.get(key));
var id = nextId++;
stringToId.set(key, id);
idToString.set(id, key);
return changetype<Symbol>(id);
}
static keyFor(sym: Symbol): string | null {
return idToString !== null && idToString.has(changetype<usize>(sym))
? idToString.get(changetype<usize>(sym))
: null;
}
constructor(description: string | null = null) {
return changetype<Symbol>(nextId++);
}
}