Experiment: Build PHP with OPFS support #1030
Closed
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 is an attempt to use Emscripten to build a WASM module that uses OPFS as its filesystem.
To try it out locally, run:
Then, go to http://localhost:8000/ and open the developer tools.
OPFS backend for WASMFS causes crashes in Chrome
No matter what I did, I couldn't get OPFS to work in the browser.
It always crashes when creating a new file in the
/opfs
directory. This function call:Invokes the following asynchronous JavaScript implementation:
However, the
await
is deadly and crashes the WASM process:It's not about the
getFileHandle
call. It's about asynchronous code. The same function withawait sleep(100);
crashes in the exact same way.Potential solutions
I tried building:
-sASYNCIFY=1
and different ASYNCIFY settings and imports-sASYNCIFY=2
(after enabling JSPI at chrome://flags)-sWASM_WORKERS
Unfortunately, the final bundle always crashes in the same way.
Weirdly, it crashes on file creation but not on directory creation. The
mkdir
code path seems to somehow support stack switching.I don't have any futher ideas so I'll abandon these explorations for now and leave this PR for posterity.
Implementation details
This PR explores Emscripten's new WASMFS filesystem with OPFS backend.
main.c
contains the C code where the OPFS directory is created:It's then built into a WASM module via
build.sh
(orbuild-in-docker.sh
for convenience) and loaded in the browser by index.html.Building
Run
build-in-docker.sh
WASMFS Resources
There's no documentation out there, I've been learning from GitHub discussions and code in the Emscripten repo. Here's a few useful links: