-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdouady.txt
46 lines (36 loc) · 1.49 KB
/
douady.txt
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
= Compiling to WebAssembly =
This page loads wasm that draws a set studied by Douady and Hubbard.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
<canvas id="canvas" width="640" height="400"></canvas>
<script>
var e;
var ctx = document.getElementById("canvas").getContext("2d");
function sq(x, y) { ctx.fillRect(x*4, y*4, 4, 4); }
WebAssembly.instantiateStreaming(fetch('douady.wasm'), {
env:{ plot:sq }
}).then(obj => {
e = obj.instance.exports;
e.rts_init();
e.draw();
});
</script>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The Haskell source:
[source,haskell]
------------------------------------------------------------------------
include::wasm/douady.hs[]
------------------------------------------------------------------------
To turn the output of our compiler into wasm, we need minimal implementations
of `malloc` and `memcpy`:
[source,c]
------------------------------------------------------------------------
include::wasm/std.c[]
------------------------------------------------------------------------
Then we supply certain options to Clang:
------------------------------------------------------------------------
$ (cat rts.c && lonely < douady.hs) > douady.c
$ alias wcc=clang -O3 -c --target=wasm32 -Wall
$ alias wld=wasm-ld-8 --export-dynamic --allow-undefined --no-entry
$ wcc douady.c std.c
$ wld --initial-memory=33554432 douady.o std.o -o douady.wasm
------------------------------------------------------------------------