Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file cstubs.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176(*
* Copyright (c) 2014 Jeremy Yallop.
*
* This file is distributed under the terms of the MIT License.
* See the file LICENSE for details.
*)(* Cstubs public interface. *)[@@@warning"-27-32"]moduletypeFOREIGN=Ctypes.FOREIGNmoduletypeFOREIGN'=FOREIGNwithtype'aresult=unitmoduletypeBINDINGS=functor(F:FOREIGN')->sigendtypeconcurrency_policy=[`Sequential|`Lwt_jobs|`Lwt_preemptive|`Unlocked]typeerrno_policy=[`Ignore_errno|`Return_errno]letgen_c~concurrency~errnoprefixfmt:(moduleFOREIGN')=(modulestructletcounter=ref0letvarprefixname=incrcounter;Printf.sprintf"%s_%d_%s"prefix!counternametype'afn='aCtypes.fntype'areturn='atype'aresult=unitletforeigncnamefn=Cstubs_generate_c.fn~concurrency~errno~cname~stub_name:(varprefixcname)fmtfnletforeign_valuecnametyp=Cstubs_generate_c.value~cname~stub_name:(varprefixcname)fmttypletreturning=Ctypes.returninglet(@->)=Ctypes.(@->)end)typebind=Bind:string*string*('a->'b)Ctypes.fn->bindtypeval_bind=Val_bind:string*string*'aCtypes.typ->val_bindletwrite_return:concurrency:concurrency_policy->errno:errno_policy->Format.formatter->unit=fun~concurrency~errnofmt->matchconcurrency,errnowith(`Sequential|`Unlocked),`Ignore_errno->Format.fprintffmt"type 'a return = 'a@\n"|(`Sequential|`Unlocked),`Return_errno->Format.fprintffmt"type 'a return = 'a * Signed.sint@\n"|(`Lwt_jobs|`Lwt_preemptive),`Ignore_errno->beginFormat.fprintffmt"type 'a return = { lwt: 'a Lwt.t }@\n";Format.fprintffmt"let box_lwt lwt = {lwt}@\n";end|(`Lwt_jobs|`Lwt_preemptive),`Return_errno->beginFormat.fprintffmt"type 'a return = { lwt: ('a * Signed.sint) Lwt.t }@\n";Format.fprintffmt"let box_lwt lwt = {lwt}@\n";endletwrite_fn~concurrency~errnofmt=beginFormat.fprintffmt"type 'a fn =@\n";Format.fprintffmt" | Returns : 'a CI.typ -> 'a return fn@\n";Format.fprintffmt" | Function : 'a CI.typ * 'b fn -> ('a -> 'b) fn@\n"endletwrite_map_result~concurrency~errnofmt=matchconcurrency,errnowith(`Sequential|`Unlocked),`Ignore_errno->Format.fprintffmt"let map_result f x = f x@\n"|(`Sequential|`Unlocked),`Return_errno->Format.fprintffmt"let map_result f (x, y) = (f x, y)@\n"|(`Lwt_jobs|`Lwt_preemptive),`Ignore_errno->Format.fprintffmt"let map_result f x = Lwt.map f x@\n"|(`Lwt_jobs|`Lwt_preemptive),`Return_errno->Format.fprintffmt"let map_result f v = Lwt.map (fun (x, y) -> (f x, y)) v@\n"letwrite_foreign~concurrency~errnofmtbindingsval_bindings=Format.fprintffmt"type 'a result = 'a@\n";write_return~concurrency~errnofmt;write_fn~concurrency~errnofmt;write_map_result~concurrency~errnofmt;Format.fprintffmt"let returning t = Returns t@\n";Format.fprintffmt"let (@@->) f p = Function (f, p)@\n";Format.fprintffmt"let foreign : type a b. string -> (a -> b) fn -> (a -> b) =@\n";Format.fprintffmt" fun name t -> match t, name with@\n@[<v>";ListLabels.iterbindings~f:(fun(Bind(stub_name,external_name,fn))->Cstubs_generate_ml.case~concurrency~errno~stub_name~external_namefmtfn);Format.fprintffmt"@[<hov 2>@[|@ _,@ s@ ->@]@ ";Format.fprintffmt" @[Printf.ksprintf@ failwith@ \"No match for %%s\" s@]@]@]@.@\n";Format.fprintffmt"@\n";Format.fprintffmt"let foreign_value : type a. string -> a Ctypes.typ -> a Ctypes.ptr =@\n";Format.fprintffmt" fun name t -> match t, name with@\n@[<v>";ListLabels.iterval_bindings~f:(fun(Val_bind(stub_name,external_name,typ))->Cstubs_generate_ml.val_case~stub_name~external_namefmttyp);Format.fprintffmt"@[<hov 2>@[|@ _,@ s@ ->@]@ ";Format.fprintffmt" @[Printf.ksprintf@ failwith@ \"No match for %%s\" s@]@]@]@.@\n"letgen_ml~concurrency~errnoprefixfmt:(moduleFOREIGN')*(unit->unit)=letbindings=ref[]andval_bindings=ref[]andcounter=ref0inletvarprefixname=incrcounter;Printf.sprintf"%s_%d_%s"prefix!counternamein(modulestructtype'afn='aCtypes.fntype'areturn='alet(@->)=Ctypes.(@->)letreturning=Ctypes.returningtype'aresult=unitletforeigncnamefn=letname=varprefixcnameinbindings:=Bind(cname,name,fn)::!bindings;Cstubs_generate_ml.extern~concurrency~errno~stub_name:name~external_name:namefmtfnletforeign_valuecnametyp=letname=varprefixcnameinCstubs_generate_ml.extern~concurrency:`Sequential~errno:`Ignore_errno~stub_name:name~external_name:namefmtCtypes.(void@->returning(ptrvoid));val_bindings:=Val_bind(cname,name,typ)::!val_bindingsletreturning=Ctypes.returninglet(@->)=Ctypes.(@->)end),fun()->write_foreign~concurrency~errnofmt!bindings!val_bindingsletsequential=`Sequentialletlwt_jobs=`Lwt_jobsletlwt_preemptive=`Lwt_preemptiveletignore_errno=`Ignore_errnoletreturn_errno=`Return_errnoletunlocked=`Unlockedletconcurrency_headers=function`Sequential->[]|`Lwt_jobs|`Lwt_preemptive->["\"lwt_unix.h\"";"<caml/memory.h>"]|`Unlocked->["<caml/threads.h>"]leterrno_headers=function`Ignore_errno->[]|`Return_errno->["<errno.h>"]letheaders:concurrency_policy->errno_policy->stringlist=funconcurrencyerrno->["\"ctypes_cstubs_internals.h\""]@errno_headerserrno@concurrency_headersconcurrencyletwrite_c?(concurrency=`Sequential)?(errno=`Ignore_errno)fmt~prefix(moduleB:BINDINGS)=List.iter(Format.fprintffmt"#include %s@\n")(headersconcurrencyerrno);letmoduleM=B((valgen_c~concurrency~errnoprefixfmt))in()letwrite_ml?(concurrency=`Sequential)?(errno=`Ignore_errno)fmt~prefix(moduleB:BINDINGS)=letforeign,finally=gen_ml~concurrency~errnoprefixfmtinlet()=Format.fprintffmt"module CI = Cstubs_internals@\n@\n"inletmoduleM=B((valforeign))infinally()moduleTypes=Cstubs_structs