Re: Throwing exceptions from XS code

2011-10-27 Thread Emmanuel Rodriguez
On Wed, Oct 26, 2011 at 22:02, Steffen Mueller smuel...@cpan.org wrote:
 On 10/26/2011 09:11 PM, Emmanuel Rodriguez wrote:

 Is it possible to throw an blessed exception from XS code? If so how
 can I achieve this? Otherwise, how can I propagate an error as a
 string and free the string?

 Haven't tried myself, but: Set $@ yourself and then croak(NULL). From
 perlapi.pod:

                  errsv = get_sv(@, GV_ADD);
                  sv_setsv(errsv, exception_object);
                  croak(NULL);


It works, at least no crashes so far :)

Another question: since I'm returning now the sv exception_object, do
you know if I have to call sv_2mortal() on it?

Thanks!
-- 
Emmanuel Rodriguez


Re: Throwing exceptions from XS code

2011-10-27 Thread Steffen Mueller
 On Wed, Oct 26, 2011 at 22:02, Steffen Mueller smuel...@cpan.org wrote:
 It works, at least no crashes so far :)

 Another question: since I'm returning now the sv exception_object, do
 you know if I have to call sv_2mortal() on it?

You're not returning it, but assigning it to $@, which I believe is
refcounted normally. So I guess you do not have to mortalize.

You can, of course, just try it and see if it crashes. If in doubt,
valgrind will tell you in either case.

Cheers,
Steffen



Re: Throwing exceptions from XS code

2011-10-26 Thread Steffen Mueller

On 10/26/2011 09:11 PM, Emmanuel Rodriguez wrote:

Is it possible to throw an blessed exception from XS code? If so how
can I achieve this? Otherwise, how can I propagate an error as a
string and free the string?


Haven't tried myself, but: Set $@ yourself and then croak(NULL). From 
perlapi.pod:


  errsv = get_sv(@, GV_ADD);
  sv_setsv(errsv, exception_object);
  croak(NULL);

See the croak docs in perlapi.

--Steffen