Skip to content

Commit 59cb965

Browse files
authored
Mark --preload-file as incompatible with MINIMAL_RUNTIME. NFC (#24910)
The file preloading mechanism relies on either `Module.preRun` or `Module.calledRun`, neither of which are available with `MINIMAL_RUNTIME`.
1 parent dfd6162 commit 59cb965

File tree

7 files changed

+44
-20
lines changed

7 files changed

+44
-20
lines changed

src/lib/libcore.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2104,6 +2104,14 @@ addToLibrary({
21042104
}
21052105
},
21062106

2107+
$asyncLoad: async (url) => {
2108+
var arrayBuffer = await readAsync(url);
2109+
#if ASSERTIONS
2110+
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2111+
#endif
2112+
return new Uint8Array(arrayBuffer);
2113+
},
2114+
21072115
#else // MINIMAL_RUNTIME
21082116
// MINIMAL_RUNTIME doesn't support the runtimeKeepalive stuff
21092117
$callUserCallback: (func) => func(),
@@ -2116,14 +2124,6 @@ addToLibrary({
21162124
return x.startsWith('dynCall_') ? x : '_' + x;
21172125
},
21182126

2119-
$asyncLoad: async (url) => {
2120-
var arrayBuffer = await readAsync(url);
2121-
#if ASSERTIONS
2122-
assert(arrayBuffer, `Loading data file "${url}" failed (no arrayBuffer).`);
2123-
#endif
2124-
return new Uint8Array(arrayBuffer);
2125-
},
2126-
21272127
$alignMemory: (size, alignment) => {
21282128
#if ASSERTIONS
21292129
assert(alignment, "alignment argument is required");

src/lib/libfs.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
var LibraryFS = {
88
$FS__deps: ['$randomFill', '$PATH', '$PATH_FS', '$TTY', '$MEMFS',
9-
'$FS_createPreloadedFile',
109
'$FS_modeStringToFlags',
1110
'$FS_getMode',
1211
'$intArrayFromString',
@@ -27,6 +26,9 @@ var LibraryFS = {
2726
#endif
2827
#if ASSERTIONS
2928
'$strError', '$ERRNO_CODES',
29+
#endif
30+
#if !MINIMAL_RUNTIME
31+
'$FS_createPreloadedFile',
3032
#endif
3133
],
3234
$FS__postset: () => {
@@ -35,7 +37,9 @@ var LibraryFS = {
3537
addAtPostCtor('FS.ignorePermissions = false;');
3638
addAtExit('FS.quit();');
3739
return `
40+
#if !MINIMAL_RUNTIME
3841
FS.createPreloadedFile = FS_createPreloadedFile;
42+
#endif
3943
FS.staticInit();`;
4044
},
4145
$FS: {

src/lib/libfs_shared.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ addToLibrary({
2929
});
3030
return handled;
3131
},
32-
#endif
3332

3433
// Preloads a file asynchronously. You can call this before run, for example in
3534
// preRun. run will be delayed until this file arrives and is set up.
@@ -48,9 +47,7 @@ addToLibrary({
4847
'$PATH_FS',
4948
'$FS_createDataFile',
5049
'$getUniqueRunDependency',
51-
#if !MINIMAL_RUNTIME
5250
'$FS_handledByPreloadPlugin',
53-
#endif
5451
],
5552
$FS_createPreloadedFile: (parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile, canOwn, preFinish) => {
5653
// TODO we should allow people to just pass in a complete filename instead
@@ -66,15 +63,12 @@ addToLibrary({
6663
onload?.();
6764
removeRunDependency(dep);
6865
}
69-
#if !MINIMAL_RUNTIME
70-
if (FS_handledByPreloadPlugin(byteArray, fullname, finish, () => {
66+
if (!FS_handledByPreloadPlugin(byteArray, fullname, finish, () => {
7167
onerror?.();
7268
removeRunDependency(dep);
7369
})) {
74-
return;
70+
finish(byteArray);
7571
}
76-
#endif
77-
finish(byteArray);
7872
}
7973
addRunDependency(dep);
8074
if (typeof url == 'string') {
@@ -83,6 +77,8 @@ addToLibrary({
8377
processData(url);
8478
}
8579
},
80+
#endif
81+
8682
// convert the 'r', 'r+', etc. to it's corresponding set of O_* flags
8783
$FS_modeStringToFlags: (str) => {
8884
var flagModes = {

test/code_size/test_codesize_hello_O0.json

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

test/other/codesize/test_codesize_minimal_O0.expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ Module['FS_createPreloadedFile'] = FS.createPreloadedFile;
903903
'runtimeKeepalivePop',
904904
'callUserCallback',
905905
'maybeExit',
906-
'asmjsMangle',
907906
'asyncLoad',
907+
'asmjsMangle',
908908
'alignMemory',
909909
'mmapAlloc',
910910
'HandleAllocator',

test/test_other.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,9 @@ def test_minimal_runtime_errors(self):
16671667
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMINIMAL_RUNTIME', '-sMINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION', '-oout.html', '-sSINGLE_FILE'])
16681668
self.assertContained('emcc: error: MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION is not compatible with SINGLE_FILE', err)
16691669

1670+
err = self.expect_fail([EMCC, test_file('hello_world.c'), '-sMINIMAL_RUNTIME', '--preload-file', 'foo'])
1671+
self.assertContained('emcc: error: MINIMAL_RUNTIME is not compatible with --preload-file', err)
1672+
16701673
def test_export_all_and_exported_functions(self):
16711674
# EXPORT_ALL should not export library functions by default.
16721675
# This means that to export library function you also need to explicitly
@@ -16472,3 +16475,21 @@ def test_TextDecoder_invalid(self):
1647216475

1647316476
def test_reallocarray(self):
1647416477
self.do_other_test('test_reallocarray.c')
16478+
16479+
def test_create_preloaded_file(self):
16480+
# Test that the FS.createPreloadedFile API works
16481+
create_file('post.js', "FS.createPreloadedFile('/', 'someotherfile.txt', 'somefile.txt', true, false);")
16482+
create_file('somefile.txt', 'hello')
16483+
create_file('main.c', r'''
16484+
#include <stdio.h>
16485+
#include <assert.h>
16486+
#include <sys/stat.h>
16487+
16488+
int main() {
16489+
struct stat buf;
16490+
int rtn = stat("someotherfile.txt", &buf);
16491+
assert(rtn == 0);
16492+
printf("done\n");
16493+
return 0;
16494+
}''')
16495+
self.do_runf('main.c', 'done\n', cflags=['-sFORCE_FILESYSTEM', '--post-js=post.js'])

tools/link.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,9 @@ def limit_incoming_module_api():
893893
if options.use_preload_plugins or len(options.preload_files):
894894
exit_with_error('MODULARIZE=instance is not compatible with --embed-file/--preload-file')
895895

896+
if settings.MINIMAL_RUNTIME and len(options.preload_files):
897+
exit_with_error('MINIMAL_RUNTIME is not compatible with --preload-file')
898+
896899
if options.oformat in (OFormat.WASM, OFormat.BARE):
897900
if options.emit_tsd:
898901
exit_with_error('Wasm only output is not compatible --emit-tsd')

0 commit comments

Comments
 (0)