wasm-utils/runner/index.html
2017-04-12 16:03:06 +03:00

74 lines
2.9 KiB
HTML

<html>
<head>
<script>
// Check for wasm support.
if (!('WebAssembly' in window)) {
alert('you need a browser with wasm support enabled :(');
}
function loadWebAssembly(filename, imports) {
return fetch(filename)
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.compile(buffer))
.then(module => {
imports = imports || {};
imports.env = imports.env || {};
var env = imports.env;
imports.env.memoryBase = imports.env.memoryBase || 0;
imports.env.tableBase = imports.env.tableBase || 0;
/* rust/emscripten imports
(import "env" "DYNAMICTOP_PTR" (global (;0;) i32))
(import "env" "STACKTOP" (global (;1;) i32))
(import "env" "STACK_MAX" (global (;2;) i32))
(import "env" "enlargeMemory" (func (;0;) (type 0)))
(import "env" "getTotalMemory" (func (;1;) (type 0)))
(import "env" "abortOnCannotGrowMemory" (func (;2;) (type 0)))
(import "env" "_abort" (func (;3;) (type 1)))
(import "env" "___setErrNo" (func (;4;) (type 2)))
(import "env" "memory" (memory (;0;) 256 256))
(import "env" "table" (table (;0;) 0 0 anyfunc))
(import "env" "memoryBase" (global (;3;) i32))
(import "env" "tableBase" (global (;4;) i32))
*/
env.DYNAMICTOP_PTR = env.DYNAMICTOP_PTR || 0;
env.STACKTOP = env.STACKTOP || 0;
env.STACK_MAX = env.STACK_MAX || 5*1024*1024;
env.enlargeMemory = env.enlargeMemory || function() { throw "error enlarging memory"; };
env.getTotalMemory = env.getTotalMemory || function() { return 65536 * 256; };
env.abortOnCannotGrowMemory = env.abortOnCannotGrowMemory || function() { throw "abort growing memory"; };
env._abort = env.abortOnCannotGrowMemory || function() { throw "abort"; };
env.___setErrNo = env.abortOnCannotGrowMemory || function() { throw "setting error no"; };
env.memoryBase = env.memoryBase || 0;
env.tableBase = env.tableBase || 0;
if (!imports.env.memory) {
imports.env.memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
}
if (!imports.env.table) {
imports.env.table = new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' });
}
return new WebAssembly.Instance(module, imports);
});
}
loadWebAssembly('out/contract.wasm')
.then(instance => {
var exports = instance.exports;
var call = exports._call;
var button = document.getElementById('do-call');
button.value = 'Execute call';
button.addEventListener('click', function() {
call();
}, false);
}
);
</script>
</head>
<body>
<input type="button" id="do-call" value="(waiting for WebAssembly)"/>
</body>
</html>