wasm-utils/runner/index.html

126 lines
5.2 KiB
HTML

<html>
<head>
<meta charset="UTF-8">
<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 || 1024;
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" "invoke_vi" (func (;4;) (type 3)))
(import "env" "invoke_v" (func (;5;) (type 1)))
(import "env" "_rust_begin_unwind" (func (;6;) (type 4)))
(import "env" "_emscripten_memcpy_big" (func (;9;) (type 5)))
(import "env" "___gxx_personality_v0" (func (;10;) (type 6)))
(import "env" "_llvm_trap" (func (;11;) (type 0)))
(import "env" "___resumeException" (func (;12;) (type 1)))
(import "env" "___cxa_find_matching_catch_2" (func (;13;) (type 2)))
(import "env" "___gxx_personality_v0" (func (;14;) (type 7)))
(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))
*/
var memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
env.STACKTOP = env.STACKTOP || 0;
env.STACK_MAX = env.STACK_MAX || 5*1024*1024;
env.DYNAMICTOP_PTR = env.STACK_MAX;
env.enlargeMemory = env.enlargeMemory || function() {
console.log("called enlargeMemory(" + JSON.stringify(arguments) + ");");
return 1;
};
env.getTotalMemory = env.getTotalMemory || function() {
return 16 * 1024 * 1024;
};
env.abortOnCannotGrowMemory = env.abortOnCannotGrowMemory || function() { throw "abort growing memory"; };
env._abort = env._abort || function() { throw "_abort"; };
env.abort = env.abort || function() { throw "abort"; };
env.___setErrNo = env.___setErrNo || function() { throw "setting error no"; };
// dead symbols in rust wasm32-unknown-emscripten target
// todo: strip/raise issue in rust compiler
env.invoke_vi = function() { throw "invoke_vi: unreachable!"; }
env.invoke_v = function() { throw "invoke_v: unreachable!"; }
// todo: also test unwind about those two
env._rust_begin_unwind = function() { throw "_rust_begin_unwind: unreachable!"; }
env._llvm_trap = function() { throw "_llvm_trap: unreachable!"; }
env._emscripten_memcpy_big = function() { throw "_emscripten_memcpy_big: unreachable!"; }
env.___gxx_personality_v0 = function() { throw "___gxx_personality_v0: unreachable!"; }
env.___resumeException = function() { throw "___resumeException: unreachable!"; }
env.___cxa_find_matching_catch_2 = function() { throw "___cxa_find_matching_catch_2: unreachable!"; }
env.___gxx_personality_v0 = function() { throw "___gxx_personality_v0: unreachable!"; }
env.memoryBase = env.memoryBase || 0;
env.tableBase = env.tableBase || 0;
env.gas = function(upd) {
console.log("used " + upd + " gas");
}
if (!imports.env.memory) {
imports.env.memory = memory;
}
if (!imports.env.table) {
imports.env.table = new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' });
}
window.testMemory = memory;
return new WebAssembly.Instance(module, imports);
});
}
loadWebAssembly('out/contract.wasm')
.then(instance => {
debugger;
var exports = instance.exports;
var call = exports._call;
var malloc = exports._malloc;
// raw call context
var context_ptr = malloc(256);
// raw persistent storage
var storage_ptr = malloc(8192);
console.log("context_ptr: " + context_ptr);
console.log("storage_ptr: " + storage_ptr);
var button = document.getElementById('do-call');
button.value = 'Execute call';
button.addEventListener('click', function() {
let new_storage_ptr = call(context_ptr, storage_ptr);
console.log("Call succeded");
}, false);
}
);
</script>
</head>
<body>
<input type="button" id="do-call" value="(waiting for WebAssembly)"/>
</body>
</html>