in reply to Re^3: Overload '+=' with XSub
in thread Overload '+=' with XSub

It pushes a value on the return stack

Yeaaah ... it pushes a value onto the stack ... but does that mean it returns a value ? (It's also pushing onto the stack something that's already on the stack, isn't it ? .... which doesn't do much to help the intellectually impaired :-) I thought there would have to be an XSRETURN() before anything was returned ... or a RETVAL ....

We see (in INLINE.h):
#define Inline_Stack_Push(x) XPUSHs(x)
Also:
#define Inline_Stack_Return(x) XSRETURN(x)
In Inline::C, we have to Inline_Stack_Return(x) if we wish to return anything from a 'void' function. Since Inline::C and XS are essentially one and the same thing, I expected that we would have to explicitly XSRETURN(x) from an XSub (if we wanted to return anything).

Let's concentrate (for the moment, anyway) on how to rewrite my 'overload_add_eq' in such a way that the refcount increase is not needed. I tried:
void overload_add_eq(SV * obj, SV * addon, SV * third) { dXSARGS; Card* c = (Card *)SvIV(SvRV(obj)); c->value += SvIV(addon); XPUSHs(obj); }
but still got the same failure with the '+=" operator. (Again, when explicitly calling overload_add_eq($obj, $addon, 0); there's no problem.)

Shit!! ... but hang on ... I also tried:
void overload_add_eq(SV * obj, SV * addon, SV * third) { dXSARGS; Card* c = (Card *)SvIV(SvRV(obj)); c->value += SvIV(addon); XPUSHs(obj); XSRETURN(1); }
and that worked fine with the '+=' overloading ... and no increase to the refcount !!! It also worked fine when I explicitly called overload_add_eq($obj, $addon, 0);(This might even constitute "progress".)

So ... I guess one remaining question is "how come I have to call both dXSARGS and XSRETURN (whereas Robert Hyde's GMP code didn't have to do that) ?"

Another question is "Has anything been achieved by rewriting 'overload_add_eq' ?". I guess the answer to that question will be "no" ... the proof will be in the benchmarking.

Thanks for the feedback, Anno. Please don't take my questions/objections/refutations as anything other than the ramblings of one who is both ill-informed and a little dim :-)

Cheers,
Rob

Replies are listed 'Best First'.
Re^5: Overload '+=' with XSub
by Anno (Deacon) on Mar 29, 2007 at 17:30 UTC
    To answer thoroughly I'd have to re-read and test more than I find myself inclined, so all I can give you is some hand-waving. You wrote:

    Since Inline::C and XS are essentially one and the same thing ...

    Careful there. They differ quite a bit in some formalities, especially in parameter passing. That's what we're dealing with here.

    So ... I guess one remaining question is "how come I have to call both dXSARGS and XSRETURN (whereas Robert Hyde's GMP code didn't have to do that) ?"

    As far as I remember you normally call neither dXSARGS nor XSRETURN very often yourself. That's true for Inline as well as for bare XS. RETVAL is only for a single scalar return value.

    Speaking of which, why are you doing this the hard way? You have to return exactly one scalar (the modified object), so why not declare the method that way and leave it to Inline?

    Anno