diff --git a/src/library_lz4.js b/src/library_lz4.js index 6adb67d3e..210210570 100644 --- a/src/library_lz4.js +++ b/src/library_lz4.js @@ -20,7 +20,7 @@ mergeInto(LibraryManager.library, { })(); LZ4.CHUNK_SIZE = LZ4.codec.CHUNK_SIZE; }, - loadPackage: function (pack) { + loadPackage: function (pack, preloadPlugin) { LZ4.init(); var compressedData = pack['compressedData']; if (!compressedData) compressedData = LZ4.codec.compressPackage(pack['data']); @@ -42,6 +42,31 @@ mergeInto(LibraryManager.library, { end: file.end, }); }); + // Preload files if necessary. This code is largely similar to + // createPreloadedFile in library_fs.js. However, a main difference here + // is that we only decompress the file if it can be preloaded. + // Abstracting out the common parts seems to be more effort than it is + // worth. + if (preloadPlugin) { + Browser.init(); + pack['metadata'].files.forEach(function(file) { + var handled = false; + var fullname = file.filename; + Module['preloadPlugins'].forEach(function(plugin) { + if (handled) return; + if (plugin['canHandle'](fullname)) { + var dep = getUniqueRunDependency('fp ' + fullname); + addRunDependency(dep); + var finish = function() { + removeRunDependency(dep); + } + var byteArray = FS.readFile(fullname); + plugin['handle'](byteArray, fullname, finish, finish); + handled = true; + } + }); + }); + } }, createNode: function (parent, name, mode, dev, contents, mtime) { var node = FS.createNode(parent, name, mode); diff --git a/tests/test_browser.py b/tests/test_browser.py index 555ea8bc3..0bd89ec99 100644 --- a/tests/test_browser.py +++ b/tests/test_browser.py @@ -2415,7 +2415,11 @@ void *getBindBuffer() { self.compile_btest([path_from_root('tests', 'browser_module.cpp'), '-o', 'lib.wasm', '-O2', '-s', 'SIDE_MODULE', '-s', 'EXPORTED_FUNCTIONS=[_one,_two]']) self.btest('browser_main.cpp', args=['-O2', '-s', 'MAIN_MODULE'], expected='8') - def test_preload_module(self): + @parameterized({ + 'non-lz4': ([],), + 'lz4': (['-s', 'LZ4'],) + }) + def test_preload_module(self, args): create_test_file('library.c', r''' #include int library_func() { @@ -2449,7 +2453,7 @@ void *getBindBuffer() { ''') self.btest_exit( 'main.c', - args=['-s', 'MAIN_MODULE', '--preload-file', '.@/', '-O2', '--use-preload-plugins', '-s', 'EXPORT_ALL'], + args=['-s', 'MAIN_MODULE', '--preload-file', '.@/', '-O2', '--use-preload-plugins', '-s', 'EXPORT_ALL'] + args, expected='0') def test_mmap_file(self): diff --git a/tools/file_packager.py b/tools/file_packager.py index c9baadae0..cd70b4b7d 100755 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -516,9 +516,9 @@ def main(): var compressedData = %s; compressedData['data'] = byteArray; assert(typeof Module.LZ4 === 'object', 'LZ4 not present - was your app build with -s LZ4=1 ?'); - Module.LZ4.loadPackage({ 'metadata': metadata, 'compressedData': compressedData }); + Module.LZ4.loadPackage({ 'metadata': metadata, 'compressedData': compressedData }, %s); Module['removeRunDependency']('datafile_%s'); - ''' % (meta, shared.JS.escape_for_js_string(data_target)) + ''' % (meta, "true" if use_preload_plugins else "false", shared.JS.escape_for_js_string(data_target)) package_uuid = uuid.uuid4() package_name = data_target