Skip to content

Support OPFS on the main thread with JSPI. - #19063

Merged
brendandahl merged 10 commits into
emscripten-core:mainfrom
brendandahl:jspi-opfs
Apr 5, 2023
Merged

brendandahl merged 10 commits into
emscripten-core:mainfrom
brendandahl:jspi-opfs

Conversation

@brendandahl

Copy link
Copy Markdown
Collaborator

The OPFS backend will now use async reaad/writes when used on the main thread. The ProxyWorker is stubbed out and calls are directly dispatched into JS. The JS side uses a new FileSystemASyncAccessHandle class that matches the sync version of the class, but has async read/writes. Hopefully, we can one day replace that with something from the platform.

Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread emcc.py Resolved
Comment thread emcc.py Resolved
Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread test/test_browser.py Resolved
Comment thread src/jsifier.js Resolved
Comment thread src/library_async.js Resolved
Comment thread src/jsifier.js Resolved
Comment thread src/library_wasmfs_opfs.js Resolved
Comment thread src/jsifier.js Resolved
The OPFS backend will now use async reaad/writes when used on the main
thread. The ProxyWorker is stubbed out and calls are directly dispatched
into JS. The JS side uses a new FileSystemASyncAccessHandle class that matches
the sync version of the class, but has async read/writes. Hopefully, we
can one day replace that with something from the platform.
@brendandahl
brendandahl requested a review from tlively April 5, 2023 17:03

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % small comments.

What happens if you don't enable threads and also don't enable JSPI? Does it blow up or give a nice error message or somewhere in between?

Comment thread src/library_wasmfs_opfs.js Outdated
'$wasmfsOPFSAccessHandles'],
'$wasmfsOPFSAccessHandles', '$wasmfsOPFSProxyFinish',
#if !USE_PTHREADS
'$wasmfsOPFSCreateASyncAccessHandle'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'$wasmfsOPFSCreateASyncAccessHandle'
'$wasmfsOPFSCreateAsyncAccessHandle'

Comment thread src/library_wasmfs_opfs.js Outdated
{mode: "in-place"});
}
#else
accessHandle = await wasmfsOPFSCreateASyncAccessHandle(fileHandle);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
accessHandle = await wasmfsOPFSCreateASyncAccessHandle(fileHandle);
accessHandle = await wasmfsOPFSCreateAsyncAccessHandle(fileHandle);

Comment thread src/library_wasmfs_opfs.js Outdated
},

$wasmfsOPFSCreateASyncAccessHandle__deps: ['$FileSystemAsyncAccessHandle'],
$wasmfsOPFSCreateASyncAccessHandle: function(fileHandle) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$wasmfsOPFSCreateASyncAccessHandle: function(fileHandle) {
$wasmfsOPFSCreateAsyncAccessHandle: function(fileHandle) {

// can be directly executed since they are all async.
template <typename T>
void operator()(T func) {
if constexpr (std::is_invocable<T&, ProxyingQueue::ProxyingCtx>::value) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very slightly simpler:

Suggested change
if constexpr (std::is_invocable<T&, ProxyingQueue::ProxyingCtx>::value) {
if constexpr (std::is_invocable_v<T&, ProxyingQueue::ProxyingCtx>) {

@brendandahl

Copy link
Copy Markdown
Collaborator Author

What happens if you don't enable threads and also don't enable JSPI? Does it blow up or give a nice error message or somewhere in between?

In debug builds the same assert as before will trigger:
https://github.com/emscripten-core/emscripten/pull/19063/files#diff-e874b8e3fe2a7d014fb887216199449e89a3031e5e72a9e82e6a1f9a37667193R501

@brendandahl
brendandahl enabled auto-merge (squash) April 5, 2023 21:08
@tlively

tlively commented Apr 5, 2023

Copy link
Copy Markdown
Member

Makes sense. I guess if we add a new constructor that returns a promise (and is therefore usable on the main thread even without JSPI), we have to have a similar assertion to avoid trying to execute any async file operations when neither JSPI nor threads are enabled?

@brendandahl
brendandahl disabled auto-merge April 5, 2023 23:13
@brendandahl
brendandahl merged commit 12e2c96 into emscripten-core:main Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants