Version française
Home     About     Download     Resources     Contact us    
Browse thread
How to raise_with_arg() a tuple?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Thomas Fischbacher <Thomas.Fischbacher@P...>
Subject: How to raise_with_arg() a tuple?

Hello together,

I'd like to throw an exception from within C code which provides more 
information to Caml than just a string.

If I do it that way:

=== Caml ===

exception Test_exn of int * int

external test_raise_tuple: bool -> (int*int) = "caml_test_raise_tuple"

let _ = Callback.register_exception "ocaml_exn_test" (Test_exn (0,0));;

=== C ===

CAMLprim caml_test_raise_tuple(value x)
{
  CAMLparam1(x);
  int cx;

  cx=Int_val(x);
  fprintf(stderr,"DDD x=%d\n",cx);

  CAMLlocal1(ex);
  ex=alloc_tuple(2);

  Store_field(ex,0,Val_int(2));
  Store_field(ex,1,Val_int(4));

  if(cx)
    {
      raise_with_arg(*caml_named_value("ocaml_exn_test"), ex);
    }
  else
    {
      CAMLreturn(ex);
    }
}

======

...then just returning the tuple works as expected,
but throwing the tuple gives me crazy values - like 

Test_exn (67385386, 1408)

instead of

Test_exn (2,4).

Does anyone have an idea what is going on here, and how to repair this? 
Maybe I just missed something essential in the documentation. I tried to 
find an example in existing OCaml libraries where this is used, but 
somehow I was out of luck there so far... All I found about this 
issue is this bit of documentation in ocaml's byterun/memory.h:

===>
   Your function may raise an exception or return a [value] with the
   [CAMLreturn] macro.  Its argument is simply the [value] returned by
   your function.  Do NOT directly return a [value] with the [return]
   keyword.  If your function returns void, use [CAMLreturn0].
<===

...and the definition says:

===>
#define CAMLreturn(result) do{ \
  caml_local_roots = caml__frame; \
  return (result); \
}while(0)
<===

I tried a few obvious and non-obvious things related to what I see in 
there, but without success so far. So, how does one do that?

-- 
regards,               tf@cip.physik.uni-muenchen.de              (o_
 Thomas Fischbacher -  http://www.cip.physik.uni-muenchen.de/~tf  //\
(lambda (n) ((lambda (p q r) (p p q r)) (lambda (g x y)           V_/_
(if (= x 0) y (g g (- x 1) (* x y)))) n 1))                  (Debian GNU)