This shows how to easily sandbox a real-world library using WasmBoxC. Specifically we sandbox the zlib compression library.
To follow along with this, you can clone this gist and then do the following commands.
First, get the emsdk:
This shows how to easily sandbox a real-world library using WasmBoxC. Specifically we sandbox the zlib compression library.
To follow along with this, you can clone this gist and then do the following commands.
First, get the emsdk:
This lets code compiled to linear memory wasm (like a C++ or Rust game engine) use the host GC instead of shipping its own, with the benefits of
| --- a 2025-10-08 13:06:30.544955211 -0700 | |
| +++ b 2025-10-08 13:06:48.673083467 -0700 | |
| @@ -1290924,24 +1290924,21 @@ | |
| (i32.const 36) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| (return |
| // | |
| // Branch hints benchmark: 30% faster with the right hint (LIKELY=0), | |
| // 20% slower with the wrong one (LIKELY=1), compared to no hint. | |
| // | |
| #include <iostream> | |
| #include <vector> | |
| #ifdef LIKELY | |
| #define hint(x) __builtin_expect((x), LIKELY) |
| var data = new ArrayBuffer(1024); | |
| HEAP8 = new Int8Array(data); | |
| HEAP32 = new Int32Array(data); | |
| for (var i = 0; i < 1024; i++) { | |
| HEAP8[i] = (i & 17); | |
| } | |
| for (var n = 0; n < 1024 * 500; n++) { |
| <script> | |
| var module = WebAssembly.instantiateStreaming(fetch("linked_list.wasm"), {}); | |
| </script> |
| #include <iostream> | |
| struct A { | |
| A() { std::cout << "A\n"; } | |
| }; | |
| struct B { | |
| B() { std::cout << "B\n"; } | |
| }; | |
| // Init priority adjustment so A is inialized later. | |
| A __attribute__ ((init_priority (250))) a; // 150 vs 250 flip the order |
This shows how to build a nontrivial program using Zig+Emscripten or C+Emscripten.
In both cases Emscripten is only used as a linker, that is the frontend is either zig or clang.
"Nontrivial" here means the program uses interesting Emscripten features:
| int doubler(int x) { | |
| return 2 * x; | |
| } |
| // Compile with e.g. | |
| // | |
| // ./emcc a.cpp -pthread -sPROXY_TO_PTHREAD -O3 -s INITIAL_MEMORY=1GB | |
| // | |
| #include <atomic> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <emscripten.h> | |
| #include <emscripten/threading.h> |