WASM: Support i64 Constants
Towards #700
This adds support of i64
constants to our current asr_to_wasm
and wasm_to_wat
backends. There is slight/minor code-refactoring as needed.
Highlights:
- The generated
wat
is tested on https://webassembly.github.io/wabt/demo/wat2wasm/ and it works. - The generated
wasm
is converted towat
usingwasm2wat
tool (part ofwabt
) and tested on https://webassembly.github.io/wabt/demo/wat2wasm/ and it works. - The generated
wasm
is tested on a local server (python3 -m http.server
) and anindex.html
file (contents in appendix below) and it works. - The generated
wasm
is tested locally usingnode.js
(and testing code inmain.js
(contents in appendix below)) and it works- For
node
versions below16.x
(not sure of the exact version), we need to use the flag--experimental-wasm-bigint
while executing. For example: node--experimental-wasm-bigint main.js
- For
node
versions above16.x
(not sure of the exact version) , we can usenode main.js
for executing. - References: https://github.com/emscripten-core/emsdk/issues/476#issuecomment-617323959, https://github.com/remixlabs/wasicaml/issues/26
- For
Appendix
-
index.html
contents:
<html>
<head></head>
<body>
<div id="lfortran">hi</div>
<script type="module">
// This is from https://aransentin.github.io/cwasm/
let imports = {};
let memory = null;
let exports = null;
let request = await fetch('a.wasm');
let binary = await request.arrayBuffer();
imports['memory'] = new WebAssembly['Memory']({ 'initial': 32 });
memory = new Uint8Array(imports['memory']['buffer']);
let program = await WebAssembly['instantiate'](binary, { "env": imports });
const { test_i64, add_i64, a_sqr_i64 } = program.instance.exports;
console.log(test_i64())
console.log(add_i64(BigInt(2147483647), BigInt(5)))
console.log(a_sqr_i64(BigInt(1000000000)));
console.log("Success!");
</script>
</body>
</html>
-
main.js
contents
const fs = require("fs");
const wasmBuffer = fs.readFileSync("a.wasm");
WebAssembly.instantiate(wasmBuffer)
.then((wasmModule) => {
// Exported function live under instance.exports
const { test_i64, add_i64, a_sqr_i64 } = wasmModule.instance.exports;
console.log(test_i64())
console.log(add_i64(BigInt(2), BigInt(3)))
console.log(a_sqr_i64(BigInt(5)));
console.log("Success!");
})
.catch((e) => console.log(e));
Edited by Ubaid Shaikh