caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] C Interface question about failwith
@ 2013-03-01 11:06 Christoph Bauer
  2013-03-01 11:11 ` Romain Bardou
  0 siblings, 1 reply; 3+ messages in thread
From: Christoph Bauer @ 2013-03-01 11:06 UTC (permalink / raw)
  To: caml-list

Hi,

In C code I have a malloced-string error message, which must be freed. But I want also raise an OCaml exception
with this string. Actually what I need something like this:

static
void failwith_string_value(value msg)
{
  CAMLparam1(msg);
  caml_raise_with_arg(Field(caml_global_data, FAILURE_EXN), msg);
  CAMLnoreturn;
}

Then I could do:

  
     msg = caml_copy_string(s);
    free(s);
    failwith_string_value(msg);


But FAILURE_EXN is not defined in fail.h and more important
caml_global_data is defined in mlvalues.h, but I get an undefined reference link error.

Any ideas how to solve such a problem?

Thanks,
Christoph Bauer

    



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

* Re: [Caml-list] C Interface question about failwith
  2013-03-01 11:06 [Caml-list] C Interface question about failwith Christoph Bauer
@ 2013-03-01 11:11 ` Romain Bardou
  2013-03-01 11:40   ` Christoph Bauer
  0 siblings, 1 reply; 3+ messages in thread
From: Romain Bardou @ 2013-03-01 11:11 UTC (permalink / raw)
  To: caml-list

Hello,

I guess you could apply the generic mechanism described in the user manual:

http://caml.inria.fr/pub/docs/manual-ocaml/manual033.html
19.7.3  Registering OCaml exceptions for use in C functions

Maybe Failure is already registered for callback but I don't know…

Cheers,

-- 
Romain Bardou

Le 01/03/2013 12:06, Christoph Bauer a écrit :
> Hi,
>
> In C code I have a malloced-string error message, which must be freed. But I want also raise an OCaml exception
> with this string. Actually what I need something like this:
>
> static
> void failwith_string_value(value msg)
> {
>    CAMLparam1(msg);
>    caml_raise_with_arg(Field(caml_global_data, FAILURE_EXN), msg);
>    CAMLnoreturn;
> }
>
> Then I could do:
>
>
>       msg = caml_copy_string(s);
>      free(s);
>      failwith_string_value(msg);
>
>
> But FAILURE_EXN is not defined in fail.h and more important
> caml_global_data is defined in mlvalues.h, but I get an undefined reference link error.
>
> Any ideas how to solve such a problem?
>
> Thanks,
> Christoph Bauer
>
>
>
>
>


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

* RE: [Caml-list] C Interface question about failwith
  2013-03-01 11:11 ` Romain Bardou
@ 2013-03-01 11:40   ` Christoph Bauer
  0 siblings, 0 replies; 3+ messages in thread
From: Christoph Bauer @ 2013-03-01 11:40 UTC (permalink / raw)
  To: Romain Bardou, caml-list

Hello Romain,

Great idea, this will definitely work.  Thanks!

By checking asmrun/fail.c I also understand the link error: caml_global_data is only used for bytecode.
With ocamlopt, this could work:

static
void failwith_string_value(value msg) Noreturn;

typedef value caml_generated_constant[1];

extern caml_generated_constant caml_exn_Failure;

static
void failwith_string_value(value msg)
{
  CAMLparam1(msg);
  caml_raise_with_arg((value)caml_exn_Failure, msg);
  CAMLnoreturn;
}

Kind Regards,
Christoph Bauer

-----Original Message-----
From: caml-list-request@inria.fr [mailto:caml-list-request@inria.fr] On Behalf Of Romain Bardou
Sent: Freitag, 1. März 2013 12:11
To: caml-list@inria.fr
Subject: Re: [Caml-list] C Interface question about failwith

Hello,

I guess you could apply the generic mechanism described in the user manual:

http://caml.inria.fr/pub/docs/manual-ocaml/manual033.html
19.7.3  Registering OCaml exceptions for use in C functions

Maybe Failure is already registered for callback but I don't know.

Cheers,

--
Romain Bardou

Le 01/03/2013 12:06, Christoph Bauer a écrit :
> Hi,
>
> In C code I have a malloced-string error message, which must be freed. 
> But I want also raise an OCaml exception with this string. Actually what I need something like this:
>
> static
> void failwith_string_value(value msg)
> {
>    CAMLparam1(msg);
>    caml_raise_with_arg(Field(caml_global_data, FAILURE_EXN), msg);
>    CAMLnoreturn;
> }
>
> Then I could do:
>
>
>       msg = caml_copy_string(s);
>      free(s);
>      failwith_string_value(msg);
>
>
> But FAILURE_EXN is not defined in fail.h and more important 
> caml_global_data is defined in mlvalues.h, but I get an undefined reference link error.
>
> Any ideas how to solve such a problem?
>
> Thanks,
> Christoph Bauer
>
>
>
>
>


--
Caml-list mailing list.  Subscription management and archives:
https://sympa.inria.fr/sympa/arc/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

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

end of thread, other threads:[~2013-03-01 11:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-01 11:06 [Caml-list] C Interface question about failwith Christoph Bauer
2013-03-01 11:11 ` Romain Bardou
2013-03-01 11:40   ` Christoph Bauer

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).