-
Notifications
You must be signed in to change notification settings - Fork 12
/
change_string.html
65 lines (47 loc) · 1.26 KB
/
change_string.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
<html>
<body>
<script src="../wail.js"></script>
<!-- wabt.js used for compiling WAT to WASM -->
<script src="https://cdn.jsdelivr.net/gh/AssemblyScript/wabt.js/index.js"></script>
<script>
WabtModule().then(function(wabt) {
const logString = function(offset) {
const bytes = new Uint8Array(memory.buffer, offset);
const stringBytes = [];
for (i = 0; bytes[i] !== 0x00; i++) {
stringBytes.push(bytes[i]);
}
const stringArray = new Uint8Array(stringBytes);
const string = new TextDecoder('utf8').decode(stringArray);
console.log(string);
}
const wasmText = `
(module
(import "env" "log" (func $log (param i32)))
(import "env" "mem" (memory 1))
(data (i32.const 0) "Old Text")
(func (export "printMsg")
i32.const 0
call $log))
`;
const wat = wabt.parseWat('change_string.wast', wasmText);
const binary = wat.toBinary({});
const memory = new WebAssembly.Memory({initial:1});
const importObject = {
env: {
log: logString,
mem: memory
},
};
const wail = new WailParser(binary.buffer);
wail.editDataEntry(0, {
data: "New, longer text"
});
wail.parse();
WebAssembly.instantiate(wail.write(), importObject).then(function(wasm) {
wasm.instance.exports.printMsg();
});
});
</script>
</body>
</html>