63 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-03-14 12:46:36 +01:00
import { runtime } from "./runtime";
2019-03-13 22:35:47 +01:00
/** Garbage collector interface. */
export namespace gc {
2019-03-14 04:33:58 +01:00
/** Whether the garbage collector interface is implemented. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
@lazy
export const implemented: bool = isDefined(
// @ts-ignore: stub
__gc_register
);
2019-03-14 04:33:58 +01:00
2019-03-13 22:35:47 +01:00
/** Gets the computed unique class id of a class type. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
@unsafe @builtin
export declare function classId<T>(): u32;
2019-03-13 22:35:47 +01:00
/** Iterates reference root objects. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
@unsafe @builtin
export declare function iterateRoots(fn: (ref: usize) => void): void;
2019-03-13 22:35:47 +01:00
/** Registers a managed object to be tracked by the garbage collector. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
2019-03-14 12:46:36 +01:00
@unsafe @inline
export function register<T>(ref: usize): T {
runtime.unrefUnregistered(ref).classId = classId<T>();
2019-03-14 06:09:49 +01:00
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__gc_register)) __gc_register(ref);
2019-03-14 12:46:36 +01:00
return changetype<T>(ref);
2019-03-13 22:35:47 +01:00
}
/** Links a registered object with the registered object now referencing it. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
2019-03-14 12:46:36 +01:00
@unsafe @inline
export function link<T, TParent>(ref: T, parentRef: TParent): void {
assert(changetype<usize>(ref) >= HEAP_BASE + runtime.Header.SIZE); // must be a heap object
var header = changetype<runtime.Header>(changetype<usize>(ref) - runtime.Header.SIZE);
assert(header.classId != runtime.Header.MAGIC && header.gc1 != 0 && header.gc2 != 0); // must be registered
2019-03-14 06:09:49 +01:00
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__gc_link)) __gc_link(ref, parentRef);
2019-03-13 22:35:47 +01:00
}
/** Marks an object as being reachable. */
2019-03-14 06:09:49 +01:00
// @ts-ignore: decorator
@unsafe
export function mark(ref: usize): void {
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__gc_mark)) __gc_mark(ref);
else ERROR("missing implementation: gc.mark");
2019-03-13 22:35:47 +01:00
}
/** Performs a full garbage collection cycle. */
2019-03-14 04:33:58 +01:00
export function collect(): void {
2019-03-14 06:09:49 +01:00
// @ts-ignore: stub
2019-03-14 04:33:58 +01:00
if (isDefined(__gc_collect)) __gc_collect();
else WARNING("missing implementation: gc.collect");
2019-03-13 22:35:47 +01:00
}
}
// TODO: move marking into userspace using builtins like iterateFields?