The source project of this merge request has been removed.
Draft: Initial support for WebAssembly
This is initial support for WebAssembly. It generates wasm binaries in the right format, we can execute it in the browser, but there are bugs, see below.
Using this Fortran source as example/Hello WASM
program main
use iso_c_binding
print *, add(1, 1)
contains
integer(kind=c_int) function add(x, y) result(r) bind(c, name="add")
integer(kind=c_int), intent(in) :: x, y
r = x + y + 2021
end function
end program
Compile and link
lfortran -c wasm.f90 --target wasm32 -o wasm.o
wasm-ld --no-entry --import-memory wasm.o -o wasm.wasm --export add
Running wasm.wasm
in browser. Create an HTML page and add
<script type="module">
// This is from https://aransentin.github.io/cwasm/
let imports = {};
let memory = null;
let exports = null;
let request = await fetch('wasm.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 } );
// BUG: Should print "2030" = (2 + 7 + 2021). It currently prints "2021".
document.getElementById("lfortran").innerHTML = program.instance.exports.add(2, 7);
</script>
Demo: https://fortran.rocks/wasm.html
As you can see, it should print "2030", but it currently prints "2021".
References: