Mailing List Archive

stack depth in XSUB?
Hello,

How can I get the current Perl stack depth from an XSUB?

I see that CvDEPTH(find_runcv(NULL)) works, but is CvDEPTH considered part of the API?

Thank you!

cheers,
-Felipe
Re: stack depth in XSUB? [ In reply to ]
On Tue, Jan 12, 2021 at 10:05:19PM -0500, Felipe Gasper wrote:
> Hello,
>
> How can I get the current Perl stack depth from an XSUB?
>
> I see that CvDEPTH(find_runcv(NULL)) works, but is CvDEPTH considered part of the API?

That's the recursion depth for the CV:

$ perl -MB -E 'sub foo { say (my $d = $_[0]->DEPTH); foo($_[0]) if $d < 5 } foo(B::svref_2object(\&foo))'
1
2
3
4
5

The depth of the argument stack is SP (or PL_stack_sp) less the base PL_stack_base.

See Perl_debstackptrs() or PL_debstack() in deb.c for example.

Tony
Re: stack depth in XSUB? [ In reply to ]
> On Jan 12, 2021, at 10:26 PM, Tony Cook <tony@develop-help.com> wrote:
>
> On Tue, Jan 12, 2021 at 10:05:19PM -0500, Felipe Gasper wrote:
>> Hello,
>>
>> How can I get the current Perl stack depth from an XSUB?
>>
>> I see that CvDEPTH(find_runcv(NULL)) works, but is CvDEPTH considered part of the API?
>
> That's the recursion depth for the CV:
>
> $ perl -MB -E 'sub foo { say (my $d = $_[0]->DEPTH); foo($_[0]) if $d < 5 } foo(B::svref_2object(\&foo))'
> 1
> 2
> 3
> 4
> 5
>
> The depth of the argument stack is SP (or PL_stack_sp) less the base PL_stack_base.
>
> See Perl_debstackptrs() or PL_debstack() in deb.c for example.

Thank you!

… and thank you also to Paul, who (as I now see) documented this stuff in perlguts after my workstation’s default perl (5.26) was published.

cheers,
-Felipe
Re: stack depth in XSUB? [ In reply to ]
On Wed, 13 Jan 2021 06:16:43 -0500
Felipe Gasper <felipe@felipegasper.com> wrote:

> … and thank you also to Paul, who (as I now see) documented this
> stuff in perlguts after my workstation’s default perl (5.26) was
> published.

:)

--
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/