Enhace constants pool handling.
[hs-java.git] / JVM / Converter.hs
index 911024c4fd8551f8042d79f465442d8e1dc8e8f1..6380168a71ea7997105f6e897deee63dd735447f 100644 (file)
@@ -18,6 +18,7 @@ import Data.Bits
 import Data.Binary
 import Data.Default () -- import instances only
 import qualified Data.ByteString.Lazy as B
+import qualified Data.ByteString.Lazy.Char8 ()
 import qualified Data.Set as S
 import qualified Data.Map as M
 
@@ -62,7 +63,7 @@ classDirect2File (Class {..}) =
   in d {
     constsPoolSize = fromIntegral (M.size poolInfo + 1),
     constsPool = poolInfo,
-    accessFlags = access2word16 accessFlags,
+    accessFlags = accessDirect2File accessFlags,
     thisClass = force "this" $ poolClassIndex poolInfo thisClass,
     superClass = force "super" $ poolClassIndex poolInfo superClass,
     interfacesCount = fromIntegral (length interfaces),
@@ -103,9 +104,9 @@ poolDirect2File pool = result
 
 -- | Find index of given string in the list of constants
 poolIndex :: (Throws NoItemInPool e) => Pool File -> B.ByteString -> EM e Word16
-poolIndex list name = case findIndex test (M.elems list) of
+poolIndex list name = case mapFindIndex test list of
                         Nothing -> throw (NoItemInPool name)
-                        Just i ->  return $ fromIntegral $ i+1
+                        Just i ->  return $ fromIntegral i
   where
     test (CUTF8 s)    | s == name = True
     test (CUnicode s) | s == name = True
@@ -113,11 +114,11 @@ poolIndex list name = case findIndex test (M.elems list) of
 
 -- | Find index of given string in the list of constants
 poolClassIndex :: (Throws NoItemInPool e) => Pool File -> B.ByteString -> EM e Word16
-poolClassIndex list name = case findIndex checkString (M.elems list) of
+poolClassIndex list name = case mapFindIndex checkString list of
                         Nothing -> throw (NoItemInPool name)
-                        Just i ->  case findIndex (checkClass $ fromIntegral $ i+1) (M.elems list) of
-                                     Nothing -> throw (NoItemInPool $ i+1)
-                                     Just j  -> return $ fromIntegral $ j+1
+                        Just i ->  case mapFindIndex (checkClass $ fromIntegral i) list of
+                                     Nothing -> throw (NoItemInPool i)
+                                     Just j  -> return $ fromIntegral j
   where
     checkString (CUTF8 s)    | s == name = True
     checkString (CUnicode s) | s == name = True
@@ -129,9 +130,9 @@ poolClassIndex list name = case findIndex checkString (M.elems list) of
 poolNTIndex list x@(NameType n t) = do
     ni <- poolIndex list n
     ti <- poolIndex list (byteString t)
-    case findIndex (check ni ti) (M.elems list) of
+    case mapFindIndex (check ni ti) list of
       Nothing -> throw (NoItemInPool x)
-      Just i  -> return $ fromIntegral (i+1)
+      Just i  -> return $ fromIntegral i
   where
     check ni ti (CNameType n' t')
       | (ni == n') && (ti == t') = True
@@ -139,7 +140,7 @@ poolNTIndex list x@(NameType n t) = do
 
 fieldDirect2File :: Pool File -> Field Direct -> Field File
 fieldDirect2File pool (Field {..}) = Field {
-    fieldAccessFlags = access2word16 fieldAccessFlags,
+    fieldAccessFlags = accessDirect2File fieldAccessFlags,
     fieldName = force "field name" $ poolIndex pool fieldName,
     fieldSignature = force "signature" $ poolIndex pool (encode fieldSignature),
     fieldAttributesCount = fromIntegral (arsize fieldAttributes),
@@ -150,7 +151,7 @@ fieldDirect2File pool (Field {..}) = Field {
 
 methodDirect2File :: Pool File -> Method Direct -> Method File
 methodDirect2File pool (Method {..}) = Method {
-    methodAccessFlags = access2word16 methodAccessFlags,
+    methodAccessFlags = accessDirect2File methodAccessFlags,
     methodName = force "method name" $ poolIndex pool methodName,
     methodSignature = force "method sig" $ poolIndex pool (encode methodSignature),
     methodAttributesCount = fromIntegral (arsize methodAttributes),
@@ -175,10 +176,13 @@ poolFile2Direct ps = pool
 
     convertNameType :: (HasSignature a) => Word16 -> NameType a
     convertNameType i =
-      let (CNameType n s) = pool ! i
-      in  NameType n (decode s)
+      case pool ! i of
+        CNameType n s -> NameType n (decode s)
+        x -> error $ "Unexpected: " ++ show i
 
-    convert (CClass i) = CClass $ getString $ pool ! i
+    convert (CClass i) = case pool ! i of
+                          CUTF8 name -> CClass name
+                          x -> error $ "Unexpected class name: " ++ show x ++ " at " ++ show i
     convert (CField i j) = CField (className $ pool ! i) (convertNameType j)
     convert (CMethod i j) = CMethod (className $ pool ! i) (convertNameType j)
     convert (CIfaceMethod i j) = CIfaceMethod (className $ pool ! i) (convertNameType j)
@@ -205,8 +209,8 @@ accessFile2Direct w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then
    ACC_INTERFACE,
    ACC_ABSTRACT ]
 
-access2word16 :: AccessFlags Direct -> AccessFlags File
-access2word16 fs = bitsOr $ map toBit $ S.toList fs
+accessDirect2File :: AccessFlags Direct -> AccessFlags File
+accessDirect2File fs = bitsOr $ map toBit $ S.toList fs
   where
     bitsOr = foldl (.|.) 0
     toBit f = 1 `shiftL` (fromIntegral $ fromEnum f)