1 module Mate.ClassHierarchy
6 import qualified Data.Map as M
7 import qualified Data.ByteString.Lazy as B
8 import Control.Applicative
15 import Mate.NativeSizes
21 { clMtable :: NativeWord
22 , clSuperClass :: NativeWord
23 , clInterfaces :: [Interface]
26 { clMtable :: NativeWord
31 { ifSuperInterfaces :: [Interface]
34 type HierMap = (M.Map NativeWord Class)
35 classHier :: IORef HierMap
36 {-# NOINLINE classHier #-}
37 classHier = unsafePerformIO $ newIORef M.empty
39 readHier :: IO HierMap
40 readHier = readIORef classHier
42 writeHier :: HierMap -> IO ()
43 writeHier = writeIORef classHier
46 isInstanceOf :: NativeWord -> B.ByteString -> IO Bool
47 isInstanceOf 0 _ = return False
48 isInstanceOf obj classname = do
49 obj_mtable <- peek (intPtrToPtr . fromIntegral $ obj)
50 class_mtable <- getMethodTable classname
52 return $ checkInstance obj_mtable class_mtable ch
54 checkInstance :: NativeWord -> NativeWord -> HierMap -> Bool
55 checkInstance obj cl_mtable ch
56 | obj == cl_mtable = True
59 Class _ super _ -> checkInstance super cl_mtable ch
60 JavaLangObject _ -> False
62 addClassEntry :: NativeWord -> NativeWord -> IO ()
63 addClassEntry mtable 0 = do
65 writeHier (M.insert mtable (JavaLangObject mtable) ch)
66 addClassEntry mtable super_mtable = do
68 when (not $ M.member super_mtable ch) $ error "classhierarchy: superclass should be in hierarchy!"
69 let cl = Class mtable super_mtable []
70 writeHier (M.insert mtable cl ch)