wasm-utils/runner/index.html

120 lines
5.0 KiB
HTML
Raw Normal View History

2017-04-12 13:58:22 +03:00
<html>
<head>
2017-04-12 16:13:48 +03:00
<meta charset="UTF-8">
2017-04-12 13:58:22 +03:00
<script>
// Check for wasm support.
if (!('WebAssembly' in window)) {
alert('you need a browser with wasm support enabled :(');
}
2017-04-18 14:22:24 +03:00
2017-04-12 13:58:22 +03:00
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;
2017-04-18 14:22:24 +03:00
imports.env.memoryBase = imports.env.memoryBase || 1024;
2017-04-12 13:58:22 +03:00
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)))
2017-04-13 01:03:41 +03:00
(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)))
2017-04-12 13:58:22 +03:00
(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))
*/
2017-04-18 14:22:24 +03:00
var memory = new WebAssembly.Memory({ initial: 256, maximum: 256 });
2017-04-12 13:58:22 +03:00
env.STACKTOP = env.STACKTOP || 0;
2017-04-12 16:03:06 +03:00
env.STACK_MAX = env.STACK_MAX || 5*1024*1024;
2017-04-18 14:22:24 +03:00
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;
};
2017-04-12 13:58:22 +03:00
env.abortOnCannotGrowMemory = env.abortOnCannotGrowMemory || function() { throw "abort growing memory"; };
2017-04-13 01:03:41 +03:00
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!"; }
2017-04-12 13:58:22 +03:00
env.memoryBase = env.memoryBase || 0;
env.tableBase = env.tableBase || 0;
2017-04-12 19:12:48 +03:00
env.gas = function(upd) {
console.log("used " + upd + " gas");
}
2017-04-12 13:58:22 +03:00
if (!imports.env.memory) {
2017-04-18 14:22:24 +03:00
imports.env.memory = memory;
2017-04-12 13:58:22 +03:00
}
if (!imports.env.table) {
2017-04-18 14:22:24 +03:00
imports.env.table = new WebAssembly.Table({ initial: 0, maximum: 0, element: 'anyfunc' });
2017-04-12 13:58:22 +03:00
}
return new WebAssembly.Instance(module, imports);
});
}
loadWebAssembly('out/contract.wasm')
.then(instance => {
2017-04-18 14:22:24 +03:00
debugger;
2017-04-12 13:58:22 +03:00
var exports = instance.exports;
var call = exports._call;
2017-04-18 14:22:24 +03:00
var malloc = exports._malloc;
var data_ptr = malloc(1024*8);
console.log("data_ptr: " + data_ptr);
2017-04-12 13:58:22 +03:00
var button = document.getElementById('do-call');
button.value = 'Execute call';
button.addEventListener('click', function() {
call();
2017-04-12 19:12:48 +03:00
console.log("Call succeded");
2017-04-12 13:58:22 +03:00
}, false);
}
);
</script>
</head>
<body>
<input type="button" id="do-call" value="(waiting for WebAssembly)"/>
</body>
</html>