4 ms·
You can compile any C / C++ app down to wasm. In fact that’s the raison d’être of the technology: to provide a portable safe way to run binaries. Here is a link
by batmansmk 4y ago
You can compile any C / C++ app down to wasm. In fact that’s the raison d’être of the technology: to provide a portable safe way to run binaries. Here is a link to Postgres in wasm for instance: https://supabase.com/blog/postgres-wasm https://supabase.com/blog/postgres-wasm.
The way it works is that instead of outputting assembly for a given architecture in the backend compiler, it outputs wasm instructions that are designed to map all architectures, not like the jvm which is designed primarily for Java in the front end.
- mytherin 4y agoC/C++ code can certainly be compiled down to WASM, but you cannot interface with the operating system as you would in a normal C/C++ program. To get around that restriction postgres-wasm ships an entire Linux distribution that is run inside the browser. This comes with an immense performance penalty. To get an impression of the performance penalty, just run the following query: SELECT SUM(i) FROM generate_series(0, 1000000, 1) tbl(i); This simple query completes in 100ms locally on my laptop, but takes 17265ms in postgres-wasm. That is a slowdown of 170x. Now that is not WASM's fault - when running the same query in duckdb-wasm [1] on my laptop the query takes 10ms using WASM, and 5ms when run locally, with a slow-down of only a factor of 2. But in order to achieve those results we did have to adapt the DuckDB codebase to compile natively to WASM. That is absolutely possible but it does take engineering effort - particularly when it comes to larger older projects that are not designed from the ground up with this in mind. [1] https://shell.duckdb.org https://shell.duckdb.org
- riddleronroof 4y agoThank you for these details. Always suspected these claims but hadn’t dug deep enough. Seems like some X can now run in wasm should come with disclaimer (includes Linux)
- pjmlp 4y agoHere, have your C and C++ compiler for JVM bytecode from 2006. http://nestedvm.ibex.org/ http://nestedvm.ibex.org/
- kenjackson 4y agoBut don’t I have to compile all my dependencies too? This seems like a lot to ask, but maybe it’s common in some domains.
- cle 4y ago> You can compile any C / C++ app down to wasm. This is incorrect, there is a long list of limitations that your C/C++ code must conform to in order to compile to WASM. There's a whole section dedicated to this in the Emscripten docs: https://emscripten.org/docs/porting/index.html https://emscripten.org/docs/porting/index.html. The chances your existing C/C++ app will compile to WASM and run correctly are much smaller than with Docker. However, the chances your WASM-compiled code will be able to run in a browser are much higher than with Docker (which is the real "killer use case" IMO). > not like the jvm which is designed primarily for Java in the front end This is also wrong, JVM bytecode is explicitly designed to be polylingual and is the compilation target for many non-Java languages like Scala, Kotlin, and Clojure. WASM being a compilation target is not what makes it unique from the JVM.
- mike_hearn 4y agoIronically you actually can run LLVM bitcode on the JVM these days, including inside an optional sandbox. It can run "anything" in the unsandboxed mode (as long as it can compile with LLVM), because it allows native calls out to the OS. So the universal [J]VM vision has actually happened, it's just nobody really knows about it. LLVM bitcode isn't all that portable though, and of course, the binary will still be OS specific because C/C++ code relies on native APIs. The primary reason to do this is so the Graal JIT compiler can optimize native code and higher level dynamic script/bytecode together and remove interop overhead. However, you can theoretically run whole programs this way inside the Graal sandbox. If you do that you get an emulation of POSIX that is reimplemented on top of the Java standard library, so the code becomes portable, and in managed mode there's an additional party trick - the native C/C++ malloc is replaced with garbage collected allocations and memory accesses are bounds checked. So code run this way gets all the memory safety errors blocked automatically. This upgrade comes with two costs though, one is slower execution/more memory usage, and the other is you have to buy GraalVM EE. The community edition can run bitcode, but not in the sandboxed/managed mode. Oh and GraalVM can also run WASM. So you can have cake and eat it, everything running together via their 'polyglot' interop system.
- jjtheblunt 4y agohow does pthread_create compile down?
- azakai 4y agoIn Emscripten that uses a pthread implementation layer built on top of Web Workers + a shared wasm Memory. Basically memory is shared, and you have atomic instructions, and each thread of execution gets its own Web Worker. That has some limitations, but for the most part it works just like you would expect pthreads to.
- jjtheblunt 4y agowhat's the non-most part that differs?
- azakai 4y agoThe main thread is a little special on the Web since it can't block, which can cause issues (like pthread_create doesn't immediately create an available pthread). There are workarounds for most of those issues (like pre-allocating a threadpool), and many applications work well, but sometimes not out of the box. See https://emscripten.org/docs/porting/pthreads.html#special-considerations https://emscripten.org/docs/porting/pthreads.html#special-co...
- jjtheblunt 4y agothank you