Skip to content

Latest commit

 

History

History
46 lines (38 loc) · 911 Bytes

README.md

File metadata and controls

46 lines (38 loc) · 911 Bytes

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'