Skip to content

Avoid unnecessary use of array.forEach in JS. NFC #24895

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 4 additions & 6 deletions src/lib/libfs_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ addToLibrary({
if (typeof Browser != 'undefined') Browser.init();
#endif

var handled = false;
preloadPlugins.forEach((plugin) => {
if (handled) return;
for (var plugin of preloadPlugins) {
if (plugin['canHandle'](fullname)) {
plugin['handle'](byteArray, fullname, finish, onerror);
handled = true;
return true;
}
});
return handled;
}
return false;
},
#endif

Expand Down
16 changes: 7 additions & 9 deletions src/lib/liblz4.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ addToLibrary({
compressedData['cachedOffset'] + (i+1)*LZ4.CHUNK_SIZE);
assert(compressedData['cachedChunks'][i].length === LZ4.CHUNK_SIZE);
}
pack['metadata'].files.forEach((file) => {
for (var file of pack['metadata'].files) {
var dir = PATH.dirname(file.filename);
var name = PATH.basename(file.filename);
FS.createPath('', dir, true, true);
Expand All @@ -40,29 +40,27 @@ addToLibrary({
start: file.start,
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((file) => {
var handled = false;
for (var file of pack['metadata'].files) {
var fullname = file.filename;
preloadPlugins.forEach((plugin) => {
if (handled) return;
for (var plugin of preloadPlugins) {
if (plugin['canHandle'](fullname)) {
var dep = getUniqueRunDependency('fp ' + fullname);
addRunDependency(dep);
var finish = () => removeRunDependency(dep);
var byteArray = FS.readFile(fullname);
plugin['handle'](byteArray, fullname, finish, finish);
handled = true;
break;
}
});
});
}
}
}
},
createNode(parent, name, mode, dev, contents, mtime) {
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 246847,
"a.out.js": 246838,
"a.out.nodebug.wasm": 597826,
"total": 844673,
"total": 844664,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
5 changes: 3 additions & 2 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -15596,10 +15596,11 @@ def test_add_js_function_bigint(self, memory64, wasm_function):

@parameterized({
'': ([],),
'lz4': (['-sLZ4'],),
'pthread': (['-g', '-pthread', '-Wno-experimental', '-sPROXY_TO_PTHREAD', '-sEXIT_RUNTIME'],),
})
def test_preload_module(self, args):
if args:
if '-pthread' in args:
self.setup_node_pthreads()
# TODO(sbc): This test is copyied from test_browser.py. Perhaps find a better way to
# share code between them.
Expand Down Expand Up @@ -15649,7 +15650,7 @@ def test_preload_module(self, args):
}
''')
expected = 'done\n'
if args:
if '-pthread' in args:
expected = "sharedModules: { '/library.so': Module [WebAssembly.Module] {} }\ndone\n"
self.do_runf('main.c', expected, cflags=['-sMAIN_MODULE=2', '--preload-file', '[email protected]', '--use-preload-plugins'] + args)

Expand Down