Discussion:
Throwing exceptions from XS code
(too old to reply)
Emmanuel Rodriguez
2011-10-26 19:11:39 UTC
Permalink
Hi,

I'm writing XS bindings for JavaScriptCore (WebKit's JavaScript
engine) and a lot of the functions in the API throw an exception. The
exception it self is a JavaScript variable (kind of like an SV* for
Perl).

At first I though of rethrowing that variable wrapped as an SV but I
don't know how to do this. I read somewhere that Perl_croak_sv can do
that but I get a runtime because the symbol is undefined:

/* undefined symbol: Perl_croak_sv */
SV *err;
err = jsc_perl_js_value_to_sv(ctx, exception);
Perl_croak_sv(aTHX_ err);

For now I reverted at returning the error as a JSON string and to use
croak but I'm leaking the string with the message since I can't free
it after croak():

char *error;
error = jsc_perl_js_value_to_json(ctx, exception);
croak("%s", error);/* How can we throw an SV ? */
free(error);/* FIXME is this free called ? */

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?
--
Emmanuel Rodriguez
Steffen Mueller
2011-10-26 20:02:04 UTC
Permalink
Post by Emmanuel Rodriguez
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
Emmanuel Rodriguez
2011-10-27 08:13:15 UTC
Permalink
Post by Emmanuel Rodriguez
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?
                 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
Loading...