WebKitKai Tamkun7 min readintermediate
Fixing Top-Level Await in Safari
Summary
Safari’s module loader was rewritten in C++ to follow the ECMAScript spec, fixing ordering and uninitialized‑export bugs that broke top‑level await. The new loader passes test262, WPT, and a custom fuzzer, and restores reliable module loading performance.
- Old loader was a self‑hosted JavaScript builtin based on a dead WHATWG proposal; it couldn’t correctly handle async module execution.
- Bug manifested as out‑of‑order dynamic imports and "Cannot access … before initialization" errors when using top‑level await.
- Rewrite replaced the JS builtin with native C++ code, implementing spec algorithms directly and using a flow‑graph to order implementation.
- Testing leveraged Bun’s real‑world cases, a generated module‑graph fuzzer, and full test262/WPT suites to verify correctness and performance.
Top‑level await is now part of the JavaScript spec and widely used for async initialization (e.g., loading config, auth, or WASM). Reliable support in Safari removes a long‑standing compatibility gap, letting developers use the same module patterns across all browsers without work‑arounds.
8/10



