{-- JVM-Bridge -- bridge from FP languages and others to the Java VM Copyright (C) 2001 Ashley Yakeley This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --} module Foreign.JavaVM.VM.Witness where { import Foreign.JavaVM.VM.Ref; import Platform.JavaVM; import Data.Witness; data VMType t where { Tprimitive :: PrimitiveType t -> VMType t; Tref :: VMType VMRef; }; instance Witness VMType where { withWitness f t@(Tprimitive Tboolean) = f t; withWitness f t@(Tprimitive Tbyte) = f t; withWitness f t@(Tprimitive Tchar) = f t; withWitness f t@(Tprimitive Tshort) = f t; withWitness f t@(Tprimitive Tint) = f t; withWitness f t@(Tprimitive Tlong) = f t; withWitness f t@(Tprimitive Tfloat) = f t; withWitness f t@(Tprimitive Tdouble) = f t; withWitness f t@Tref = f t; }; instance TotalWitness VMType where { matchWitnessF (Tprimitive ta) (Tprimitive tb) = matchWitnessF ta tb; matchWitnessF Tref Tref = Just id; matchWitnessF _ _ = Nothing; }; instance Is VMType Jboolean where { witness = Tprimitive Tboolean; }; instance Is VMType Jbyte where { witness = Tprimitive Tbyte; }; instance Is VMType Jchar where { witness = Tprimitive Tchar; }; instance Is VMType Jshort where { witness = Tprimitive Tshort; }; instance Is VMType Jint where { witness = Tprimitive Tint; }; instance Is VMType Jlong where { witness = Tprimitive Tlong; }; instance Is VMType Jfloat where { witness = Tprimitive Tfloat; }; instance Is VMType Jdouble where { witness = Tprimitive Tdouble; }; instance Is VMType VMRef where { witness = Tref; }; javaToJNIValueType :: ValueType -> AnyWitness VMType; javaToJNIValueType MkBooleanType= AnyWitness (Tprimitive Tboolean); javaToJNIValueType MkByteType = AnyWitness (Tprimitive Tbyte); javaToJNIValueType MkCharType = AnyWitness (Tprimitive Tchar); javaToJNIValueType MkShortType = AnyWitness (Tprimitive Tshort); javaToJNIValueType MkIntType = AnyWitness (Tprimitive Tint); javaToJNIValueType MkLongType = AnyWitness (Tprimitive Tlong); javaToJNIValueType MkFloatType = AnyWitness (Tprimitive Tfloat); javaToJNIValueType MkDoubleType = AnyWitness (Tprimitive Tdouble); javaToJNIValueType _ = AnyWitness Tref; data VMReturnType t where { Tvalue :: VMType t -> VMReturnType t; Tvoid :: VMReturnType Jvoid; }; instance Witness VMReturnType where { withWitness f t@(Tvalue (Tprimitive Tboolean)) = f t; withWitness f t@(Tvalue (Tprimitive Tbyte)) = f t; withWitness f t@(Tvalue (Tprimitive Tchar)) = f t; withWitness f t@(Tvalue (Tprimitive Tshort)) = f t; withWitness f t@(Tvalue (Tprimitive Tint)) = f t; withWitness f t@(Tvalue (Tprimitive Tlong)) = f t; withWitness f t@(Tvalue (Tprimitive Tfloat)) = f t; withWitness f t@(Tvalue (Tprimitive Tdouble)) = f t; withWitness f t@(Tvalue Tref) = f t; withWitness f t@Tvoid = f t; }; instance TotalWitness VMReturnType where { matchWitnessF (Tvalue ta) (Tvalue tb) = matchWitnessF ta tb; matchWitnessF Tvoid Tvoid = Just id; matchWitnessF _ _ = Nothing; }; instance Is VMReturnType Jboolean where { witness = Tvalue (Tprimitive Tboolean); }; instance Is VMReturnType Jbyte where { witness = Tvalue (Tprimitive Tbyte); }; instance Is VMReturnType Jchar where { witness = Tvalue (Tprimitive Tchar); }; instance Is VMReturnType Jshort where { witness = Tvalue (Tprimitive Tshort); }; instance Is VMReturnType Jint where { witness = Tvalue (Tprimitive Tint); }; instance Is VMReturnType Jlong where { witness = Tvalue (Tprimitive Tlong); }; instance Is VMReturnType Jfloat where { witness = Tvalue (Tprimitive Tfloat); }; instance Is VMReturnType Jdouble where { witness = Tvalue (Tprimitive Tdouble); }; instance Is VMReturnType VMRef where { witness = Tvalue Tref; }; instance Is VMReturnType Jvoid where { witness = Tvoid; }; javaToJNIReturnType :: ReturnType -> AnyWitness VMReturnType; javaToJNIReturnType MkVoidType = AnyWitness Tvoid; javaToJNIReturnType (MkValueType t) = (\(AnyWitness w) -> AnyWitness (Tvalue w)) (javaToJNIValueType t); }