Skip to content

Assert if MODULARIZE factory function is used with new. #23210

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 1 commit into from
Dec 18, 2024

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Dec 18, 2024

Once we start using async for for this function then new stops
working: #23157.

Using new in this was always an antipattern but there was nothing
stopping users from doing this.

@sbc100 sbc100 requested a review from kripken December 18, 2024 03:43
@sbc100 sbc100 force-pushed the modularize_misuse branch 2 times, most recently from 112b953 to 7aca82a Compare December 18, 2024 04:17
@sbc100 sbc100 changed the title Assert if MODULARIZE factory function is used with new. Assert if MODULARIZE factory function is used with new. Dec 18, 2024
@sbc100 sbc100 force-pushed the modularize_misuse branch 2 times, most recently from f04ba5a to 4f32743 Compare December 18, 2024 17:06
@sbc100 sbc100 force-pushed the modularize_misuse branch 5 times, most recently from ae674ad to ced299a Compare December 18, 2024 20:37
@sbc100
Copy link
Collaborator Author

sbc100 commented Dec 18, 2024

I re-wrote this change is much more minimal way. PTAL

Once we start using `async` for for this function then `new` stops
working: emscripten-core#23157.

Using `new` in this was was always an antipattern but there was nothing
stopping users from doing this.
@sbc100 sbc100 merged commit 669e01a into emscripten-core:main Dec 18, 2024
21 of 29 checks passed
@sbc100 sbc100 deleted the modularize_misuse branch December 18, 2024 21:38
sbc100 added a commit that referenced this pull request Dec 19, 2024
The advantage if using `await` in the cases where we can is that it
avoids the generation or wrapper function for each export.

So instead of:

```
var wasmExport = createWasm(); // returns empty object
...
var malloc = (..) => (malloc = wasmExports['malloc'])(..);
```

We can generate:

```
var wasmExport = await createWasm(); // returns actual exports
...
var malloc = wasmExports['malloc'];
```

This only currently works in MODULARIZE mode where the code is running
inside a factory function. Otherwise it would end up using
top-level-await.

One wrinkle here is that this is not currently supported when closure is
enabled because we run closure only on the contents of the factory
function so closure ends up seeing this as a top level await when its
not.

There are two minor observable effects of this change:

1. In `MODULARIZE` mode its no longer possible to call `new Foo()` with
factory function. We already added a debug error for in #23210.
2. Because we now can `await` for createWasm to return, the `run` method
can run on first call, which means it runs `main` on first call, which
means main will now run before `postjs` code, just like it would with
sync instantiation.
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.

2 participants