mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-14 23:41:30 +00:00
Polyfill move_memory and set_memory and remove Heap
This commit is contained in:
21
std/portable/heap.d.ts
vendored
21
std/portable/heap.d.ts
vendored
@ -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;
|
||||
}
|
@ -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];
|
||||
};
|
||||
|
Reference in New Issue
Block a user