mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-21 18:51:43 +00:00
static array stuff
This commit is contained in:
@ -15,7 +15,7 @@ export namespace gc {
|
||||
export function register(ref: usize): void {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__gc_register)) __gc_register(ref);
|
||||
else ERROR("missing implementation: gc.register");
|
||||
else WARNING("missing implementation: gc.register");
|
||||
}
|
||||
|
||||
/** Links a registered object with the registered object now referencing it. */
|
||||
@ -24,7 +24,7 @@ export namespace gc {
|
||||
export function link(ref: usize, parentRef: usize): void {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__gc_link)) __gc_link(ref, parentRef);
|
||||
else ERROR("missing implementation: gc.link");
|
||||
else WARNING("missing implementation: gc.link");
|
||||
}
|
||||
|
||||
/** Marks an object as being reachable. */
|
||||
@ -33,7 +33,7 @@ export namespace gc {
|
||||
export function mark(ref: usize): void {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__gc_mark)) __gc_mark(ref);
|
||||
else ERROR("missing implementation: gc.mark");
|
||||
else WARNING("missing implementation: gc.mark");
|
||||
}
|
||||
|
||||
/** Performs a full garbage collection cycle. */
|
||||
|
@ -65,7 +65,9 @@ export class Map<K,V> {
|
||||
|
||||
get size(): i32 { return this.entriesCount; }
|
||||
|
||||
constructor() { this.clear(); }
|
||||
constructor() {
|
||||
this.clear();
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
const bucketsSize = INITIAL_CAPACITY * <i32>BUCKET_SIZE;
|
||||
|
@ -51,7 +51,7 @@ export namespace memory {
|
||||
export function allocate(size: usize): usize {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__memory_allocate)) return __memory_allocate(size);
|
||||
else ERROR("missing implementation: memory.allocate");
|
||||
else WARNING("missing implementation: memory.allocate");
|
||||
return <usize>unreachable();
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ export namespace memory {
|
||||
export function free(ptr: usize): void {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__memory_free)) __memory_free(ptr);
|
||||
else ERROR("missing implementation: memory.free");
|
||||
else WARNING("missing implementation: memory.free");
|
||||
}
|
||||
|
||||
/** Resets the memory to its initial state. Arena allocator only. */
|
||||
@ -70,7 +70,7 @@ export namespace memory {
|
||||
export function reset(): void {
|
||||
// @ts-ignore: stub
|
||||
if (isDefined(__memory_reset)) __memory_reset();
|
||||
else ERROR("missing implementation: memory.reset");
|
||||
else WARNING("missing implementation: memory.reset");
|
||||
}
|
||||
|
||||
/** Repeats a section of memory at a specific address. */
|
||||
|
@ -138,6 +138,33 @@ export function DISCARD(ref: usize): void {
|
||||
memory.free(changetype<usize>(ref - HEADER_SIZE));
|
||||
}
|
||||
|
||||
/** Wraps a static buffer within an array by copying its contents. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @inline
|
||||
export function WRAPARRAY<T>(buffer: ArrayBuffer): T[] {
|
||||
// TODO: this is quite a lot to compile inline
|
||||
var array = REGISTER<T[]>(ALLOCATE(offsetof<T[]>()));
|
||||
var bufferSize = <usize>buffer.byteLength;
|
||||
var newBuffer = REGISTER<ArrayBuffer>(ALLOCATE(bufferSize));
|
||||
changetype<ArrayBufferView>(array).data = newBuffer; // links
|
||||
changetype<ArrayBufferView>(array).dataStart = changetype<usize>(newBuffer);
|
||||
changetype<ArrayBufferView>(array).dataEnd = changetype<usize>(newBuffer) + bufferSize;
|
||||
store<i32>(changetype<usize>(array), <i32>(bufferSize >>> alignof<T>()), offsetof<T[]>("length_"));
|
||||
if (isManaged<T>()) {
|
||||
ERROR("unexpected managed type"); // not used currently
|
||||
let dataOffset: usize = 0;
|
||||
while (dataOffset < bufferSize) {
|
||||
let element: T = load<T>(changetype<usize>(buffer) + dataOffset);
|
||||
store<T>(changetype<usize>(newBuffer) + dataOffset, element);
|
||||
LINK(element, array);
|
||||
dataOffset += sizeof<T>();
|
||||
}
|
||||
} else {
|
||||
memory.copy(changetype<usize>(newBuffer), changetype<usize>(buffer), bufferSize);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
/** Asserts that a managed object is still unregistered. */
|
||||
|
Reference in New Issue
Block a user