WebAssembly image dithering example

Allen Garvey allen.garvey at gmail.com
Thu Aug 2 22:04:50 UTC 2018


Hi all. Using the WebAssembly tutorial from a few week ago, I 
created a small example to dither an image. You can see the 
source at https://github.com/allen-garvey/wasm-dither-example and 
the demo at https://allen-garvey.github.io/wasm-dither-example/. 
I had heard about WebAssembly before this, but this was my first 
experience actually using it, so it was an interesting project 
for me, and overall a positive experience.

I also wanted to share some of the challenges I overcame for the 
benefits of others and I would be grateful if someone more 
knowledgeable than me can point out if these are the results of 
my own stupidity, or if they are due to gaps in LDC 
documentation/implementation or the limits of WebAssembly in 
general.

(For reference, I was compiling on Kubuntu 18.04 with LDC 1.11.0 
beta2)

1. I had to comment out `"-L--no-warn-search-mismatch"` in LDC's 
`/etc/ldc2.conf` or I would get this error: -d: error: unknown 
argument: --no-warn-search-mismatch

2. I had to use the disable array bounds checking flag when using 
arrays or I would get this error: undefined symbol: __assert

3. I was confused for a while when trying to use global arrays or 
stack allocated arrays since I was getting pointers to garbage. 
To be fair to LDC, I'm not sure if WebAssembly actually supports 
doing this, as from my research I'm starting to think all arrays 
might have to be heap allocated, but it would be nice to get some 
sort of error or warning if you tried doing this. Also to be 
fair, array bounds checking was turned off at this point, which 
might have thrown an exception to let me know something was wrong.

4. I was not able to use any of the optimization flags except for 
enable inlining, since using any of them would optimize out the 
entire program. I'm assuming this is because there is no main 
function, so the compiler can't tell which functions are actually 
being called. I tried using the `export` or `public` keywords, 
but that didn't seem to make a difference. Is there some keyword 
or syntax I'm missing, or is that just a limitation of LDC?

5. When trying to import the std library for the cbrt and max and 
min function, I got various errors about undefined identifier and 
such. This wasn't such a big deal in this instance, as the cbrt 
calculation was a compile time constant, and writing functions to 
find the min and max values of 3 numbers is not difficult.


More information about the digitalmars-d-ldc mailing list