worker-build --emscripten - #1061
Open
guybedford wants to merge 13 commits into
Open
Conversation
Build Workers for wasm32-unknown-emscripten through a worker-build provisioned toolchain: the pinned emsdk release is installed into the cache directory and the frontend patched with backports the Rust link needs (marker-based -sWASM_BINDGEN, NODERAWSOCKETS DNS). rustc drives emcc as the linker for a bin target, emcc runs wasm-bindgen post-link, and the output is collected into the standard build/index.js shape. The worker crate's start function becomes private, since its public init export collided with Emscripten's own.
The exnref translation failure on debug builds was wasm-bindgen 0.2.128's --keep-debug output (walrus 0.27.1 .debug_loc), fixed on main by #5328, not binaryen on the linked module. The CLI floor already covers it.
The worker crate no longer registers the abort reinit hook on wasm32-unknown-emscripten, so the released wasm-bindgen CLI builds --emscripten --release; only debuginfo builds still need #5328. Export and wasm import detection now handle emcc's minified release output.
The runtime serves the whole cloudflare: namespace (cloudflare:node, cloudflare:test, ...), not only the three that were listed, and a wasm-bindgen module import of one of them must reach the output as an import rather than fail to resolve in esbuild.
Backport the JSPI lifecycle hooks, REENTRANT_JSPI fiber stacks and epoll listener API to the 6.0.9 frontend, and take Binaryen from a release carrying the jspi-hooks pass (WebAssembly/binaryen#9102). The release sysroot stamp is dropped after patching so emcc installs the new headers.
wasm-bindgen/wasm-bindgen#5333 instruments jspi exports and suspending imports with emscripten's fiber hooks, so under -sREENTRANT_JSPI each fetch activation runs on its own stack. The example takes wasm-bindgen from the submodule until it is released.
Pass --cfg tokio_jspi_hooks so Tokio's emscripten port registers the JSPI lifecycle hooks, and move the example to that Tokio branch. Concurrent fetch activations each drive their own runtime.
The runtime exposes RPC on a Durable Object class only when it derives from cloudflare:workers' DurableObject; the wasm-bindgen classes are plain, so splice its prototype in.
On wasm32-unknown-emscripten the handler wrappers are #[wasm_bindgen(jspi)] exports suspending on the handler future, so each activation is its own fiber. The wrappers are left for rustc to expand: expanding both cfg variants in the proc macro deduplicated their export descriptors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
worker-build --emscripten, building Workers forwasm32-unknown-emscriptenso crates that need a libc, epoll sockets or blocking calls (stock Tokionet, etc.) run unmodified, parking through JSPI.worker-build/patches/emscripten/to the frontend: the marker-based-sWASM_BINDGENbackport (Unify wasm-bindgen output under -sWASM_BINDGEN with marker-based detection emscripten-core/emscripten#27208) and NODERAWSOCKETS DNS (Real DNS resolution for getaddrinfo under -sNODERAWSOCKETS emscripten-core/emscripten#27693). Each is dropped as the pin moves past it.EMSCRIPTEN/EMSDKenv vars override for local toolchain work.emccas the linker for a bin target (cdylib cannot link on emscripten); emcc runs wasm-bindgen post-link, and the output is collected into the standardbuild/index.js+ wasm shape, so wrangler config only changes the build command (plus thenew_module_registrycompat flag forimport.meta.url). Codegen and link flags go throughRUSTFLAGS, as--panic-unwinddoes.worker::initbecomes a private start function (its publicinitexport collided with Emscripten's own) and no longer registers the abort reinit hook on emscripten, where Emscripten owns instantiation.emscripten-tcpexample documents the[patch.crates-io]block.--releasebuilds work with the released wasm-bindgen CLI. Debuginfo builds need a CLI with wasm-bindgen/wasm-bindgen#5328 (DWARF that survives the exnref translation) until 0.2.129; the submodule is updated to include it along with wasm-bindgen/wasm-bindgen#5332.Tested: the
emscripten-tcpexample (workertypes +tokio::net::TcpStreamunder JSPI) builds with--devand--releaseand serves/?host=example.comunderwrangler dev; CI adds anEmscripten examplejob running both builds and that smoke test. Unit tests cover the patch applier.