mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 14:31:28 +00:00
Add initial std Symbol; Fix some type inference issues
This commit is contained in:
14
std/assembly.d.ts
vendored
14
std/assembly.d.ts
vendored
@ -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
27
std/assembly/symbol.ts
Normal 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++);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user