Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -1813,9 +1813,7 @@ addToLibrary({

gethostbyname__deps: ['$getHostByName'],
gethostbyname__proxy: 'sync',
gethostbyname: (name) => {
return getHostByName(UTF8ToString(name));
},
gethostbyname: (name) => getHostByName(UTF8ToString(name)),

$getHostByName__deps: ['malloc', '$stringToNewUTF8', '$DNS', '$inetPton4'],
$getHostByName: (name) => {
Expand Down
6 changes: 4 additions & 2 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ function modifyJSFunction(text, func) {
let async_;
let args;
let rest;
let oneliner = false;
let match = text.match(/^\s*(async\s+)?function\s+([^(]*)?\s*\(([^)]*)\)/);
if (match) {
async_ = match[1] || '';
Expand All @@ -701,6 +702,8 @@ function modifyJSFunction(text, func) {
async_ = match[3] || '';
args = match[4];
rest = text.substr(match[0].length);
rest = rest.trim();
oneliner = rest[0] != '{';
} else {
// Match a function without a name (we could probably use a single regex
// for both, but it would be more complex).
Expand All @@ -712,9 +715,8 @@ function modifyJSFunction(text, func) {
}
}
let body = rest;
const bodyStart = rest.indexOf('{');
let oneliner = bodyStart < 0;
if (!oneliner) {
const bodyStart = rest.indexOf('{');
const bodyEnd = rest.lastIndexOf('}');
assert(bodyEnd > 0);
body = rest.substring(bodyStart + 1, bodyEnd);
Expand Down
24 changes: 24 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3884,6 +3884,30 @@ def test_js_lib_quoted_key(self):

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

def test_js_lib_proxying(self):
# Regression test for a bug we had where jsifier would find and use
# the inner function in a library function consisting of a single
# line arrow function.
# See https://github.com/emscripten-core/emscripten/issues/20264
create_file('lib.js', r'''
addToLibrary({
foo__proxy: 'sync',
foo: () => setTimeout(() => {
console.log('should not see this before "done"');
}),
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this a little racey? A timeout of 0 means it will run in the next event loop iteration, but the runtime might exit before that? That is, we might get only "main" in the output, sometimes.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, wait, I misread this, done is not from here but from below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the problem is that the main thread which calls foo could hit its even loop at print "should not see this before "done"" from the setTimeout before the secondary thread sees that foo is complete and allow itself to continue executing and print "done"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The key here is that "foo" is proxied to the main thread.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yes... it must be the proxying, yeah, good point.

});
''')
create_file('src.c', r'''
#include <stdio.h>
void foo();
int main() {
printf("main\n");
foo();
printf("done\n");
}
''')
self.do_runf('src.c', 'main\ndone\n', emcc_args=['-sEXIT_RUNTIME', '-pthread', '-sPROXY_TO_PTHREAD', '--js-library', 'lib.js'])

def test_js_lib_exported(self):
create_file('lib.js', r'''
addToLibrary({
Expand Down