Skip to content

Commit 533d492

Browse files
authored
Use Asyncify shorthands. NFC (#24266)
Noticed a few places where Asyncify.handleSleep was used with promises, for which handleAsync is a more natural fit; also switched a few places to arrow functions. It makes code slightly cleaner, and, if used with JSPI where Promises are first-class, avoids jumping through handleSleep wrapping as well.
1 parent ed115c5 commit 533d492

File tree

4 files changed

+34
-54
lines changed

4 files changed

+34
-54
lines changed

src/lib/libasync.js

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,10 @@ addToLibrary({
426426
//
427427
// This is particularly useful for native JS `async` functions where the
428428
// returned value will "just work" and be passed back to C++.
429-
handleAsync(startAsync) {
430-
return Asyncify.handleSleep((wakeUp) => {
431-
// TODO: add error handling as a second param when handleSleep implements it.
432-
startAsync().then(wakeUp);
433-
});
434-
},
429+
handleAsync: (startAsync) => Asyncify.handleSleep((wakeUp) => {
430+
// TODO: add error handling as a second param when handleSleep implements it.
431+
startAsync().then(wakeUp);
432+
}),
435433

436434
#elif ASYNCIFY == 2
437435
//
@@ -452,9 +450,7 @@ addToLibrary({
452450
{{{ runtimeKeepalivePop(); }}}
453451
}
454452
},
455-
handleSleep(startAsync) {
456-
return Asyncify.handleAsync(() => new Promise(startAsync));
457-
},
453+
handleSleep: (startAsync) => Asyncify.handleAsync(() => new Promise(startAsync)),
458454
makeAsyncFunction(original) {
459455
#if ASYNCIFY_DEBUG
460456
dbg('asyncify: makeAsyncFunction for', original);
@@ -466,33 +462,24 @@ addToLibrary({
466462

467463
emscripten_sleep__deps: ['$safeSetTimeout'],
468464
emscripten_sleep__async: true,
469-
emscripten_sleep: (ms) => {
470-
// emscripten_sleep() does not return a value, but we still need a |return|
471-
// here for stack switching support (ASYNCIFY=2). In that mode this function
472-
// returns a Promise instead of nothing, and that Promise is what tells the
473-
// wasm VM to pause the stack.
474-
return Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms));
475-
},
465+
emscripten_sleep: (ms) => Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)),
476466

477467
emscripten_wget_data__deps: ['$asyncLoad', 'malloc'],
478468
emscripten_wget_data__async: true,
479-
emscripten_wget_data: (url, pbuffer, pnum, perror) => {
480-
return Asyncify.handleSleep((wakeUp) => {
481-
/* no need for run dependency, this is async but will not do any prepare etc. step */
482-
asyncLoad(UTF8ToString(url)).then((byteArray) => {
483-
// can only allocate the buffer after the wakeUp, not during an asyncing
484-
var buffer = _malloc(byteArray.length); // must be freed by caller!
485-
HEAPU8.set(byteArray, buffer);
486-
{{{ makeSetValue('pbuffer', 0, 'buffer', '*') }}};
487-
{{{ makeSetValue('pnum', 0, 'byteArray.length', 'i32') }}};
488-
{{{ makeSetValue('perror', 0, '0', 'i32') }}};
489-
wakeUp();
490-
}, () => {
491-
{{{ makeSetValue('perror', 0, '1', 'i32') }}};
492-
wakeUp();
493-
});
494-
});
495-
},
469+
emscripten_wget_data: (url, pbuffer, pnum, perror) => Asyncify.handleAsync(async () => {
470+
/* no need for run dependency, this is async but will not do any prepare etc. step */
471+
try {
472+
const byteArray = await asyncLoad(UTF8ToString(url));
473+
// can only allocate the buffer after the wakeUp, not during an asyncing
474+
var buffer = _malloc(byteArray.length); // must be freed by caller!
475+
HEAPU8.set(byteArray, buffer);
476+
{{{ makeSetValue('pbuffer', 0, 'buffer', '*') }}};
477+
{{{ makeSetValue('pnum', 0, 'byteArray.length', 'i32') }}};
478+
{{{ makeSetValue('perror', 0, '0', 'i32') }}};
479+
} catch (err) {
480+
{{{ makeSetValue('perror', 0, '1', 'i32') }}};
481+
}
482+
}),
496483

497484
emscripten_scan_registers__deps: ['$safeSetTimeout'],
498485
emscripten_scan_registers__async: true,

src/lib/libdylink.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,17 +1245,19 @@ var LibraryDylink = {
12451245
#if ASYNCIFY
12461246
_dlopen_js__async: true,
12471247
#endif
1248-
_dlopen_js: {{{ asyncIf(ASYNCIFY == 2) }}} (handle) => {
1248+
_dlopen_js: {{{ asyncIf(ASYNCIFY == 2) }}} (handle) =>
12491249
#if ASYNCIFY
1250-
return Asyncify.handleSleep((wakeUp) => {
1250+
Asyncify.handleSleep((wakeUp) =>
12511251
dlopenInternal(handle, { loadAsync: true })
1252-
.then(wakeUp)
1253-
.catch(() => wakeUp(0));
1254-
});
1252+
.then(wakeUp)
1253+
// Note: this currently relies on being able to catch errors even from `wakeUp` callback itself.
1254+
// That's why we can't refactor it to `handleAsync` at the moment.
1255+
.catch(() => wakeUp(0))
1256+
)
12551257
#else
1256-
return dlopenInternal(handle, { loadAsync: false });
1258+
dlopenInternal(handle, { loadAsync: false })
12571259
#endif
1258-
},
1260+
,
12591261

12601262
// Async version of dlopen.
12611263
_emscripten_dlopen_js__deps: ['$dlopenInternal', '$callUserCallback', '$dlSetError'],

src/lib/libpromise.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,10 @@ addToLibrary({
266266
#if RUNTIME_DEBUG
267267
dbg(`emscripten_promise_await: ${id}`);
268268
#endif
269-
return Asyncify.handleSleep((wakeUp) => {
270-
getPromise(id).then((value) => {
271-
setPromiseResult(returnValuePtr, true, value);
272-
wakeUp();
273-
}, (value) => {
274-
setPromiseResult(returnValuePtr, false, value);
275-
wakeUp();
276-
});
277-
});
269+
return Asyncify.handleAsync(() => getPromise(id).then(
270+
value => setPromiseResult(returnValuePtr, true, value),
271+
error => setPromiseResult(returnValuePtr, false, error)
272+
));
278273
#else
279274
abort('emscripten_promise_await is only available with ASYNCIFY');
280275
#endif

src/lib/libwasi.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -544,11 +544,7 @@ var WasiLibrary = {
544544
return;
545545
}
546546
mount.type.syncfs(mount, false, (err) => {
547-
if (err) {
548-
wakeUp({{{ cDefs.EIO }}});
549-
return;
550-
}
551-
wakeUp(0);
547+
wakeUp(err ? {{{ cDefs.EIO }}} : 0);
552548
});
553549
});
554550
#else

0 commit comments

Comments
 (0)