|
| 1 | +# BigInt WebWorker Serialization Fix |
| 2 | + |
| 3 | +This document explains the vendored zarith runtime and why it's needed. |
| 4 | + |
| 5 | +**Vendored file:** `vendor/zarith_native_bigint_runtime.js` |
| 6 | +**Source:** `zarith_stubs_js` v0.17.0 |
| 7 | +**Purpose:** Fixes BigInt serialization through WebWorker `postMessage` |
| 8 | + |
| 9 | +### The Problem |
| 10 | + |
| 11 | +Hazel uses a WebWorker for evaluation to avoid blocking the UI. Data (including |
| 12 | +the AST with BigInt values) is sent to/from the worker via `postMessage`, which |
| 13 | +uses the browser's [structured clone algorithm](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm). |
| 14 | + |
| 15 | +The `zarith_stubs_js` package (v0.16.x) that provides BigInt support for |
| 16 | +js_of_ocaml uses the [BigInteger.js](https://github.com/peterolson/BigInteger.js) |
| 17 | +library internally. BigInteger.js creates custom JavaScript objects with |
| 18 | +prototype methods like `.lt()`, `.add()`, etc. When these objects pass through |
| 19 | +structured clone, **the prototype chain is lost** - the data survives but the |
| 20 | +methods don't. This caused crashes when displaying evaluation results containing |
| 21 | +large integers. |
| 22 | + |
| 23 | +### The Solution |
| 24 | + |
| 25 | +Starting in v0.17.0, `zarith_stubs_js` switched to using **native JavaScript |
| 26 | +`BigInt`** instead of BigInteger.js. Native `BigInt` is a primitive type that |
| 27 | +fully survives structured clone. |
| 28 | + |
| 29 | +However, we cannot simply upgrade to zarith_stubs_js v0.17.0 because it requires |
| 30 | +upgrading the Jane Street packages. At the time of this fix (January 2026), |
| 31 | +bonsai v0.17.0 on opam constrains `js_of_ocaml < 5.7.0`, and versions of |
| 32 | +`js_of_ocaml-compiler` before 5.7.0 require `ocaml < 5.2`. Note that bonsai's |
| 33 | +master branch on GitHub has updated to require `js_of_ocaml >= 6.0.0`, so a |
| 34 | +future bonsai release should resolve this constraint issue. |
| 35 | + |
| 36 | +### Our Approach |
| 37 | + |
| 38 | +We vendor the `runtime.js` from zarith_stubs_js v0.17.0 and copy it into the |
| 39 | +opam switch at build time, replacing the v0.16.x version. This gives us native |
| 40 | +BigInt support without upgrading the package. |
| 41 | + |
| 42 | +The `Makefile` has a `setup-zarith` target that performs this copy: |
| 43 | + |
| 44 | +```makefile |
| 45 | +setup-zarith: |
| 46 | + cp vendor/zarith_native_bigint_runtime.js "$$(opam var lib)/zarith_stubs_js/runtime.js" |
| 47 | +``` |
| 48 | + |
| 49 | +This target is automatically run before builds (`make dev`, `make release`, etc.) |
| 50 | +and in CI. |
| 51 | + |
| 52 | +### Alternatives Considered |
| 53 | + |
| 54 | +1. **Full package upgrade** - Blocked by OCaml version / js_of_ocaml constraints |
| 55 | + as described above. |
| 56 | + |
| 57 | +2. **O(n) serialization shim** - Our previous approach: recursively walk the |
| 58 | + entire message payload before/after `postMessage`, converting BigInts to |
| 59 | + tagged strings (`{__hazel_bigint__: "123"}`) and back. This worked but added |
| 60 | + overhead proportional to message size (4 traversals per worker round-trip). |
| 61 | + |
| 62 | +### Future Considerations |
| 63 | + |
| 64 | +- **When a new bonsai release is published to opam** (with the js_of_ocaml >= 6.0.0 |
| 65 | + constraint from master), we should be able to remove this vendored file and do |
| 66 | + a proper upgrade. At that point: |
| 67 | + 1. Upgrade zarith_stubs_js to v0.17.0+ via opam |
| 68 | + 2. Remove `vendor/zarith_native_bigint_runtime.js` |
| 69 | + 3. Remove the `setup-zarith` target from the Makefile |
| 70 | + 4. Remove the `setup-zarith` dependencies from other Makefile targets |
| 71 | + |
| 72 | +- **If zarith_stubs_js changes its internal representation again**, we may need |
| 73 | + to update the vendored file. Check the [zarith_stubs_js releases](https://github.com/janestreet/zarith_stubs_js) |
| 74 | + for changes. |
| 75 | + |
| 76 | +- **The vendored runtime.js API is stable** - it implements the same `ml_z_*` |
| 77 | + functions as the original, just with native BigInt instead of BigInteger.js. |
| 78 | + There should be no compatibility issues. |
| 79 | + |
| 80 | +### Related Files |
| 81 | + |
| 82 | +- `vendor/zarith_native_bigint_runtime.js` - The vendored runtime file |
| 83 | +- `src/web/util/WorkerClient.re` - Client-side worker communication |
| 84 | +- `src/web/util/WorkerServer.re` - Worker-side message handling |
| 85 | +- `Makefile` - `setup-zarith` target |
| 86 | +- `.github/workflows/deploy_branches.yml` - CI setup-zarith step |
0 commit comments