{-# LANGUAGE ForeignFunctionInterface #-} module MLP.Object where import Foreign.Ptr import Foreign.C.Types import Foreign.C.String import Foreign.Marshal.Array import qualified MLP import qualified MLP.Context import qualified MLP.Runtime foreign import ccall unsafe "mlp.h mlp_object_new" c_new :: Ptr MLP.C_context -> Ptr MLP.C_runtime -> CString -> Ptr (Ptr MLP.C_value) -> CInt -> CString -> IO (Ptr MLP.C_value) new :: MLP.Context -> MLP.Runtime -> String -> [MLP.Value] -> String -> IO MLP.Value new ctx@(MLP.Context c_ctx) rt@(MLP.Runtime c_rt) clazz args hint = do MLP.withFuncDebug "Object new" $ do MLP.Context.try ctx $ do withCString clazz $ \c_class -> do withCString hint $ \c_hint -> do withArray (map unWrap args) $ \c_args -> do c_val <- c_new c_ctx c_rt c_class c_args (fromIntegral $ length args) c_hint return $ MLP.Value c_val where unWrap (MLP.Value c_val) = c_val foreign import ccall unsafe "mlp.h mlp_object_call_method" c_call_method :: Ptr MLP.C_context -> Ptr MLP.C_runtime -> Ptr MLP.C_object -> CString -> Ptr (Ptr MLP.C_value) -> CInt -> CString -> IO (Ptr MLP.C_value) callMethod :: MLP.Context -> MLP.Runtime -> MLP.Object -> String -> [MLP.Value] -> String -> IO MLP.Value callMethod ctx@(MLP.Context c_ctx) rt@(MLP.Runtime c_rt) obj@(MLP.Object c_obj) method args hint = do MLP.withFuncDebug "Object call_method" $ do MLP.Context.try ctx $ do withCString method $ \c_method -> do withCString hint $ \c_hint -> do withArray (map unWrap args) $ \c_args -> do c_val <- c_call_method c_ctx c_rt c_obj c_method c_args (fromIntegral $ length args) c_hint return $ MLP.Value c_val where unWrap (MLP.Value c_val) = c_val foreign import ccall unsafe "mlp.h mlp_object_delete" c_delete :: Ptr MLP.C_context -> Ptr MLP.C_object -> IO () delete :: MLP.Context -> MLP.Object -> IO () delete ctx@(MLP.Context c_ctx) obj@(MLP.Object c_obj) = do MLP.withFuncDebug "Object delete" $ MLP.Context.try ctx $ c_delete c_ctx c_obj foreign import ccall unsafe "mlp.h mlp_object_to_string" c_to_string :: Ptr MLP.C_object -> IO (CString) toString :: MLP.Object -> IO String toString obj@(MLP.Object c_obj) = do c_str <- c_to_string c_obj peekCString c_str