A little help for programmers, who wants to run C/C++ code in the browser.<p>(This is my second attempt to show it, first time I got banned bcoz of my personal page domain, I don't really understand it why is is suspicious.)
This is really cool! I never looked into WASM before, I'm really happy to see it's not that difficult to build a simple example like this.<p>One thing that's bothering me about that code is the declaration of `memory` as an uint8_t when it's clearly being used for its address:<p><pre><code> extern uint8_t memory;
</code></pre>
And then in a <i>lot</i> of places:<p><pre><code> uint8_t* ptr = &memory;
ptr[FOO] = BAR;
</code></pre>
Declaring `memory` as an array is much more idiomatic C, and generates the exact same assembly:<p><pre><code> extern uint8_t memory[];
</code></pre>
And then<p><pre><code> memory[FOO] = BAR;
</code></pre>
I just tested it, and it seems to work exactly as one would expect from a plain C linker (so there's no WASM magic I can see). Am I missing something?
If anyone is curious, I wrote a guide on Chip-8 emulation in Rust, and the final step was building the emulator to work in a browser via WASM. I do use a Cargo package to assist with targets and compilation, but other than that it allows you to build and load a wasm module into any old website.<p><a href="https://github.com/aquova/chip8-book/blob/master/src/wasm.md" rel="nofollow">https://github.com/aquova/chip8-book/blob/master/src/wasm.md</a>
Very cool!<p>I've been desperately looking for something similar for Rust. I'm too old for all these magic frameworks and "deployers" :-)<p>I have some Rust code, possibly linking in some objects compiled from C. How do I compile to wasm and deploy without magic? Any pointers?
This is really awesome, good work!<p>We did a similar thing running Clang on the browser, which is now uploaded to WAPM: <a href="https://wapm.io/syrusakbary/clang" rel="nofollow">https://wapm.io/syrusakbary/clang</a>
directly when reading this code I cannot help myself but to get into review-mode. It is an awesome example, which i will probably use myself one day.<p>However...<p>inc.cpp:27<p>Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.
Meta comment: your committer email is not in sync with your profile so the Github UI shows the commits attached to a blank user. If you want these two in sync you can either add your committing email to your Github account or you can modify your git config to use your Github email as your commit email.<p>Or you can just leave it as is too. :)
Not trying to steal your thunder, but here is another nostdlib clang -> wasm example with malloc, a few math functions, rand, and writing to a canvas doing animation.<p>=> <a href="https://github.com/robrohan/wefx" rel="nofollow">https://github.com/robrohan/wefx</a>
This looks good! In my experience the hardest part for compiling WebAssembly is third party dependencies. Getting arbitrary libraries to build can be rather challenging.
See also my example I put together some time ago: <a href="https://github.com/PetterS/clang-wasm" rel="nofollow">https://github.com/PetterS/clang-wasm</a><p>At the time, I could not find something like this online.<p>It's fun to write your own malloc! :-)
Since I haven't seen it mentioned in the comments yet, here's another interesting project in the general area of "WASM without Emscripten":<p><a href="https://github.com/schellingb/wajic" rel="nofollow">https://github.com/schellingb/wajic</a><p>This provides an alternative implementation of Emscripten's EM_JS() magic (embed Javascript snippets right in the C/C++ source code), but without requiring a full Emscripten SDK installation. It still needs some additional tools next to Clang though, so it sits somewhere between "pure Clang" and "full Emscripten SDK".
(Obligatory:) Neat! Still, a lot of copying bits around, and more JS than one might hope for. Could you send a pointer to canvas into the C code and operate on it in a direct, zero-copy way there?
These are also good resources on using wasm without dependencies:<p><a href="https://depth-first.com/articles/2019/10/16/compiling-c-to-webassembly-and-running-it-without-emscripten/" rel="nofollow">https://depth-first.com/articles/2019/10/16/compiling-c-to-w...</a><p><a href="https://github.com/diekmann/wasm-fizzbuzz" rel="nofollow">https://github.com/diekmann/wasm-fizzbuzz</a>
That is half of what I would need for a project, the other half being Clang itself running in the browser (to use for teaching). In theory there is [1] since many years, but in practice it never worked for me (even now I get "Runtime error: memory access out of bounds").<p>[1] <a href="https://tbfleming.github.io/cib/" rel="nofollow">https://tbfleming.github.io/cib/</a>
It seems your first attempt to post this was flagged because the domain name sounds like it'll act like a redirect, and the page itself only had an image with no elaboration and just the link to github.