This commit is contained in:
dcode 2019-03-21 17:34:51 +01:00
parent d4d5814fc2
commit d9463c5484
11 changed files with 4254 additions and 3544 deletions

View File

@ -1,4 +1,4 @@
import { ALLOCATE, REALLOCATE, DISCARD, RETAIN, RELEASE, REGISTER, MAX_BYTELENGTH, ArrayBufferView } from "./runtime";
import { ALLOCATE, REALLOCATE, DISCARD, RETAIN, RELEASE, REGISTER, MAX_BYTELENGTH, ArrayBufferView, MOVE } from "./runtime";
import { ArrayBuffer } from "./arraybuffer";
import { COMPARATOR, SORT } from "./util/sort";
import { itoa, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./util/number";
@ -76,8 +76,10 @@ export class Array<T> extends ArrayBufferView {
if (isManaged<T>()) {
let offset = this.dataStart + (<usize>index << alignof<T>());
let oldValue = load<T>(offset);
store<T>(offset, RETAIN<T,this>(value, this));
RELEASE<T,this>(oldValue, this); // order is important
if (value !== oldValue) {
RELEASE<T,this>(oldValue, this);
store<T>(offset, RETAIN<T,this>(value, this));
}
} else {
store<T>(this.dataStart + (<usize>index << alignof<T>()), value);
}
@ -155,8 +157,7 @@ export class Array<T> extends ArrayBufferView {
if (isManaged<T>()) {
let thisStart = this.dataStart;
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
let element = load<T>(thisStart + offset);
store<T>(outStart + offset, RETAIN<T,Array<T>>(element, out));
store<T>(outStart + offset, RETAIN<T,Array<T>>(load<T>(thisStart + offset), out));
}
let otherStart = other.dataStart;
let otherSize = <usize>otherLen << alignof<T>();
@ -332,13 +333,11 @@ export class Array<T> extends ArrayBufferView {
var thisStart = this.dataStart;
var thisBase = thisStart + (<usize>start << alignof<T>());
for (let i = 0; i < deleteCount; ++i) {
let deleted = load<T>(thisBase + (<usize>i << alignof<T>()));
if (isManaged<T>()) {
store<T>(resultStart + (<usize>i << alignof<T>()), RETAIN<T,Array<T>>(deleted, result));
RELEASE<T,this>(deleted, this); // order is important
} else {
store<T>(resultStart + (<usize>i << alignof<T>()), deleted);
}
store<T>(resultStart + (<usize>i << alignof<T>()),
isManaged<T>()
? MOVE<T,this,Array<T>>(load<T>(thisBase + (<usize>i << alignof<T>())), this, result)
: load<T>(thisBase + (<usize>i << alignof<T>()))
);
}
memory.copy(
result.dataStart,

View File

@ -0,0 +1,35 @@
// A dummy GC for looking at generated GC code without actually implementing it.
// @ts-ignore: decorator
@inline
const TRACE = false;
// @ts-ignore: decorator
@global @unsafe
function __gc_register(ref: usize): void {
if (TRACE) trace("gc.register", 1, ref);
}
// @ts-ignore: decorator
@global @unsafe
function __gc_retain(ref: usize, parentRef: usize): void {
if (TRACE) trace("gc.retain", 2, ref, parentRef);
}
// @ts-ignore: decorator
@global @unsafe
function __gc_release(ref: usize, parentRef: usize): void {
if (TRACE) trace("gc.release", 2, ref, parentRef);
}
// @ts-ignore: decorator
@global @unsafe
function __gc_move(ref: usize, oldParentRef: usize, newParentRef: usize): void {
if (TRACE) trace("gc.move", 3, ref, oldParentRef, newParentRef);
}
// @ts-ignore: decorator
@global @unsafe
function __gc_collect(): void {
if (TRACE) trace("gc.collect");
}

View File

@ -214,7 +214,7 @@ function objToRef(obj: ManagedObject): usize {
// @ts-ignore: decorator
@global @unsafe
export function __gc_register(ref: usize): void {
if (TRACE) trace("gc.register", 2, ref);
if (TRACE) trace("gc.register", 1, ref);
step(); // also makes sure it's initialized
var obj = refToObj(ref);
obj.color = white;

View File

@ -38,8 +38,10 @@ export class FixedArray<T> {
if (isManaged<T>()) {
let offset = changetype<usize>(this) + (<usize>index << alignof<T>());
let oldValue = load<T>(offset);
store<T>(offset, RETAIN<T,this>(value, this));
RELEASE<T,this>(oldValue, this); // order is important
if (value !== oldValue) {
RELEASE<T,this>(oldValue, this);
store<T>(offset, RETAIN<T,this>(value, this));
}
} else {
store<T>(changetype<usize>(this) + (<usize>index << alignof<T>()), value);
}

View File

@ -106,8 +106,10 @@ export class Map<K,V> {
if (entry) {
if (isManaged<V>()) {
let oldValue = entry.value;
entry.value = RETAIN<V,this>(value, this);
RELEASE<V,this>(oldValue, this); // order is important
if (value !== oldValue) {
RELEASE<V,this>(oldValue, this);
entry.value = RETAIN<V,this>(value, this);
}
} else {
entry.value = value;
}

View File

@ -1,5 +1,5 @@
// The runtime provides a set of macros for dealing with common AssemblyScript internals, like
// allocation, memory management in general, integration with a (potenial) garbage collector
// allocation, memory management in general, integration with a (potential) garbage collector
// and interfaces to hard-wired data types like buffers and their views. Doing so ensures that
// no matter which underlying implementation of a memory allocator or garbage collector is used,
// as long as all runtime/managed objects adhere to the runtime conventions, it'll all play well
@ -19,7 +19,7 @@ import { HEAP_BASE, memory } from "./memory";
// Changes the size of a previously allocated, but not yet registered, runtime object, for
// example when a pre-allocated buffer turned out to be too small or too large. This works by
// aligning dynamic allocations to actual block size internally so in the best case REALLOCATE
// only changes a size while in the worst case moves the object to larger block.
// only updates payload size while in the worst case moves the object to larger a block.
//
// DISCARD(ref)
// ------------
@ -45,6 +45,14 @@ import { HEAP_BASE, memory } from "./memory";
// ignore this by design, while a reference counting collector decrements the reference count
// and potentially frees the runtime object.
//
// MOVE<T,TOldParent,TNewParent>(ref, oldParentRef, newParentRef)
// --------------------------------------------------------------
// Moves a reference to ref hold by oldParentRef to be now hold by newParentRef. This is a
// special case of first RELEASE'ing a reference on one and instantly RETAIN'ing the reference
// on another parent. A tracing garbage collector will most likely link the runtime object as if
// RETAIN'ed on the new parent only, while a reference counting collector can skip increment and
// decrement, as decrementing might otherwise involve a costly check for cyclic garbage.
//
// ALLOCATE_UNMANAGED(size)
// ------------------------
// Allocates an unmanaged struct-like object. This is used by the compiler as an abstraction
@ -209,7 +217,11 @@ function doRegister(ref: usize, classId: u32): usize {
export function RETAIN<T,TParent>(ref: T, parentRef: TParent): T {
if (!isManaged<T>()) ERROR("managed reference expected");
if (!isManaged<TParent>()) ERROR("managed reference expected");
doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
if (isNullable<T>()) {
if (ref !== null) doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
} else {
doRetain(changetype<usize>(ref), changetype<usize>(parentRef));
}
return ref;
}
@ -228,7 +240,13 @@ function doRetain(ref: usize, parentRef: usize): void {
export function RELEASE<T,TParent>(ref: T, parentRef: TParent): void {
if (!isManaged<T>()) ERROR("managed reference expected");
if (!isManaged<TParent>()) ERROR("managed reference expected");
doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
// FIXME: new Array<Ref>(10) has non-nullable elements but still contains `null`s.
// In the future, something like this should probably initialize with `new Ref()`s.
// if (isNullable<T>()) {
if (ref !== null) doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
// } else {
// doRelease(changetype<usize>(ref), changetype<usize>(parentRef));
// }
}
function doRelease(ref: usize, parentRef: usize): void {
@ -240,6 +258,41 @@ function doRelease(ref: usize, parentRef: usize): void {
if (GC_IMPLEMENTED) __gc_release(changetype<usize>(ref), changetype<usize>(parentRef));
}
/** Moves a registered object from one parent to another. */
// @ts-ignore: decorator
@unsafe @inline
export function MOVE<T,TOldParent,TNewParent>(ref: T, oldParentRef: TOldParent, newParentRef: TNewParent): T {
if (!isManaged<T>()) ERROR("managed reference expected");
if (!isManaged<TOldParent>()) ERROR("managed reference expected");
if (!isManaged<TNewParent>()) ERROR("managed reference expected");
if (isNullable<T>()) {
if (ref !== null) doMove(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
} else {
doMove(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
}
return ref;
}
function doMove(ref: usize, oldParentRef: usize, newParentRef: usize): void {
if (!ASC_NO_ASSERT) {
assertRegistered(ref);
assertRegistered(oldParentRef);
assertRegistered(newParentRef);
}
if (GC_IMPLEMENTED) {
// @ts-ignore: stub
if (isDefined(__gc_move)) {
// @ts-ignore: stub
__gc_move(changetype<usize>(ref), changetype<usize>(oldParentRef), changetype<usize>(newParentRef));
} else {
// @ts-ignore: stub
__gc_retain(changetype<usize>(ref), changetype<usize>(newParentRef));
// @ts-ignore: stub
__gc_release(changetype<usize>(ref), changetype<usize>(oldParentRef));
}
}
}
/** Discards an unregistered object that turned out to be unnecessary. */
// @ts-ignore: decorator
@unsafe @inline
@ -283,7 +336,7 @@ function assertUnregistered(ref: usize): void {
/** Asserts that a managed object has already been registered. */
// @ts-ignore: decorator
function assertRegistered(ref: usize): void {
// may be a static string or buffer (not a heap object)
assert(ref !== null); // may be a static string or buffer (not a heap object)
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
}

View File

@ -357,16 +357,16 @@ import { compareImpl, parse, CharCode, isWhiteSpaceOrLineTerminator } from "./ut
let result = new Array<String>(length);
let resultStart = changetype<ArrayBufferView>(result).dataStart;
for (let i: isize = 0; i < length; ++i) {
let charStr = ALLOCATE(2);
let charStr = REGISTER<String>(ALLOCATE(2));
store<u16>(
charStr,
changetype<usize>(charStr),
load<u16>(changetype<usize>(this) + (<usize>i << 1))
);
// result[i] = charStr
store<String>(resultStart + (<usize>i << alignof<usize>()),
isManaged<String>()
? RETAIN<String,Array<String>>(REGISTER<String>(charStr), result)
: REGISTER<String>(charStr)
? RETAIN<String,Array<String>>(charStr, result)
: charStr
);
}
return result;

View File

@ -231,9 +231,8 @@ tests.forEach(filename => {
let memory = new WebAssembly.Memory({ initial: 10 });
let exports = {};
const RUNTIME_HEADER_SIZE = 8; // 16 if GC is present
function getString(ptr) {
const RUNTIME_HEADER_SIZE = exports[".capabilities"] & 2 ? 16 : 8;
if (!ptr) return "null";
var U32 = new Uint32Array(exports.memory ? exports.memory.buffer : memory.buffer);
var U16 = new Uint16Array(exports.memory ? exports.memory.buffer : memory.buffer);

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,10 @@
import "allocator/arena";
import "collector/dummy";
import { Array } from "array";
import { COMPARATOR } from "util/sort";
@start export function main(): void {}
// Obtains the internal capacity of an array from its backing buffer.
function internalCapacity<T>(array: Array<T>): i32 {
// the memory region used by the backing buffer might still be larger in that the ArrayBuffer

File diff suppressed because it is too large Load Diff