X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=Mate%2FUtilities.hs;h=89020a5d1d65cfda9dedd217c42a568ec13620d0;hb=e956c113f38ae5cf78d79cf00de776f0331a332c;hp=54d32d30db2bf7fcd4d46a468ae0727c835f2dc8;hpb=d41300b14108d0fb6d478050852af46bc19069fa;p=mate.git diff --git a/Mate/Utilities.hs b/Mate/Utilities.hs index 54d32d3..89020a5 100644 --- a/Mate/Utilities.hs +++ b/Mate/Utilities.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} module Mate.Utilities where @@ -10,32 +11,55 @@ import JVM.ClassFile import Mate.Types --- TODO: actually this function already exists in hs-java-0.3! -lookupMethod :: B.ByteString -> Class Resolved -> Maybe (Method Resolved) -lookupMethod name cls = look (classMethods cls) - where - look [] = Nothing - look (f:fs) - | methodName f == name = Just f - | otherwise = look fs - -buildMethodID :: Class Resolved -> Word16 -> MethodInfo +buildMethodID :: Class Direct -> Word16 -> MethodInfo buildMethodID cls idx = MethodInfo (ntName nt) rc (ntSignature nt) - where - (CMethod rc nt) = (constsPool cls) M.! idx + where (rc, nt) = case constsPool cls M.! idx of + (CMethod rc' nt') -> (rc', nt') + (CIfaceMethod rc' nt') -> (rc', nt') + _ -> error "buildMethodID: something wrong. abort." + +buildStaticFieldID :: Class Direct -> Word16 -> StaticFieldInfo +buildStaticFieldID cls idx = StaticFieldInfo rc (ntName fnt) + where (CField rc fnt) = constsPool cls M.! idx -methodGetArgsCount :: Class Resolved -> Word16 -> Word32 +buildFieldOffset :: Class Direct -> Word16 -> (B.ByteString, B.ByteString) +buildFieldOffset cls idx = (rc, ntName fnt) + where (CField rc fnt) = constsPool cls M.! idx + +buildClassID :: Class Direct -> Word16 -> B.ByteString +buildClassID cls idx = cl + where (CClass cl) = constsPool cls M.! idx + +methodGetArgsCount :: Class Direct -> Word16 -> Word32 methodGetArgsCount cls idx = fromIntegral $ length args where - (CMethod _ nt) = (constsPool cls) M.! idx + nt = case constsPool cls M.! idx of + (CMethod _ nt') -> nt' + (CIfaceMethod _ nt') -> nt' + _ -> error "methodGetArgsCount: something wrong. abort." (MethodSignature args _) = ntSignature nt -- TODO(bernhard): Extend it to more than just int, and provide typeinformation -methodHaveReturnValue :: Class Resolved -> Word16 -> Bool +methodHaveReturnValue :: Class Direct -> Word16 -> Bool methodHaveReturnValue cls idx = case ret of ReturnsVoid -> False; + (Returns BoolType) -> True + (Returns CharByte) -> True (Returns IntType) -> True; - _ -> error "methodHaveReturnValue: todo" + (Returns (Array _ _)) -> True + (Returns (ObjectType _)) -> True; + _ -> error $ "methodHaveReturnValue: todo: " ++ show ret where - (CMethod _ nt) = (constsPool cls) M.! idx + nt = case constsPool cls M.! idx of + (CMethod _ nt') -> nt' + (CIfaceMethod _ nt') -> nt' + _ -> error "methodHaveReturnValue: something wrong. abort." (MethodSignature _ ret) = ntSignature nt + +lookupMethodSig :: B.ByteString -> MethodSignature -> Class Direct -> Maybe (Method Direct) +lookupMethodSig name sig cls = look (classMethods cls) + where + look [] = Nothing + look (f:fs) + | methodName f == name && methodSignature f == sig = Just f + | otherwise = look fs