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

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++);
}
}