caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* How to raise_with_arg() a tuple?
@ 2005-07-07 18:05 Thomas Fischbacher
  2005-07-07 18:42 ` [Caml-list] " Olivier Andrieu
  0 siblings, 1 reply; 5+ messages in thread
From: Thomas Fischbacher @ 2005-07-07 18:05 UTC (permalink / raw)
  To: caml-list


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)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-07-08 16:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-07 18:05 How to raise_with_arg() a tuple? Thomas Fischbacher
2005-07-07 18:42 ` [Caml-list] " Olivier Andrieu
2005-07-08 15:55   ` Thomas Fischbacher
2005-07-08 16:24     ` Kenneth Knowles
2005-07-08 16:58       ` Thomas Fischbacher

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).