NERDVANA has asked for the wisdom of the Perl Monks concerning the following question:
next_kv is a method that returns one or more key+value pairs, as a list. So, the function could return up to 2x the number of nodes in the tree, or zero, or usually just one for most of the ways the function can be called. To make things more complicated, I decided to share the implementation of this method with the 'next_keys', 'next_values', and 'next' methods, which are all basically the same algorithm but return different things to the caller. I used perl's "ix" aliasing mechanism.
Here is the XS code.
The line of the test that fails is
is( [ $tree->iter(1)->next_kv(2) ], [ 1, 2, 2, 4 ], 'next_kv' );
which means "starting from element key==1, return the next two key+value pairs, which in this unit test happen to be { 1 => 2, 2 => 4 }
Here is The C code it generates, with a warning added for every occurrence of EXTEND and XSRETURN and usage of ST(x) in the case where ix == 3 (which is what is failing).
My understanding of XS is that in a xsub declared with PPCODE, the SP is pointed at the first argument, so the stack is at least as long as the number of arguments received. If you want to return more elements than the number of arguments passed to you, you need to call EXTEND(SP, n) where n is the number of return values. (in the code, I make the assumption that there was always one argument because it is a method that requires one, and so ST(0) should always be safe to use.)
This is the relevant output of the unit test:
EXTEND(SP, 4 at t/21-iter.t line 167. Accessing ST(0), ST(1) at t/21-iter.t line 167. Accessing ST(2), ST(3) at t/21-iter.t line 167. XSRETURN(4) at t/21-iter.t line 167. not ok 6 - iter_get_multi { ok 1 - next_keys(*) ok 2 - next_keys overshoot ok 3 - reverse next_keys overshoot ok 4 - reverse next_keys for 1 ok 5 - iterate nothing ok 6 - next_values(4) ok 7 - next_nodes(2) } # Failed test 'iter_get_multi' # at t/21-iter.t line 172. # Caught exception in subtest: panic: XSUB Tree::RB::XS::Iter::next_kv + (TreeRBXS.c) failed to extend arg stack: base=56076974fa88, sp=56076 +974faa8, hwm=56076974faa0
If you want to compile it, the source uses Dist::Zilla which I know offends some people. However, you can just grab it from CPAN because the current HEAD of the git repo is exactly what is published right now.
Note that the assertion only fails on debug builds of perl. I was unable to reproduce the error until I realized that.
I used:
perlbrew install -j 10 --as p536-debug --noman --debug --thread perl-5.36.0
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XS debugging "failed to extend arg stack"
by hv (Prior) on Jun 03, 2022 at 02:05 UTC | |
by ikegami (Patriarch) on Jun 03, 2022 at 16:03 UTC | |
by hv (Prior) on Jun 03, 2022 at 16:59 UTC | |
by Anonymous Monk on Jun 04, 2022 at 04:32 UTC | |
by NERDVANA (Priest) on Jun 03, 2022 at 16:58 UTC | |
by NERDVANA (Priest) on Jun 03, 2022 at 05:18 UTC | |
by khw (Acolyte) on Jul 03, 2022 at 13:04 UTC | |
by NERDVANA (Priest) on Jul 06, 2022 at 01:18 UTC | |
by NERDVANA (Priest) on Jun 03, 2022 at 16:49 UTC |