Skip to content

Commit 5f17fe8

Browse files
sbc100hedwigz
authored andcommitted
Convert asyncLoad from callbacks to promise. NFC (emscripten-core#23065)
This is an internal function that should not have any external users.
1 parent 7ec8611 commit 5f17fe8

File tree

7 files changed

+23
-27
lines changed

7 files changed

+23
-27
lines changed

src/library.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,25 +2195,19 @@ addToLibrary({
21952195
},
21962196

21972197
$asyncLoad__docs: '/** @param {boolean=} noRunDep */',
2198-
$asyncLoad: (url, onload, onerror, noRunDep) => {
2199-
var dep = !noRunDep ? getUniqueRunDependency(`al ${url}`) : '';
2200-
readAsync(url).then(
2201-
(arrayBuffer) => {
2202-
#if ASSERTIONS
2203-
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2204-
#endif
2205-
onload(new Uint8Array(arrayBuffer));
2206-
if (dep) removeRunDependency(dep);
2207-
},
2208-
(err) => {
2209-
if (onerror) {
2210-
onerror();
2211-
} else {
2212-
throw `Loading data file "${url}" failed.`;
2213-
}
2214-
}
2215-
);
2216-
if (dep) addRunDependency(dep);
2198+
$asyncLoad: (url, noRunDep) => {
2199+
return new Promise((resolve, reject) => {
2200+
var dep = !noRunDep ? getUniqueRunDependency(`al ${url}`) : '';
2201+
if (dep) addRunDependency(dep);
2202+
readAsync(url).then(
2203+
(arrayBuffer) => {
2204+
#if ASSERTIONS
2205+
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2206+
#endif
2207+
resolve(new Uint8Array(arrayBuffer));
2208+
if (dep) removeRunDependency(dep);
2209+
}, reject);
2210+
});
22172211
},
22182212

22192213
$alignMemory: (size, alignment) => {

src/library_async.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ addToLibrary({
470470
emscripten_wget_data__async: true,
471471
emscripten_wget_data: (url, pbuffer, pnum, perror) => {
472472
return Asyncify.handleSleep((wakeUp) => {
473-
asyncLoad(UTF8ToString(url), (byteArray) => {
473+
/* no need for run dependency, this is async but will not do any prepare etc. step */
474+
asyncLoad(UTF8ToString(url), /*noRunDep=*/true).then((byteArray) => {
474475
// can only allocate the buffer after the wakeUp, not during an asyncing
475476
var buffer = _malloc(byteArray.length); // must be freed by caller!
476477
HEAPU8.set(byteArray, buffer);
@@ -481,7 +482,7 @@ addToLibrary({
481482
}, () => {
482483
{{{ makeSetValue('perror', 0, '1', 'i32') }}};
483484
wakeUp();
484-
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
485+
});
485486
});
486487
},
487488

src/library_dylink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ var LibraryDylink = {
10241024

10251025
var libFile = locateFile(libName);
10261026
if (flags.loadAsync) {
1027-
return new Promise((resolve, reject) => asyncLoad(libFile, resolve, reject));
1027+
return asyncLoad(libFile);
10281028
}
10291029

10301030
// load the binary synchronously

src/library_fs_shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ addToLibrary({
7676
}
7777
addRunDependency(dep);
7878
if (typeof url == 'string') {
79-
asyncLoad(url, processData, onerror);
79+
asyncLoad(url).then(processData, onerror);
8080
} else {
8181
processData(url);
8282
}

src/library_wget.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ var LibraryWget = {
6464
emscripten_async_wget_data__proxy: 'sync',
6565
emscripten_async_wget_data: (url, userdata, onload, onerror) => {
6666
{{{ runtimeKeepalivePush() }}}
67-
asyncLoad(UTF8ToString(url), (byteArray) => {
67+
/* no need for run dependency, this is async but will not do any prepare etc. step */
68+
asyncLoad(UTF8ToString(url), /*noRunDep=*/true).then((byteArray) => {
6869
{{{ runtimeKeepalivePop() }}}
6970
callUserCallback(() => {
7071
var buffer = _malloc(byteArray.length);
@@ -79,7 +80,7 @@ var LibraryWget = {
7980
{{{ makeDynCall('vp', 'onerror') }}}(userdata);
8081
});
8182
}
82-
}, true /* no need for run dependency, this is async but will not do any prepare etc. step */ );
83+
});
8384
},
8485

8586
emscripten_async_wget2__deps: ['$PATH_FS', '$wget', '$stackRestore', '$stringToUTF8OnStack'],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6339
1+
6298
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13936
1+
13867

0 commit comments

Comments
 (0)