2018-02-02 19:05:49 +01:00
|
|
|
const fs = require("fs");
|
2018-04-03 23:56:48 +02:00
|
|
|
const COMMON_MAX = 1 << 30;
|
2018-02-01 17:35:51 +01:00
|
|
|
|
2018-01-16 17:52:48 +01:00
|
|
|
function test(file) {
|
2018-02-01 17:35:51 +01:00
|
|
|
console.log("Testing '" + file + "' ...\n");
|
2018-01-16 05:25:03 +01:00
|
|
|
|
2018-05-06 00:00:54 +02:00
|
|
|
const exports = new WebAssembly.Instance(new WebAssembly.Module(fs.readFileSync(__dirname + "/" + file)), {
|
2018-01-16 17:52:48 +01:00
|
|
|
env: {
|
2018-02-01 17:35:51 +01:00
|
|
|
abort: function(msg, file, line, column) {
|
|
|
|
throw Error("Assertion failed: " + (msg ? "'" + getString(msg) + "' " : "") + "at " + getString(file) + ":" + line + ":" + column);
|
|
|
|
},
|
|
|
|
log: function(ptr) { console.log(getString(ptr)); },
|
|
|
|
logi: function(i) { console.log(i); }
|
2018-01-16 05:25:03 +01:00
|
|
|
}
|
2018-01-16 17:52:48 +01:00
|
|
|
}).exports;
|
|
|
|
|
2019-03-29 11:23:40 +01:00
|
|
|
const RUNTIME_HEADER_SIZE = exports[".capabilities"] & 2 ? 16 : 8;
|
|
|
|
|
2018-02-01 17:35:51 +01:00
|
|
|
function getString(ptr) {
|
2019-03-29 11:23:40 +01:00
|
|
|
if (!ptr) return "null";
|
|
|
|
var U32 = new Uint32Array(exports.memory.buffer);
|
|
|
|
var U16 = new Uint16Array(exports.memory.buffer);
|
|
|
|
var len16 = U32[(ptr - RUNTIME_HEADER_SIZE + 4) >>> 2] >>> 1;
|
|
|
|
var ptr16 = ptr >>> 1;
|
|
|
|
return String.fromCharCode.apply(String, U16.subarray(ptr16, ptr16 + len16));
|
2018-01-16 05:25:03 +01:00
|
|
|
}
|
2018-02-01 17:35:51 +01:00
|
|
|
|
2018-04-11 23:35:19 +02:00
|
|
|
require("./runner")(exports, 20, 20000);
|
2018-02-19 22:45:31 +01:00
|
|
|
|
2018-02-01 17:35:51 +01:00
|
|
|
console.log("mem final: " + exports.memory.buffer.byteLength);
|
2018-01-16 17:52:48 +01:00
|
|
|
console.log();
|
2018-04-03 23:56:48 +02:00
|
|
|
|
2019-05-23 03:33:32 +02:00
|
|
|
const alloc = exports["__alloc"];
|
2018-07-18 23:49:32 +02:00
|
|
|
var overflow = false;
|
2018-04-08 00:43:38 +02:00
|
|
|
try {
|
2018-07-18 23:49:32 +02:00
|
|
|
alloc(COMMON_MAX + 1); // unreachable
|
|
|
|
overflow = true;
|
2018-04-08 00:43:38 +02:00
|
|
|
} catch (e) {}
|
2018-07-18 23:49:32 +02:00
|
|
|
if (overflow) throw Error("allocation can overflow COMMON_MAX + 1");
|
2018-04-08 00:43:38 +02:00
|
|
|
try {
|
2018-07-18 23:49:32 +02:00
|
|
|
alloc(0xffffffff); // unreachable
|
|
|
|
overflow = true;
|
2018-04-08 00:43:38 +02:00
|
|
|
} catch (e) {}
|
2018-07-18 23:49:32 +02:00
|
|
|
if (overflow) throw Error("allocation can overflow 0xffffffff");
|
2018-01-16 05:25:03 +01:00
|
|
|
}
|
|
|
|
|
2018-03-04 17:25:32 +01:00
|
|
|
if (process.argv.length > 2) {
|
|
|
|
test(process.argv[2] + "/untouched.wasm");
|
|
|
|
test(process.argv[2] + "/optimized.wasm");
|
|
|
|
} else {
|
|
|
|
console.error("Usage: npm test <allocator>");
|
|
|
|
process.exit(1);
|
|
|
|
}
|