assemblyscript/std/assembly/rt/index-stub.ts

86 lines
2.0 KiB
TypeScript
Raw Normal View History

2019-05-12 13:50:28 +02:00
import { AL_MASK, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "rt/common";
2019-04-18 12:53:48 +02:00
// @ts-ignore: decorator
@lazy
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
// @ts-ignore: decorator
@lazy
var offset: usize = startOffset;
// @ts-ignore: decorator
@unsafe @global
2019-05-12 13:50:28 +02:00
export function __alloc(size: usize, id: u32): usize {
2019-04-18 12:53:48 +02:00
if (size > BLOCK_MAXSIZE) unreachable();
var ptr = offset + BLOCK_OVERHEAD;
var newPtr = (ptr + max<usize>(size, 1) + AL_MASK) & ~AL_MASK;
var pagesBefore = memory.size();
if (newPtr > <usize>pagesBefore << 16) {
let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
}
}
offset = newPtr;
2019-05-12 13:50:28 +02:00
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
2019-04-18 12:53:48 +02:00
block.rtId = id;
block.rtSize = size;
return ptr;
}
// @ts-ignore: decorator
@unsafe @global
2019-05-12 13:50:28 +02:00
export function __realloc(ref: usize, size: usize): usize {
var block = changetype<BLOCK>(ref - BLOCK_OVERHEAD);
2019-04-18 13:08:49 +02:00
var oldSize = <usize>block.rtSize;
if (size > oldSize) {
2019-05-12 13:50:28 +02:00
let newRef = __alloc(size, block.rtId);
2019-04-18 13:08:49 +02:00
memory.copy(newRef, ref, oldSize);
ref = newRef;
} else {
block.rtSize = size;
}
return ref;
2019-04-18 12:53:48 +02:00
}
// @ts-ignore: decorator
@unsafe @global
2019-05-12 13:50:28 +02:00
export function __free(ref: usize): void {
2019-04-18 12:53:48 +02:00
}
// @ts-ignore: decorator
2019-05-12 13:50:28 +02:00
// @unsafe @global
// export function __reset(): void { // special
// offset = startOffset;
// }
// @ts-ignore: decorator
@global @unsafe
export function __retain(ref: usize): usize {
return ref;
2019-04-18 12:53:48 +02:00
}
2019-05-12 13:50:28 +02:00
// @ts-ignore: decorator
@global @unsafe
export function __release(ref: usize): void {
}
2019-04-18 12:53:48 +02:00
// @ts-ignore: decorator
@global @unsafe
2019-05-12 13:50:28 +02:00
function __visit(ref: usize, cookie: u32): void {
2019-04-18 12:53:48 +02:00
}
// @ts-ignore: decorator
@global @unsafe
2019-05-12 13:50:28 +02:00
function __retainRelease(ref: usize, oldRef: usize): usize {
return ref;
2019-04-18 12:53:48 +02:00
}
// @ts-ignore: decorator
@global @unsafe
2019-05-12 13:50:28 +02:00
export function __collect(): void {
2019-04-18 12:53:48 +02:00
}
2019-05-12 13:50:28 +02:00
export { __instanceof, __typeinfo } from "rt/common";