Skip to content

Commit

Permalink
Make noExitRuntime into a library symbol (emscripten-core#20336)
Browse files Browse the repository at this point in the history
Hopefully we can completely remove this symbols at some point, but
converting into a library symbol at least means it only gets added
when needed.
  • Loading branch information
sbc100 authored Oct 2, 2023
1 parent 00e3406 commit 4e72237
Show file tree
Hide file tree
Showing 39 changed files with 84 additions and 38 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ See docs/process.md for more on how version tagging works.

3.1.47 (in development)
-----------------------
- The `noExitRuntime` global is now a JS library symbol that will only be
included as needed. User of `noExitRuntime` will now need to declare a
dependency on it. It will get implicitly included if `noExitRuntime` is part
of `INCOMING_MODULE_JS_API.` (which it is by default), or it can be added to
either `DEFAULT_LIBRARY_FUNCS_TO_INCLUDE` or `noExitRuntime`. (#20336)
- The egl, html5, sdl and webgpu libraries now support basic functionality with
`-sMEMORY64`. (#20276)
- Value types in `emscripten/html5.h` that correspond the WebIDL `long` type are
Expand Down
3 changes: 3 additions & 0 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,9 @@ def phase_linker_setup(options, state, newargs):
system_libpath = '-L' + str(cache.get_lib_dir(absolute=True))
add_link_flag(state, sys.maxsize, system_libpath)

if 'noExitRuntime' in settings.INCOMING_MODULE_JS_API:
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('$noExitRuntime')

if settings.OPT_LEVEL >= 1:
default_setting('ASSERTIONS', 0)

Expand Down
6 changes: 6 additions & 0 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ function(${args}) {
if (typeof dep == 'function') {
return dep();
}
// $noExitRuntime is special since there are conditional usages of it
// in library.js and library_pthread.js. These happen before deps are
// processed so depending on it via `__deps` doesn't work.
if (dep === '$noExitRuntime') {
error('noExitRuntime cannot be referenced via __deps mechansim. Use DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or EXPORTED_RUNTIME_METHODS')
}
return addFromLibrary(dep, `${symbol}, referenced by ${dependent}`, dep === aliasTarget);
}
let contentText;
Expand Down
10 changes: 9 additions & 1 deletion src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -3269,8 +3269,10 @@ addToLibrary({
_emscripten_runtime_keepalive_clear__proxy: 'sync',
_emscripten_runtime_keepalive_clear: () => {
#if !MINIMAL_RUNTIME
#if isSymbolNeeded('$noExitRuntime')
noExitRuntime = false;
#endif
#if !MINIMAL_RUNTIME
runtimeKeepaliveCounter = 0;
#endif
},
Expand Down Expand Up @@ -3388,7 +3390,11 @@ addToLibrary({
$runtimeKeepaliveCounter: 0,
$keepRuntimeAlive__deps: ['$runtimeKeepaliveCounter'],
#if isSymbolNeeded('$noExitRuntime')
$keepRuntimeAlive: () => noExitRuntime || runtimeKeepaliveCounter > 0,
#else
$keepRuntimeAlive: () => runtimeKeepaliveCounter > 0,
#endif
// Callable in pthread without __proxy needed.
$runtimeKeepalivePush__deps: ['$runtimeKeepaliveCounter'],
Expand Down Expand Up @@ -3646,6 +3652,8 @@ addToLibrary({
$wasmTable: undefined,
#endif
$noExitRuntime: "{{{ makeModuleReceiveExpr('noExitRuntime', !EXIT_RUNTIME) }}}",
// We used to define these globals unconditionally in support code.
// Instead, we now define them here so folks can pull it in explicitly, on
// demand.
Expand Down
2 changes: 1 addition & 1 deletion src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ var LibraryPThread = {
#endif
#endif

#if !MINIMAL_RUNTIME
#if isSymbolNeeded('$noExitRuntime')
// The default behaviour for pthreads is always to exit once they return
// from their entry point (or call pthread_exit). If we set noExitRuntime
// to true here on pthreads they would never complete and attempt to
Expand Down
12 changes: 12 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,18 @@ function expectToReceiveOnModule(name) {
return INCOMING_MODULE_JS_API.has(name);
}

// Return true if the user requested that a library symbol be included
// either via DEFAULT_LIBRARY_FUNCS_TO_INCLUDE or EXPORTED_RUNTIME_METHODS.
function isSymbolNeeded(symName) {
if (DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.includes(symName)) {
return true;
}
if (symName.startsWith('$') && symName.slice(1) in EXPORTED_RUNTIME_METHODS) {
return true;
}
return false;
}

function makeRemovedModuleAPIAssert(moduleName, localName) {
if (!ASSERTIONS) return '';
if (!localName) localName = moduleName;
Expand Down
1 change: 0 additions & 1 deletion src/preamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ out = err = () => {};
#endif

{{{ makeModuleReceiveWithVar('wasmBinary') }}}
{{{ makeModuleReceiveWithVar('noExitRuntime', undefined, EXIT_RUNTIME ? 'false' : 'true') }}}

#if WASM != 2 && MAYBE_WASM2JS
#if !WASM2JS
Expand Down
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25031
25027
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_ctors2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24999
24995
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29164
29160
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_except_wasm.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24805
24801
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_mangle.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
29163
29159
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_noexcept.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25031
25027
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_cxx_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12440
12436
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_js_fs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19930
19926
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7070
7065
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
23549
23522
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O1.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8062
8058
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O2.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5724
5719
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5571
5566
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5571
5566
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5537
5533
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14810
14806
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_wasmfs.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5571
5566
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4855
4850
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4904
4899
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5752
5748
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6074
6070
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5468
5464
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5399
5395
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4906
4901
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4904
4899
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4904
4899
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_O0.jssize
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20027
20004
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
58384
58355
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_no_asserts.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31749
31752
2 changes: 1 addition & 1 deletion test/other/test_unoptimized_code_size_strict.js.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
57098
57069
22 changes: 17 additions & 5 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -13888,17 +13888,17 @@ def test_webidl_empty(self):

def test_noExitRuntime(self):
onexit_called = 'onExit called'
create_file('pre.js', f'Module.onExit = () => console.log("${onexit_called}");')
create_file('pre.js', f'Module.onExit = () => console.log("${onexit_called}");\n')
self.emcc_args += ['--pre-js=pre.js']
self.set_setting('EXIT_RUNTIME')

# Normally with EXIT_RUNTIME set we expect onExit to be called;
# Normally, with EXIT_RUNTIME set we expect onExit to be called.
output = self.do_runf(test_file('hello_world.c'), 'hello, world')
self.assertContained(onexit_called, output)

# However, if we set `Module.noExitRuntime = true`, then we expect
# it not to be called.
create_file('noexit.js', 'Module.noExitRuntime = true;')
# However, if we set `Module.noExitRuntime = true`, then it should
# not be called.
create_file('noexit.js', 'Module.noExitRuntime = true;\n')
output = self.do_runf(test_file('hello_world.c'), 'hello, world', emcc_args=['--pre-js=noexit.js'])
self.assertNotContained(onexit_called, output)

Expand All @@ -13907,3 +13907,15 @@ def test_noExitRuntime(self):
create_file('noexit_oninit.js', 'Module.preRun = () => { noExitRuntime = true; }')
output = self.do_runf(test_file('hello_world.c'), 'hello, world', emcc_args=['--pre-js=noexit_oninit.js'])
self.assertNotContained(onexit_called, output)

def test_noExitRuntime_deps(self):
create_file('lib.js', r'''
addToLibrary({
foo__deps: ['$noExitRuntime'],
foo: () => {
return 0;
}
});
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js', '-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=foo'])
self.assertContained('error: noExitRuntime cannot be referenced via __deps mechansim', err)
1 change: 1 addition & 0 deletions tools/gen_struct_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def inspect_headers(headers, cflags):
'-nostdlib',
compiler_rt,
'-sBOOTSTRAPPING_STRUCT_INFO',
'-sINCOMING_MODULE_JS_API=',
'-sSTRICT',
'-sASSERTIONS=0'] + node_flags

Expand Down

0 comments on commit 4e72237

Please sign in to comment.