Pedantically, it might even be undefined behaviour (not sure, maybe a point of contention, similar to container_of macro). It's unlikely to cause problems, but the way you suggested is better (and can even be accompanied by a length field).
Thank you to the author though! It is a nice little example.
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.
I made some emulators in Rust as a learning project during the start of the pandemic, and ran into the exact same issue when I wanted to make a wasm version to run in a browser. Eventually, I was able to figure out how to do it, although I do use the 'wasm-pack' Cargo package to assist with it (I think you can get away without it if you're really motivated, you just need to set up the targets and other elements yourself). Basically you define some Rust API to expose whatever you need from your project, then that and the project get compiled into one .wasm binary and some (surprisingly readable) JavaScript "glue" gets generated which allows for easy inclusion into a web page. It works well for code in the std, but I've had issues with 3rd party packages.
Running Clang with WASI requires compiling LLVM to WASI first, which unfortunately currently requires a fork (because of the "hackyness" of it, as some syscalls are mocked or certain code is just skipped).
Also, you must work in linear colour space. In OP's case you would have to apply the inverse sRGB transform, do the calculation, then apply the forward sRGB transform. Best if you just work with linear colour end to end, if you have the memory to spare.
Well, it's certainly traditional to get that wrong. Technically nothing's wrong in graphics as long as you get some kind of image out of it, it's just different :)
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.
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.
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.
Thanks for warning! Anyway, I just got a new Mac, but I started with installing brew and then install CLANG comes with brew.
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...
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?
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").
I'm investigating v86 [1] (x86-compatible CPU and hardware emulator in JS) to get a c compiler in the browser. There's an example with a 5.5 mb Build Root Linux example [2] where you can pass files and commands between the browser and vm. The vm boots within a few seconds in Firefox on my Pixel 2 XL. There's also an example where Lua code is passed from the host, but you have to download the GitHub repo to see it [3]
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.
It’s not ready just yet but if you’re curious about Zig they’re going to have a nice self-hosted WebAssembly toolchain soon, with no LLVM dependency. I’m really looking forward to it and I’m happy to see the Zig compiler work making lots of progress these days. I hope I’ll be able to start playing with this in a couple of weeks or so.
I was quite excited about `zig cc (although I understand why they would want to throw LLVM away), but despite what the crab people shout all day I still need C++.
That's fast! But it looks like it still needs a server, and I'd rather avoid giving the internet arbitrary code execution rights to some server I pay for and risk having my bill explode or my account terminated for abuse.
I'm way more worried about ending up hosting miners or other malicious crap, Godbolt himself said users have found all sorts of ways to escape the compiler explorer sandbox over the years. I'd rather serve the compiler as a static blob that runs in the browser.
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.
Just rename the file and remove extern "C" stuff :). It started as C, but then I switched to CPP, to see, can I export simple symbol names, not mangled ones C++ produces.
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.)
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:
And then in a lot of places: Declaring `memory` as an array is much more idiomatic C, and generates the exact same assembly: And then 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?