2019-03-27 17:21:52 +01:00
|
|
|
// The runtime provides common functionality that links runtime interfaces for memory management
|
2019-04-01 22:23:11 +02:00
|
|
|
// and garbage collection to the standard library, making sure it all plays well together.
|
2019-03-21 12:29:49 +01:00
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
import { HEADER, HEADER_SIZE, allocate, register } from "./util/runtime";
|
2019-04-04 02:25:22 +02:00
|
|
|
import { E_NOTIMPLEMENTED } from "./util/error";
|
2019-04-05 01:59:01 +02:00
|
|
|
import { memory } from "./memory";
|
|
|
|
import { ArrayBufferView } from "./arraybuffer";
|
2019-03-21 12:29:49 +01:00
|
|
|
|
2019-04-02 10:12:57 +02:00
|
|
|
/** Gets the computed unique id of a class type. */
|
2019-03-15 09:26:31 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-04-05 01:59:01 +02:00
|
|
|
@builtin
|
2019-04-02 10:12:57 +02:00
|
|
|
export declare function __runtime_id<T>(): u32;
|
2019-03-15 09:26:31 +01:00
|
|
|
|
2019-04-02 21:30:47 +02:00
|
|
|
/** Tests if a managed class is the same as or a superclass of another. */
|
|
|
|
// @ts-ignore: decorator
|
2019-04-05 01:59:01 +02:00
|
|
|
@builtin
|
2019-04-02 21:30:47 +02:00
|
|
|
export declare function __runtime_instanceof(id: u32, superId: u32): bool;
|
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
/** Runtime flags. */
|
|
|
|
const enum RuntimeFlags { // keep in sync with src/program.ts
|
|
|
|
NONE = 0,
|
|
|
|
/** Type is an `Array`. */
|
|
|
|
ARRAY = 1 << 0,
|
|
|
|
/** Type is a `Set`. */
|
|
|
|
SET = 1 << 1,
|
|
|
|
/** Type is a `Map`. */
|
|
|
|
MAP = 1 << 2,
|
|
|
|
/** Value alignment of 1 byte. */
|
|
|
|
VALUE_ALIGN_0 = 1 << 3,
|
|
|
|
/** Value alignment of 2 bytes. */
|
|
|
|
VALUE_ALIGN_1 = 1 << 4,
|
|
|
|
/** Value alignment of 4 bytes. */
|
|
|
|
VALUE_ALIGN_2 = 1 << 5,
|
|
|
|
/** Value alignment of 8 bytes. */
|
|
|
|
VALUE_ALIGN_3 = 1 << 6,
|
|
|
|
/** Value alignment of 16 bytes. */
|
|
|
|
VALUE_ALIGN_4 = 1 << 7,
|
|
|
|
/** Value type is nullable. */
|
|
|
|
VALUE_NULLABLE = 1 << 8,
|
|
|
|
/** Value type is managed. */
|
|
|
|
VALUE_MANAGED = 1 << 9,
|
|
|
|
/** Key alignment of 1 byte. */
|
|
|
|
KEY_ALIGN_0 = 1 << 10,
|
|
|
|
/** Key alignment of 2 bytes. */
|
|
|
|
KEY_ALIGN_1 = 1 << 11,
|
|
|
|
/** Key alignment of 4 bytes. */
|
|
|
|
KEY_ALIGN_2 = 1 << 12,
|
|
|
|
/** Key alignment of 8 bytes. */
|
|
|
|
KEY_ALIGN_3 = 1 << 13,
|
|
|
|
/** Key alignment of 16 bytes. */
|
|
|
|
KEY_ALIGN_4 = 1 << 14,
|
|
|
|
/** Key type is nullable. */
|
|
|
|
KEY_NULLABLE = 1 << 15,
|
|
|
|
/** Key type is managed. */
|
|
|
|
KEY_MANAGED = 1 << 16
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Gets the runtime flags of the managed type represented by the specified runtime id. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@builtin
|
|
|
|
export declare function __runtime_flags(id: u32): RuntimeFlags;
|
|
|
|
|
2019-04-04 02:25:22 +02:00
|
|
|
/** Marks root objects when a tracing GC is present. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @builtin
|
|
|
|
export declare function __gc_mark_roots(): void;
|
|
|
|
|
|
|
|
/** Marks class members when a tracing GC is present. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe @builtin
|
|
|
|
export declare function __gc_mark_members(classId: u32, ref: usize): void;
|
|
|
|
|
2019-04-03 00:26:41 +02:00
|
|
|
/** Runtime implementation. */
|
|
|
|
@unmanaged
|
|
|
|
export class runtime {
|
|
|
|
private constructor() { return unreachable(); }
|
|
|
|
|
2019-04-03 21:47:38 +02:00
|
|
|
/** Determines whether a managed object is considered to be an instance of the class represented by the specified runtime id. */
|
2019-04-03 00:26:41 +02:00
|
|
|
@unsafe
|
|
|
|
static instanceof(ref: usize, id: u32): bool { // keyword
|
|
|
|
return ref
|
|
|
|
? __runtime_instanceof(
|
|
|
|
changetype<HEADER>(ref - HEADER_SIZE).classId,
|
|
|
|
id
|
|
|
|
)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
}
|
2019-04-01 22:23:11 +02:00
|
|
|
export namespace runtime {
|
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
export function flags(id: u32): RuntimeFlags {
|
|
|
|
return __runtime_flags(id);
|
2019-04-01 22:23:11 +02:00
|
|
|
}
|
2019-03-14 06:09:49 +01:00
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
/** Allocates and registers, but doesn't initialize the data of, a new managed object of the specified kind. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
2019-04-05 01:59:01 +02:00
|
|
|
export function newObject(payloadSize: u32, id: u32): usize {
|
|
|
|
return register(allocate(<usize>payloadSize), id);
|
2019-04-01 22:23:11 +02:00
|
|
|
}
|
2019-03-14 06:09:49 +01:00
|
|
|
|
2019-04-03 21:47:38 +02:00
|
|
|
/** Allocates and registers, but doesn't initialize the data of, a new `String` of the specified length. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe
|
|
|
|
export function newString(length: i32): usize {
|
2019-04-05 01:59:01 +02:00
|
|
|
return newObject(length << 1, __runtime_id<String>());
|
2019-04-03 21:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocates and registers, but doesn't initialize the data of, a new `ArrayBuffer` of the specified byteLength. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe
|
|
|
|
export function newArrayBuffer(byteLength: i32): usize {
|
2019-04-05 01:59:01 +02:00
|
|
|
return newObject(byteLength, __runtime_id<ArrayBuffer>());
|
2019-04-03 21:47:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Allocates and registers, but doesn't initialize the data of, a new `Array` of the specified length and element alignment.*/
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
2019-04-03 21:47:38 +02:00
|
|
|
export function newArray(length: i32, alignLog2: usize, id: u32, data: usize = 0): usize {
|
|
|
|
// TODO: This API isn't great, but the compiler requires it for array literals anyway,
|
|
|
|
// that is when an array literal is encountered and its data is static, this function is
|
|
|
|
// called and the static buffer provided as `data`. This function can also be used to
|
|
|
|
// create typed arrays in that `Array` also implements `ArrayBufferView` but has an
|
|
|
|
// additional `.length_` property that remains unused overhead for typed arrays.
|
2019-04-04 02:25:22 +02:00
|
|
|
var array = register(allocate(offsetof<i32[]>()), id);
|
2019-04-03 21:47:38 +02:00
|
|
|
var bufferSize = <usize>length << alignLog2;
|
2019-04-04 02:25:22 +02:00
|
|
|
var buffer = register(allocate(bufferSize), __runtime_id<ArrayBuffer>());
|
2019-04-01 22:23:11 +02:00
|
|
|
changetype<ArrayBufferView>(array).data = changetype<ArrayBuffer>(buffer); // links
|
|
|
|
changetype<ArrayBufferView>(array).dataStart = buffer;
|
|
|
|
changetype<ArrayBufferView>(array).dataLength = bufferSize;
|
2019-04-03 21:47:38 +02:00
|
|
|
store<i32>(changetype<usize>(array), length, offsetof<i32[]>("length_"));
|
|
|
|
if (data) memory.copy(buffer, data, bufferSize);
|
2019-04-05 01:59:01 +02:00
|
|
|
// TODO: a way to determine whether the array has managed elements that must be linked?
|
2019-04-01 22:23:11 +02:00
|
|
|
return array;
|
2019-03-13 03:09:24 +01:00
|
|
|
}
|
2019-04-04 02:25:22 +02:00
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
// export function newArray2(id: u32, buffer: usize): usize {
|
|
|
|
// var flags = __runtime_flags(id); // traps if invalid
|
|
|
|
// var alignLog2 = (flags / RuntimeFlags.VALUE_ALIGN_0) & 31;
|
|
|
|
// var byteLength: i32;
|
|
|
|
// if (!buffer) {
|
|
|
|
// buffer = newArrayBuffer(byteLength = 0);
|
|
|
|
// } else {
|
|
|
|
// byteLength = changetype<ArrayBuffer>(buffer).byteLength;
|
|
|
|
// }
|
|
|
|
// var array = register(allocate(offsetof<i32[]>()), id);
|
|
|
|
// changetype<ArrayBufferView>(array).data = changetype<ArrayBuffer>(buffer); // links
|
|
|
|
// changetype<ArrayBufferView>(array).dataStart = buffer;
|
|
|
|
// changetype<ArrayBufferView>(array).dataLength = byteLength;
|
|
|
|
// store<i32>(changetype<usize>(array), byteLength >>> alignLog2, offsetof<i32[]>("length_"));
|
|
|
|
// if (flags & RuntimeFlags.VALUE_MANAGED) {
|
|
|
|
// let cur = buffer;
|
|
|
|
// let end = cur + <usize>byteLength;
|
|
|
|
// while (cur < end) {
|
|
|
|
// let ref = load<usize>(cur);
|
|
|
|
// if (ref) {
|
|
|
|
// if (isDefined(__ref_link)) __ref_link(ref, array);
|
|
|
|
// else if (isDefined(__ref_retain)) __ref_retain(ref);
|
|
|
|
// else assert(false);
|
|
|
|
// }
|
|
|
|
// cur += sizeof<usize>();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return array;
|
|
|
|
// }
|
|
|
|
|
2019-04-04 02:25:22 +02:00
|
|
|
/** Retains a managed object externally, making sure that it doesn't become collected. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe
|
|
|
|
export function retain(ref: usize): void {
|
|
|
|
if (isDefined(__ref_collect)) {
|
|
|
|
if (isDefined(__ref_link)) __ref_link(ref, changetype<usize>(ROOT));
|
|
|
|
else if (isDefined(__ref_retain)) __ref_retain(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Releases a managed object externally, allowing it to become collected. */
|
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe
|
|
|
|
export function release(ref: usize): void {
|
|
|
|
if (isDefined(__ref_collect)) {
|
|
|
|
if (isDefined(__ref_unlink)) __ref_unlink(ref, changetype<usize>(ROOT));
|
|
|
|
else if (isDefined(__ref_release)) __ref_release(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
/** Performs a full garbage collection cycle. */
|
2019-04-04 12:53:42 +02:00
|
|
|
// @ts-ignore: decorator
|
|
|
|
@unsafe
|
2019-04-04 02:25:22 +02:00
|
|
|
export function collect(): void {
|
2019-04-04 12:53:42 +02:00
|
|
|
// FIXME: annotated unsafe because calling it in the middle of a function collects inner
|
|
|
|
// references prematurely with a tracing GC, which is pretty bad actually.
|
|
|
|
|
|
|
|
// function explode(): Ref {
|
|
|
|
// var ref = new Ref();
|
|
|
|
// gc.collect(); // collects ref
|
|
|
|
// return ref;
|
|
|
|
// }
|
|
|
|
|
2019-04-04 02:25:22 +02:00
|
|
|
if (isDefined(__ref_collect)) __ref_collect();
|
|
|
|
else throw new Error(E_NOTIMPLEMENTED);
|
|
|
|
}
|
2019-03-08 22:04:20 +01:00
|
|
|
}
|
2019-04-04 02:25:22 +02:00
|
|
|
|
|
|
|
class Root {}
|
|
|
|
|
2019-04-05 01:59:01 +02:00
|
|
|
/** A root object to retain managed objects on externally. */
|
2019-04-04 02:25:22 +02:00
|
|
|
// @ts-ignore
|
|
|
|
@lazy
|
|
|
|
var ROOT = new Root();
|