mirror of
https://github.com/fluencelabs/wasm-utils
synced 2025-03-27 16:31:05 +00:00
107 lines
4.7 KiB
HTML
107 lines
4.7 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 || 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" "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))
|
|
*/
|
|
|
|
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._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 = new WebAssembly.Memory({ initial: 256, maximum: 256 });
|
|
}
|
|
if (!imports.env.table) {
|
|
imports.env.table = new WebAssembly.Table({ initial: 8, maximum: 8, 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();
|
|
console.log("Call succeded");
|
|
}, false);
|
|
}
|
|
);
|
|
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<input type="button" id="do-call" value="(waiting for WebAssembly)"/>
|
|
</body>
|
|
</html> |