WebAssembly image dithering example

Allen Garvey allen.garvey at gmail.com
Fri Aug 3 19:47:14 UTC 2018


On Friday, 3 August 2018 at 19:01:02 UTC, kinke wrote:
> I won't be able to run your full example (I'll keep my personal 
> machine Node.js-free! ;)), so I just used `ldc2 
> -mtriple=wasm32-unknown-unknown-wasm -betterC -O main.wasm` 
> with current LDC master (and added the missing __assert stub).
> The produced .wasm is 620 bytes small (without -O: 3,235 
> bytes); loading it in Firefox shows that the functions are 
> there, here's an excerpt:
>
>   (func $fillBayerMatrix (;3;) (param $var0 i32) (param $var1 
> i32)
>     get_local $var1
>     i64.const 4469670136257392600
>     i64.store align=4
>     get_local $var1
>     i32.const 8
>     i32.add
>     i64.const -4753701902744867000
>     i64.store align=4
>   )
>   (func $dither (;5;) (param $var0 i32) (param $var1 i32) 
> (param $var2 i32) (param $var3 i32)
>     get_local $var2
>     i64.const 4469670136257392600
>     i64.store align=4
>     get_local $var2
>     i32.const 8
>     i32.add
>     i64.const -4753701902744867000
>     i64.store align=4
>     block $label0
>       get_local $var0
>       get_local $var1
>       i32.mul
>       i32.const 2
>       i32.shl
>       i32.const 1
>       i32.ge_s
>       br_if $label0
>       return
>     end $label0
>     unreachable
>     unreachable
>   )

Thanks for taking the time to look in to this for me. I think on 
a slightly earlier version of my code, when compiling with the 
optimizations the output was around 350 bytes and the error was 
something about the dither function not being able to be found. 
Compiling it now (still using beta 2) with the -O flag and the 
assert stub I get a similar 618 byte output to you, but when I 
try to run it, the browser gives this error: Uncaught 
RuntimeError: unreachable. The wasm output for the dither 
function is this

(func $dither (export "dither") (type $t4) (param $p0 i32) (param 
$p1 i32) (param $p2 i32) (param $p3 i32)
     (i64.store align=4
       (get_local $p2)
       (i64.const 4469670136257392629))
     (i64.store align=4
       (i32.add
         (get_local $p2)
         (i32.const 8))
       (i64.const -4753701902744866827))
     (block $B0
       (br_if $B0
         (i32.ge_s
           (i32.shl
             (i32.mul
               (get_local $p0)
               (get_local $p1))
             (i32.const 2))
           (i32.const 1)))
       (return))
     (unreachable)
     (unreachable))

and the browser says the error is on the first unreachable 
declaration at the end of the function.

Also, if you have either python 2 or python 3 on your system, you 
can run a server by cd-ing into the docs directory of the project 
and running `python -m SimpleHTTPServer 3000` (python 2) or 
`python3 -m http.server 3000` (python 3). They both do the same 
thing as the npm script, which is to serve the site on 
localhost:3000.

I will try seeing if I can build the current LDC master from 
source to test with that, but I have had mixed success with 
building projects from source in the past.




More information about the digitalmars-d-ldc mailing list