64 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-06-05 23:15:39 +02:00
import { memcmp, memmove, memset } from "./util/memory";
import { E_NOTIMPLEMENTED } from "./util/error";
2018-08-02 18:23:02 +02:00
2019-06-05 23:15:39 +02:00
/** Memory manager interface. */
export namespace memory {
2017-12-08 16:11:58 +01:00
2019-06-05 23:15:39 +02:00
/** Gets the size of the memory in pages. */
// @ts-ignore: decorator
@builtin
export declare function size(): i32;
/** Grows the memory by the given size in pages and returns the previous size in pages. */
// @ts-ignore: decorator
@unsafe @builtin
export declare function grow(pages: i32): i32;
/** Fills a section in memory with the specified byte value. */
// @ts-ignore: decorator
@unsafe @builtin
export function fill(dst: usize, c: u8, n: usize): void {
memset(dst, c, n); // fallback if "bulk-memory" isn't enabled
}
2019-06-05 23:15:39 +02:00
/** Copies a section of memory to another. Has move semantics. */
// @ts-ignore: decorator
@unsafe @builtin
export function copy(dst: usize, src: usize, n: usize): void {
memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled
}
2019-06-05 23:15:39 +02:00
/** Initializes a memory segment. */
// @ts-ignore: decorator
@unsafe
export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void {
throw new Error(E_NOTIMPLEMENTED);
}
2019-06-05 23:15:39 +02:00
/** Drops a memory segment. */
// @ts-ignore: decorator
@unsafe
export function drop(segmentIndex: u32): void {
throw new Error(E_NOTIMPLEMENTED);
}
2019-06-05 23:15:39 +02:00
/** Repeats a section of memory at a specific address. */
// @ts-ignore: decorator
@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;
}
}
2019-06-05 23:15:39 +02:00
/** Compares a section of memory to another. */
// @ts-ignore: decorator
@inline
export function compare(vl: usize, vr: usize, n: usize): i32 {
return memcmp(vl, vr, n);
}
}