1
0
Fork
You've already forked c-ffi
0
Generic C FFI utilities for Idris
  • Idris 63.8%
  • C 34%
  • Shell 2.2%
Find a file
2023年06月03日 11:06:23 +01:00
.github/workflows add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
c add missing C function sizeof_double ( #3 ) 2023年06月01日 22:26:39 +01:00
src/Prim add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
test add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
.gitignore add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
c-ffi.ipkg install shared lib to lib dir ( #4 ) 2023年06月03日 11:06:23 +01:00
LICENSE Initial commit 2023年05月29日 13:56:07 +01:00
pack.toml add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
postbuild.sh install shared lib to lib dir ( #4 ) 2023年06月03日 11:06:23 +01:00
postinstall.sh install shared lib to lib dir ( #4 ) 2023年06月03日 11:06:23 +01:00
readme.ipkg add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00
README.md add installation instructions ( #2 ) 2023年05月29日 22:48:23 +01:00
test.ipkg add utilities for arrays of Idris primitives ( #1 ) 2023年05月29日 22:36:04 +01:00

c-ffi

Generic C FFI utilities for Idris.

This library is intended to remove the need to build and package some of the more common C FFI utilities with Idris projects.

Install

Install pack, then run

pack install c-ffi

Example

We can create a C array double*

doubles : IO (Ptr Double)
doubles = do
 xs <- malloc (3 * cast prim__sizeOfDouble)
 let xs = prim__castPtr xs
 primIO $ prim__setArrayDouble xs 0 3.14
 primIO $ prim__setArrayDouble xs 1 (-1.23)
 primIO $ prim__setArrayDouble xs 2 2.71
 pure xs

and then read and print the array

showDoubles : IO ()
showDoubles = do
 ds <- doubles
 let ds' = map (prim__getArrayDouble ds) [0, 1, 2]
 free (prim__forgetPtr ds)
 printLn ds'