Daniel Wirtz 39b489bee2
Rename memory instructions; Rework constant handling (#177)
* Rename memory instructions as proposed by the bulk-memory-operations spec.
* Rename memory manager functions to memory.* as well
* Remove automatic inlining of constant globals (Binaryen does this now)
* Improve 'const' enum compatibility
* Improve module-level export generation
* Enable the inline decorator for constant variables
* Add ERROR, WARNING and INFO macros that emit a user-defined diagnostic
* Reintroduce builtin decorator so these can appear anywhere in stdlib again
* Inline isNaN and isFinite by default
* Make an interface around gc.* similar to memory.*
* Emit an error when trying to inline a mutable variable
* Slim down CI stages
* Add a more convenient tracing utility for debugging
* Implement some prequesites for an eventual bundled GC
2018-07-20 22:53:33 +02:00

38 lines
1.2 KiB
TypeScript

import "allocator/arena";
import "collector/itcm";
// a class to test with
class MyObject {
a: u32;
}
function MyObject_visit(ref: usize): void {} // function table index == classId ?
// allocate a managed instance
var obj: MyObject | null = changetype<MyObject>(gc.allocate(offsetof<MyObject>(), MyObject_visit));
obj.a = 123;
// check header
{
let head = changetype<usize>(obj) - 16;
let next = load<u32>(head, 0) & ~3;
let prev = load<u32>(head, 4);
assert(next != 0 && prev != 0 && next == prev);
let visitFn = load<u32>(head, 8);
assert(visitFn == changetype<u32>(MyObject_visit));
let unused = load<u32>(head, 12);
assert(unused == 0);
let a = load<u32>(head, 16);
assert(a == 123);
}
gc.collect(); // should keep 'obj' because it's a referenced root (see trace output)
obj = null;
gc.collect(); // should free 'obj' because it isn't referenced anymore (see trace output)
var obj2: MyObject; // should also iterate globals defined late
export function main(): i32 { return 0; }
// BEWARE: The compiler does not emit any integrations except gc.iterateRoots yet, hence trying to
// use the GC with a 'normally' allocated object will break it, as it has no managed header!