forked from Mirrors/wasm3
parent
e80ff08e7e
commit
457e5715bd
@ -0,0 +1,28 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
if (typeof(process) != 'undefined') { // Node.js environment?
|
||||||
|
var scriptArgs = process.argv.slice(2);
|
||||||
|
const fs = require('fs');
|
||||||
|
var readFile = (fn) => new Uint8Array(fs.readFileSync(fn));
|
||||||
|
} else {
|
||||||
|
var readFile = (fn) => read(fn, 'binary');
|
||||||
|
}
|
||||||
|
|
||||||
|
let instances = [];
|
||||||
|
|
||||||
|
(async() => {
|
||||||
|
const wasm = scriptArgs[0];
|
||||||
|
const func = scriptArgs[1];
|
||||||
|
const args = scriptArgs.slice(2);
|
||||||
|
|
||||||
|
const binary = readFile(wasm);
|
||||||
|
|
||||||
|
for (let i=0; i<1000; i++) { // V8: 1028 max, SpiderMonkey: 32650 max
|
||||||
|
let instance = (await WebAssembly.instantiate(binary)).instance;
|
||||||
|
|
||||||
|
instances[i] = instance;
|
||||||
|
|
||||||
|
let result = instance.exports[func](...args);
|
||||||
|
//console.log(i, result);
|
||||||
|
}
|
||||||
|
})();
|
@ -0,0 +1,129 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/*
|
||||||
|
Node.js
|
||||||
|
-------
|
||||||
|
node --v8-options | grep -A1 wasm
|
||||||
|
--print_wasm_code --code-comments
|
||||||
|
--wasm_interpret_all --trace_wasm_interpreter
|
||||||
|
|
||||||
|
SpiderMonkey
|
||||||
|
------------
|
||||||
|
export PATH=/opt/jsshell/:$PATH
|
||||||
|
js --help | grep wasm
|
||||||
|
--wasm-compiler=baseline/ion/cranelift/baseline+ion/baseline+cranelift
|
||||||
|
--wasm-verbose
|
||||||
|
--ion-full-warmup-threshold=1
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (typeof(process) != 'undefined') { // Node.js environment?
|
||||||
|
var scriptArgs = process.argv.slice(2);
|
||||||
|
const fs = require('fs');
|
||||||
|
var readFile = (fn) => new Uint8Array(fs.readFileSync(fn));
|
||||||
|
} else {
|
||||||
|
var readFile = (fn) => read(fn, 'binary');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Encode string into Uint8Array (with '\0' terminator)
|
||||||
|
// Could use TextEncoder instead
|
||||||
|
function encode(str) {
|
||||||
|
const len = str.length;
|
||||||
|
const res = new Uint8Array(len + 1);
|
||||||
|
let pos = 0;
|
||||||
|
for (let i = 0; i < len; i++) {
|
||||||
|
const point = str.charCodeAt(i);
|
||||||
|
if (point <= 0x007f) {
|
||||||
|
res[pos++] = point;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.subarray(0, pos + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let instance;
|
||||||
|
let runtimes = {};
|
||||||
|
|
||||||
|
const imports = {
|
||||||
|
"env": {
|
||||||
|
"emscripten_notify_memory_growth": function() {},
|
||||||
|
"emscripten_get_sbrk_ptr": function() {},
|
||||||
|
},
|
||||||
|
"wasi_snapshot_preview1": {
|
||||||
|
"fd_close": function() { return -1; },
|
||||||
|
"fd_seek": function() { return -1; },
|
||||||
|
"fd_write": function() { return -1; },
|
||||||
|
"proc_exit": function() { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function load(buff) {
|
||||||
|
const runtime = instance.exports.new_runtime();
|
||||||
|
const ptr = instance.exports.malloc(buff.length);
|
||||||
|
const mem = new Uint8Array(instance.exports.memory.buffer);
|
||||||
|
mem.set(buff, ptr);
|
||||||
|
instance.exports.load(runtime, ptr, buff.length);
|
||||||
|
runtimes[runtime] = { binary_ptr: ptr }
|
||||||
|
return runtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
function unload(runtime) {
|
||||||
|
if (!runtimes[runtime]) return;
|
||||||
|
instance.exports.free_runtime(runtime);
|
||||||
|
instance.exports.free(runtimes[runtime].binary_ptr);
|
||||||
|
runtimes[runtime] = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function call(runtime, fname, args) {
|
||||||
|
// Convert names to buffers
|
||||||
|
args = [fname].concat(args).map(arg => encode(arg.toString()));
|
||||||
|
|
||||||
|
const arglen = args.length;
|
||||||
|
let argbytes = arglen*4;
|
||||||
|
for (let arg of args) {
|
||||||
|
argbytes += arg.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate the required memory
|
||||||
|
const buff = instance.exports.malloc(argbytes);
|
||||||
|
const mem = new Uint8Array(instance.exports.memory.buffer);
|
||||||
|
const ptrs = new Uint32Array(mem.buffer, buff, arglen);
|
||||||
|
|
||||||
|
// Fill-in memory
|
||||||
|
let ptr = buff + ptrs.byteLength;
|
||||||
|
for (let i=0; i<arglen; i++) {
|
||||||
|
const arg = args[i];
|
||||||
|
ptrs[i] = ptr;
|
||||||
|
mem.set(arg, ptr);
|
||||||
|
ptr += arg.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actual call
|
||||||
|
const result = instance.exports.call(runtime, arglen, buff);
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
instance.exports.free(buff);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
(async() => {
|
||||||
|
instance = (await WebAssembly.instantiate(readFile('wasm3.wasm'), imports)).instance;
|
||||||
|
instance.exports.init();
|
||||||
|
|
||||||
|
const wasm = scriptArgs[0];
|
||||||
|
const func = scriptArgs[1];
|
||||||
|
const args = scriptArgs.slice(2);
|
||||||
|
|
||||||
|
const binary = readFile(wasm);
|
||||||
|
|
||||||
|
for (let i=0; i<100000; i++) {
|
||||||
|
let module = load(binary);
|
||||||
|
|
||||||
|
let result = call(module, func, args);
|
||||||
|
//console.log(i, result);
|
||||||
|
|
||||||
|
unload(module);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Memory size: ${instance.exports.memory.buffer.byteLength/(1024*1024)} MB`);
|
||||||
|
})();
|
@ -1,139 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
/*
|
|
||||||
Node.js
|
|
||||||
-------
|
|
||||||
node --v8-options | grep -A1 wasm
|
|
||||||
--print_wasm_code --code-comments
|
|
||||||
--wasm_interpret_all --trace_wasm_interpreter
|
|
||||||
|
|
||||||
SpiderMonkey
|
|
||||||
------------
|
|
||||||
export PATH=/opt/jsshell/:$PATH
|
|
||||||
js --help | grep wasm
|
|
||||||
--wasm-compiler=baseline/ion/cranelift/baseline+ion/baseline+cranelift
|
|
||||||
--wasm-verbose
|
|
||||||
--ion-full-warmup-threshold=1
|
|
||||||
*/
|
|
||||||
|
|
||||||
const isNodeJS = (typeof(process) != 'undefined');
|
|
||||||
|
|
||||||
function encode(str) {
|
|
||||||
var Len = str.length, resPos = -1;
|
|
||||||
var resArr = new Uint8Array(Len * 3);
|
|
||||||
for (var i = 0; i !== Len; i += 1) {
|
|
||||||
var point = str.charCodeAt(i);
|
|
||||||
if (point <= 0x007f) {
|
|
||||||
resArr[resPos += 1] = point;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resArr.subarray(0, resPos + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNodeJS) {
|
|
||||||
var scriptArgs = process.argv.slice(2);
|
|
||||||
const fs = require('fs');
|
|
||||||
var readFile = (fn) => new Uint8Array(fs.readFileSync(fn));
|
|
||||||
} else {
|
|
||||||
var readFile = (fn) => read(fn, 'binary');
|
|
||||||
}
|
|
||||||
|
|
||||||
const env = {
|
|
||||||
__memory_base: 0,
|
|
||||||
__table_base: 0,
|
|
||||||
"env": {
|
|
||||||
"emscripten_notify_memory_growth": function() {},
|
|
||||||
"emscripten_get_sbrk_ptr": function() {},
|
|
||||||
},
|
|
||||||
"wasi_snapshot_preview1": {
|
|
||||||
"fd_close": function() {},
|
|
||||||
"fd_seek": function() {},
|
|
||||||
"fd_write": function() { return -1; },
|
|
||||||
"proc_exit": function() {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var instance;
|
|
||||||
//let encoder = new TextEncoder('utf-8');
|
|
||||||
|
|
||||||
function strToMem(s) {
|
|
||||||
const data = encode(s);
|
|
||||||
const ptr = instance.exports.malloc(data.length+1);
|
|
||||||
const buf = new Uint8Array(instance.exports.memory.buffer, ptr, data.length+1);
|
|
||||||
buf.set(data)
|
|
||||||
buf[data.length] = 0;
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
function bufToMem(data) {
|
|
||||||
const ptr = instance.exports.malloc(data.length);
|
|
||||||
const buf = new Uint8Array(instance.exports.memory.buffer, ptr, data.length);
|
|
||||||
buf.set(data)
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function new_runtime() {
|
|
||||||
return instance.exports.new_runtime();
|
|
||||||
}
|
|
||||||
|
|
||||||
function free_runtime(runtime) {
|
|
||||||
instance.exports.free_runtime(runtime);
|
|
||||||
}
|
|
||||||
|
|
||||||
function load(runtime, buff) {
|
|
||||||
const ptr = bufToMem(buff);
|
|
||||||
instance.exports.load(runtime, ptr, buff.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
function call(runtime, fname, args) {
|
|
||||||
const namePtr = strToMem(fname)
|
|
||||||
|
|
||||||
const argsCnt = args.length;
|
|
||||||
const argsPtr = instance.exports.malloc(argsCnt*4);
|
|
||||||
const argsArray = new Uint32Array(
|
|
||||||
instance.exports.memory.buffer,
|
|
||||||
argsPtr, argsCnt
|
|
||||||
);
|
|
||||||
|
|
||||||
args.forEach((item, idx) => {
|
|
||||||
argsArray[idx] = strToMem(item.toString());
|
|
||||||
})
|
|
||||||
|
|
||||||
return instance.exports.call(runtime, namePtr, argsCnt, argsPtr);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO:
|
|
||||||
* - callback/hook exported function calls
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const FILE = scriptArgs[0];
|
|
||||||
const FUNC = scriptArgs[1];
|
|
||||||
const ARGS = scriptArgs.slice(2);
|
|
||||||
|
|
||||||
for (let i=0; i<25000; i++) {
|
|
||||||
let rt = new_runtime();
|
|
||||||
|
|
||||||
load(rt, readFile(FILE));
|
|
||||||
|
|
||||||
let result = call(rt, FUNC, [1]);
|
|
||||||
//console.log(i, result);
|
|
||||||
|
|
||||||
free_runtime(rt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
WebAssembly.instantiate(readFile('wasm3.wasm'), env).then(result => {
|
|
||||||
instance = result.instance;
|
|
||||||
instance.exports.init();
|
|
||||||
main();
|
|
||||||
|
|
||||||
console.log(`Memory size: ${instance.exports.memory.buffer.byteLength}`);
|
|
||||||
})
|
|
||||||
} catch (e) {
|
|
||||||
console.log(JSON.stringify(e.message));
|
|
||||||
}
|
|
Loading…
Reference in new issue