From fec8e7ad7392f0853fc1471ad01e49d10f422815 Mon Sep 17 00:00:00 2001
From: Cheng Shao <terrorjack@type.dance>
Date: Mon, 17 Mar 2025 19:31:26 +0000
Subject: [PATCH] wasm: fix dyld downsweep filepath handling in browser

The wasm dyld downsweep logic used to rely on nodejs path module to
handle filepaths. That's not available in browsers, so this commit
implements poor man's filepath handling in js, which is not elegant
for sure but works for both nodejs and the browser.

(cherry picked from commit d9b71e823f1ae819fc39888a2d1ad3045cdf45e5)
---
 utils/jsffi/dyld.mjs | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/utils/jsffi/dyld.mjs b/utils/jsffi/dyld.mjs
index 5113e05057f..96ddf87f666 100755
--- a/utils/jsffi/dyld.mjs
+++ b/utils/jsffi/dyld.mjs
@@ -423,7 +423,9 @@ class DyLD {
   // WebAssembly.Module in loadDLL logic. This way we can harness some
   // background parallelism.
   async #downsweep(p) {
-    const soname = path.basename(p);
+    const toks = p.split("/");
+
+    const soname = toks[toks.length - 1];
 
     if (this.#loadedSos.has(soname)) {
       return [];
@@ -432,11 +434,12 @@ class DyLD {
     // Do this before loading dependencies to break potential cycles.
     this.#loadedSos.add(soname);
 
-    if (path.isAbsolute(p)) {
+    if (p.startsWith("/")) {
       // GHC may attempt to load libghc_tmp_2.so that needs
       // libghc_tmp_1.so in a temporary directory without adding that
       // directory via addLibrarySearchPath
-      this.addLibrarySearchPath(path.dirname(p));
+      toks.pop();
+      this.addLibrarySearchPath(toks.join("/"));
     } else {
       p = await this.findSystemLibrary(p);
     }
-- 
GitLab