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

Lwt return void #460

Merged
merged 2 commits into from
Oct 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/cstubs/cstubs_generate_c.ml
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,14 @@ struct
let structure_type stub_name =
structure (sprintf "job_%s" stub_name)

let structure ~errno ~stub_name fmt fn args result =
let structure (type r) ~errno ~stub_name fmt fn args (result : r typ) =
let open Ctypes in
let s = structure_type stub_name in
let (_ : (_,_) field) = field s "job" lwt_unix_job in
let (_ : (_,_) field) = field s "result" result in
let () = match result with
Void -> let (_ : (_,_) field) = field s "result" int in ()
| result -> let (_ : (_,_) field) = field s "result" result in ()
in
let () = match errno with
`Ignore_errno -> ()
| `Return_errno -> ignore (field s "error_status" sint)
Expand All @@ -373,19 +376,21 @@ struct
let () = seal s in
fprintf fmt "@[%a@];@\n" (fun t -> format_typ t) s

let worker ~errno ~cname ~stub_name fmt f result args =
let worker (type r) ~errno ~cname ~stub_name fmt f (result : r typ) args =
let fn' = { fname = cname;
allocates = false;
reads_ocaml_heap = false;
fn = Fn f }
and j = "j", Ty (ptr (structure_type stub_name)) in
let rec body args : _ -> ccomp = function
[] ->
let r c =
Generate_C.cast ~from:(Ty result) ~into:(Ty Void)
(`LetAssign (`PointerField (`Local j, "result"),
`App (fn', List.rev args),
c))
let r c = match result with
| Void -> Generate_C.(`App (fn', List.rev args) >> c)
| result ->
Generate_C.cast ~from:(Ty result) ~into:(Ty Void)
(`LetAssign (`PointerField (`Local j, "result"),
`App (fn', List.rev args),
c))
in
begin match errno with
`Ignore_errno -> r (`Return (Ty Void, (`Int Signed.SInt.zero)))
Expand All @@ -407,7 +412,7 @@ struct
body [] args,
`Static))

let result ~errno ~stub_name fmt fn result =
let result (type r) ~errno ~stub_name fmt fn (result : r typ) =
begin
fprintf fmt "@[static@ value@ result_%s@;@[(struct@ job_%s@ *j)@]@]@;@[<2>{@\n"
stub_name stub_name;
Expand All @@ -421,12 +426,17 @@ struct
fprintf fmt "@[Store_field(rv,@ 1,@ ctypes_copy_sint(j->error_status));@]@\n";
fprintf fmt "@[Store_field(rv,@ 0,@ ";
in
fprintf fmt "%a);@]@\n"
(fun fmt ty ->
Cstubs_emit_c.ceff fmt
(Generate_C.inj ty
(`Local ("j->result", Cstubs_c_language.(Ty ty)))))
result;
fprintf fmt "%a);@]@\n"
(let f (type r) fmt : r typ -> _ = function
Void ->
Cstubs_emit_c.ceff fmt Generate_C.val_unit
| ty ->
Cstubs_emit_c.ceff fmt
(Generate_C.inj ty
(`Local ("j->result", Cstubs_c_language.(Ty ty))))
in f
)
result;
fprintf fmt "@[lwt_unix_free_job(&j->job)@];@\n";
fprintf fmt "@[CAMLreturn@ (rv)@];@]@\n";
fprintf fmt "}@\n";
Expand Down
6 changes: 6 additions & 0 deletions tests/clib/test_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,12 @@ int return_10(void)
return 10;
}

void return_void(int *x)
{
*x = 10;
return;
}

int callback_returns_char_a(char (*f)(void))
{
return f() == 'a' ? 1 : 0;
Expand Down
1 change: 1 addition & 0 deletions tests/clib/test_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ void *retrieve_ocaml_value(void);

int sixargs(int, int, int, int, int, int);
int return_10(void);
void return_void(int *);

int callback_returns_char_a(char (*)(void));

Expand Down
3 changes: 3 additions & 0 deletions tests/test-lwt-jobs/stubs/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ struct

let return_10 = foreign "return_10"
(void @-> returning int)

let return_void = foreign "return_void"
(ptr int @-> returning void)
end
13 changes: 13 additions & 0 deletions tests/test-lwt-jobs/test_lwt_jobs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ let test_no_args _ =
assert_equal 10 i;
Lwt.return ())

(*
Test calling functions that return void.
*)
let test_return_void _ =
let open Lwt.Infix in
Lwt_unix.run
(let x_p = allocate_n ~count:1 int in
(Bindings.return_void x_p).Generated_bindings.lwt >>= fun () ->
assert_equal 10 (!@ x_p);
Lwt.return ())

let suite = "Lwt job tests" >:::
["calling sqrt"
Expand All @@ -104,6 +114,9 @@ let suite = "Lwt job tests" >:::

"functions with no arguments"
>:: test_no_args;

"functions that return void"
>:: test_return_void;
]


Expand Down
3 changes: 3 additions & 0 deletions tests/test-lwt-preemptive/stubs/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ struct

let return_10 = foreign "return_10"
(void @-> returning int)

let return_void = foreign "return_void"
(ptr int @-> returning void)
end
14 changes: 14 additions & 0 deletions tests/test-lwt-preemptive/test_lwt_jobs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ let test_no_args _ =
assert_equal 10 i;
Lwt.return ())

(*
Test calling functions that return void.
*)
let test_return_void _ =
let open Lwt.Infix in
Lwt_unix.run
(let x_p = allocate_n ~count:1 int in
(Bindings.return_void x_p).Generated_bindings.lwt >>= fun () ->
assert_equal 10 (!@ x_p);
Lwt.return ())


let suite = "Lwt job tests" >:::
["calling sqrt"
Expand All @@ -104,6 +115,9 @@ let suite = "Lwt job tests" >:::

"functions with no arguments"
>:: test_no_args;

"functions that return void"
>:: test_return_void;
]


Expand Down
3 changes: 3 additions & 0 deletions tests/test-returning-errno-lwt-jobs/stubs/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ struct

let return_10 = foreign "return_10"
(void @-> returning int)

let return_void = foreign "return_void"
(ptr int @-> returning void)
end
14 changes: 14 additions & 0 deletions tests/test-returning-errno-lwt-jobs/test_returning_errno.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ let test_no_args _ =
assert_equal 10 i;
Lwt.return ())

(*
Test calling functions that return void.
*)
let test_return_void _ =
let open Lwt.Infix in
Lwt_unix.run
(let x_p = allocate_n ~count:1 int in
(Bindings.return_void x_p).Generated_bindings.lwt >>= fun ((), errno) ->
assert_equal 10 (!@ x_p);
Lwt.return ())


let suite = "Errno tests" >:::
["calling stat"
Expand All @@ -63,6 +74,9 @@ let suite = "Errno tests" >:::

"functions with no arguments"
>:: test_no_args;

"functions that return void"
>:: test_return_void;
]


Expand Down
3 changes: 3 additions & 0 deletions tests/test-returning-errno-lwt-preemptive/stubs/functions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ struct

let return_10 = foreign "return_10"
(void @-> returning int)

let return_void = foreign "return_void"
(ptr int @-> returning void)
end
14 changes: 14 additions & 0 deletions tests/test-returning-errno-lwt-preemptive/test_returning_errno.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ let test_no_args _ =
assert_equal 10 i;
Lwt.return ())

(*
Test calling functions that return void.
*)
let test_return_void _ =
let open Lwt.Infix in
Lwt_unix.run
(let x_p = allocate_n ~count:1 int in
(Bindings.return_void x_p).Generated_bindings.lwt >>= fun ((), errno) ->
assert_equal 10 (!@ x_p);
Lwt.return ())


let suite = "Errno tests" >:::
["calling stat"
Expand All @@ -63,6 +74,9 @@ let suite = "Errno tests" >:::

"functions with no arguments"
>:: test_no_args;

"functions that return void"
>:: test_return_void;
]


Expand Down