-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Export mutable wasm globals as JS objects. NFC #25530
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include <emscripten/em_asm.h> | ||
#include <emscripten.h> | ||
|
||
__asm__( | ||
".section .data.my_global,\"\",@\n" | ||
".globl my_global\n" | ||
".globaltype my_global, i32\n" | ||
"my_global:\n" | ||
); | ||
|
||
int get_global() { | ||
int val; | ||
// Without volatile here this test fails in O1 and above. | ||
__asm__ volatile ("global.get my_global\n" | ||
"local.set %0\n" : "=r" (val)); | ||
return val; | ||
} | ||
|
||
void set_global(int val) { | ||
__asm__("local.get %0\n" | ||
"global.set my_global\n" : : "r" (val)); | ||
} | ||
|
||
int main() { | ||
printf("in main: %d\n", get_global()); | ||
set_global(42); | ||
printf("new value: %d\n", get_global()); | ||
EM_ASM({ | ||
// With the ESM integration, the Wasm global be exported as a regular | ||
// number. Otherwise it will be a WebAssembly.Global object. | ||
#ifdef ESM_INTEGRATION | ||
assert(typeof _my_global == 'number', typeof _my_global); | ||
out('from js:', _my_global); | ||
_my_global += 1 | ||
#else | ||
assert(typeof _my_global == 'object', typeof _my_global); | ||
out('from js:', _my_global.value); | ||
_my_global.value += 1 | ||
#endif | ||
}); | ||
printf("done: %d\n", get_global()); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
in main: 0 | ||
new value: 42 | ||
from js: 42 | ||
done: 43 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 dohasOwnProperty
or something?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we do have such exports. All data symbols (addresses) are exported in this way.
There was a problem hiding this comment.
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 inEXPORTED_FUNCTIONS
.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.