Skip to content

Commit 1682bfe

Browse files
committed
do not export getValue and setValue by default. add a mechanism in ASSERTIONS that emits stubs that give an error message, so if a user depended on those being exported, in ASSERTIONS they get a clear error message
1 parent 606f722 commit 1682bfe

7 files changed

+34
-5
lines changed

src/modules.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,17 @@ EXPORTED_RUNTIME_METHODS = unset(EXPORTED_RUNTIME_METHODS_SET);
272272
EXTRA_EXPORTED_RUNTIME_METHODS = [];
273273

274274
function maybeExport(name) {
275+
// if requested to be exported, export it
275276
if (name in EXPORTED_RUNTIME_METHODS_SET) {
276277
return 'Module["' + name + '"] = ' + name + ';';
277-
} else {
278-
return '';
279278
}
279+
// do not export it. but if ASSERTIONS, emit a
280+
// stub with an error, so the user gets a message
281+
// if it is used, that they should export it
282+
if (ASSERTIONS) {
283+
return 'Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS.") };';
284+
}
285+
return '';
280286
}
281287

282288
var PassManager = {

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ function demangle(func) {
805805
stringToUTF8(s, buf, len);
806806
var status = _malloc(4);
807807
var ret = __cxa_demangle_func(buf, 0, 0, status);
808-
if (getValue(status, 'i32') === 0 && ret) {
808+
if ({{{ makeGetValue('status', '0', 'i32') }}} === 0 && ret) {
809809
return Pointer_stringify(ret);
810810
}
811811
// otherwise, libcxxabi failed

src/settings.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,6 @@ var EXPORTED_RUNTIME_METHODS = [ // Runtime elements that are exported on Module
353353
'Runtime',
354354
'ccall',
355355
'cwrap',
356-
'setValue',
357-
'getValue',
358356
'ALLOC_NORMAL',
359357
'ALLOC_STACK',
360358
'ALLOC_STATIC',

tests/core/getValue_setValue.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include<emscripten.h>
2+
3+
int main() {
4+
EM_ASM({
5+
Module['setValue'](8, 1234, 'i32');
6+
Module['print']('|' + Module['getValue'](8, 'i32') + '|');
7+
});
8+
}
9+

tests/core/getValue_setValue.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
|1234|
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'setValue' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS.

tests/test_core.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5936,6 +5936,20 @@ def process(filename):
59365936
self.emcc_args += ['--closure', '1']
59375937
self.do_run_in_out_file_test('tests', 'core', 'test_ccall', post_build=post)
59385938

5939+
def test_getValue_setValue(self):
5940+
# these used to be exported, but no longer are by default
5941+
def test(output_prefix=''):
5942+
self.do_run(open(path_from_root('tests', 'core', 'getValue_setValue.cpp')).read(),
5943+
open(path_from_root('tests', 'core', 'getValue_setValue' + output_prefix + '.txt')).read())
5944+
# see that with assertions, we get a nice error message
5945+
Settings.EXTRA_EXPORTED_RUNTIME_METHODS = []
5946+
Settings.ASSERTIONS = 1
5947+
test('_assert')
5948+
Settings.ASSERTIONS = 0
5949+
# see that when we export them, things work
5950+
Settings.EXTRA_EXPORTED_RUNTIME_METHODS = ['getValue', 'setValue']
5951+
test()
5952+
59395953
@no_wasm_backend('DEAD_FUNCTIONS elimination is done by the JSOptimizer')
59405954
def test_dead_functions(self):
59415955
src = r'''

0 commit comments

Comments
 (0)