Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Deprecate export of module handlers via EXPORTED_RUNTIME_METHODS
`print` / `printErr` are handlers that can be overridden by the user
on the incoming module (just like `onAbort`) and not runtime elements
that can be exported. Deprecate the export of `print` / `printErr`
via `EXPORTED_RUNTIME_METHODS` in favor of `out` / `err`.
  • Loading branch information
kleisauke committed Sep 30, 2022
commit 689dc115d4b2883c896b8f0a6066acb4b3e81454
23 changes: 17 additions & 6 deletions src/modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ function addMissingLibraryStubs() {
function exportRuntime() {
const EXPORTED_RUNTIME_METHODS_SET = new Set(EXPORTED_RUNTIME_METHODS);

const legacyRuntimeElements = new Map();
legacyRuntimeElements.set('print', 'out');
legacyRuntimeElements.set('printErr', 'err');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does JS not have a literal syntax for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


// optionally export something.
// in ASSERTIONS mode we show a useful error if it is used without
// being exported. how we show the message depends on whether it's
Expand All @@ -323,10 +327,8 @@ function exportRuntime() {
if (isFSPrefixed(exported)) {
// this is a filesystem value, FS.x exported as FS_x
exported = 'FS.' + exported.substr(3);
} else if (exported === 'print') {
exported = 'out';
} else if (exported === 'printErr') {
exported = 'err';
} else if (legacyRuntimeElements.has(exported)) {
exported = legacyRuntimeElements.get(exported);
}
return `Module["${name}"] = ${exported};`;
}
Expand Down Expand Up @@ -361,8 +363,8 @@ function exportRuntime() {
'registerFunctions',
'prettyPrint',
'getCompilerSetting',
'print',
'printErr',
'out',
'err',
'callMain',
'abort',
'keepRuntimeAlive',
Expand Down Expand Up @@ -413,6 +415,15 @@ function exportRuntime() {
}
}

// Only export legacy runtime elements when explicitly
// requested.
for (const name of EXPORTED_RUNTIME_METHODS_SET) {
if (legacyRuntimeElements.has(name)) {
const newName = legacyRuntimeElements.get(name);
warn(`deprecated item in EXPORTED_RUNTIME_METHODS: ${name} use ${newName} instead.`);
runtimeElements.push(name);
}
}

// Add JS library elements such as FS, GL, ENV, etc. These are prefixed with
// '$ which indicates they are JS methods.
Expand Down
2 changes: 1 addition & 1 deletion test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4904,7 +4904,7 @@ def test_unicode_html_shell(self):
# Tests the functionality of the emscripten_thread_sleep() function.
@requires_threads
def test_emscripten_thread_sleep(self):
self.btest_exit(test_file('pthread/emscripten_thread_sleep.c'), args=['-sUSE_PTHREADS', '-sEXPORTED_RUNTIME_METHODS=[print]'])
self.btest_exit(test_file('pthread/emscripten_thread_sleep.c'), args=['-sUSE_PTHREADS'])

# Tests that Emscripten-compiled applications can be run from a relative path in browser that is different than the address of the current page
def test_browser_run_from_different_directory(self):
Expand Down
13 changes: 8 additions & 5 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -7130,7 +7130,7 @@ def test(contents):
self.run_process([EMXX, 'src.cpp', '-O2']) # optimized, so no assertions
self.assertNotContained(error, read_file('a.out.js'))

def test_warn_module_print_err(self):
def test_warn_module_out_err(self):
error = 'was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ)'

def test(contents, expected, args=[], assert_returncode=0): # noqa
Expand All @@ -7145,12 +7145,15 @@ def test(contents, expected, args=[], assert_returncode=0): # noqa
self.assertContained(expected, self.run_js('a.out.js', assert_returncode=assert_returncode))

# error shown (when assertions are on)
test("Module.print('x')", error, assert_returncode=NON_ZERO)
test("Module['print']('x')", error, assert_returncode=NON_ZERO)
test("Module.printErr('x')", error, assert_returncode=NON_ZERO)
test("Module['printErr']('x')", error, assert_returncode=NON_ZERO)
test("Module.out('x')", error, assert_returncode=NON_ZERO)
test("Module['out']('x')", error, assert_returncode=NON_ZERO)
test("Module.err('x')", error, assert_returncode=NON_ZERO)
test("Module['err']('x')", error, assert_returncode=NON_ZERO)

# when exported, all good
test("Module['out']('print'); Module['err']('err'); ", 'print\nerr', ['-sEXPORTED_RUNTIME_METHODS=out,err'])

# test backwards compatibility
test("Module['print']('print'); Module['printErr']('err'); ", 'print\nerr', ['-sEXPORTED_RUNTIME_METHODS=print,printErr'])

def test_warn_unexported_main(self):
Expand Down