Skip to content

Fix loading wasm in electron by falling back to XHR #12921

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 16 commits into from
Jan 6, 2021

Conversation

0x53A
Copy link
Contributor

@0x53A 0x53A commented Nov 30, 2020

fixes mono/mono#20592 (which was my original issue)

should fix #11671


see electron/electron#3922

When attempting to reference a relative url (ala scripts/gamename/scenename.xml) fetch will fail with " url scheme "file" is not supported", while an XmlHttpRequest will succeed for the same request.

I did a quick test by editing dotnet.js in my electron output folder, and it seems to work, but would appreciate any feedback.

@welcome
Copy link

welcome bot commented Nov 30, 2020

Thank you for submitting a pull request! If this is your first PR, make sure to add yourself to AUTHORS.

@kripken
Copy link
Member

kripken commented Dec 1, 2020

Thanks for the PR @0x53A - I agree we should fix this, but I think we may want a different approach. This regresses code size, which we try to avoid unless absolutely necessary.

I think an approach that can work is the one suggested in #12203 That is, we could create an "electron shim" JS file that replaces fetch, or just assigns it null maybe (if I understood the solution here right - does it just avoid doing a fetch?). If that makes sense, we can maybe find a way to do that even before #12203 is implemented.

@0x53A
Copy link
Contributor Author

0x53A commented Dec 1, 2020

if I understood the solution here right - does it just avoid doing a fetch?

It replaces the fetch with XmlHttpRequest IF the url is file://because fetch does not work with file:// but XHR does.

@kripken
Copy link
Member

kripken commented Dec 4, 2020

Ok, then yes, I think that approach can be used here. That is, fetch would be wrapped around, so that it redirects a file URL to an XHR instead. Then that wrapping would be in a file that is only included in builds for electron, which would avoid a code size increase here.

If that makes sense, then I can find time to set up the general infrastructure for adding such a file, if it's not obvious to you where to start for that.

@0x53A
Copy link
Contributor Author

0x53A commented Dec 4, 2020

If I understood you correctly, this would replace (wrap) fetch globally for the whole app? That does sound like it could introduce subtle bugs.

If it is possible to detect electron at build time, wouldn't it be easier to wrap the else branch in #if electron?

I already wrapped the else in ENVIRONMENT_MAY_BE_WEBVIEW, so there is currently NO code size increase in pure web targets.

@kripken
Copy link
Member

kripken commented Dec 5, 2020

Oh, I meant to wrap it just inside the emscripten output. Not to modify the global state. (I assume we never want electron to fetch a file URL from our code?)

But yes, if we can just ifdef by the build type then that is even better! Is the remaining code size increase in the diff (on that one test) unintentional then, and can be removed?

@0x53A
Copy link
Contributor Author

0x53A commented Dec 5, 2020

No, the second one (test_INCOMING_MODULE_JS_API) seems to have ENVIRONMENT_MAY_BE_WEBVIEW, so there is a real code size increase.

Only test_small_js_flags was affected by the #ifdef.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

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

I see, thanks - yes, it makes sense for size to increase in that test, then.

src/preamble.js Outdated
}

// Otherwise, getBinary should be able to get it synchronously
return Promise.resolve().then(getBinary);
Copy link
Member

Choose a reason for hiding this comment

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

does getBinary() not work on Electron? If it allows sync xhrs on the main thread etc., then the addition above may not be necessary.

Copy link
Contributor Author

@0x53A 0x53A Dec 8, 2020

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Interesting, even on electron? I thought maybe since it has more APIs than the Web, that includes a way to synchronously get a file.

@0x53A
Copy link
Contributor Author

0x53A commented Dec 18, 2020

Thank you for the review. I finally had some more time for this and I think I've fixed all your comments.
Can you please take a look again?

Regarding

Interesting, even on electron? I thought maybe since it has more APIs than the Web, that includes a way to synchronously get a file.

I think there is a way to mark a protocol as "safe", which might even allow fetch on file, but the goal of this PR is to make WASM in Electron "just work" without every user having to code a workaround.

@kripken
Copy link
Member

kripken commented Jan 6, 2021

Sorry for the late response - holidays etc...

Looks good. I added an indentation fix now. Once tests pass, will merge.

@kripken
Copy link
Member

kripken commented Jan 6, 2021

Test failures here look like existing issues on trunk, so looks safe to merge.

@kripken kripken merged commit 81ae876 into emscripten-core:master Jan 6, 2021
@0x53A
Copy link
Contributor Author

0x53A commented Jan 7, 2021

Thank you! Hope you had some good holidays, and happy new year, etc ;D

I'll ping the mono team after the next release.

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.

[WASM] initialization doesn't work in electron from file:// Unable to load wasm in electron.
2 participants