Discussion:
Problem on perl callbacks from XS
(too old to reply)
Cosimo
2008-05-24 21:54:35 UTC
Permalink
Hi to perl.xs,

I have posted this question also to perlmonks,
under http://www.perlmonks.org/?node_id=688332.

I've been fighting with this problem for a while, and despite my best
efforts, I'm still stuck. Maybe very near to the solution, but still
stuck. I believe that the following simple case is the root of the
problem. Feel free to correct/direct me. Relevant code follows:

GV *to_string = gv_fetchmethod (SvSTASH (sv), "toString");
if (to_string) {
dSP;
SV *res_sv; /* result scalar */

ENTER;
SAVETMPS;

/* Declare function arguments */
PUSHMARK (SP);
XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
PUTBACK;

/* Call in scalar context (G_SCALAR) */
call_sv ((SV *)GvCV (to_string), G_SCALAR);
SPAGAIN;

/* Fetch returned SV from the stack */
res_sv = POPs;
PUTBACK;

/* Append returned SvPVX to our SV */
append_sv(res_sv, result);

FREETMPS;
LEAVE;
}

This code is partly taken from JSON::XS. I'm trying to write an XS
function that takes an array or hashref in sv and, with some custom
rules, serializes the content into a result scalar as one big string
with everything.

When an hashref is really a blessed object, I want its toString()
method to be called to dump the hashref content to my result SV.
Calling the object's toString() method is the task for the code I
shown you.

I think the problem lies here: what happens when, eventually, an
object's toString() method code, for whatever reason, happens to
contain a call to my XS function? (that could, in turn, call other
toString() methods for other kind of objects...).

Does this break my stack manipulation? I think so, because I see
segfaults and other bad things when toString() completes and the the
code tries to return the resulting SV to the caller.

I feel I'm missing something about the stack manipulation in this
case, but I really don't know how to proceed. Thank you for any help
you can provide.
Cosimo
2008-05-25 18:25:34 UTC
Permalink
Post by Cosimo
Hi to perl.xs,
I have posted this question also to perlmonks,
under http://www.perlmonks.org/?node_id=688332.
I solved the problem.
Perlmonks node was updated with info on how I did it.

--
Cosimo

Loading...