Skip to content

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:

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

Merge request reports