-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
106 lines (96 loc) · 3.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8" />
<title>Wasm Test</title>
</head>
<body>
<div id="cppLog">
<p>CPP log</p>
</div>
<br>
<div id="rustLog">
<p>Rust log</p>
</div>
<br>
<div id="asLog">
<p>AS log</p>
</div>
<script type='text/javascript'>
var memoryUint8Array = null;
function uintToString(uintArray) {
var encodedString = String.fromCharCode.apply(null, uintArray),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
}
function readBuff(buffer,offset){
var current = buffer[offset];
var start = offset;
var end = start;
while(current!== 0){
offset++;
current = buffer[offset];
}
end = offset;
return new TextDecoder().decode(buffer.slice(start,end));
}
var myImports = {
env: {
rust_begin_unwind: function(){},
import_function: function(i){
console.log("Import called with param",i);
return i*2
},
print_function: function(str){
console.log(readBuff(memoryUint8Array,str));
}
}
}
var rustLog = document.getElementById('rustLog');
var cppLog = document.getElementById('cppLog');
var asLog = document.getElementById('asLog');
fetch('/wasm/cpp/build/cpp.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.compile(bytes)
).then(function(mod) {
var imports = WebAssembly.Module.imports(mod);
var inst = new WebAssembly.Instance(mod,myImports);
memoryUint8Array = new Uint8Array(inst.exports.memory.buffer);
var lib = inst.exports;
cppLog.innerHTML += "hello from c "+lib.export_function(41)+"<br>";
//cppLog.innerHTML += "Hello from print export "+readBuff(memoryUint8Array,lib.print_export())+"<br>";
console.log("Hello from c",lib.export_function(41));
//console.log("Hello from print export",readBuff(memoryUint8Array,lib.print_export()));
}).catch(function(e){
cppLog.innerHTML += "Fetching CPP error:"+e+"<br>";
});
fetch('/wasm/rust/hello-rust.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.compile(bytes)
).then(function(mod) {
var imports = WebAssembly.Module.imports(mod);
var inst = new WebAssembly.Instance(mod,myImports);
var lib = inst.exports;
rustLog.innerHTML += "Hello from rust "+lib.export_function(41)+"<br>";
console.log("hello from rust",lib.export_function(41));
}).catch(function(e){
rustLog.innerHTML += "Fetching Rust error: "+e+"<br>";
});
fetch('/wasm/as/hello-as.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.compile(bytes)
).then(function(mod) {
var imports = WebAssembly.Module.imports(mod);
var inst = new WebAssembly.Instance(mod,myImports);
var lib = inst.exports;
asLog.innerHTML += "Hello from AssemblyScript "+lib.export_function(41)+"<br>";
console.log("hello from AssemblyScript",lib.export_function(41));
}).catch(function(e){
asLog.innerHTML += "Fetching AssemblyScript error: "+e+"<br>";
});
</script>
</body>
</html>