1 | #define PERL_NO_GET_CONTEXT |
1 | #define PERL_NO_GET_CONTEXT |
2 | |
2 | |
3 | #include "EXTERN.h" |
3 | #include "EXTERN.h" |
4 | #include "perl.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
5 | #include "XSUB.h" |
|
|
6 | |
|
|
7 | #include "patchlevel.h" |
|
|
8 | |
|
|
9 | #define PERL_VERSION_ATLEAST(a,b,c) \ |
|
|
10 | (PERL_REVISION > (a) \ |
|
|
11 | || (PERL_REVISION == (a) \ |
|
|
12 | && (PERL_VERSION > (b) \ |
|
|
13 | || (PERL_VERSION == (b) && PERL_SUBVERSION >= (c))))) |
|
|
14 | |
|
|
15 | /* apparently < 5.8.8 */ |
|
|
16 | #ifndef SvSTASH_set |
|
|
17 | # define SvSTASH_set(x,a) SvSTASH(x) = (a) |
|
|
18 | #endif |
|
|
19 | |
|
|
20 | #ifndef PERL_MAGIC_ext |
|
|
21 | # define PERL_MAGIC_ext '~' |
|
|
22 | #endif |
6 | |
23 | |
7 | static HV *guard_stash; |
24 | static HV *guard_stash; |
8 | |
25 | |
9 | static SV * |
26 | static SV * |
10 | guard_get_cv (pTHX_ SV *cb_sv) |
27 | guard_get_cv (pTHX_ SV *cb_sv) |
… | |
… | |
78 | |
95 | |
79 | MODULE = Guard PACKAGE = Guard |
96 | MODULE = Guard PACKAGE = Guard |
80 | |
97 | |
81 | BOOT: |
98 | BOOT: |
82 | guard_stash = gv_stashpv ("Guard", 1); |
99 | guard_stash = gv_stashpv ("Guard", 1); |
|
|
100 | CvNODEBUG_on (get_cv ("Guard::scope_guard", 0)); /* otherwise calling scope can be the debugger */ |
83 | |
101 | |
84 | void |
102 | void |
85 | scope_guard (SV *block) |
103 | scope_guard (SV *block) |
86 | PROTOTYPE: & |
104 | PROTOTYPE: & |
87 | CODE: |
105 | CODE: |
… | |
… | |
92 | SV * |
110 | SV * |
93 | guard (SV *block) |
111 | guard (SV *block) |
94 | PROTOTYPE: & |
112 | PROTOTYPE: & |
95 | CODE: |
113 | CODE: |
96 | { |
114 | { |
97 | SV *cv = guard_get_cv (aTHX_ block); |
115 | SV *cv = guard_get_cv (aTHX_ block); |
98 | SV *guard = NEWSV (0, 0); |
116 | SV *guard = NEWSV (0, 0); |
99 | SvUPGRADE (guard, SVt_PVMG); |
117 | SvUPGRADE (guard, SVt_PVMG); |
100 | sv_magicext (guard, cv, PERL_MAGIC_ext, &guard_vtbl, 0, 0); |
118 | sv_magicext (guard, cv, PERL_MAGIC_ext, &guard_vtbl, 0, 0); |
101 | RETVAL = newRV_noinc (guard); |
119 | RETVAL = newRV_noinc (guard); |
102 | SvOBJECT_on (guard); |
120 | SvOBJECT_on (guard); |
|
|
121 | #if !PERL_VERSION_ATLEAST(5,18,0) |
103 | ++PL_sv_objcount; |
122 | ++PL_sv_objcount; |
|
|
123 | #endif |
104 | SvSTASH_set (guard, (HV*)SvREFCNT_inc ((SV *)guard_stash)); |
124 | SvSTASH_set (guard, (HV*)SvREFCNT_inc ((SV *)guard_stash)); |
105 | } |
125 | } |
106 | OUTPUT: |
126 | OUTPUT: |
107 | RETVAL |
127 | RETVAL |
108 | |
128 | |