1 {-# LANGUAGE OverloadedStrings #-}
2 module Mate.Utilities where
6 import qualified Data.Map as M
7 import qualified Data.ByteString.Lazy as B
8 -- import qualified Data.ByteString.Lazy.Char8 as B8
9 import Codec.Binary.UTF8.String hiding (encode,decode)
16 data MethodInfo = MethodInfo {
17 methodname :: B.ByteString,
18 classname :: B.ByteString,
19 signature :: MethodSignature,
22 instance Eq MethodInfo where
23 (MethodInfo m_a c_a s_a i_a) == (MethodInfo m_b c_b s_b i_b) =
24 (m_a == m_b) && (c_a == c_b) && (s_a == s_b) && (i_a == i_b)
26 -- TODO(bernhard): not really efficient. also, outsource that to hs-java
27 instance Ord MethodSignature where
28 compare (MethodSignature args_a ret_a) (MethodSignature args_b ret_b)
29 | cmp_args /= EQ = cmp_args
30 | otherwise = (show ret_a) `compare` (show ret_b)
32 cmp_args = (show args_a) `compare` (show args_b)
34 instance Ord MethodInfo where
35 compare (MethodInfo m_a c_a s_a i_a) (MethodInfo m_b c_b s_b i_b)
39 | otherwise = i_a `compare` i_b
41 cmp_m = m_a `compare` m_b
42 cmp_c = c_a `compare` c_b
43 cmp_s = s_a `compare` s_b
45 instance Show MethodInfo where
46 show (MethodInfo method c sig idx) =
47 (toString c) ++ "." ++ (toString method) ++ "." ++ (show sig) ++ "@" ++ (show idx)
50 -- TODO: actually this function already exists in hs-java-0.3!
51 lookupMethod :: B.ByteString -> Class Resolved -> Maybe (Method Resolved)
52 lookupMethod name cls = look (classMethods cls)
56 | methodName f == name = Just f
59 toString :: B.ByteString -> String
60 toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
62 buildMethodID :: Class Resolved -> Word16 -> MethodInfo
63 buildMethodID cls idx = MethodInfo (ntName nt) rc (ntSignature nt) idx
65 (CMethod rc nt) = (constsPool cls) M.! idx
67 methodGetArgsCount :: Class Resolved -> Word16 -> Word32
68 methodGetArgsCount cls idx = fromIntegral $ length args
70 (CMethod _ nt) = (constsPool cls) M.! idx
71 (MethodSignature args _) = ntSignature nt
73 -- TODO(bernhard): Extend it to more than just int, and provide typeinformation
74 methodHaveReturnValue :: Class Resolved -> Word16 -> Bool
75 methodHaveReturnValue cls idx = case ret of
77 (Returns IntType) -> True;
78 _ -> error "methodHaveReturnValue: todo"
80 (CMethod _ nt) = (constsPool cls) M.! idx
81 (MethodSignature _ ret) = ntSignature nt