This commit is contained in:
dcode
2019-03-13 22:35:47 +01:00
parent 6f70826e45
commit e38f627c8b
10 changed files with 326 additions and 276 deletions

View File

@ -19,7 +19,7 @@ import { AL_MASK, MAX_SIZE_32 } from "./util/allocator";
// runtime will most likely change significantly once reftypes and WASM GC are a thing.
/** Whether a GC is present or not. */
@inline export const GC = isImplemented(gc.register) && isImplemented(gc.link);
@inline export const GC = isImplemented(gc.register);
/** Size of the common runtime header. */
@inline export const HEADER_SIZE: usize = GC
@ -132,20 +132,9 @@ function unref(ref: usize): HEADER {
if (GC) gc.link(changetype<usize>(ref), changetype<usize>(parentRef)); // tslint:disable-line
}
export abstract class ArrayBufferBase {
export abstract class ArrayBufferView {
@lazy static readonly MAX_BYTELENGTH: i32 = MAX_SIZE_32 - HEADER_SIZE;
constructor(length: i32) {
if (<u32>length > <u32>ArrayBufferBase.MAX_BYTELENGTH) throw new RangeError("Invalid array buffer length");
return REGISTER<ArrayBuffer>(ALLOC(<usize>length));
}
get byteLength(): i32 {
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize;
}
}
export abstract class ArrayBufferView {
[key: number]: number;
@unsafe data: ArrayBuffer;
@ -153,9 +142,8 @@ export abstract class ArrayBufferView {
@unsafe dataEnd: usize;
constructor(length: i32, alignLog2: i32) {
if (<u32>length > <u32>ArrayBufferBase.MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length");
var byteLength = length << alignLog2;
var buffer = new ArrayBuffer(byteLength);
if (<u32>length > <u32>ArrayBufferView.MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length");
var buffer = new ArrayBuffer(length << alignLog2);
this.data = buffer;
this.dataStart = changetype<usize>(buffer);
this.dataEnd = changetype<usize>(buffer) + <usize>length;
@ -170,83 +158,7 @@ export abstract class ArrayBufferView {
}
get length(): i32 {
ERROR("not implemented");
ERROR("concrete implementation must provide this");
return unreachable();
}
}
export abstract class StringBase {
@lazy static readonly MAX_LENGTH: i32 = (MAX_SIZE_32 - HEADER_SIZE) >> 1;
get length(): i32 {
return changetype<HEADER>(changetype<usize>(this) - HEADER_SIZE).payloadSize >> 1;
}
}
import { memcmp, memmove, memset } from "./util/memory";
export namespace memory {
@builtin export declare function size(): i32;
@builtin @unsafe export declare function grow(pages: i32): i32;
@builtin @unsafe @inline export function fill(dst: usize, c: u8, n: usize): void {
memset(dst, c, n); // fallback if "bulk-memory" isn't enabled
}
@builtin @unsafe @inline export function copy(dst: usize, src: usize, n: usize): void {
memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled
}
@unsafe export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void {
ERROR("not implemented");
}
@unsafe export function drop(segmentIndex: u32): void {
ERROR("not implemented");
}
@stub @inline export function allocate(size: usize): usize {
ERROR("stub: missing memory manager");
return <usize>unreachable();
}
@stub @unsafe @inline export function free(ptr: usize): void {
ERROR("stub: missing memory manager");
}
@stub @unsafe @inline export function reset(): void {
ERROR("stub: not supported by memory manager");
}
@inline export function compare(vl: usize, vr: usize, n: usize): i32 {
return memcmp(vl, vr, n);
}
@unsafe export function repeat(dst: usize, src: usize, srcLength: usize, count: usize): void {
var index: usize = 0;
var total = srcLength * count;
while (index < total) {
memory.copy(dst + index, src, srcLength);
index += srcLength;
}
}
}
export namespace gc {
@builtin @unsafe export declare function classId<T>(): u32;
@builtin @unsafe export declare function iterateRoots(fn: (ref: usize) => void): void;
@stub @unsafe export function register(ref: usize): void {
ERROR("stub: missing garbage collector");
}
@stub @unsafe export function link(ref: usize, parentRef: usize): void {
ERROR("stub: missing garbage collector");
}
@stub export function collect(): void {
WARNING("stub: missing garbage collector");
}
}