(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.)
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:
extern uint8_t memory;
And then in a lot of places: uint8_t* ptr = &memory;
ptr[FOO] = BAR;
Declaring `memory` as an array is much more idiomatic C, and generates the exact same assembly: extern uint8_t memory[];
And then memory[FOO] = BAR;
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?Thank you to the author though! It is a nice little example.
https://github.com/aquova/chip8-book/blob/master/src/wasm.md
I've been desperately looking for something similar for Rust. I'm too old for all these magic frameworks and "deployers" :-)
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?
It's focused on emulation development, but I wrote a document that describes the process I followed: https://github.com/aquova/chip8-book/blob/master/src/wasm.md
We did a similar thing running Clang on the browser, which is now uploaded to WAPM: https://wapm.io/syrusakbary/clang
This talk from Ben Smith goes into a bit more depth on what was required to change on LLVM to compile to WASI: https://www.youtube.com/watch?v=5N4b-rU-OAA
However...
inc.cpp:27
Gray is not the average of RGB, it is usally approximated with `Y = 0.299 R + 0.587 G + 0.114 B`.
https://github.com/ern0/howto-wasm-minimal/blob/master/inc.c...
It should be:
uint32_t gray = 0.299 * ptr[i] + 0.587 * ptr[i+1] + 0.114 * ptr[i+2];
ptr[i] = gray;
ptr[i+1] = gray;
ptr[i+2] = gray;Or you can just leave it as is too. :)
I have had more headache with my not-too-but-old-enough Linux systems, 2 of 2 failed: one got stuck when linking (yes, the linker hangs!), another one produced perfect size .wasm output, with... wait for it... full of zero bytes. Full story here: https://stackoverflow.com/questions/71573019/cant-compile-to...
At the time, I could not find something like this online.
It's fun to write your own malloc! :-)
https://github.com/schellingb/wajic
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".
https://stackoverflow.com/questions/42806037/modify-canvas-f...
https://depth-first.com/articles/2019/10/16/compiling-c-to-w...
It likely isn't suited for compiling big c programs, but I think one can get far with preparing a few shared libraries and Tiny C Compiler or similar.
[1] https://github.com/copy/v86
[2] https://copy.sh/v86/?profile=buildroot
[3] https://github.com/copy/v86/blob/master/examples/lua.html
I don't know if it compiles in the browser or on a server.
Anyway, you can use C++ as C, using just as many C++ features as you need. (Insert "throwing the guy out of window" meme picture here.)