caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Thomas Fischbacher <Thomas.Fischbacher@Physik.Uni-Muenchen.DE>
To: caml-list@inria.fr
Subject: How to raise_with_arg() a tuple?
Date: Thu, 7 Jul 2005 20:05:52 +0200 (CEST)	[thread overview]
Message-ID: <Pine.LNX.4.61.0507071949030.20672@katrin.cip.physik.uni-muenchen.de> (raw)


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)


             reply	other threads:[~2005-07-07 18:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-07 18:05 Thomas Fischbacher [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.61.0507071949030.20672@katrin.cip.physik.uni-muenchen.de \
    --to=thomas.fischbacher@physik.uni-muenchen.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).