Skip to content

Commit eab2b47

Browse files
committed
Test output for EXPORT_KEEPALIVE instead of doing string check
1 parent 25071fd commit eab2b47

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/settings.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1832,7 +1832,10 @@ var SUPPORT_ERRNO = true;
18321832
// MINIMAL_RUNTIME=2 to further enable even more code size optimizations. These
18331833
// opts are quite hacky, and work around limitations in Closure and other parts
18341834
// of the build system, so they may not work in all generated programs (But can
1835-
// be useful for really small programs)
1835+
// be useful for really small programs).
1836+
//
1837+
// By default, no symbols will be exported on the `Module` object. In order
1838+
// to export kept alive symbols, please use `-sEXPORT_KEEPALIVE=1`.
18361839
// [link]
18371840
var MINIMAL_RUNTIME = 0;
18381841

test/test_other.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,36 +1307,51 @@ 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_modularize_export_keepalive(self):
1310+
def test_export_keepalive(self):
13111311
create_file('main.c', r'''
13121312
#include <emscripten.h>
13131313
EMSCRIPTEN_KEEPALIVE int libf1() { return 42; }
13141314
''')
13151315

1316-
# By default, all kept alive functions should be exported.
1317-
self.emcc('main.c', ['-sMODULARIZE=1'], output_filename='test.js')
1318-
1319-
# print(read_file('test.js'))
1316+
create_file('main.js', '''
1317+
var Module = {
1318+
onRuntimeInitialized: function() {
1319+
console.log(Module._libf1 ? Module._libf1() : 'unexported');
1320+
}
1321+
};
1322+
''')
13201323

1321-
assert ("Module[\"_libf1\"] = " in read_file('test.js'))
1324+
# By default, all kept alive functions should be exported.
1325+
self.emcc('main.c', ['--pre-js', 'main.js'], output_filename='test.js')
1326+
self.assertContained('42\n', self.run_js('test.js'))
13221327

13231328
# Ensures that EXPORT_KEEPALIVE=0 remove the exports
1324-
self.emcc('main.c', ['-sMODULARIZE=1', '-sEXPORT_KEEPALIVE=0'], output_filename='test.js')
1325-
assert (not ("Module[\"_libf1\"] = " in read_file('test.js')))
1329+
self.emcc('main.c', ['-sEXPORT_KEEPALIVE=0', '--pre-js', 'main.js'], output_filename='test.js')
1330+
self.assertContained('unexported', self.run_js('test.js'))
13261331

1332+
@requires_node
13271333
def test_minimal_modularize_export_keepalive(self):
13281334
create_file('main.c', r'''
13291335
#include <emscripten.h>
13301336
EMSCRIPTEN_KEEPALIVE int libf1() { return 42; }
13311337
''')
13321338

1339+
# With MINIMAL_RUNTIME, the module isn't exported.
1340+
def write_js_main():
1341+
runtime = read_file('test.js')
1342+
write_file('main.js', f'{runtime}\nModule().then((mod) => console.log(mod._libf1()));')
1343+
13331344
# By default, no symbols should be exported when using MINIMAL_RUNTIME.
13341345
self.emcc('main.c', ['-sMODULARIZE=1', '-sMINIMAL_RUNTIME=2'], output_filename='test.js')
1335-
assert (not ("Module[\"_libf1\"] = " in read_file('test.js')))
1346+
write_js_main()
1347+
output = self.expect_fail(config.NODE_JS + ['main.js'])
1348+
self.assertContained('TypeError: mod._libf1 is not a function', output)
13361349

13371350
# Ensures that EXPORT_KEEPALIVE=1 exports the symbols.
1338-
self.emcc('main.c', ['-sMODULARIZE=1', '-sMINIMAL_RUNTIME=2', '-sEXPORT_KEEPALIVE'], output_filename='test.js')
1339-
assert ("Module[\"_libf1\"] = " in read_file('test.js'))
1351+
self.emcc('main.c', ['-sMODULARIZE=1', '-sMINIMAL_RUNTIME=2', '-sEXPORT_KEEPALIVE=1'], output_filename='test.js')
1352+
write_js_main()
1353+
output = self.run_process(config.NODE_JS + ['main.js'], stdout=PIPE, stderr=PIPE)
1354+
self.assertContained('42\n', output.stdout)
13401355

13411356
def test_minimal_runtime_export_all_modularize(self):
13421357
"""This test ensures that MODULARIZE and EXPORT_ALL work simultaneously.

0 commit comments

Comments
 (0)