Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for returning errno from generated stubs #392

Merged
merged 11 commits into from
Jun 1, 2016
Merged

Support for returning errno from generated stubs #392

merged 11 commits into from
Jun 1, 2016

Conversation

yallop
Copy link
Owner

@yallop yallop commented May 26, 2016

This pull request adds support for returning errno alongside the return value of bound functions in generated stubs. As with #391, the interface changes are fairly modest: there is an additional argument, errno to write_c and write_ml, specifying the policy to use.

Besides the default, ignore_errno, there is one additional option, return_errno, which changes the return type of each function binding from t to t * int.

For example, here is the example bindings description for puts and rmdir from #391:

module Bindings(F: Cstubs.FOREIGN) =
struct
  open F
  let puts = foreign "puts" (string @-> returning int)
  let rmdir = foreign "rmdir" (string @-> returning int)
end

The C and OCaml portions of the bindings may be generated in the usual way, specifying return_errno with a keyword argument:

write_c fmt ~errno:return_errno ~prefix (module Bindings)
write_ml fmt ~errno:return_errno ~prefix (module Bindings)

In the module signature built by the linking step (i.e. by applying Bindings to the generated OCaml module) each function binding returns a pair of the return value of the bound C function and the value of errno just after the call:

(*
sig
  val puts : string -> int * int
  val rmdir : string -> int * int
end
*)
module C_errno = Bindings(Generated_module)

Errno and Lwt

There is also support for the combination of the new errno and concurrency options.

Passing ~concurrency:lwt_jobs to write_c and write_ml changes the return type of a function binding from t to Lwt.t (or rather to the wrapper type return).

Passing ~errno:return_errno changes the return type from t to t * int.

Passing both ~concurrency:lwt_jobs and ~errno:return_errno changes the return type from t to (t * int) Lwt.t --- that is, to an Lwt job that returns both the result of calling the bound C function and the value of errno just after the call.

@yallop yallop merged commit 90fd380 into yallop:master Jun 1, 2016
@yallop yallop deleted the errno-stubs branch June 1, 2016 16:26
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.5%) to 87.592% when pulling d883ab2 on yallop:errno-stubs into c6a99c1 on ocamllabs:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants