root/lang/perl/arguments/trunk/arguments.xs @ 14595

Revision 14595, 1.7 kB (checked in by takesako, 5 years ago)

fixed up for shibuya.pm#9 XS Nite

Line 
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5/* base xs code from PadWalker.xs */
6I32
7_dopoptosub_at(pTHX_ PERL_CONTEXT *cxstk, I32 starting_block)
8{
9    dTHR;
10    I32 i;
11    PERL_CONTEXT *cx;
12    for (i = starting_block; i >= 0; i--) {
13        cx = &cxstk[i];
14        switch (CxTYPE(cx)) {
15        default:
16            continue;
17        case CXt_SUB:
18#ifdef CXt_FORMAT /* In Perl 5.005, formats just used CXt_SUB */
19        case CXt_FORMAT:
20#endif
21            return i;
22        }
23    }
24    return i;
25}
26
27PERL_CONTEXT*
28_upcontext(pTHX_ I32 count)
29{
30    PERL_SI *top_si = PL_curstackinfo;
31    I32 cxix = _dopoptosub_at(aTHX_ cxstack, cxstack_ix);
32    PERL_CONTEXT *ccstack = cxstack;
33
34    for (;;) {
35        while (cxix < 0 && top_si->si_type != PERLSI_MAIN) {
36            top_si  = top_si->si_prev;
37            ccstack = top_si->si_cxstack;
38            cxix = _dopoptosub_at(aTHX_ ccstack, top_si->si_cxix);
39        }
40        if (cxix < 0) {
41            return (PERL_CONTEXT *) NULL;
42        }
43        if (PL_DBsub && ccstack[cxix].blk_sub.cv == GvCV(PL_DBsub))
44            count++;
45        if (!count--)
46            break;
47
48        cxix = _dopoptosub_at(aTHX_ ccstack, cxix - 1);
49    }
50    return &ccstack[cxix];
51}
52
53/* base xs code from Devel-Caller/Caller.xs */
54
55MODULE = arguments              PACKAGE = arguments
56PROTOTYPES: DISABLE
57
58void
59upcontext(uplevel)
60I32 uplevel
61  PPCODE:
62    PERL_CONTEXT *cx = _upcontext(aTHX_ uplevel);
63    if (!cx) {
64        ST(0) = sv_2mortal(newRV( (SV *) NULL) );
65    } else {
66        if (cx->cx_type != CXt_SUB)
67            croak("cx_type is %d not CXt_SUB\n", cx->cx_type);
68        if (!cx->blk_sub.cv)
69            croak("Context has no CV!\n");
70        ST(0) = (SV*) newRV_inc( (SV*) cx->blk_sub.cv );
71    }
72    XSRETURN(1);
73
Note: See TracBrowser for help on using the browser.