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

@ -14,23 +14,17 @@ for (var key in binaryen)
if (/^_(?:Binaryen|Relooper)/.test(key))
globalScope[key] = binaryen[key];
// Use Binaryen's heap
Object.defineProperties(globalScope["Heap"] = {
allocate: function allocate(size) {
if (!size) return 0; // should be safe in our case
return binaryen._malloc(size);
},
dispose: function dispose(ptr) {
if (ptr) binaryen._free(ptr);
},
copy: function copy(dest, src, n) {
return binaryen._memcpy(dest, src, n);
}
}, {
free: { get: function() { return binaryen.HEAPU8.length; } },
used: { get: function() { return 0; } },
size: { get: function() { return binaryen.HEAPU8.length; } }
});
// Use Binaryen's heap instead of std heap
globalScope["allocate_memory"] = function allocate_memory(size) {
if (!size) return 0; // should be safe in our case
return binaryen._malloc(size);
};
globalScope["free_memory"] = function free_memory(ptr) {
if (ptr) binaryen._free(ptr);
};
globalScope["move_memory"] = function move_memory(dest, src, n) {
return binaryen._memmove(dest, src, n);
};
globalScope["store"] = function store(ptr, val) {
binaryen.HEAPU8[ptr] = val;
};
@ -42,11 +36,11 @@ globalScope["load"] = function load(ptr) {
var Module = require("../module").Module;
Module.prototype.toBinary = function toBinary(bufferSize) {
if (!bufferSize) bufferSize = 1024 * 1024; // FIXME: see binaryen.js-post.js in Binaryen
var ptr = Heap.allocate(bufferSize);
var ptr = allocate_memory(bufferSize);
var len = this.write(ptr, bufferSize);
var ret = new Uint8Array(len);
ret.set(binaryen.HEAPU8.subarray(ptr, ptr + len));
Heap.dispose(ptr);
free_memory(ptr);
return ret;
};
Module.prototype.toText = function toText() {