Skip to content

Make exported __cpp_exception into a global. NFC #24282

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
May 9, 2025

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented May 7, 2025

This still is not quite a generic as I would like so as a followup we can probably make this work for all tags, and perhaps unify the updating of the globals when the exports are available.

This still is not quite a generic as I would like so as a followup we
can probably make this work for all tags, and perhaps unify the updating
of the globals when the exports are available.
@sbc100 sbc100 requested review from brendandahl and kripken May 8, 2025 16:25
// In static linking, tags are defined within the wasm module and are
// exported, whereas in dynamic linking, tags are defined in libcore.js in
// JS code and wasm modules import them.
$getCppExceptionTag: () => ___cpp_exception,
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this splits the code from this file to two places, here and preamble.js. That seems less simple at first glance - are there other benefits?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The removal of the dependency on wasmExports is the main benefit.

That currently doesn't work in WASM_ESM_INTEGRATION mode. We could make it work I suppose by adding an import * as wasmExports statement.. but it seems less precise that just importing what is used.

In the long run I think we should have a generic way to handle the non-function, non-global exports of a wasm module.

As of today we have special cases in a lot of places for memory, __indirect_function_table and __cpp_exception but I'd like to eliminate those if possible and replace them with generic export handling.

Copy link
Member

Choose a reason for hiding this comment

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

Now I see, thanks.

@@ -924,6 +924,11 @@ function getWasmImports() {
#endif
#endif

#if hasExportedSymbol('__cpp_exception') && !RELOCATABLE
Copy link
Member

Choose a reason for hiding this comment

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

Why do we check for not relocatable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because in relocatable mode symbols like this (along with the wasm table and memory) are not exported by the wasm but created in JS and imported by each wasm module.

Copy link
Member

Choose a reason for hiding this comment

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

In that case wouldn't hasExportedSymbol be false when relocatable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes we expect hasExportedSymbol to be false in relocatable mode and true for static builds. Here are are checking for static builds we check for hasExportedSymbol to be true.

Copy link
Member

Choose a reason for hiding this comment

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

But then is

A = hasExportedSymbol('__cpp_exception') && !RELOCATABLE

not equivalent to

B = hasExportedSymbol('__cpp_exception')

? Looking at the two cases,

  • If relocatable, then hasExportedSymbol is false, A = false && false => false, and B = false.
  • If not relocatable, hasExportedSymbol may be true or false, but !relocatable is always true, so it does not cause a difference between A and B.

So can't we do this?

Suggested change
#if hasExportedSymbol('__cpp_exception') && !RELOCATABLE
#if hasExportedSymbol('__cpp_exception')

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sure you could argue these are redundant. I'm mostly copying the existing logic for __indirect_function_table above.

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.

lgtm regardless - if you think adding !RELOCATABLE is helpful to clarify, I don't object.

(or maybe I just misunderstood something)

@sbc100
Copy link
Collaborator Author

sbc100 commented May 8, 2025

I think I might wait on landing this until I can come up with a more generic solution,..

@sbc100 sbc100 merged commit 5b489fc into emscripten-core:main May 9, 2025
28 checks passed
@sbc100 sbc100 deleted the __cpp_exception branch May 9, 2025 00:57
@sbc100
Copy link
Collaborator Author

sbc100 commented May 9, 2025

This did actually turn out to be necessary for my ESM integration work.

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