Skip to content

Support OPFS on the main thread with JSPI. #19063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Apr 5, 2023

Conversation

brendandahl
Copy link
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.

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
Copy link
Member

@tlively tlively left a comment

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?

'$wasmfsOPFSAccessHandles'],
'$wasmfsOPFSAccessHandles', '$wasmfsOPFSProxyFinish',
#if !USE_PTHREADS
'$wasmfsOPFSCreateASyncAccessHandle'
Copy link
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'

// TODO: Remove this once the Access Handles API has settled.
if (FileSystemFileHandle.prototype.createSyncAccessHandle.length == 0) {
accessHandle = await fileHandle.createSyncAccessHandle();
} else {
accessHandle = await fileHandle.createSyncAccessHandle(
{mode: "in-place"});
}
#else
accessHandle = await wasmfsOPFSCreateASyncAccessHandle(fileHandle);
Copy link
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);

},

$wasmfsOPFSCreateASyncAccessHandle__deps: ['$FileSystemAsyncAccessHandle'],
$wasmfsOPFSCreateASyncAccessHandle: function(fileHandle) {
Copy link
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
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
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
Copy link
Member

tlively commented Apr 5, 2023

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