From f4c70d4d30beec504b964d0bc23f1ccebea57a54 Mon Sep 17 00:00:00 2001 From: Christian Brier Date: Sat, 4 May 2024 13:02:35 +0200 Subject: [PATCH] add:Serialization Framework --- src/ClassFile.hs | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/ClassFile.hs b/src/ClassFile.hs index d5322a1..08f2038 100644 --- a/src/ClassFile.hs +++ b/src/ClassFile.hs @@ -30,6 +30,7 @@ import Data.Word import Data.Int +import Data.ByteString data ConstantInfo = ClassInfo Word16 | FieldRefInfo Word16 Word16 @@ -48,15 +49,45 @@ data MemberInfo = MemberInfo { } deriving Show data ClassFile = ClassFile { - magic :: Word32, - minorVersion :: Word16, - majorVersion :: Word16, constantPool :: [ConstantInfo], accessFlags :: Word16, thisClass :: Word16, - superClass :: Word16, - interfaces :: [Word16], fields :: [MemberInfo], methods :: [MemberInfo], attributes :: [Attribute] -} deriving Show \ No newline at end of file +} deriving Show + +class Serializable a where + serialize :: a -> [Word8] -> [Word8] + +instance Serializable ClassFile where + serialize :: ClassFile -> [Word8] -> [Word8] + serialize classfile buffer = buffer ++ [0xC0, 0xFE, 0xBA, 0xBE] + ++ [0, 0] + ++ [0, 49] + ++ [0, 1] + ++ [0, 1] + ++ [0, 0] + ++ [0, 0] + ++ [0, 0] + ++ [0, 0] + ++ [0, 0] + ++ [0, 0] + + + + +example = ClassFile{ + constantPool, + accessFlags, + thisClass, + fields, + methods, + attributes + } where + constantPool = [] + accessFlags = 1 + thisClass = 0 + fields = [] + methods = [] + attributes = []