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. */
|
|
|
|
@lazy export const implemented: bool = isDefined(__gc_register);
|
|
|
|
|
2019-03-13 22:35:47 +01:00
|
|
|
/** Gets the computed unique class id of a class type. */
|
2019-03-14 04:33:58 +01:00
|
|
|
@unsafe @builtin export declare function classId<T>(): u32;
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Iterates reference root objects. */
|
2019-03-14 04:33:58 +01:00
|
|
|
@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 04:33:58 +01:00
|
|
|
@unsafe export function register(ref: usize): void {
|
|
|
|
if (isDefined(__gc_register)) __gc_register(ref);
|
|
|
|
else ERROR("missing implementation: gc.register");
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Links a registered object with the registered object now referencing it. */
|
2019-03-14 04:33:58 +01:00
|
|
|
@unsafe export function link(ref: usize, parentRef: usize): void {
|
|
|
|
if (isDefined(__gc_link)) __gc_link(ref, parentRef);
|
|
|
|
else ERROR("missing implementation: gc.link");
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Marks an object as being reachable. */
|
2019-03-14 04:33:58 +01:00
|
|
|
@unsafe export function mark(ref: usize): void {
|
|
|
|
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 {
|
|
|
|
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?
|