162 lines
6.7 KiB
TypeScript
Raw Normal View History

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-03 21:47:38 +02:00
import { HEADER, HEADER_SIZE, HEADER_MAGIC, adjust } from "./util/runtime";
2019-03-14 04:33:58 +01:00
import { HEAP_BASE, memory } from "./memory";
2019-04-01 22:23:11 +02:00
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
@unsafe @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
@unsafe @builtin
export declare function __runtime_instanceof(id: u32, superId: u32): bool;
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
/** Runtime implementation. */
export namespace runtime {
2019-04-03 21:47:38 +02:00
/** Allocates the memory necessary to represent a managed object of the specified size. */
2019-04-01 22:23:11 +02:00
// @ts-ignore: decorator
@unsafe
export function allocate(payloadSize: usize): usize {
var header = changetype<HEADER>(memory.allocate(adjust(payloadSize)));
header.classId = HEADER_MAGIC;
header.payloadSize = payloadSize;
if (isDefined(__ref_collect)) {
header.reserved1 = 0;
header.reserved2 = 0;
}
return changetype<usize>(header) + HEADER_SIZE;
}
2019-03-16 07:26:33 +01:00
2019-04-03 21:47:38 +02:00
/** Reallocates the memory of a managed object that turned out to be too small or too large. */
2019-04-01 22:23:11 +02:00
// @ts-ignore: decorator
@unsafe
export function reallocate(ref: usize, newPayloadSize: usize): usize {
// Background: When managed objects are allocated these aren't immediately registered with GC
// but can be used as scratch objects while unregistered. This is useful in situations where
// the object must be reallocated multiple times because its final size isn't known beforehand,
// e.g. in Array#filter, with only the final object making it into GC'ed userland.
var header = changetype<HEADER>(ref - HEADER_SIZE);
var payloadSize = header.payloadSize;
if (payloadSize < newPayloadSize) {
let newAdjustedSize = adjust(newPayloadSize);
if (select(adjust(payloadSize), 0, ref > HEAP_BASE) < newAdjustedSize) {
// move if the allocation isn't large enough or not a heap object
let newHeader = changetype<HEADER>(memory.allocate(newAdjustedSize));
newHeader.classId = header.classId;
if (isDefined(__ref_collect)) {
newHeader.reserved1 = 0;
newHeader.reserved2 = 0;
}
let newRef = changetype<usize>(newHeader) + HEADER_SIZE;
memory.copy(newRef, ref, payloadSize);
memory.fill(newRef + payloadSize, 0, newPayloadSize - payloadSize);
if (header.classId == HEADER_MAGIC) {
// free right away if not registered yet
assert(ref > HEAP_BASE); // static objects aren't scratch objects
memory.free(changetype<usize>(header));
} else if (isDefined(__ref_collect)) {
// if previously registered, register again
// @ts-ignore: stub
__ref_register(ref);
}
header = newHeader;
ref = newRef;
} else {
// otherwise just clear additional memory within this block
memory.fill(ref + payloadSize, 0, newPayloadSize - payloadSize);
2019-03-09 02:37:05 +01:00
}
2019-03-08 22:04:20 +01:00
} else {
2019-04-01 22:23:11 +02:00
// if the size is the same or less, just update the header accordingly.
// unused space is cleared when grown, so no need to do this here.
2019-03-08 22:04:20 +01:00
}
2019-04-01 22:23:11 +02:00
header.payloadSize = newPayloadSize;
return ref;
2019-03-26 23:35:08 +01:00
}
2019-03-15 09:26:31 +01:00
2019-04-03 21:47:38 +02:00
/** Discards the memory of a managed object that hasn't been registered yet. */
2019-03-14 05:11:03 +01:00
// @ts-ignore: decorator
2019-03-14 06:09:49 +01:00
@unsafe
2019-04-01 22:23:11 +02:00
export function discard(ref: usize): void {
if (!ASC_NO_ASSERT) {
assert(ref > HEAP_BASE); // must be a heap object
let header = changetype<HEADER>(ref - HEADER_SIZE);
assert(header.classId == HEADER_MAGIC);
memory.free(changetype<usize>(header));
} else {
memory.free(changetype<usize>(ref - HEADER_SIZE));
}
}
2019-03-14 06:09:49 +01:00
2019-04-03 21:47:38 +02:00
/** Registers a managed object of the kind represented by the specified runtime id. */
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 register(ref: usize, id: u32): usize {
2019-04-01 22:23:11 +02:00
if (!ASC_NO_ASSERT) {
assert(ref > HEAP_BASE); // must be a heap object
let header = changetype<HEADER>(ref - HEADER_SIZE);
assert(header.classId == HEADER_MAGIC);
2019-04-03 21:47:38 +02:00
header.classId = id;
2019-04-01 22:23:11 +02:00
} else {
2019-04-03 21:47:38 +02:00
changetype<HEADER>(ref - HEADER_SIZE).classId = id;
2019-04-01 22:23:11 +02:00
}
if (isDefined(__ref_register)) __ref_register(ref);
return ref;
}
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 {
return runtime.register(runtime.allocate(<usize>length << 1), __runtime_id<String>());
}
/** 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 {
return runtime.register(runtime.allocate(<usize>byteLength), __runtime_id<ArrayBuffer>());
}
/** 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-02 10:12:57 +02:00
var array = runtime.register(runtime.allocate(offsetof<i32[]>()), id);
2019-04-03 21:47:38 +02:00
var bufferSize = <usize>length << alignLog2;
var buffer = runtime.register(runtime.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-01 22:23:11 +02:00
return array;
2019-03-13 03:09:24 +01:00
}
2019-03-08 22:04:20 +01:00
}