invokevirtual: implemented. not very well tested though
[mate.git] / Mate / Utilities.hs
1 {-# LANGUAGE OverloadedStrings #-}
2 module Mate.Utilities where
3
4 import Data.Word
5 import qualified Data.Map as M
6 import qualified Data.ByteString.Lazy as B
7
8 import JVM.ClassFile
9
10 import Mate.Types
11
12
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)
16   where
17     look [] = Nothing
18     look (f:fs)
19       | methodName f == name = Just f
20       | otherwise  = look fs
21
22 buildMethodID :: Class Resolved -> Word16 -> MethodInfo
23 buildMethodID cls idx = MethodInfo (ntName nt) rc (ntSignature nt)
24   where
25   (CMethod rc nt) = (constsPool cls) M.! idx
26
27 buildStaticFieldID :: Class Resolved -> Word16 -> StaticFieldInfo
28 buildStaticFieldID cls idx = StaticFieldInfo rc (ntName fnt)
29   where (CField rc fnt) = (constsPool cls) M.! idx
30
31 buildFieldOffset :: Class Resolved -> Word16 -> (B.ByteString, B.ByteString)
32 buildFieldOffset cls idx = (rc, ntName fnt)
33   where (CField rc fnt) = (constsPool cls) M.! idx
34
35 buildClassID :: Class Resolved -> Word16 -> B.ByteString
36 buildClassID cls idx = cl
37   where (CClass cl) = (constsPool cls) M.! idx
38
39 methodGetArgsCount :: Class Resolved -> Word16 -> Word32
40 methodGetArgsCount cls idx = fromIntegral $ length args
41   where
42   (CMethod _ nt) = (constsPool cls) M.! idx
43   (MethodSignature args _) = ntSignature nt
44
45 -- TODO(bernhard): Extend it to more than just int, and provide typeinformation
46 methodHaveReturnValue :: Class Resolved -> Word16 -> Bool
47 methodHaveReturnValue cls idx = case ret of
48     ReturnsVoid -> False;
49     (Returns IntType) -> True;
50     _ -> error "methodHaveReturnValue: todo"
51   where
52   (CMethod _ nt) = (constsPool cls) M.! idx
53   (MethodSignature _ ret) = ntSignature nt