7 ms·
Show HN: Compiling C in the browser using WebAssembly
- Retr0id 2y ago> note: it requires a 100MB download Is this how big a clang toolchain usually is?
- syrusakbary 2y agoThe Clang WASI SDK weights about 100Mb compressed. We optimized things a bit but still have a way to go (we are not yet compressing in the network). I believe we can serve everything in about 30Mb
- 01HNNWZ0MV43FF 2y agoMB, right? Mb is megabit I only have to bring this up because network providers still insist on measuring bits
- kevin_thibedeau 2y agoThey insist on it because it is the proper way to measure data rates on serial bit streams where out-of-band encoding doesn't divide up on octet boundaries.
- msla 2y agoThey insist on it because big number sell better.
- 01HNNWZ0MV43FF 2y agoThe bits do me no good May as well count the 8/10 encoding in a CD-ROM as extra bits. Oh excuse me, extra megabytes
- corysama 2y agoIs 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?
- fwip 2y agoDefinitely been possible for at least 5 years now. Would probably be a weekend project now.
- legobmw99 2y agoSee: emception https://github.com/jprendes/emception https://github.com/jprendes/emception
- nunobrito 2y agoThanks, I'm seeing but the documentation is so scarce and I'm not a proefficient C expert. What syntax can be used to run emception? Thank you.
- legobmw99 2y agoIt’s sadly a bit more of a proof of concept than a hackable project. The docker build in the readme did work last time I tried, and there is a demo site at https://jprendes.github.io/emception/ https://jprendes.github.io/emception/, but I’ve failed to modify it in the past to do other things There is a fork at https://github.com/emception/emception https://github.com/emception/emception that is trying to make it more production ready, but it looks like that may have stalled
- nunobrito 2y agoI guess the search for compiling C++ on some kind of bytecode continues. Thanks a bunch for the links and details, much appreciated.
- halb 2y agoAbsolutely! You can even run clang in wasm targeting x86_64, and then emulate the resulting program using the blink x86_64 emulator. I'm working on something similar, where students can compile intel assembly and run it client-side: https://github.com/robalb/x86-64-playground https://github.com/robalb/x86-64-playground
- marmaduke 2y agoCouldn’t a tcc or similarly simple C compiler be used instead of a 100MB Clang? Where’s the C to wasm compiler hiding?
- kachapopopow 2y agoclang can target wasm already.
- syrusakbary 2y agoThe whole LLVM toolchain is a bit big. I think we can reduce much more the size. We actually researched on using tcc but unfortunately tcc doesn’t have a wasm backend (for generating wasm output). It would be awesome if they added it!
- fuhsnn 2y agoCheck out https://github.com/tyfkda/xcc https://github.com/tyfkda/xcc, I've only used the native backend, but it's small and fast.
- syrusakbary 2y agoNice! I didn’t know the project. Thanks for sharing!
- mdhb 2y agoThis project is also very much worth checking out. https://cranelift.dev/ https://cranelift.dev/ From the page: Cranelift is a fast, secure, relatively simple and innovative compiler backend. It takes an intermediate representation of a program generated by some frontend and compiles it to executable machine code. Cranelift is meant to be used as a library within an "embedder". It is in successful use by the Wasmtime WebAssembly virtual machine, for just-in-time (JIT) and ahead-of-time (AOT) compilation, and also as an experimental backend for the Rust compiler. Cranelift is an optimizing compiler, but it aims to take a fresh look at which optimizations are necessary. We have explicitly avoided features -- such as advanced alias analysis or use of undefined behavior -- that have historically led to subtle miscompilations in other compilers. Cranelift consists of about 200 thousand lines of code; in contrast, e.g. LLVM consists of over 20 million lines of code, a hundred times larger. This difference also allows Cranelift to be relatively approachable to developers, researchers, auditors and others who wish to understand how it works.
- CyberDildonics 2y ago100MB on every page refresh just to compile C is a pretty bold direction to go in.
- legobmw99 2y agoIf 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)
- Muromec 2y agoImporting a wasm module from a wasm module is (non)surprisingly impossible to do -- you have to have a linker, abi and all that.
- okwhateverdude 2y agoIt is possible provided some care. I was looking into this with WAForth which compiles the wasm and loads it via a host function (ie. it is the hosts responsibility to make it available). I wanted to enable dynamic loading of words from disk which requires some book keeping and shuffling a bunch of bytes around during compilation to write out the bits necessary to have the host do that linking. It isn't impossible to do, just tedious and in my case, having to write it in WAT is a pain.
- Muromec 2y agoYep, you need to do the nasty bits by hand, that's what I mean.
- whytevuhuni 2y agoNow 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.
- dangerwill 2y agoVery interesting idea but I have to say that those goals are not possible with a simple OS, at least by OS definitions of simple :P
- whytevuhuni 2y agoThe old https://webassembly.sh/ https://webassembly.sh/ and the new https://wasmer.sh/ https://wasmer.sh/ came a long way already. All you need is a virtual filesystem of some sort, a way to download, a way to upload, an editor, a compiler, and a VT100 JS library. We already have WASI for the rest. If the JS is too undesired, then perhaps go the old framebuffer graphics mode (e.g. a region of the WASM memory that is interpreted as an ASCII screen, or maybe even as a full bitmap buffer). Then JavaScript side just needs to forward keyboard/mouse into memory and that screen region out of memory.
- Arshia001 2y agoThe framebuffer idea is used in this wasm doom port: https://github.com/diekmann/wasm-fizzbuzz/tree/main/doom https://github.com/diekmann/wasm-fizzbuzz/tree/main/doom WASIX already does all the other stuff you mentioned, including in the browser. The one thing it's missing is GUI, mainly because there's no standard GUI interface in POSIX.
- baudaux 2y agoIt is possible. I already embedded chibicc in exaequOS. I will continue with xcc and clang
- jauntywundrkind 2y agoAnd then use webrtc (or ideally someone can revive a webtransport-p2p please!) to serve itself from a page to other people. Ideally http3 over webtransport-p2p! Then add some network discovery so we can advertise & find what's available on our networks!
- egnehots 2y agoIt'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 :/
- bilekas 2y ago> Skipping any discussion of performance feels like sweeping it under the marketing rug Expecting performance while compiling C in the browser feels redundant right now though.
- bilekas 2y ago"Yeah, yeah, but your scientists were so preoccupied with whether or not they could that they didn't stop to think if they should." ....
- _ache_ 2y ago"We do what we must Because we can"
- zh2408 2y agoWhat's the use case?
- kylewlacy 2y agoVery 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-webassembly/79073 https://discourse.llvm.org/t/rfc-building-llvm-for-webassemb... [2]: https://github.com/WebAssembly/WASI/issues/414 https://github.com/WebAssembly/WASI/issues/414
- westurner 2y agoCling (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 https://github.com/jupyter-xeus/xeus-cling There's not yet a JupyterLite (WASM) kernel for C or C++.
- zengid 2y agoDidn't Gary Bernhardt do this in 2014? /sarcasm
- zengid 2y agoreference https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript https://www.destroyallsoftware.com/talks/the-birth-and-death...
- apignotti 2y agoGCC? That's easy! :-) What about a complete system? https://webvm.io 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 https://cheerpx.io/hackathon
- baudaux 2y agoCan I compete with exaequOS?
- apignotti 2y agoNo, the competition is explicitly based around CheerpX: an X86 virtualization technology built on top of WebAssembly
- jedisct1 2y agoWhat about something that doesn't even require WebAssembly and is faster? https://bellard.org/jslinux/ https://bellard.org/jslinux/
- yuri91 2y agovery unscientific benchmark of `clang hello.c`, after a few runs to make sure the code is downloaded/cached: jslinux: 4.7s wasmer: 1.3s webvm: 1.2s
- syrusakbary 2y agoNice, thanks for the benchmark Yuri!
- yuri91 2y agoIt is a bit unfair to Wasmer, because it incur in the (presumed) overhead of `wasmer run ...`, but I could not figure out if the actual clang binary is directly available after it is downloaded the first time.
- ancientstraits 2y agoA 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.
- ttflee 2y agoEvery few years, new progresses might remind me of this talk by Gary Bernhardt: https://www.destroyallsoftware.com/talks/the-birth-and-death-of-javascript https://www.destroyallsoftware.com/talks/the-birth-and-death...
- pjmlp 2y agoYeah, mostly because WebAssembly is the new kid in bytecode town.
- pjmlp 2y agoNot 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
- mati365 2y agoYou can compile C using JavaScript and target DOS if you are hard core enough. https://github.com/Mati365/ts-c-compiler https://github.com/Mati365/ts-c-compiler
- deleted 2y ago[deleted]