Skip to content

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Oct 8, 2025

For immutable wasm globals we currently export them as simple JS numbers at build time. This is because data symbol addresses are exported as immutable globals.

However, its also useful to export real wasm globals. For now we treat mutable globals and immutable global differently here but we should look into perhaps changing that in the future.

@sbc100 sbc100 requested a review from dschuff October 10, 2025 00:20
@sbc100 sbc100 force-pushed the wasm_globals branch 4 times, most recently from acef802 to 906bef0 Compare October 10, 2025 01:01
Copy link
Member

@dschuff dschuff left a comment

Choose a reason for hiding this comment

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

can yo remind me what we wanted to use this for?

$isImmutableGlobal: (val) => {
if (val instanceof WebAssembly.Global) {
try {
val.value = val.value;
Copy link
Member

Choose a reason for hiding this comment

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

why this particular method of checking for the presence of the value property? Wouldn't it be easier to do hasOwnProperty or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This check is only checking for mutability, which is not something that the JS spec exposes.

Copy link
Member

Choose a reason for hiding this comment

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

Ah right. Since we expose immutable globals as numbers, should we have some kind of assertion that there aren't any immutable globals exported as wasm globals?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah right. Since we expose immutable globals as numbers, should we have some kind of assertion that there aren't any immutable globals exported as wasm globals?

But we do have such exports. All data symbols (addresses) are exported in this way.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh I guess you are saying, do we have tests that confirm if data exports are working? I assume we have many such tests.. basically any test that does EMSCRIPTEN_KEEPALIVE on a data symbol, or included a data symbols in EXPORTED_FUNCTIONS.

Copy link
Member

Choose a reason for hiding this comment

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

More like, we expect everything exported as a WebAssembly.Global to be mutable (because up until now, all data exports are immutable and exported as numbers and we never exported anything as a mutable global other than the stack pointer). So if we ever see an immutable WebAssembly.Global it would be an error, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right, but this function (isImmutableGlobal) is used to disciminate bewtween those two types of exports.

Type 1: Mutable globals that we are exporting with the intent that they are exported as first class globals (e.g. __stack_pointer.
Type 2: Immutable globals that are exported as addresses, and are visible on the outside a simply numbers. These one also get relocated (+= __memory_base) so they can be used to index into the HEAP directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i.e. at this point in the code we expect to see both types.

@dschuff
Copy link
Member

dschuff commented Oct 10, 2025

Remind me what the use you had in mind for this was? Just so users could export their own globals?

@sbc100
Copy link
Collaborator Author

sbc100 commented Oct 10, 2025

Remind me what the use you had in mind for this was? Just so users could export their own globals?

My initial use case was to make exporting the __stack_pointer from the main module and then importing into the side module "just work" without special casing.

But I can imagine we maybe have other mutable globals that also want to be import/exported between modules.

For immutable wasm globals we currently export them as simple JS numbers
at build time.  This is because data symbol addresses are exported as
immutable globals.

However, its also useful to export real wasm globals.  For now we treat
mutable globals and immutable global differently here but we should look
into perhaps changing that in the future.
@sbc100 sbc100 merged commit 1967514 into emscripten-core:main Oct 11, 2025
34 checks passed
@sbc100 sbc100 deleted the wasm_globals branch October 11, 2025 16:00
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