develooper Front page | perl.perl5.porters | Postings from October 2013

[perl #120100] Should PUSHSTACKi() set the mark?

Thread Previous
Nicholas Clark
October 4, 2013 12:14
[perl #120100] Should PUSHSTACKi() set the mark?
Message ID:
# New Ticket Created by  Nicholas Clark 
# Please include the string:  [perl #120100]
# in the subject line of all future correspondence about this issue. 
# <URL: >

As best I can tell, PUSHSTACKi() is completely undocumented. It pushes a new
(Perl) stack (ie the thing for @_), so that the old stack doesn't need to
move in memory if it needs to get larger.

If I understand it correctly, the mark stack marks places on the perl stack.
marks are stored as I32s [yay :-(] which give the index of the marked spot
on the Perl stack. ie, positions in the array which implements the current
stack. It works like this:

#define PUSHMARK(p)	\
	STMT_START {					\
	    if (++PL_markstack_ptr == PL_markstack_max)	\
	    markstack_grow();				\
	    *PL_markstack_ptr = (I32)((p) - PL_stack_base);\

and that last assignment is using pointer arithmetic to calculate the offset.

Now, when PUSHSTACKi() does its stuff, it calls SWITCHSTACK, which does this:

#define SWITCHSTACK(f,t) \
    STMT_START {							\
	AvFILLp(f) = sp - PL_stack_base;				\
	PL_stack_base = AvARRAY(t);					\
	PL_stack_max = PL_stack_base + AvMAX(t);			\
	sp = PL_stack_sp = PL_stack_base + AvFILLp(t);			\
	PL_curstack = t;						\
    } STMT_END

ie, use a different array. So at this point, the index represented by the
mark is garbage.

This strikes me as a bit of gotcha. So should we change SWITCHSTACK() to
set a mark at the start of the swapped-in stack?
I don't think that anything on CPAN is using SWITCHSTACK() directly, and
the 9 non-multicall users of PUSHSTACK()/PUSHTACKi() on CPAN all seem to set
a mark themselves (or I think don't need it). Likewise all the core code
either sets a mark, or calls Perl_load_module(). So I don't think that there
are any current bugs. It just feels like something we could make more robust.

Nicholas Clark

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About