Live data from Hacker News

Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

github.com

31–40 of 44 posts

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#32
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.

https://github.com/aquova/chip8-book/blob/master/src/wasm.md

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#34

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"). [1] https://tbfleming.github.io/cib/

This was a topic at CppCon'19 (seems like exactly the same use case as you are describing: teaching!). Take a look at https://www.youtube.com/watch?v=5N4b-rU-OAA. I think it relies on the LLVM fork, so not sure how well it'll work for you, quick web search points to: https://github.com/binji/llvm-project.

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#35
post #29

(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?

Unfortunately, you cannot.

https://stackoverflow.com/questions/42806037/modify-canvas-f...

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#36
post #27

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. 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`.

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 :)

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#37
post #23

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. 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 idiomati…

[deleted]

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#38
Since I haven't seen it mentioned in the comments yet, here's another interesting project in the general area of "WASM without Emscripten":

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".

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#39

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"). [1] https://tbfleming.github.io/cib/

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.

[1] https://github.com/copy/v86

[2] https://copy.sh/v86/?profile=buildroot

[3] https://github.com/copy/v86/blob/master/examples/lua.html

Re: Show HN: How to compile C/C++ for WASM, pure Clang, no libs, no framework

#40

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. 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`.

For future readers, this is the code that is being referred: 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;

I know it (see: https://github.com/ern0/kolorwheel.js ), but I did not wanted use floats.
Post reply on HN