From b938950d98945f437f1e28b15a0a3629bfe336c2 Mon Sep 17 00:00:00 2001
From: Luite Stegeman <stegeman@gmail.com>
Date: Thu, 3 Aug 2023 12:15:48 +0900
Subject: [PATCH] JS: Fix missing local variable declarations

This fixes some missing local variable declarations that were
found by running the testsuite in strict mode.

Fixes #23775
---
 compiler/GHC/JS/Make.hs                           | 2 +-
 compiler/GHC/StgToJS/Expr.hs                      | 2 +-
 rts/js/arith.js                                   | 6 +++---
 rts/js/environment.js                             | 2 +-
 rts/js/thread.js                                  | 1 +
 testsuite/tests/javascript/js-callback03.hs       | 2 +-
 testsuite/tests/programs/seward-space-leak/test.T | 1 -
 7 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/compiler/GHC/JS/Make.hs b/compiler/GHC/JS/Make.hs
index 96175ee4e414..754e5da9c3b0 100644
--- a/compiler/GHC/JS/Make.hs
+++ b/compiler/GHC/JS/Make.hs
@@ -268,7 +268,7 @@ jVar f = UnsatBlock . IS $ do
            (block, is) <- runIdentSupply $ toSat_ f []
            let addDecls (BlockStat ss) =
                   BlockStat $ map decl is ++ ss
-               addDecls x = x
+               addDecls x = BlockStat (map decl is ++ [x])
            return $ addDecls block
 
 jFunction :: Ident -> [Ident] -> JStat -> JStat
diff --git a/compiler/GHC/StgToJS/Expr.hs b/compiler/GHC/StgToJS/Expr.hs
index e05f5b05e62a..76ba0f3d2dec 100644
--- a/compiler/GHC/StgToJS/Expr.hs
+++ b/compiler/GHC/StgToJS/Expr.hs
@@ -509,7 +509,7 @@ optimizeFree offset ids = do
 allocCls :: Maybe JStat -> [(Id, CgStgRhs)] -> G JStat
 allocCls dynMiddle xs = do
    (stat, dyn) <- partitionEithers <$> mapM toCl xs
-   ac <- allocDynAll True dynMiddle dyn
+   ac <- allocDynAll False dynMiddle dyn
    pure (mconcat stat <> ac)
   where
     -- left = static, right = dynamic
diff --git a/rts/js/arith.js b/rts/js/arith.js
index 902be190c9a7..88f11147bd8d 100644
--- a/rts/js/arith.js
+++ b/rts/js/arith.js
@@ -394,9 +394,9 @@ function h$decodeFloatInt(d) {
             h$convertFloat[0] = d*8388608; // put d in the normal range (~ shift left 23 bits)
             i = h$convertInt[0];
             s = (i&8388607) | 8388608;
-            e = ((i >> 23) & 0xff) - 173; // take into account normalization above (150+23)
-            TRACE_ARITH("decodeFloatInt s: " + (sgn * s) +  " e: " + e)
-            RETURN_UBX_TUP2(sgn*s, e)
+            exp = ((i >> 23) & 0xff) - 173; // take into account normalization above (150+23)
+            TRACE_ARITH("decodeFloatInt s: " + (sgn * s) +  " e: " + exp)
+            RETURN_UBX_TUP2(sgn*s, exp)
         }
     } else {
       TRACE_ARITH("decodeFloatInt s: " + (sgn * (s|8388608)) +  " e: " + (exp-150))
diff --git a/rts/js/environment.js b/rts/js/environment.js
index f9e91fc531aa..b3b329dc5d60 100644
--- a/rts/js/environment.js
+++ b/rts/js/environment.js
@@ -166,7 +166,7 @@ function h$getProgArgv(argc_v,argc_off,argv_v,argv_off) {
 }
 
 function h$setProgArgv(n, ptr_d, ptr_o) {
-  args = [];
+  var args = [];
   for(var i=0;i<n;i++) {
     var off = ptr_o+4*i;
     GET_ADDR(ptr_d,off,p,o);
diff --git a/rts/js/thread.js b/rts/js/thread.js
index 1c51fa25696c..23e9fbfbc55a 100644
--- a/rts/js/thread.js
+++ b/rts/js/thread.js
@@ -138,6 +138,7 @@ function h$getThreadLabel(t) {
 
 function h$listThreads() {
   var r = h$newArray(0,null);
+  var t;
 
   if (h$currentThread) r.push(h$currentThread);
 
diff --git a/testsuite/tests/javascript/js-callback03.hs b/testsuite/tests/javascript/js-callback03.hs
index 311c2634f4d5..984bc97d0f09 100644
--- a/testsuite/tests/javascript/js-callback03.hs
+++ b/testsuite/tests/javascript/js-callback03.hs
@@ -10,7 +10,7 @@ foreign import javascript "((x) => { globalF(x); })"
 foreign import javascript "((x,y) => { return x + y })"
   js_plus :: JSVal -> JSVal -> IO JSVal
 
-foreign import javascript "((g) => { globalG = g; })"
+foreign import javascript "((g) => { globalThis.globalG = g; })"
   setG :: Callback (JSVal -> JSVal -> IO JSVal) -> IO ()
 
 foreign import javascript "((x,y) => { return globalG(x,y); })"
diff --git a/testsuite/tests/programs/seward-space-leak/test.T b/testsuite/tests/programs/seward-space-leak/test.T
index e1525de9c66c..2a482f5790a5 100644
--- a/testsuite/tests/programs/seward-space-leak/test.T
+++ b/testsuite/tests/programs/seward-space-leak/test.T
@@ -1,5 +1,4 @@
 test('seward-space-leak', [extra_files(['Main.lhs'])
                           , when(fast(), skip)
-                          , js_broken(22352)
                           ], multimod_compile_and_run,
      ['Main', ''])
-- 
GitLab