Alan Burlison <[EMAIL PROTECTED]> writes:
>Nick Ing-Simmons wrote:
>
>> Ah, oops sorry I missed the PPCODE vs CODE issue.
>> (The machine-level programmer in me distrusts moving the stack back before
>> one is finished with it so for that reason I seldom use PPCODE.)
>
>I'm pushing variably sized lists onto the stack, for example when called
>in a scalar context the routine returns a single value, and a list when
>called in a list context (or an empty list on error). According to
>perlxs if you are going to do that sort of thing you need to use PPCODE
>instead of CODE: Isn't that correct?
Yes or do your own SP twiddling.
Guts of what Tk uses is:
SV **args = &ST(0);
int offset = args - sp;
if (count > items)
{
EXTEND(sp, (count - items));
}
/* Now move 'args' to 0'th arg position in current stack */
args = sp + offset;
if (count)
{
int i = count;
while (i-- > 0)
{
args[i] = some_sv();
}
}
PUTBACK;
XSRETURN(count);
PPCODE bit me once when C code called back into perl after PPCODE
had moved back SP, then tried to use ST(N) after perl had returned
back to the XSUB - and of course it had been overwritten by then.
Not that Tk is a perfect model of how to do it - it has its own particular
problems. (Perls stack is passed as args to core tk, which can call
callbacks and hence back into perl.)
--
Nick Ing-Simmons