Skip to content
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

Copy the wasm exports object if we intend to modify it #8950

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

Conversation

kripken
Copy link
Member

@kripken kripken commented Jul 9, 2019

That makes it work if it is the ES6 exports object of a wasm module, which is read-only.

Fixes #8678 and also bysyncify in ES6 environments.

@VirtualTim - thoughts? Also, I am having a hard time writing a testcase for this - I'm not really familiar with ES6 modules I guess, so I haven't added a test or verified this works yet. Help would be welcome :)

… work if it is the ES6 exports object of a wasm module, which is read-only. Fixes #8678
src/preamble.js Outdated
// To work around that, we simply make a copy of them.
#if EXPORT_ES6 && (ASSERTIONS || BYSYNCIFY)
var originalExports = exports;
for (var x in originalexports) exports[x] = originalExports[x];
Copy link
Collaborator

Choose a reason for hiding this comment

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

originalexports should have capital E

@VirtualTim
Copy link
Collaborator

Sorry, doesn't fix the issue for me. I still get Cannot assign to read only property '___cxa_can_catch' of object '[object Object]'
and the generated js still outputs:

var real____cxa_can_catch = asm["___cxa_can_catch"];
asm["___cxa_can_catch"] = function() {
  assert(runtimeInitialized, 'you need to wait for the runtime to be ready (e.g. wait for main() to be called)');
  assert(!runtimeExited, 'the runtime was exited (use NO_EXIT_RUNTIME to keep it alive after main() exits)');
  return real____cxa_can_catch.apply(null, arguments);
};
[...]
var ___cxa_can_catch = Module["___cxa_can_catch"] = function() {
  assert(runtimeInitialized, 'you need to wait for the runtime to be ready (e.g. wait for main() to be called)');
  assert(!runtimeExited, 'the runtime was exited (use NO_EXIT_RUNTIME to keep it alive after main() exits)');
  return Module["asm"]["___cxa_can_catch"].apply(null, arguments)
};

I am still compiling with the old backend (WASM_OBJECT_FILES=0), so I don't know if this changes anything.

@kripken
Copy link
Member Author

kripken commented Jul 10, 2019

Thanks for the comments, I fixed those issues and tried to add a test.

But the test is still not valid, as it passes even without this fix. @VirtualTim you say you still see the bug - is the test not written properly to catch what you are doing to hit it?

@kripken
Copy link
Member Author

kripken commented Jul 10, 2019

(which backend is used should not affect this, I don't think, and I see the test passes in both backends)

@kripken
Copy link
Member Author

kripken commented Jul 10, 2019

This isn't related to the real issue, but the tests pass locally for me but fail on CI. It's like on CI there is no support for es6 modules, even though based on caniuse there should be. I guess we need a flag or something?

@VirtualTim
Copy link
Collaborator

VirtualTim commented Jul 11, 2019

@VirtualTim you say you still see the bug - is the test not written properly to catch what you are doing to hit it?

I figured out the problem with the test, the issue is only there when a thread/webworker is created, so you'll need to add -s PTHREAD_POOL_SIZE=1 to see it.

@VirtualTim
Copy link
Collaborator

Unfortunately this has revealed that MODULARIZE_INSTANCE isn't compatible with EXPORT_ES6 + USE_PTHREADS. The first bit is that is uses document.currentScript.src, which is undefined in the worker. I've added the fix for that to #8940, since I was changing that anyway.

After fixing that I'm still getting {{{ EXPORT_NAME }}}.default is not a function from here:

Module = {{{ EXPORT_NAME }}}.default(Module);

That one is also easy to fix, that line just shouldn't be there in MODULARIZE_INSTANCE.

However the biggest issue is that Module overrides don't appear to work in MODULARIZE_INSTANCE.
In this specific case I'm defining things like Module['ENVIRONMENT_IS_PTHREAD'] = true;, then calling Module = {{{ EXPORT_NAME }}}.default(Module); . However since MODULARIZE_INSTANCE initialises itself that line shouldn't be there, and so no overrides can be basses into the module. I think this is an overall MODULARIZE_INSTANCE + USE_PTHREADS issue.
If you look at the compiled output_name.worker.js you'll see a bunch of Module overrides just before the import(e.data.urlOrBlob) call (eg Module['STACK_MAX'] = Module['STACKTOP'] = 0x7FFFFFFF;). I don't know how that can work in MODULARIZE_INSTANCE.

I hope that all makes sense?


@requires_threads
def test_es6_threads(self):
# TODO: use PROXY_TO_PTHREAD and/or '-s', 'PTHREAD_POOL_SIZE=1' once https://bugs.chromium.org/p/chromium/issues/detail?id=680046 is fixed
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should work on Chrome if --enable-experimental-web-platform-features is set (sorry, forgot to mention that).
The Firefox bug is also: https://bugzilla.mozilla.org/show_bug.cgi?id=1540913

@sbc100 sbc100 closed this Jan 30, 2020
@sbc100
Copy link
Collaborator

sbc100 commented Jan 30, 2020

The deletion of the incoming branch triggered the automatic closing of this branch. My apologies. This was an unexpected side effect.

Re-opening and re-targeting to master.

@sbc100 sbc100 reopened this Jan 30, 2020
@sbc100 sbc100 changed the base branch from incoming to master January 30, 2020 20:41
@stale
Copy link

stale bot commented Jan 30, 2021

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Jan 30, 2021
Base automatically changed from master to main March 8, 2021 23:49
@stale stale bot removed the wontfix label Mar 8, 2021
@kleisauke
Copy link
Collaborator

kleisauke commented Aug 22, 2021

It looks like this PR has been superseded by #8926. Although, the Firefox bug mentioned by @VirtualTim is still not fixed and prevents the -sEXPORT_ES6 + -pthread combo from working.

Should I open a new issue to track that? Not sure if we should track Firefox issues, but maybe we should document that somehow. FWIW, the wasm-vips playground doesn't currently work on Firefox due to this. Edit: fixed with commit kleisauke/wasm-vips@ba6f7ee.

@VirtualTim
Copy link
Collaborator

@kleisauke if you're interested in getting this working in Firefox, I managed to get my use case working by using a custom build of Shimport 1.0.1 containing the fix mentioned here.

@stale
Copy link

stale bot commented Sep 21, 2022

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.

@stale stale bot added the wontfix label Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EXPORT_ES6 + USE_PTHREADS not compatible with ASSERTIONS
4 participants