Implement reference counting (#592)

This commit is contained in:
Daniel Wirtz
2019-06-05 23:15:39 +02:00
committed by GitHub
parent 3ed76a97f0
commit 0484a6b740
601 changed files with 261645 additions and 146131 deletions

View File

@@ -1,10 +1,87 @@
import { Map } from "./map";
@lazy var stringToId: Map<string, usize>;
@lazy var idToString: Map<usize, string>;
@lazy var nextId: usize = 12; // Symbol.unscopables + 1
// @ts-ignore: decorator
@lazy
var stringToId: Map<string, usize>;
// @ts-ignore: decorator
@lazy
var idToString: Map<usize, string>;
// @ts-ignore: decorator
@lazy
var nextId: usize = 12; // Symbol.unscopables + 1
@unmanaged @sealed abstract class _Symbol {
// TODO: all of the following default symbols are unused currently yet add to
// binary size if #toString becomes compiled. Ultimately we'll most likely want
// to remove the unsupported ones and only keep what's actually supported.
// @ts-ignore: decorator
@lazy
static readonly hasInstance: symbol = changetype<symbol>(1);
// @ts-ignore: decorator
@lazy
static readonly isConcatSpreadable: symbol = changetype<symbol>(2);
// @ts-ignore: decorator
@lazy
static readonly isRegExp: symbol = changetype<symbol>(3);
// @ts-ignore: decorator
@lazy
static readonly iterator: symbol = changetype<symbol>(3);
// @ts-ignore: decorator
@lazy
static readonly match: symbol = changetype<symbol>(4);
// @ts-ignore: decorator
@lazy
static readonly replace: symbol = changetype<symbol>(5);
// @ts-ignore: decorator
@lazy
static readonly search: symbol = changetype<symbol>(6);
// @ts-ignore: decorator
@lazy
static readonly species: symbol = changetype<symbol>(7);
// @ts-ignore: decorator
@lazy
static readonly split: symbol = changetype<symbol>(8);
// @ts-ignore: decorator
@lazy
static readonly toPrimitive: symbol = changetype<symbol>(9);
// @ts-ignore: decorator
@lazy
static readonly toStringTag: symbol = changetype<symbol>(10);
// @ts-ignore: decorator
@lazy
static readonly unscopables: symbol = changetype<symbol>(11);
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++;
if (!id) unreachable(); // out of ids
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;
}
@unmanaged export class symbol {
toString(): string {
var id = changetype<usize>(this);
var str = "";
@@ -35,37 +112,7 @@ export function Symbol(description: string | null = null): symbol {
return changetype<symbol>(id);
}
export namespace Symbol {
export type Symbol = _Symbol;
// well-known symbols
@lazy export const hasInstance = changetype<symbol>(1);
@lazy export const isConcatSpreadable = changetype<symbol>(2);
@lazy export const isRegExp = changetype<symbol>(3);
@lazy export const iterator = changetype<symbol>(3);
@lazy export const match = changetype<symbol>(4);
@lazy export const replace = changetype<symbol>(5);
@lazy export const search = changetype<symbol>(6);
@lazy export const species = changetype<symbol>(7);
@lazy export const split = changetype<symbol>(8);
@lazy export const toPrimitive = changetype<symbol>(9);
@lazy export const toStringTag = changetype<symbol>(10);
@lazy export const unscopables = changetype<symbol>(11);
/* tslint:disable */// not valid TS
export function 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++;
if (!id) unreachable(); // out of ids
stringToId.set(key, id);
idToString.set(id, key);
return changetype<symbol>(id);
}
/* tslint:enable */
export function keyFor(sym: symbol): string | null {
return idToString !== null && idToString.has(changetype<usize>(sym))
? idToString.get(changetype<usize>(sym))
: null;
}
}
// @ts-ignore: nolib
export type symbol = _Symbol;