@@ -1307,6 +1307,53 @@ def test_export_all(self):
13071307 self.emcc('lib.c', ['-Oz', '-sEXPORT_ALL', '-sLINKABLE', '--pre-js', 'main.js'], output_filename='a.out.js')
13081308 self.assertContained('libf1\nlibf2\n', self.run_js('a.out.js'))
13091309
1310+ def test_export_keepalive(self):
1311+ create_file('main.c', r'''
1312+ #include <emscripten.h>
1313+ EMSCRIPTEN_KEEPALIVE int libf1() { return 42; }
1314+ ''')
1315+
1316+ create_file('pre.js', '''
1317+ Module.onRuntimeInitialized = () => {
1318+ console.log(Module._libf1 ? Module._libf1() : 'unexported');
1319+ };
1320+ ''')
1321+
1322+ # By default, all kept alive functions should be exported.
1323+ self.do_runf('main.c', '42\n', emcc_args=['--pre-js', 'pre.js'])
1324+
1325+ # Ensures that EXPORT_KEEPALIVE=0 remove the exports
1326+ self.do_runf('main.c', 'unexported\n', emcc_args=['-sEXPORT_KEEPALIVE=0', '--pre-js', 'pre.js'])
1327+
1328+ def test_minimal_modularize_export_keepalive(self):
1329+ self.set_setting('MODULARIZE')
1330+ self.set_setting('MINIMAL_RUNTIME')
1331+
1332+ create_file('main.c', r'''
1333+ #include <emscripten.h>
1334+ EMSCRIPTEN_KEEPALIVE int libf1() { return 42; }
1335+ ''')
1336+
1337+ def write_js_main():
1338+ """
1339+ With MINIMAL_RUNTIME, the module instantiation function isn't exported neither as a UMD nor as an ES6 module.
1340+ Thus, it's impossible to use `require` or `import`.
1341+
1342+ This function simply appends the instantiation code to the generated code.
1343+ """
1344+ runtime = read_file('test.js')
1345+ write_file('main.js', f'{runtime}\nModule().then((mod) => console.log(mod._libf1()));')
1346+
1347+ # By default, no symbols should be exported when using MINIMAL_RUNTIME.
1348+ self.emcc('main.c', [], output_filename='test.js')
1349+ write_js_main()
1350+ self.assertContained('TypeError: mod._libf1 is not a function', self.run_js('main.js', assert_returncode=NON_ZERO))
1351+
1352+ # Ensures that EXPORT_KEEPALIVE=1 exports the symbols.
1353+ self.emcc('main.c', ['-sEXPORT_KEEPALIVE=1'], output_filename='test.js')
1354+ write_js_main()
1355+ self.assertContained('42\n', self.run_js('main.js'))
1356+
13101357 def test_minimal_runtime_export_all_modularize(self):
13111358 """This test ensures that MODULARIZE and EXPORT_ALL work simultaneously.
13121359
0 commit comments