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:
| import json | |
| import re | |
| import sys | |
| def tokenize(s_expr): | |
| """ | |
| Extracts tokens from an S-expression string. | |
| Matches: | |
| 1. Strings enclosed in double quotes (handling escaped characters) | |
| 2. Parentheses '(' and ')' |
| (module | |
| (rec | |
| (type $0 (sub (struct))) | |
| (type $2 (sub $0 (struct))) | |
| (type $1 (sub $0 (struct))) | |
| (type $4 (sub final $0 (struct))) | |
| (type $5 (sub final $2 (struct))) | |
| (type $6 (sub final $0 (struct))) | |
| (type $7 (sub final $1 (struct))) | |
| (type $8 (sub final $1 (struct))) |
| ;; $ bin/wasm-opt a.wat -all --closed-world --signature-pruning --roundtrip | |
| ;; [parse exception: incompatible continuation types in cont.bind: source type (type $cont.0 (cont $func.0)) has fewer parameters than target (type $cont.0 (cont $func.0)) (at 0:75)] | |
| ;; Fatal: error in parsing wasm binary | |
| (module | |
| (rec | |
| (type $0 (sub (struct))) | |
| (type $1 (func (param (ref null $0)) (result (ref null $0)))) | |
| (type $2 (func (param (ref null $0) (ref null $0) (ref null $0)) (result (ref null $0)))) | |
| (type $3 (cont $1)) |
| # 1 "//Dev/emscripten/test/other/test_std_filesystem.cpp" | |
| # 1 "<built-in>" 1 | |
| # 1 "<built-in>" 3 | |
| # 445 "<built-in>" 3 | |
| # 1 "<command line>" 1 | |
| # 1 "<built-in>" 2 | |
| # 1 "//Dev/emscripten/test/other/test_std_filesystem.cpp" 2 | |
| # 1 "//Dev/emscripten/cache/sysroot/include/c++/v1/cstdint" 1 3 | |
| # 146 "//Dev/emscripten/cache/sysroot/include/c++/v1/cstdint" 3 | |
| # 1 "//Dev/emscripten/cache/sysroot/include/c++/v1/__config" 1 3 |
| def matrix_math(matrix, offset, vector): | |
| ''' | |
| Computes | |
| matrix*vector + offset | |
| Matrix is mxn, vector ix 1xn, offset is 1xn | |
| ''' | |
| m = len(matrix) |
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) |