Skip to content

Commit 0fe3e31

Browse files
authored
Fix for --preload-file + --embed-file (#17457)
Without this the embedded file is loaded twice (once via JS and once via the object file) causing a runtime error. I noticed this while working on the interactive tests, one of which actually does this.
1 parent b0579ab commit 0fe3e31

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

tests/test_other.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,11 +1554,13 @@ def test_export_from_archive(self):
15541554

15551555
@parameterized({
15561556
'embed': (['--embed-file', 'somefile.txt'],),
1557-
'embed-twice': (['--embed-file', 'somefile.txt', '--embed-file', 'somefile.txt'],),
1558-
'preload': (['--preload-file', 'somefile.txt'],)
1557+
'embed_twice': (['--embed-file', 'somefile.txt', '--embed-file', 'somefile.txt'],),
1558+
'preload': (['--preload-file', 'somefile.txt'],),
1559+
'preload_and_embed': (['--preload-file', 'somefile.txt', '--embed-file', 'hello.txt'],)
15591560
})
15601561
def test_include_file(self, args):
15611562
create_file('somefile.txt', 'hello from a file with lots of data and stuff in it thank you very much')
1563+
create_file('hello.txt', 'hello world')
15621564
create_file('main.c', r'''
15631565
#include <assert.h>
15641566
#include <stdio.h>

tools/file_packager.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,13 @@ def generate_js(data_target, data_files, metadata):
682682
dirname = os.path.dirname(filename)
683683
basename = os.path.basename(filename)
684684
if file_.mode == 'embed':
685-
# Embed
686-
data = base64_encode(utils.read_binary(file_.srcpath))
687-
code += " var fileData%d = '%s';\n" % (counter, data)
688-
# canOwn this data in the filesystem (i.e. there is no need to create a copy in the FS layer).
689-
code += (" Module['FS_createDataFile']('%s', '%s', decodeBase64(fileData%d), true, true, true);\n"
690-
% (dirname, basename, counter))
685+
if not options.obj_output:
686+
# Embed (only needed when not generating object file output)
687+
data = base64_encode(utils.read_binary(file_.srcpath))
688+
code += " var fileData%d = '%s';\n" % (counter, data)
689+
# canOwn this data in the filesystem (i.e. there is no need to create a copy in the FS layer).
690+
code += (" Module['FS_createDataFile']('%s', '%s', decodeBase64(fileData%d), true, true, true);\n"
691+
% (dirname, basename, counter))
691692
elif file_.mode == 'preload':
692693
# Preload
693694
metadata_el = {

0 commit comments

Comments
 (0)