mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-17 00:41:32 +00:00
test a few things
This commit is contained in:
@ -25,7 +25,7 @@ export class HEADER {
|
|||||||
/** Magic value used to validate common runtime headers. */
|
/** Magic value used to validate common runtime headers. */
|
||||||
@inline export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
@inline export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
||||||
|
|
||||||
/** Aligns an allocation to actual block size. */
|
/** Aligns an allocation to actual block size. Primarily targets TLSF. */
|
||||||
export function ALIGN(payloadSize: usize): usize {
|
export function ALIGN(payloadSize: usize): usize {
|
||||||
// round up to power of 2, e.g. with HEADER_SIZE=8:
|
// round up to power of 2, e.g. with HEADER_SIZE=8:
|
||||||
// 0 -> 2^3 = 8
|
// 0 -> 2^3 = 8
|
||||||
@ -36,14 +36,6 @@ export function ALIGN(payloadSize: usize): usize {
|
|||||||
return <usize>1 << <usize>(<u32>32 - clz<u32>(payloadSize + HEADER_SIZE - 1));
|
return <usize>1 << <usize>(<u32>32 - clz<u32>(payloadSize + HEADER_SIZE - 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets to the common runtime header of the specified reference. */
|
|
||||||
export function UNREF(ref: usize): HEADER {
|
|
||||||
assert(ref >= HEAP_BASE + HEADER_SIZE); // must be a heap object
|
|
||||||
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
|
||||||
assert(header.classId == HEADER_MAGIC); // must be unregistered
|
|
||||||
return header;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Allocates a new object and returns a pointer to its payload. */
|
/** Allocates a new object and returns a pointer to its payload. */
|
||||||
export function ALLOC(payloadSize: u32): usize {
|
export function ALLOC(payloadSize: u32): usize {
|
||||||
var header = changetype<HEADER>(memory.allocate(ALIGN(payloadSize)));
|
var header = changetype<HEADER>(memory.allocate(ALIGN(payloadSize)));
|
||||||
@ -60,7 +52,7 @@ export function ALLOC(payloadSize: u32): usize {
|
|||||||
|
|
||||||
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
||||||
export function REALLOC(ref: usize, newPayloadSize: u32): usize {
|
export function REALLOC(ref: usize, newPayloadSize: u32): usize {
|
||||||
var header = UNREF(ref);
|
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||||
var payloadSize = header.payloadSize;
|
var payloadSize = header.payloadSize;
|
||||||
if (payloadSize < newPayloadSize) {
|
if (payloadSize < newPayloadSize) {
|
||||||
let newAlignedSize = ALIGN(newPayloadSize);
|
let newAlignedSize = ALIGN(newPayloadSize);
|
||||||
@ -93,13 +85,17 @@ export function REALLOC(ref: usize, newPayloadSize: u32): usize {
|
|||||||
|
|
||||||
/** Frees an object. Must not have been registered with GC yet. */
|
/** Frees an object. Must not have been registered with GC yet. */
|
||||||
export function FREE(ref: usize): void {
|
export function FREE(ref: usize): void {
|
||||||
var header = UNREF(ref);
|
assert(ref >= HEAP_BASE + HEADER_SIZE); // must be a heap object
|
||||||
|
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||||
|
assert(header.classId == HEADER_MAGIC); // must be unregistered
|
||||||
memory.free(changetype<usize>(header));
|
memory.free(changetype<usize>(header));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Registers a managed object with GC. Cannot be changed anymore afterwards. */
|
/** Registers a managed object with GC. Cannot be changed anymore afterwards. */
|
||||||
export function REGISTER<T>(ref: usize, parentRef: usize): void {
|
export function REGISTER<T>(ref: usize, parentRef: usize): void {
|
||||||
var header = UNREF(ref);
|
assert(ref >= HEAP_BASE + HEADER_SIZE); // must be a heap object
|
||||||
|
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||||
|
assert(header.classId == HEADER_MAGIC); // must be unregistered
|
||||||
header.classId = __rt_classid<T>();
|
header.classId = __rt_classid<T>();
|
||||||
if (GC) __REGISTER_IMPL(ref, parentRef); // tslint:disable-line
|
if (GC) __REGISTER_IMPL(ref, parentRef); // tslint:disable-line
|
||||||
}
|
}
|
||||||
|
@ -17,5 +17,3 @@ var b_ref: B = changetype<B>(32); // global root, non-nullable
|
|||||||
var i: i32 = 0;
|
var i: i32 = 0;
|
||||||
__rt_iterateroots((ref: usize): void => { assert(<u32>ref == ++i << 3); });
|
__rt_iterateroots((ref: usize): void => { assert(<u32>ref == ++i << 3); });
|
||||||
assert(i == 4);
|
assert(i == 4);
|
||||||
|
|
||||||
assert(__rt_classid<A>() != __rt_classid<B>());
|
|
||||||
|
@ -58,18 +58,6 @@
|
|||||||
call $~lib/env/abort
|
call $~lib/env/abort
|
||||||
unreachable
|
unreachable
|
||||||
end
|
end
|
||||||
i32.const 43
|
|
||||||
i32.const 44
|
|
||||||
i32.ne
|
|
||||||
i32.eqz
|
|
||||||
if
|
|
||||||
i32.const 0
|
|
||||||
i32.const 8
|
|
||||||
i32.const 21
|
|
||||||
i32.const 0
|
|
||||||
call $~lib/env/abort
|
|
||||||
unreachable
|
|
||||||
end
|
|
||||||
)
|
)
|
||||||
(func $start (; 3 ;) (type $FUNCSIG$v)
|
(func $start (; 3 ;) (type $FUNCSIG$v)
|
||||||
call $start:std/gc-integration
|
call $start:std/gc-integration
|
||||||
|
3002
tests/compiler/std/runtime.optimized.wat
Normal file
3002
tests/compiler/std/runtime.optimized.wat
Normal file
File diff suppressed because it is too large
Load Diff
60
tests/compiler/std/runtime.ts
Normal file
60
tests/compiler/std/runtime.ts
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
import "allocator/tlsf";
|
||||||
|
import {
|
||||||
|
HEADER,
|
||||||
|
HEADER_SIZE,
|
||||||
|
HEADER_MAGIC,
|
||||||
|
ALIGN,
|
||||||
|
ALLOC,
|
||||||
|
REALLOC,
|
||||||
|
FREE,
|
||||||
|
REGISTER
|
||||||
|
} from "internal/runtime";
|
||||||
|
|
||||||
|
class A {}
|
||||||
|
class B {}
|
||||||
|
assert(__rt_classid<A>() != __rt_classid<B>());
|
||||||
|
|
||||||
|
function isPowerOf2(x: i32): bool {
|
||||||
|
return x != 0 && (x & (x - 1)) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(ALIGN(0) > 0);
|
||||||
|
for (let i = 0; i < 9000; ++i) {
|
||||||
|
assert(isPowerOf2(ALIGN(i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
var barrier1 = ALIGN(0);
|
||||||
|
var barrier2 = barrier1 + 1;
|
||||||
|
while (ALIGN(barrier2 + 1) == ALIGN(barrier2)) ++barrier2;
|
||||||
|
var barrier3 = barrier2 + 1;
|
||||||
|
while (ALIGN(barrier3 + 1) == ALIGN(barrier3)) ++barrier3;
|
||||||
|
|
||||||
|
trace("barrier1", 1, barrier1);
|
||||||
|
trace("barrier2", 1, barrier2);
|
||||||
|
trace("barrier3", 1, barrier3);
|
||||||
|
|
||||||
|
var ref1 = ALLOC(1);
|
||||||
|
var header1 = changetype<HEADER>(ref1 - HEADER_SIZE);
|
||||||
|
assert(header1.classId == HEADER_MAGIC);
|
||||||
|
assert(header1.payloadSize == 1);
|
||||||
|
assert(ref1 == REALLOC(ref1, barrier1)); // same segment
|
||||||
|
assert(header1.payloadSize == barrier1);
|
||||||
|
var ref2 = REALLOC(ref1, barrier2);
|
||||||
|
assert(ref1 != ref2); // moves
|
||||||
|
var header2 = changetype<HEADER>(ref2 - HEADER_SIZE);
|
||||||
|
assert(header2.payloadSize == barrier2);
|
||||||
|
FREE(ref2);
|
||||||
|
var ref3 = ALLOC(barrier2);
|
||||||
|
assert(ref1 == ref3); // reuses space of ref1 (free'd in realloc), ref2 (explicitly free'd)
|
||||||
|
|
||||||
|
var ref4 = ALLOC(barrier1);
|
||||||
|
var called = false;
|
||||||
|
@global function __REGISTER_IMPL(ref: usize, parentRef: usize): void {
|
||||||
|
assert(ref == ref4);
|
||||||
|
assert(parentRef == ref3);
|
||||||
|
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||||
|
assert(header.classId == __rt_classid<A>());
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
REGISTER<A>(ref4, ref3); // TODO
|
||||||
|
assert(called);
|
3733
tests/compiler/std/runtime.untouched.wat
Normal file
3733
tests/compiler/std/runtime.untouched.wat
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user