From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id C4AFBBCAE for ; Thu, 7 Jul 2005 20:42:06 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j67Ig626007992 for ; Thu, 7 Jul 2005 20:42:06 +0200 Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA15793 for ; Thu, 7 Jul 2005 20:42:05 +0200 (MET DST) Received: from mallaury.nerim.net (smtp-104-thursday.noc.nerim.net [62.4.17.104]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j67Ig5uP000531 for ; Thu, 7 Jul 2005 20:42:05 +0200 Received: from karryall.dnsalias.org (karryall.dnsalias.org [213.41.180.99]) by mallaury.nerim.net (Postfix) with ESMTP id AA1EF4F3A4; Thu, 7 Jul 2005 20:41:53 +0200 (CEST) Received: by karryall.dnsalias.org (Postfix, from userid 500) id 1345B5978BC; Thu, 7 Jul 2005 20:42:05 +0200 (CEST) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-ID: <17101.30588.827486.654309@karryall.dnsalias.org> Date: Thu, 7 Jul 2005 20:42:04 +0200 From: Olivier Andrieu To: Thomas Fischbacher Cc: caml-list@inria.fr Subject: Re: [Caml-list] How to raise_with_arg() a tuple? In-Reply-To: References: X-Mailer: VM 7.19 under Emacs 21.4.1 X-Miltered: at nez-perce with ID 42CD777E.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 42CD777D.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; andrieu:01 andrieu:01 ijm:01 caml-list:01 exn:01 defines:01 exn:01 bool:01 ocaml:01 camlprim:01 camlparam:01 val:01 fprintf:01 stderr:01 alloc:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: Thomas Fischbacher [Thursday 7 July 2005] : > > > 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 It's the usual gotcha with variant constructor that have multiple arguments. The exception you defines has two arguments, whereas exception Test_exn of (int * int) has only one argument -- a tuple. It's not the same thing. > 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); here you are raising an exception with one tuple argument, this doesn't match the exception you defined. There's no function to directly create an exception value with multiple argument, you have to do this (IIRC, not tested) : ex = alloc(3, 0); Store_field(ex, 0, *caml_named_value("ocaml_exn_test"); Store_field(ex, 1, Val_int(2)); Store_field(ex, 2, Val_int(4)); caml_raise(ex); > ===> > 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]. > <=== actually it's ok to raise exception directly (ie, not through CAMLreturn), the runtime takes care of releasing the local GC roots. -- Olivier