summaryrefslogtreecommitdiff
path: root/testsuite/tests/callback/test6.ml
blob: e3ff477f5b6da2a1d697f0b5e8fb962c2792fa8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
(* TEST
   include unix
   modules = "test6_.c"
   * libunix
   ** bytecode
   ** native
*)

(* Tests nested calls from C (main C) to OCaml (main OCaml) to C (caml_to_c) to
 * OCaml (c_to_caml) to C (printf functions). Exception is raised in a
 * Callback, which unwinds the C stack and gets caught in OCaml. *)

exception E

let printf = Printf.printf

let c_to_caml () =
  printf "[Caml] Enter c_to_caml\n%!";
  printf "[Caml] c_to_caml: raise exception\n%!";
  raise E

let _ = Callback.register "c_to_caml" c_to_caml

external caml_to_c : unit -> unit = "caml_to_c"

let _ =
  try
    printf "[Caml] Call caml_to_c\n%!";
    caml_to_c ();
    printf "[Caml] Return from caml_to_c\n%!"
  with E ->
    (printf "[Caml] Caught exception\n%!";
     try caml_to_c() with E -> printf "[Caml] Caught exceception again\n%!";
     printf "[Caml] Done\n%!")