Skip to content

Commit a56817a

Browse files
authored
Switch from addAtMain to addAtInit in library_fs.js (#14306)
Using addAtMain stopped working for programs without main functions (HAS_MAIN not defined) after #13901. Code that needs to populate the FS is mostly likely going to be run during preRun callback which all happen before ATINITs.
1 parent 3c0587f commit a56817a

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/library_fs.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ mergeInto(LibraryManager.library, {
2727
],
2828
$FS__postset: function() {
2929
// TODO: do we need noFSInit?
30-
addAtInit('if (!Module["noFSInit"] && !FS.init.initialized) FS.init();');
31-
addAtMain('FS.ignorePermissions = false;');
30+
addAtInit(`
31+
if (!Module["noFSInit"] && !FS.init.initialized)
32+
FS.init();
33+
FS.ignorePermissions = false;
34+
`)
3235
addAtExit('FS.quit();');
3336
// We must statically create FS.FSNode here so that it is created in a manner
3437
// that is visible to Closure compiler. That lets us use type annotations for
3538
// Closure to the "this" pointer in various node creation functions.
36-
return `var FSNode = /** @constructor */ function(parent, name, mode, rdev) {
39+
return `
40+
var FSNode = /** @constructor */ function(parent, name, mode, rdev) {
3741
if (!parent) {
3842
parent = this; // root node sets parent to itself
3943
}
@@ -97,8 +101,8 @@ FS.staticInit();` +
97101
initialized: false,
98102
// Whether we are currently ignoring permissions. Useful when preparing the
99103
// filesystem and creating files inside read-only folders.
100-
// This is set to false when the runtime is initialized, allowing you
101-
// to modify the filesystem freely before run() is called.
104+
// This is set to false during `preInit`, allowing you to modify the
105+
// filesystem freely up until that point (e.g. during `preRun`).
102106
ignorePermissions: true,
103107
trackingDelegate: {},
104108
tracking: {

src/parseTools.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,8 @@ function addAtInit(code) {
10781078
ATINITS.push(code);
10791079
}
10801080

1081+
// TODO(sbc): There are no more uses to ATMAINS or addAtMain in emscripten.
1082+
// We should look into removing these.
10811083
global.ATMAINS = [];
10821084

10831085
function addAtMain(code) {

tests/test_core.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5328,6 +5328,27 @@ def test_fs_mmap(self):
53285328
self.emcc_args += ['-lnodefs.js', '-lnoderawfs.js']
53295329
self.do_run_in_out_file_test('fs/test_mmap.c')
53305330

5331+
@parameterized({
5332+
'': [],
5333+
'minimal_runtime': ['-s', 'MINIMAL_RUNTIME=1']
5334+
})
5335+
def test_fs_no_main(self, *args):
5336+
# library_fs.js uses hooks to enable ignoreing of permisions up until ATMAINs are run. This
5337+
# test verified that they work correctly, even in programs without a main function.
5338+
create_file('pre.js', '''
5339+
Module['preRun'] = function() {
5340+
assert(FS.ignorePermissions, "ignorePermissions not set during preRun");
5341+
}
5342+
Module['onRuntimeInitialized'] = function() {
5343+
assert(!FS.ignorePermissions, "ignorePermissions not unset during onRuntimeInitialized");
5344+
assert(_foo() == 42);
5345+
}
5346+
''')
5347+
self.set_setting('EXPORTED_FUNCTIONS', '_foo')
5348+
self.set_setting('FORCE_FILESYSTEM')
5349+
self.emcc_args += ['--pre-js', 'pre.js'] + list(args)
5350+
self.do_run('int foo() { return 42; }', '', force_c=True)
5351+
53315352
@also_with_noderawfs
53325353
def test_fs_errorstack(self):
53335354
# Enables strict mode, which may catch some strict-mode-only errors

0 commit comments

Comments
 (0)