Show HN: Compiling C in the browser using WebAssembly

by syrusakbaryon 10/7/2024, 4:24 PMwith 71 comments

by marmadukeon 10/7/2024, 5:41 PM

Couldn’t a tcc or similarly simple C compiler be used instead of a 100MB Clang? Where’s the C to wasm compiler hiding?

by CyberDildonicson 10/7/2024, 6:41 PM

100MB on every page refresh just to compile C is a pretty bold direction to go in.

by kylewlacyon 10/7/2024, 7:36 PM

Very cool! I've been watching the "toolchains in Wasm" landscape for a while, and seeing a Clang/LLVM toolchain running in Wasm is awesome!

YoWASP has also had an LLVM toolchain working in Wasm for a while too[1], although it seems like this version solves the subprocess problem by providing an implementation of `posix_spawn` whereas the YoWASP one uses some patches to avoid subprocesses altogether

My biggest question marks around this version are about runtime/platform support. As I understand it, this toolchain uses WASIX, which (AFAICT) works with Wasmer's own runtime and with a browser shim, but with none of the other runtimes. Are there plans to get WASIX more widely adopted across more runtimes, or to get WASIX caught up to the latest WASI standard (preview2)? Or maybe even better, bring the missing features from WASIX to mainline WASI like `posix_spawn`[2]? I'd love to be able to adopt this toolchain, but it doesn't seem like WASIX support has really caught on across the other runtimes

[1]: https://discourse.llvm.org/t/rfc-building-llvm-for-webassemb... [2]: https://github.com/WebAssembly/WASI/issues/414

by apignottion 10/7/2024, 9:13 PM

GCC? That's easy! :-) What about a complete system? https://webvm.io

Shameless plug: we are hosting a WebVM Hackathon next week (11-14 October) over Discord. For more information: https://cheerpx.io/hackathon

by westurneron 10/7/2024, 8:08 PM

Cling (the interactive C++ interpreter) should also compile to WASM.

There's a xeus-cling Jupyter kernel, which supports interactive C++ in notebooks: https://github.com/jupyter-xeus/xeus-cling

There's not yet a JupyterLite (WASM) kernel for C or C++.

by egnehotson 10/7/2024, 7:16 PM

It's pretty misleading not to mention the performance overhead. That's an obvious downside and quite easy to benchmark. Skipping any discussion of performance feels like sweeping it under the marketing rug :/

by ancientstraitson 10/8/2024, 1:04 AM

A few weeks ago, I tried to compile Clang to WebAssembly, but got several different errors, and tried fixing a lot of them, but some of them seemed kind of impossible to fix, so I thought I would try again at a later date. However it seems I will not need to try again. I feel angry that someone made a convenient solution before I did, but also happy, because this probably implies that they made a consistent process to compile Clang for WASM.

by zengidon 10/7/2024, 8:33 PM

Didn't Gary Bernhardt do this in 2014? /sarcasm

by corysamaon 10/7/2024, 5:13 PM

Is it possible/already existing to have interactive C++ lessons where the user's C++ code is compiled an run client-side in a web page?

by mati365on 10/8/2024, 9:08 AM

You can compile C using JavaScript and target DOS if you are hard core enough. https://github.com/Mati365/ts-c-compiler

by legobmw99on 10/7/2024, 6:44 PM

If what I want is not an executable but a shared library, does this get me anything?

I currently have a use case that uses a server running an emscripten build (using SMODULARIZE and some exports, I suppose it’s not a true dylib)

by pjmlpon 10/8/2024, 7:44 AM

Not really, on Firefox

    panicked at /Users/syrusakbary/Development/wasmer/lib/api/src/js/instance.rs:62:84:
    called `Result::unwrap()` on an `Err` value: JsValue(Function(bound 846))

    Stack:

    fe/_.wbg.__wbg_new_abda76e883ba8a5f@https://wasmer.sh/assets/index-CgFg6VHw.js:17:6582
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[1125]:0x2b4276
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[2888]:0x3ab373
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[8254]:0x435ed3
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[4825]:0x3fa7de
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[517]:0x1af753
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[294]:0xbed03
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[2039]:0x34b10e
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[2896]:0x3abaa1
    @https://wasmer.sh/assets/wasmer_js_bg-BruS15W0.wasm:wasm-function[9393]:0x43fde1
    S@https://wasmer.sh/assets/index-CgFg6VHw.js:11:424
    c@https://wasmer.sh/assets/index-CgFg6VHw.js:11:264

by Retr0idon 10/7/2024, 5:12 PM

> note: it requires a 100MB download

Is this how big a clang toolchain usually is?

by zh2408on 10/7/2024, 7:31 PM

What's the use case?

by ttfleeon 10/8/2024, 2:27 AM

Every few years, new progresses might remind me of this talk by Gary Bernhardt:

https://www.destroyallsoftware.com/talks/the-birth-and-death...

by whytevuhunion 10/7/2024, 6:56 PM

Now all this needs is a simple OS running in a browser, that can edit and compile itself, post the resulting binary onto a WebDAV somewhere, and reload itself from there.

Then it becomes a fully self-sustaining OS that can live forever in a browser.

by bilekason 10/7/2024, 7:22 PM

"Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should." ....