Skip to content

Commit 3134b14

Browse files
authored
Fix wrapping/modifying of single line JS library functions (emscripten-core#20265)
Fixes: emscripten-core#20264
1 parent 4507c54 commit 3134b14

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/library.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,7 @@ addToLibrary({
18131813

18141814
gethostbyname__deps: ['$getHostByName'],
18151815
gethostbyname__proxy: 'sync',
1816-
gethostbyname: (name) => {
1817-
return getHostByName(UTF8ToString(name));
1818-
},
1816+
gethostbyname: (name) => getHostByName(UTF8ToString(name)),
18191817

18201818
$getHostByName__deps: ['malloc', '$stringToNewUTF8', '$DNS', '$inetPton4'],
18211819
$getHostByName: (name) => {

src/parseTools.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ function modifyJSFunction(text, func) {
689689
let async_;
690690
let args;
691691
let rest;
692+
let oneliner = false;
692693
let match = text.match(/^\s*(async\s+)?function\s+([^(]*)?\s*\(([^)]*)\)/);
693694
if (match) {
694695
async_ = match[1] || '';
@@ -701,6 +702,8 @@ function modifyJSFunction(text, func) {
701702
async_ = match[3] || '';
702703
args = match[4];
703704
rest = text.substr(match[0].length);
705+
rest = rest.trim();
706+
oneliner = rest[0] != '{';
704707
} else {
705708
// Match a function without a name (we could probably use a single regex
706709
// for both, but it would be more complex).
@@ -712,9 +715,8 @@ function modifyJSFunction(text, func) {
712715
}
713716
}
714717
let body = rest;
715-
const bodyStart = rest.indexOf('{');
716-
let oneliner = bodyStart < 0;
717718
if (!oneliner) {
719+
const bodyStart = rest.indexOf('{');
718720
const bodyEnd = rest.lastIndexOf('}');
719721
assert(bodyEnd > 0);
720722
body = rest.substring(bodyStart + 1, bodyEnd);

test/test_other.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3884,6 +3884,30 @@ def test_js_lib_quoted_key(self):
38843884

38853885
self.do_runf(test_file('hello_world.c'), 'hello, world!', emcc_args=['--js-library', 'lib.js'])
38863886

3887+
def test_js_lib_proxying(self):
3888+
# Regression test for a bug we had where jsifier would find and use
3889+
# the inner function in a library function consisting of a single
3890+
# line arrow function.
3891+
# See https://github.com/emscripten-core/emscripten/issues/20264
3892+
create_file('lib.js', r'''
3893+
addToLibrary({
3894+
foo__proxy: 'sync',
3895+
foo: () => setTimeout(() => {
3896+
console.log('should not see this before "done"');
3897+
}),
3898+
});
3899+
''')
3900+
create_file('src.c', r'''
3901+
#include <stdio.h>
3902+
void foo();
3903+
int main() {
3904+
printf("main\n");
3905+
foo();
3906+
printf("done\n");
3907+
}
3908+
''')
3909+
self.do_runf('src.c', 'main\ndone\n', emcc_args=['-sEXIT_RUNTIME', '-pthread', '-sPROXY_TO_PTHREAD', '--js-library', 'lib.js'])
3910+
38873911
def test_js_lib_exported(self):
38883912
create_file('lib.js', r'''
38893913
addToLibrary({

0 commit comments

Comments
 (0)