Created
October 18, 2016 09:39
-
-
Save gdeest/7f8376b2c775543450da48a32c7d4b94 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE ForeignFunctionInterface #-} | |
| {-# LANGUAGE TemplateHaskell #-} | |
| module Lib | |
| (islVersion) | |
| where | |
| import Lib.Internal | |
| import Foreign.C.Types | |
| import Foreign.C.String | |
| islVersion :: IO String | |
| islVersion = peekCString =<< cisl_version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE TemplateHaskell #-} | |
| module Lib.Internal where | |
| import Foreign.C.String (CString) | |
| import Lib.Internal.TH | |
| -- Illegal type variable name: ‘CString’ | |
| -- When splicing a TH declaration: | |
| -- foreign import ccall unsafe "isl_version" cisl_version :: GHC.Types.IO Foreign.C.String.CString | |
| $(generateDecl "cisl_version") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE ForeignFunctionInterface #-} | |
| {-# LANGUAGE QuasiQuotes #-} | |
| {-# LANGUAGE TemplateHaskellQuotes #-} | |
| module Lib.Internal.TH where | |
| import Language.Haskell.TH (Q, Dec(..), Callconv(..), Safety(..), Type(..), lookupTypeName, mkName) | |
| import Language.Haskell.TH.Syntax (Foreign(..)) | |
| generateDecl :: String -> Q [Dec] | |
| generateDecl fname = do | |
| let name = mkName fname | |
| Just io <- lookupTypeName "IO" | |
| Just cstring <- lookupTypeName "CString" | |
| let retType = AppT (VarT io) (VarT cstring) | |
| let dec = ForeignD $ ImportF CCall Unsafe "isl_version" name retType | |
| return [dec] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment