Skip to content

Commit

Permalink
Switch from addAtMain to addAtInit in library_fs.js (#14306)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sbc100 authored May 27, 2021
1 parent 3c0587f commit a56817a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/library_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ mergeInto(LibraryManager.library, {
],
$FS__postset: function() {
// TODO: do we need noFSInit?
addAtInit('if (!Module["noFSInit"] && !FS.init.initialized) FS.init();');
addAtMain('FS.ignorePermissions = false;');
addAtInit(`
if (!Module["noFSInit"] && !FS.init.initialized)
FS.init();
FS.ignorePermissions = false;
`)
addAtExit('FS.quit();');
// We must statically create FS.FSNode here so that it is created in a manner
// that is visible to Closure compiler. That lets us use type annotations for
// Closure to the "this" pointer in various node creation functions.
return `var FSNode = /** @constructor */ function(parent, name, mode, rdev) {
return `
var FSNode = /** @constructor */ function(parent, name, mode, rdev) {
if (!parent) {
parent = this; // root node sets parent to itself
}
Expand Down Expand Up @@ -97,8 +101,8 @@ FS.staticInit();` +
initialized: false,
// Whether we are currently ignoring permissions. Useful when preparing the
// filesystem and creating files inside read-only folders.
// This is set to false when the runtime is initialized, allowing you
// to modify the filesystem freely before run() is called.
// This is set to false during `preInit`, allowing you to modify the
// filesystem freely up until that point (e.g. during `preRun`).
ignorePermissions: true,
trackingDelegate: {},
tracking: {
Expand Down
2 changes: 2 additions & 0 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,8 @@ function addAtInit(code) {
ATINITS.push(code);
}

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

function addAtMain(code) {
Expand Down
21 changes: 21 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5328,6 +5328,27 @@ def test_fs_mmap(self):
self.emcc_args += ['-lnodefs.js', '-lnoderawfs.js']
self.do_run_in_out_file_test('fs/test_mmap.c')

@parameterized({
'': [],
'minimal_runtime': ['-s', 'MINIMAL_RUNTIME=1']
})
def test_fs_no_main(self, *args):
# library_fs.js uses hooks to enable ignoreing of permisions up until ATMAINs are run. This
# test verified that they work correctly, even in programs without a main function.
create_file('pre.js', '''
Module['preRun'] = function() {
assert(FS.ignorePermissions, "ignorePermissions not set during preRun");
}
Module['onRuntimeInitialized'] = function() {
assert(!FS.ignorePermissions, "ignorePermissions not unset during onRuntimeInitialized");
assert(_foo() == 42);
}
''')
self.set_setting('EXPORTED_FUNCTIONS', '_foo')
self.set_setting('FORCE_FILESYSTEM')
self.emcc_args += ['--pre-js', 'pre.js'] + list(args)
self.do_run('int foo() { return 42; }', '', force_c=True)

@also_with_noderawfs
def test_fs_errorstack(self):
# Enables strict mode, which may catch some strict-mode-only errors
Expand Down

0 comments on commit a56817a

Please sign in to comment.