Skip to content

Refactor browser.test_runtime_misuse. NFC #24391

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 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2395,7 +2395,11 @@ def test_pre_run_deps(self):
self.btest('test_pre_run_deps.c', expected='10', emcc_args=['--pre-js', 'pre.js'])

@also_with_wasm2js
def test_runtime_misuse(self):
@parameterized({
'': ([], '600'),
'no_main': (['-DNO_MAIN', '--pre-js', 'pre_runtime.js'], '601'), # 601, because no main means we *do* run another call after exit()
})
def test_runtime_misuse(self, extra_args, second_code):
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', '$ccall,$cwrap')
post_prep = '''
var expected_ok = false;
Expand Down Expand Up @@ -2453,36 +2457,31 @@ def test_runtime_misuse(self):
setTimeout(async () => {
out('done timeout noted = ' + Module.noted);
assert(Module.noted);
await fetch('http://localhost:%s/report_result?' + HEAP32[Module.noted/4]);
await fetch('http://localhost:8888/report_result?' + HEAP32[Module.noted/4]);
window.close();
}, 0);
// called from main, this is an ok time
doCcall(100);
doCwrapCall(200);
doDirectCall(300);
}
''' % self.PORT
'''

create_file('pre_runtime.js', r'''
Module.onRuntimeInitialized = myJSCallback;
''')

for filename, extra_args, second_code in [
('test_runtime_misuse.c', [], 600),
('test_runtime_misuse_2.c', ['--pre-js', 'pre_runtime.js'], 601), # 601, because no main means we *do* run another call after exit()
]:
print('\n', filename, extra_args)

print('mem init, so async, call too early')
create_file('post.js', post_prep + post_test + post_hook)
self.btest(filename, expected='600', emcc_args=['--post-js', 'post.js', '-sEXIT_RUNTIME'] + extra_args, reporting=Reporting.NONE)
print('sync startup, call too late')
create_file('post.js', post_prep + 'Module.postRun = () => { ' + post_test + ' };' + post_hook)
self.btest(filename, expected=str(second_code), emcc_args=['--post-js', 'post.js', '-sEXIT_RUNTIME'] + extra_args, reporting=Reporting.NONE)

print('sync, runtime still alive, so all good')
create_file('post.js', post_prep + 'expected_ok = true; Module.postRun = () => { ' + post_test + ' };' + post_hook)
self.btest(filename, expected='606', emcc_args=['--post-js', 'post.js'] + extra_args, reporting=Reporting.NONE)
print('mem init, so async, call too early')
create_file('post.js', post_prep + post_test + post_hook)
self.btest('test_runtime_misuse.c', expected='600', emcc_args=['--post-js', 'post.js', '-sEXIT_RUNTIME'] + extra_args, reporting=Reporting.NONE)

print('sync startup, call too late')
create_file('post.js', post_prep + 'Module.postRun = () => { ' + post_test + ' };' + post_hook)
self.btest('test_runtime_misuse.c', expected=second_code, emcc_args=['--post-js', 'post.js', '-sEXIT_RUNTIME'] + extra_args, reporting=Reporting.NONE)

print('sync, runtime still alive, so all good')
create_file('post.js', post_prep + 'expected_ok = true; Module.postRun = () => { ' + post_test + ' };' + post_hook)
self.btest('test_runtime_misuse.c', expected='606', emcc_args=['--post-js', 'post.js'] + extra_args, reporting=Reporting.NONE)

def test_cwrap_early(self):
self.btest('browser/test_cwrap_early.c', emcc_args=['-O2', '-sASSERTIONS', '--pre-js', test_file('browser/test_cwrap_early.js'), '-sEXPORTED_RUNTIME_METHODS=cwrap'], expected='0')
Expand Down
2 changes: 2 additions & 0 deletions test/test_runtime_misuse.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ EMSCRIPTEN_KEEPALIVE char* note(int n) {
return (char*)"silly-string";
}

#ifndef NO_MAIN
int main() {
EM_ASM( myJSCallback() ); // calls a global JS func
return 0;
}
#endif

21 changes: 0 additions & 21 deletions test/test_runtime_misuse_2.c

This file was deleted.