[Git][ghc/ghc][wip/js-boundsCheck] Apply 1 suggestion(s) to 1 file(s)

Josh Meredith (@JoshMeredith) gitlab at gitlab.haskell.org
Tue Apr 11 12:22:47 UTC 2023



Josh Meredith pushed to branch wip/js-boundsCheck at Glasgow Haskell Compiler / GHC


Commits:
243bb9f9 by Sylvain Henry at 2023-04-11T12:22:45+00:00
Apply 1 suggestion(s) to 1 file(s)
- - - - -


1 changed file:

- compiler/GHC/StgToJS/Prim.hs


Changes:

=====================================
compiler/GHC/StgToJS/Prim.hs
=====================================
@@ -1466,22 +1466,45 @@ newByteArray :: JExpr -> JExpr -> JStat
 newByteArray tgt len =
   tgt |= app "h$newByteArray" [len]
 
-boundsChecked :: Bool  -- ^ Should we do bounds checking?
-              -> JExpr -- ^ Array
-              -> JExpr -- ^ Index
-              -> JStat -- ^ Result
-              -> JStat
-boundsChecked False _ _ r = r
-boundsChecked True  xs i r =
-  ifS ((i .>=. zero_) .&&. (i .<. xs .^ "length")) r $
+boundsChecked'
+  :: JExpr -- ^ Max index expression
+  -> Bool  -- ^ Should we do bounds checking?
+  -> JExpr -- ^ Index
+  -> JStat -- ^ Result
+  -> JStat
+boundsChecked' _         False _ r = r
+boundsChecked' max_index True  i r =
+  ifS ((i .>=. zero_) .&&. (i .<. max_index)) r $
     returnS (app "h$exitProcess" [Int 134])
 
-boundsCheckedRangeLen :: Bool
-                   -> JExpr
-                   -> JExpr
-                   -> JExpr
-                   -> JStat
-                   -> JStat
+-- | Bounds checking using ".length" property (Arrays)
+boundsChecked
+  :: Bool  -- ^ Should we do bounds checking?
+  -> JExpr -- ^ Array
+  -> JExpr -- ^ Index
+  -> JStat -- ^ Result
+  -> JStat
+boundsChecked do_check arr = boundsChecked' (arr .^ "length") do_check
+
+-- | Bounds checking using ".len" property (ByteArrays)
+boundsCheckedLen
+  :: Bool  -- ^ Should we do bounds checking?
+  -> JExpr -- ^ Array
+  -> JExpr -- ^ Index
+  -> JStat -- ^ Result
+  -> JStat
+boundsCheckedLen do_check arr = boundsChecked' (arr .^ "len") do_check
+
+-- | Bounds checking on a range and using ".len" property (ByteArrays)
+--
+-- Empty ranges trivially pass the check
+boundsCheckedRangeLen
+  :: Bool  -- ^ Should we do bounds checking?
+  -> JExpr -- ^ Array
+  -> JExpr -- ^ Index
+  -> JExpr -- ^ Range size
+  -> JStat -- ^ Result
+  -> JStat
 boundsCheckedRangeLen False _  _ _ r = r
 boundsCheckedRangeLen True  xs i n r =
   ifS (n .<. zero_) (returnS $ app "h$exitProcess" [Int 134]) $
@@ -1490,17 +1513,6 @@ boundsCheckedRangeLen True  xs i n r =
       (boundsCheckedLen True xs (Add i (Sub n 1)) (boundsCheckedLen True xs i r))
 
 
-boundsCheckedLen :: Bool  -- ^ Should we do bounds checking?
-              -> JExpr -- ^ Array
-              -> JExpr -- ^ Index
-              -> JStat -- ^ Result
-              -> JStat
-boundsCheckedLen False _ _ r = r
-boundsCheckedLen True  xs i r =
-  -- Byte arrays use `len`
-  ifS ((i .>=. zero_) .&&. (i .<. xs .^ "len")) r $
-    returnS (app "h$exitProcess" [Int 134])
-
 byteIndex16 :: JExpr -> JExpr
 byteIndex16 i = Add 1 (Mul 2 i)
 



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/243bb9f956df8b249d811ffd8a8772406409a970

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/243bb9f956df8b249d811ffd8a8772406409a970
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230411/3b5de033/attachment-0001.html>


More information about the ghc-commits mailing list