2020-05-18 15:59:07 +03:00
|
|
|
-- Additional FFI help for more interesting C types
|
|
|
|
-- Some assumptions are made about the existence of this module in
|
|
|
|
-- Compiler.CompileExpr
|
|
|
|
|
|
|
|
module System.FFI
|
|
|
|
|
2021-06-09 01:05:10 +03:00
|
|
|
%default total
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
data Struct : String -> -- C struct name
|
|
|
|
List (String, Type) -> -- field names and types
|
|
|
|
Type where
|
|
|
|
|
|
|
|
public export
|
|
|
|
data FieldType : String -> Type -> List (String, Type) -> Type where
|
|
|
|
First : FieldType n t ((n, t) :: ts)
|
|
|
|
Later : FieldType n t ts -> FieldType n t (f :: ts)
|
|
|
|
|
|
|
|
%extern
|
2021-01-16 10:03:45 +03:00
|
|
|
prim__getField : {s : _} -> forall fs, ty .
|
2020-05-18 15:59:07 +03:00
|
|
|
Struct s fs -> (n : String) ->
|
|
|
|
FieldType n ty fs -> ty
|
|
|
|
%extern
|
2021-01-16 10:03:45 +03:00
|
|
|
prim__setField : {s : _} -> forall fs, ty .
|
2020-05-18 15:59:07 +03:00
|
|
|
Struct s fs -> (n : String) ->
|
|
|
|
FieldType n ty fs -> ty -> PrimIO ()
|
|
|
|
|
|
|
|
public export %inline
|
|
|
|
getField : {s : _} -> Struct s fs -> (n : String) ->
|
|
|
|
{auto fieldok : FieldType n ty fs} -> ty
|
|
|
|
getField s n = prim__getField s n fieldok
|
|
|
|
|
|
|
|
public export %inline
|
|
|
|
setField : {s : _} -> Struct s fs -> (n : String) ->
|
|
|
|
{auto fieldok : FieldType n ty fs} -> ty -> IO ()
|
|
|
|
setField s n val = primIO (prim__setField s n fieldok val)
|
2021-07-06 20:11:47 +03:00
|
|
|
|
|
|
|
%foreign "C:idris2_malloc, libidris2_support, idris_memory.h"
|
|
|
|
prim__malloc : (size : Int) -> PrimIO AnyPtr
|
|
|
|
|
|
|
|
%foreign "C:idris2_free, libidris2_support, idris_memory.h"
|
|
|
|
prim__free : AnyPtr -> PrimIO ()
|
|
|
|
|
|
|
|
||| Allocate memory with libc `malloc`.
|
|
|
|
export
|
|
|
|
malloc : HasIO io => (size : Int) -> io AnyPtr
|
|
|
|
malloc size = primIO $ prim__malloc size
|
|
|
|
|
|
|
|
||| Release memory with libc `free`.
|
|
|
|
export
|
|
|
|
free : HasIO io => AnyPtr -> io ()
|
|
|
|
free ptr = primIO $ prim__free ptr
|