2021-10-29 19:58:29 +03:00
|
|
|
||| Additional FFI help for more interesting C types
|
|
|
|
module System.FFI
|
2020-05-18 15:59:07 +03:00
|
|
|
-- Some assumptions are made about the existence of this module in
|
|
|
|
-- Compiler.CompileExpr
|
|
|
|
|
|
|
|
|
2021-06-09 01:05:10 +03:00
|
|
|
%default total
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| A struct with a name and a list of key-value pairs of field names and their
|
|
|
|
||| types.
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
data Struct : String -> -- C struct name
|
|
|
|
List (String, Type) -> -- field names and types
|
|
|
|
Type where
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| A proof that the field exists as an entry in the list of field names and
|
|
|
|
||| their types.
|
2020-05-18 15:59:07 +03:00
|
|
|
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 ()
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| Retrieve the value of the specified field in the given `Struct`.
|
|
|
|
|||
|
|
|
|
||| @ s the `Struct` to retrieve the value from
|
|
|
|
||| @ n the name of the field in the `Struct`.
|
2020-05-18 15:59:07 +03:00
|
|
|
public export %inline
|
2021-10-29 19:58:29 +03:00
|
|
|
getField : {sn : _} -> (s : Struct sn fs) -> (n : String) ->
|
2020-05-18 15:59:07 +03:00
|
|
|
{auto fieldok : FieldType n ty fs} -> ty
|
|
|
|
getField s n = prim__getField s n fieldok
|
|
|
|
|
2021-10-29 19:58:29 +03:00
|
|
|
||| Set the value of the specified field in the given `Struct`.
|
|
|
|
|||
|
|
|
|
||| @ s the `Struct` in which the field exists
|
|
|
|
||| @ n the name of the field to set
|
|
|
|
||| @ val the value to set the field to
|
2020-05-18 15:59:07 +03:00
|
|
|
public export %inline
|
2021-10-29 19:58:29 +03:00
|
|
|
setField : {sn : _} -> (s : Struct sn fs) -> (n : String) ->
|
|
|
|
{auto fieldok : FieldType n ty fs} -> (val : ty) -> IO ()
|
2020-05-18 15:59:07 +03:00
|
|
|
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"
|
2022-08-10 23:23:53 +03:00
|
|
|
"javascript:lambda:()=>undefined"
|
2021-07-06 20:11:47 +03:00
|
|
|
prim__free : AnyPtr -> PrimIO ()
|
|
|
|
|
|
|
|
||| Allocate memory with libc `malloc`.
|
2021-10-29 19:58:29 +03:00
|
|
|
|||
|
|
|
|
||| @ size the number of bytes to allocate
|
2021-07-06 20:11:47 +03:00
|
|
|
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
|