2019-03-13 22:35:47 +01:00
|
|
|
import { memcmp, memmove, memset } from "./util/memory";
|
2019-03-28 11:07:47 +01:00
|
|
|
import { E_NOTIMPLEMENTED } from "./util/error";
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Memory manager interface. */
|
|
|
|
export namespace memory {
|
|
|
|
|
|
|
|
/** Gets the size of the memory in pages. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@builtin
|
|
|
|
export declare function size(): i32;
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Grows the memory by the given size in pages and returns the previous size in pages. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export declare function grow(pages: i32): i32;
|
2019-03-13 22:35:47 +01:00
|
|
|
|
|
|
|
/** Fills a section in memory with the specified byte value. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export function fill(dst: usize, c: u8, n: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
memset(dst, c, n); // fallback if "bulk-memory" isn't enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Copies a section of memory to another. Has move semantics. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe @builtin
|
|
|
|
export function copy(dst: usize, src: usize, n: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
memmove(dst, src, n); // fallback if "bulk-memory" isn't enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Initializes a memory segment. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function init(segmentIndex: u32, srcOffset: usize, dstOffset: usize, n: usize): void {
|
2019-03-28 11:07:47 +01:00
|
|
|
throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Drops a memory segment. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function drop(segmentIndex: u32): void {
|
2019-03-28 11:07:47 +01:00
|
|
|
throw new Error(E_NOTIMPLEMENTED);
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Repeats a section of memory at a specific address. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@unsafe
|
|
|
|
export function repeat(dst: usize, src: usize, srcLength: usize, count: usize): void {
|
2019-03-13 22:35:47 +01:00
|
|
|
var index: usize = 0;
|
|
|
|
var total = srcLength * count;
|
|
|
|
while (index < total) {
|
|
|
|
memory.copy(dst + index, src, srcLength);
|
|
|
|
index += srcLength;
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 04:33:58 +01:00
|
|
|
|
|
|
|
/** Compares a section of memory to another. */
|
2019-03-14 05:11:03 +01:00
|
|
|
// @ts-ignore: decorator
|
2019-03-14 06:09:49 +01:00
|
|
|
@inline
|
|
|
|
export function compare(vl: usize, vr: usize, n: usize): i32 {
|
2019-03-14 04:33:58 +01:00
|
|
|
return memcmp(vl, vr, n);
|
|
|
|
}
|
2019-03-13 22:35:47 +01:00
|
|
|
}
|