Polyfill move_memory and set_memory and remove Heap

This commit is contained in:
dcodeIO
2018-01-14 02:30:20 +01:00
parent 2c009c67d3
commit ad469ca445
33 changed files with 8529 additions and 5057 deletions

View File

@ -1,21 +0,0 @@
/** A static class representing the heap. */
declare class Heap {
/** Allocates a chunk of memory and returns a pointer to it. */
static allocate(size: usize): usize;
/** Disposes a chunk of memory by its pointer. */
static dispose(ptr: usize): void;
/** Gets the amount of used heap space, in bytes. */
static readonly used: usize;
/** Gets the amount of free heap space, in bytes. */
static readonly free: usize;
/** Gets the size of the heap, in bytes. */
static readonly size: usize;
/** Copies a chunk of memory from one location to another. */
static copy(dest: usize, src: usize, n: usize): usize;
}

View File

@ -3,29 +3,37 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
var HEAP = new Uint8Array(0);
var HEAP_OFFSET = 0;
Object.defineProperties(globalScope["Heap"] = {
allocate: function allocate(size) {
if (!(size >>>= 0)) return 0;
if (HEAP_OFFSET + size > HEAP.length) {
var oldHeap = HEAP;
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
HEAP.set(oldHeap);
}
var ptr = HEAP_OFFSET;
if ((HEAP_OFFSET += size) & 7)
HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
return ptr;
},
dispose: function dispose() { },
copy: function copy(dest, src, n) {
HEAP.set(HEAP.subarray(src >>> 0, (src + n) >>> 0), dest >>> 0);
return dest;
globalScope["allocate_memory"] =
function allocate_memory(size) {
if (!(size >>>= 0))
return 0;
if (HEAP_OFFSET + size > HEAP.length) {
var oldHeap = HEAP;
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
HEAP.set(oldHeap);
}
}, {
used: { get: function get_used() { return HEAP_OFFSET; } },
free: { get: function get_free() { return HEAP.length - HEAP_OFFSET; } },
size: { get: function get_size() { return HEAP.length; } }
});
var ptr = HEAP_OFFSET;
if ((HEAP_OFFSET += size) & 7)
HEAP_OFFSET = (HEAP_OFFSET | 7) + 1;
return ptr;
};
globalScope["store"] = function store(ptr, val) { HEAP[ptr] = val; };
globalScope["load"] = function load(ptr) { return HEAP[ptr]; };
globalScope["free_memory"] =
function free_memory(ptr) {
// TODO
};
globalScope["move_memory"] =
function move_memory(dest, src, n) {
HEAP.copyWithin(dest, src, src + n);
};
globalScope["store"] =
function store(ptr, val) {
HEAP[ptr] = val;
};
globalScope["load"] =
function load(ptr) {
return HEAP[ptr];
};