Skip to content

Commit d9960a6

Browse files
committed
More use of async/await keywords in JS library code. NFC
1 parent 1baef43 commit d9960a6

12 files changed

+58
-59
lines changed

src/embind/emval.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,9 @@ var LibraryEmVal = {
447447
_emval_await__deps: ['$Emval', '$Asyncify'],
448448
_emval_await__async: true,
449449
_emval_await: (promise) => {
450-
return Asyncify.handleAsync(() => {
451-
promise = Emval.toValue(promise);
452-
return promise.then(Emval.toHandle);
450+
return Asyncify.handleAsync(async () => {
451+
var value = await Emval.toValue(promise);
452+
return Emval.toHandle(value);
453453
});
454454
},
455455
#endif
@@ -468,10 +468,9 @@ var LibraryEmVal = {
468468
},
469469

470470
_emval_coro_suspend__deps: ['$Emval', '_emval_coro_resume'],
471-
_emval_coro_suspend: (promiseHandle, awaiterPtr) => {
472-
Emval.toValue(promiseHandle).then(result => {
473-
__emval_coro_resume(awaiterPtr, Emval.toHandle(result));
474-
});
471+
_emval_coro_suspend: async (promiseHandle, awaiterPtr) => {
472+
var result = await Emval.toValue(promiseHandle);
473+
__emval_coro_resume(awaiterPtr, Emval.toHandle(result));
475474
},
476475

477476
_emval_coro_make_promise__deps: ['$Emval', '__cxa_rethrow'],

src/jsifier.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ function(${args}) {
338338
MEMORY64 && rtnType == 'p' ? 'proxyToMainThreadPtr' : 'proxyToMainThread';
339339
deps.push('$' + proxyFunc);
340340
return `
341-
function(${args}) {
341+
${async_}function(${args}) {
342342
if (ENVIRONMENT_IS_PTHREAD)
343343
return ${proxyFunc}(${proxiedFunctionTable.length}, 0, ${+sync}${args ? ', ' : ''}${args});
344344
${body}

src/library_browser.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ var LibraryBrowser = {
652652

653653
// TODO: currently not callable from a pthread, but immediately calls onerror() if not on main thread.
654654
emscripten_async_load_script__deps: ['$UTF8ToString'],
655-
emscripten_async_load_script: (url, onload, onerror) => {
655+
emscripten_async_load_script: async (url, onload, onerror) => {
656656
url = UTF8ToString(url);
657657
#if PTHREADS
658658
if (ENVIRONMENT_IS_PTHREAD) {
@@ -687,10 +687,13 @@ var LibraryBrowser = {
687687

688688
#if ENVIRONMENT_MAY_BE_NODE && DYNAMIC_EXECUTION
689689
if (ENVIRONMENT_IS_NODE) {
690-
readAsync(url, false).then((data) => {
690+
try {
691+
var data = await readAsync(url, false);
691692
eval(data);
692693
loadDone();
693-
}, loadError);
694+
} catch {
695+
loadError();
696+
}
694697
return;
695698
}
696699
#endif

src/library_trace.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@ var LibraryTracing = {
5858
},
5959

6060
// Work around CORS issues ...
61-
fetchBlob: (url) => {
62-
return fetch(url).then((rsp) => rsp.blob());
61+
fetchBlob: async (url) => {
62+
var rsp = await fetch(url);
63+
return rsp.blob();
6364
},
6465

65-
configure: (collector_url, application) => {
66+
configure: async (collector_url, application) => {
6667
EmscriptenTrace.now = _emscripten_get_now;
6768
var now = new Date();
6869
var session_id = now.getTime().toString() + '_' +
6970
Math.floor((Math.random() * 100) + 1).toString();
70-
EmscriptenTrace.fetchBlob(collector_url + 'worker.js').then((blob) => {
71-
EmscriptenTrace.worker = new Worker(window.URL.createObjectURL(blob));
72-
EmscriptenTrace.worker.addEventListener('error', (e) => {
73-
out('TRACE WORKER ERROR:');
74-
out(e);
75-
}, false);
76-
EmscriptenTrace.worker.postMessage({ 'cmd': 'configure',
77-
'data_version': EmscriptenTrace.DATA_VERSION,
78-
'session_id': session_id,
79-
'url': collector_url });
80-
EmscriptenTrace.configured = true;
81-
EmscriptenTrace.collectorEnabled = true;
82-
EmscriptenTrace.postEnabled = true;
83-
});
71+
var blob = await EmscriptenTrace.fetchBlob(collector_url + 'worker.js');
72+
EmscriptenTrace.worker = new Worker(window.URL.createObjectURL(blob));
73+
EmscriptenTrace.worker.addEventListener('error', (e) => {
74+
out('TRACE WORKER ERROR:');
75+
out(e);
76+
}, false);
77+
EmscriptenTrace.worker.postMessage({ 'cmd': 'configure',
78+
'data_version': EmscriptenTrace.DATA_VERSION,
79+
'session_id': session_id,
80+
'url': collector_url });
81+
EmscriptenTrace.configured = true;
82+
EmscriptenTrace.collectorEnabled = true;
83+
EmscriptenTrace.postEnabled = true;
8484
EmscriptenTrace.post([EmscriptenTrace.EVENT_APPLICATION_NAME, application]);
8585
EmscriptenTrace.post([EmscriptenTrace.EVENT_SESSION_NAME, now.toISOString()]);
8686
},

src/library_wget.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,26 @@ var LibraryWget = {
6262

6363
emscripten_async_wget_data__deps: ['$asyncLoad', 'malloc', 'free', '$callUserCallback'],
6464
emscripten_async_wget_data__proxy: 'sync',
65-
emscripten_async_wget_data: (url, userdata, onload, onerror) => {
65+
emscripten_async_wget_data: async (url, userdata, onload, onerror) => {
6666
{{{ runtimeKeepalivePush() }}}
6767
/* no need for run dependency, this is async but will not do any prepare etc. step */
68-
asyncLoad(UTF8ToString(url)).then((byteArray) => {
68+
try {
69+
var byteArray = await asyncLoad(UTF8ToString(url));
6970
{{{ runtimeKeepalivePop() }}}
7071
callUserCallback(() => {
7172
var buffer = _malloc(byteArray.length);
7273
HEAPU8.set(byteArray, buffer);
7374
{{{ makeDynCall('vppi', 'onload') }}}(userdata, buffer, byteArray.length);
7475
_free(buffer);
7576
});
76-
}, () => {
77+
} catch (e) {
7778
if (onerror) {
7879
{{{ runtimeKeepalivePop() }}}
7980
callUserCallback(() => {
8081
{{{ makeDynCall('vp', 'onerror') }}}(userdata);
8182
});
8283
}
83-
});
84+
}
8485
},
8586

8687
emscripten_async_wget2__deps: ['$PATH_FS', '$wget', '$stackRestore', '$stringToUTF8OnStack'],

src/postamble.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ dependenciesFulfilled = function runCaller() {
4040

4141
#if HAS_MAIN
4242
#if MAIN_READS_PARAMS
43-
function callMain(args = []) {
43+
{{{ asyncIf(ASYNCIFY == 2) }}} function callMain(args = []) {
4444
#else
45-
function callMain() {
45+
{{{ asyncIf(ASYNCIFY == 2) }}} function callMain() {
4646
#endif
4747
#if ASSERTIONS
4848
assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on Module["onRuntimeInitialized"])');
@@ -100,18 +100,12 @@ function callMain() {
100100
// The current spec of JSPI returns a promise only if the function suspends
101101
// and a plain value otherwise. This will likely change:
102102
// https://github.com/WebAssembly/js-promise-integration/issues/11
103-
Promise.resolve(ret).then((result) => {
104-
exitJS(result, /* implicit = */ true);
105-
}).catch((e) => {
106-
handleException(e);
107-
});
108-
#else
103+
ret = await ret;
104+
#endif // ASYNCIFY == 2
109105
// if we're not running an evented main loop, it's time to exit
110106
exitJS(ret, /* implicit = */ true);
111-
#endif // ASYNCIFY == 2
112107
return ret;
113-
}
114-
catch (e) {
108+
} catch (e) {
115109
return handleException(e);
116110
}
117111
#if ABORT_ON_WASM_EXCEPTIONS

src/preamble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ function getWasmImports() {
10911091
var result = await instantiateAsync(wasmBinary, wasmBinaryFile, info);
10921092
receiveInstantiationResult(result);
10931093
#if LOAD_SOURCE_MAP
1094-
receiveSourceMapJSON(await getSourceMapPromise());
1094+
receiveSourceMapJSON(await getSourceMapAsync());
10951095
#endif
10961096
return result;
10971097
#if MODULARIZE

src/source_map_support.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ function getSourceMap() {
106106
return JSON.parse(UTF8ArrayToString(buf));
107107
}
108108

109-
function getSourceMapPromise() {
109+
async function getSourceMapAsync() {
110110
if (ENVIRONMENT_IS_WEB || ENVIRONMENT_IS_WORKER) {
111-
return fetch(wasmSourceMapFile, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}})
112-
.then((response) => response.json())
113-
.catch(getSourceMap);
111+
try {
112+
var response = await fetch(wasmSourceMapFile, {{{ makeModuleReceiveExpr('fetchSettings', "{ credentials: 'same-origin' }") }}});
113+
return response.json();
114+
} catch {
115+
// Fall back to getSourceMap below
116+
}
114117
}
115-
return Promise.resolve(getSourceMap());
118+
return getSourceMap();
116119
}

test/browser_reporting.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var hasModule = typeof Module === 'object' && Module;
22

33
var reportingURL = 'http://localhost:8888/';
44

5-
function reportResultToServer(result) {
5+
async function reportResultToServer(result) {
66
if (reportResultToServer.reported) {
77
// Only report one result per test, even if the test misbehaves and tries to report more.
88
reportErrorToServer(`excessive reported results, sending ${result}, test will fail`);
@@ -11,12 +11,11 @@ function reportResultToServer(result) {
1111
if ((typeof ENVIRONMENT_IS_NODE !== 'undefined' && ENVIRONMENT_IS_NODE) || (typeof ENVIRONMENT_IS_AUDIO_WORKLET !== 'undefined' && ENVIRONMENT_IS_AUDIO_WORKLET)) {
1212
out(`RESULT: ${result}`);
1313
} else {
14-
fetch(`${reportingURL}/report_result?${encodeURIComponent(result)}`).then(() => {
15-
if (typeof window === 'object' && window && hasModule && !Module['pageThrewException']) {
16-
/* for easy debugging, don't close window on failure */
17-
window.close();
18-
}
19-
});
14+
await fetch(`${reportingURL}/report_result?${encodeURIComponent(result)}`);
15+
if (typeof window === 'object' && window && hasModule && !Module['pageThrewException']) {
16+
/* for easy debugging, don't close window on failure */
17+
window.close();
18+
}
2019
}
2120
}
2221

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
53981
1+
53980
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
29401
1+
29400
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
52777
1+
52776

0 commit comments

Comments
 (0)