Skip to content

Add test for EMSCRIPTEN_KEEPALIVE behavior - #27587

Open
dschuff wants to merge 4 commits into
emscripten-core:mainfrom
dschuff:keepalive-behavior
Open

Add test for EMSCRIPTEN_KEEPALIVE behavior#27587
dschuff wants to merge 4 commits into
emscripten-core:mainfrom
dschuff:keepalive-behavior

Conversation

@dschuff

@dschuff dschuff commented Aug 21, 2026

Copy link
Copy Markdown
Member

Check that EMSCRIPTEN_KEEPALIVE retains functions and global variables, and
exports them under their linkage name.

Check that EMSCRIPTEN_KEEPALIVE retains functions and global variables, and
exports them under their linkage name.
@dschuff

dschuff commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

@sbc I have a test locally for implementing EMSCRIPTEN_KEEPALIVE on top of llvm/llvm-project#201966 but I realized that we can just pre-commit the test. I didn't see a test that explicitly covered this behavior.

@dschuff dschuff left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

a couple questions

Comment thread test/test_other.py Outdated
'O1': (['-O1'],),
'O2': (['-O2'],),
'O3': (['-O3', '-g'],),
'lto': (['-flto'],),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

do we actually need all of these different modes? are they adding interesting coverage?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you are going to have this many variants maybe better to include in test_core.py. Seems like a good candidate for a test we want to run in every mode.

Comment thread test/test_other.py Outdated
printf("kept func: %d\n", my_kept_func(10));
return 0;
}
''')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

would it be better to have these C files out-of-line?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

My general policy is that I always prefer out-of-line files for more than a few lines (maybe more than 1/2 a screefull).

We don't have any hard and fast rules though.. perhaps we should.

@dschuff
dschuff requested a review from sbc100 August 21, 2026 21:14
@sbc100

sbc100 commented Aug 21, 2026

Copy link
Copy Markdown
Collaborator

I think test/other/test_export_global_address.c perhaps already covers this?

Comment thread test/test_other.py Outdated
''')
self.run_process([EMCC, 'main.c', '-o', 'main.html'] + args)
self.assertTrue(self.is_exported_in_wasm('my_kept_func', 'main.wasm'))
self.assertTrue(self.is_exported_in_wasm('my_kept_global', 'main.wasm'))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think s_exported_in_wasm is really enough. What you want to check is that Module['foo'] exists and has the value you expect? i.e. call the function from the outside like that, or get the global address.

You can do Module['foo'] either in EM_JS or in --pre-js / --extern-pre-js , etc

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I updated test_export_global_address.c to C++ and made it cover more cases. It does still test global underscore-prefixed names though. Maybe I should just change some of them to use Module['foo'] instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For native symbol the final exported name is always underscore-prefixed today.

I've tried to change that over time... but its tricky to say the least.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I doubt folks are actually using C++ mangled names in this way, but I guess its good to at least get a baseline for the current behaivour.

I think if we dropped support for exporting mangled names it probably wouldn't be the end of the world.

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Probably needs a better title and description now that this is more of a test update?

@dschuff

dschuff commented Aug 22, 2026

Copy link
Copy Markdown
Member Author

Yeah, it's pretty straightforward to use either linkage name or AST name, but of course if you want to actually use an overload you need linkage name.

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