Skip to content

Commit 319a76c

Browse files
authored
Replace FS_createPreloadedFile with async version (#24917)
The new `FS_preloadFile` API replaces the `FS_createPreloadedFile` API. Keeping the old version around for compatibility. Followup to #24914
1 parent b6ab3e4 commit 319a76c

14 files changed

+63
-53
lines changed

src/jsifier.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ function stringifyWithFunctions(obj) {
102102
for (const [key, value] of Object.entries(obj)) {
103103
var str = stringifyWithFunctions(value);
104104
// Handle JS method syntax where the function property starts with its own
105-
// name. e.g. foo(a) {},
106-
if (typeof value === 'function' && str.startsWith(key)) {
105+
// name. e.g. `foo(a) {}` (or `async foo(a) {}`)
106+
if (typeof value === 'function' && (str.startsWith(key) || str.startsWith('async ' + key))) {
107107
rtn += str + ',\n';
108108
} else {
109109
rtn += escapeJSONKey(key) + ':' + str + ',\n';

src/lib/libfs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var LibraryFS = {
3939
return `
4040
#if !MINIMAL_RUNTIME
4141
FS.createPreloadedFile = FS_createPreloadedFile;
42+
FS.preloadFile = FS_preloadFile;
4243
#endif
4344
FS.staticInit();`;
4445
},

src/lib/libfs_shared.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ addToLibrary({
3232
return byteArray;
3333
},
3434

35+
// Legacy version of FS_preloadFile that uses callback rather than async
36+
$FS_createPreloadedFile__deps: ['$FS_preloadFile'],
37+
$FS_createPreloadedFile: (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
38+
FS_preloadFile(parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish).then(onload).catch(onerror);
39+
},
40+
3541
// Preloads a file asynchronously. You can call this before run, for example in
3642
// preRun. run will be delayed until this file arrives and is set up.
3743
// If you call it after run(), you may want to pause the main loop until it
@@ -44,39 +50,33 @@ addToLibrary({
4450
// You can also call this with a typed array instead of a url. It will then
4551
// do preloading for the Image/Audio part, as if the typed array were the
4652
// result of an XHR that you did manually.
47-
$FS_createPreloadedFile__deps: [
53+
$FS_preloadFile__deps: [
4854
'$asyncLoad',
4955
'$PATH_FS',
5056
'$FS_createDataFile',
5157
'$getUniqueRunDependency',
5258
'$FS_handledByPreloadPlugin',
5359
],
54-
$FS_createPreloadedFile: (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
60+
$FS_preloadFile: async (parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish) => {
5561
// TODO we should allow people to just pass in a complete filename instead
5662
// of parent and name being that we just join them anyways
5763
var fullname = name ? PATH_FS.resolve(PATH.join2(parent, name)) : parent;
5864
var dep = getUniqueRunDependency(`cp ${fullname}`); // might have several active requests for the same fullname
59-
function processData(byteArray) {
60-
FS_handledByPreloadPlugin(byteArray, fullname)
61-
.then((byteArray) => {
62-
preFinish?.();
63-
if (!dontCreateFile) {
64-
FS_createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
65-
}
66-
onload?.();
67-
removeRunDependency(dep);
68-
})
69-
.catch(() => {
70-
onerror?.();
71-
removeRunDependency(dep);
72-
});
73-
}
74-
7565
addRunDependency(dep);
76-
if (typeof url == 'string') {
77-
asyncLoad(url).then(processData, onerror);
78-
} else {
79-
processData(url);
66+
67+
try {
68+
var byteArray = url;
69+
if (typeof url == 'string') {
70+
byteArray = await asyncLoad(url);
71+
}
72+
73+
byteArray = await FS_handledByPreloadPlugin(byteArray, fullname);
74+
preFinish?.();
75+
if (!dontCreateFile) {
76+
FS_createDataFile(parent, name, byteArray, canRead, canWrite, canOwn);
77+
}
78+
} finally {
79+
removeRunDependency(dep);
8080
}
8181
},
8282
#endif

src/lib/libwasmfs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ addToLibrary({
3333
'$readI53FromU64',
3434
'$FS_createDataFile',
3535
'$FS_createPreloadedFile',
36+
'$FS_preloadFile',
3637
'$FS_getMode',
3738
// For FS.readFile
3839
'$UTF8ArrayToString',
@@ -117,6 +118,10 @@ addToLibrary({
117118
return FS_createPreloadedFile(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish);
118119
},
119120

121+
async preloadFile(parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish) {
122+
return FS_preloadFile(parent, name, url, canRead, canWrite, dontCreateFile, canOwn, preFinish);
123+
},
124+
120125
#if hasExportedSymbol('_wasmfs_read_file') // Support the JS function exactly
121126
// when the __wasmfs_* function is
122127
// present to be called (otherwise,

src/lib/libwget.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var LibraryWget = {
2020
'$PATH_FS', '$callUserCallback', '$Browser',
2121
'$withStackSave', '$stringToUTF8OnStack',
2222
'$FS_mkdirTree',
23-
'$FS_createPreloadedFile',
23+
'$FS_preloadFile',
2424
'$FS_unlink',
2525
],
2626
emscripten_async_wget__proxy: 'sync',
@@ -37,12 +37,10 @@ var LibraryWget = {
3737
}
3838
}
3939
var destinationDirectory = PATH.dirname(_file);
40-
FS_createPreloadedFile(
40+
FS_preloadFile(
4141
destinationDirectory,
4242
PATH.basename(_file),
4343
_url, true, true,
44-
() => doCallback(onload),
45-
() => doCallback(onerror),
4644
false, // dontCreateFile
4745
false, // canOwn
4846
() => { // preFinish
@@ -53,7 +51,7 @@ var LibraryWget = {
5351
// if the destination directory does not yet exist, create it
5452
FS_mkdirTree(destinationDirectory);
5553
}
56-
);
54+
).then(() => doCallback(onload)).catch(() => doCallback(onerror));
5755
},
5856

5957
emscripten_async_wget_data__deps: ['$asyncLoad', 'malloc', 'free', '$callUserCallback'],

src/runtime_debug.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function isExportedByForceFilesystem(name) {
6767
return name === 'FS_createPath' ||
6868
name === 'FS_createDataFile' ||
6969
name === 'FS_createPreloadedFile' ||
70+
name === 'FS_preloadFile' ||
7071
name === 'FS_unlink' ||
7172
name === 'addRunDependency' ||
7273
#if !WASMFS

test/code_size/test_codesize_hello_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 22462,
3-
"a.out.js.gz": 8335,
2+
"a.out.js": 22503,
3+
"a.out.js.gz": 8341,
44
"a.out.nodebug.wasm": 15143,
55
"a.out.nodebug.wasm.gz": 7452,
6-
"total": 37605,
7-
"total_gz": 15787,
6+
"total": 37646,
7+
"total_gz": 15793,
88
"sent": [
99
"fd_write"
1010
],

test/code_size/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 246862,
2+
"a.out.js": 246888,
33
"a.out.nodebug.wasm": 597826,
4-
"total": 844688,
4+
"total": 844714,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/code_size/test_codesize_minimal_O0.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 17723,
3-
"a.out.js.gz": 6621,
2+
"a.out.js": 17764,
3+
"a.out.js.gz": 6628,
44
"a.out.nodebug.wasm": 1136,
55
"a.out.nodebug.wasm.gz": 659,
6-
"total": 18859,
7-
"total_gz": 7280,
6+
"total": 18900,
7+
"total_gz": 7287,
88
"sent": [],
99
"imports": [],
1010
"exports": [
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"hello_world.js": 53923,
3-
"hello_world.js.gz": 17123,
2+
"hello_world.js": 53981,
3+
"hello_world.js.gz": 17132,
44
"hello_world.wasm": 15143,
55
"hello_world.wasm.gz": 7452,
66
"no_asserts.js": 26714,
77
"no_asserts.js.gz": 8952,
88
"no_asserts.wasm": 12227,
99
"no_asserts.wasm.gz": 6008,
10-
"strict.js": 51973,
11-
"strict.js.gz": 16455,
10+
"strict.js": 52031,
11+
"strict.js.gz": 16465,
1212
"strict.wasm": 15143,
1313
"strict.wasm.gz": 7438,
14-
"total": 175123,
15-
"total_gz": 63428
14+
"total": 175239,
15+
"total_gz": 63447
1616
}

0 commit comments

Comments
 (0)