Skip to content

[WasmFS] Support file embedding #16105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/library_wasmfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var WasmfsLibrary = {
createDataFile: function(parent, name, data, canRead, canWrite, canOwn) {
// Data files must be cached until the file system itself has been initialized.
var mode = FS.getMode(canRead, canWrite);
wasmFS$preloadedFiles.push({pathName: parent, fileData: data, mode: mode});
var pathName = name ? parent + '/' + name : parent;
wasmFS$preloadedFiles.push({pathName: pathName, fileData: data, mode: mode});
},
createPath: function(parent, path, canRead, canWrite) {
// Cache file path directory names.
Expand Down
8 changes: 6 additions & 2 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -1502,7 +1502,11 @@ def test_include_file(self, args):
result = self.run_js('a.out.js', engine=config.NODE_JS)
self.assertContained('|hello from a file wi|', result)

def test_embed_file_dup(self):
@parameterized({
'': ([],),
'wasmfs': (['-sWASMFS'],),
})
def test_embed_file_dup(self, args):
ensure_dir(self.in_dir('tst', 'test1'))
ensure_dir(self.in_dir('tst', 'test2'))

Expand All @@ -1529,7 +1533,7 @@ def test_embed_file_dup(self):
}
''')

self.run_process([EMXX, 'main.cpp', '--embed-file', 'tst'])
self.run_process([EMXX, 'main.cpp', '--embed-file', 'tst'] + args)
self.assertContained('|frist|\n|sacond|\n|thard|\n', self.run_js('a.out.js'))

def test_exclude_file(self):
Expand Down
2 changes: 1 addition & 1 deletion tools/file_packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ def generate_js(data_target, data_files, metadata):
var content = HEAPU32[start32++];
var name = UTF8ToString(name_addr)
// canOwn this data in the filesystem, it is a slice of wasm memory that will never change
Module['FS_createDataFile'](undefined, name, HEAP8.subarray(content, content + len), true, true, true);
Module['FS_createDataFile'](name, null, HEAP8.subarray(content, content + len), true, true, true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems odd to me too. The way I handled this in #16050 I guess made it work both wasy:

-      var path = name ? PATH.join2(typeof parent === 'string' ? parent : FS.getPath(parent), name) : parent;
+      var path = name;
+      if (parent) {
+        parent = typeof parent === 'string' ? parent : FS.getPath(parent);
+        path = name ? PATH.join2(parent, name) : parent;
+      }

} while (HEAPU32[start32]);'''
else:
err('--obj-output is recommended when using --embed. This outputs an object file for linking directly into your application is more effecient than JS encoding')
Expand Down