Skip to content

Instantly share code, notes, and snippets.

Simple WasmBoxC Library Example

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:

Wasm host GC integration demo

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

  • Not shipping a GC.
  • Not needing to manage a shadow stack in userspace.
  • Other host GC benefits, like existing optimzations there, integration with system memory pressure events, etc.
--- 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)
@kripken
kripken / a.js
Created June 17, 2025 20:04
HEAP8 benchmark in JS and Python
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>
@kripken
kripken / init.cpp
Created May 12, 2025 18:03
C++ global object init priority
#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

Emscripten as a linker for Zig and C

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:

  • Asyncify
  • Full GLES3 support
  • GLFW3 support
@kripken
kripken / hello_world.c
Last active March 19, 2025 06:14
Standalone WebAssembly Example
int doubler(int x) {
return 2 * x;
}
@kripken
kripken / bench.cpp
Last active January 15, 2025 22:18
Parallel Benchmarks
// 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>