1 {-# LANGUAGE OverloadedStrings #-}
2 module Mate.Utilities where
5 import qualified Data.Map as M
6 import qualified Data.ByteString.Lazy as B
13 -- TODO: actually this function already exists in hs-java-0.3!
14 lookupMethod :: B.ByteString -> Class Resolved -> Maybe (Method Resolved)
15 lookupMethod name cls = look (classMethods cls)
19 | methodName f == name = Just f
22 buildMethodID :: Class Resolved -> Word16 -> MethodInfo
23 buildMethodID cls idx = MethodInfo (ntName nt) rc (ntSignature nt)
25 (CMethod rc nt) = (constsPool cls) M.! idx
27 buildStaticFieldID :: Class Resolved -> Word16 -> StaticFieldInfo
28 buildStaticFieldID cls idx = StaticFieldInfo rc (ntName fnt)
29 where (CField rc fnt) = (constsPool cls) M.! idx
31 buildFieldOffset :: Class Resolved -> Word16 -> (B.ByteString, B.ByteString)
32 buildFieldOffset cls idx = (thisClass cls, ntName fnt)
33 where (CField rc fnt) = (constsPool cls) M.! idx
35 methodGetArgsCount :: Class Resolved -> Word16 -> Word32
36 methodGetArgsCount cls idx = fromIntegral $ length args
38 (CMethod _ nt) = (constsPool cls) M.! idx
39 (MethodSignature args _) = ntSignature nt
41 -- TODO(bernhard): Extend it to more than just int, and provide typeinformation
42 methodHaveReturnValue :: Class Resolved -> Word16 -> Bool
43 methodHaveReturnValue cls idx = case ret of
45 (Returns IntType) -> True;
46 _ -> error "methodHaveReturnValue: todo"
48 (CMethod _ nt) = (constsPool cls) M.! idx
49 (MethodSignature _ ret) = ntSignature nt