| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| |
| 3 |
|
| #include "EXTERN.h" |
| 4 |
|
| #include "perl.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 |
| 23 |
|
| |
| 24 |
|
| static HV *guard_stash; |
| 25 |
|
| |
| 26 |
|
| static SV * |
| 27 |
9 |
| guard_get_cv (pTHX_ SV *cb_sv) |
| 28 |
|
| { |
| 29 |
|
| HV *st; |
| 30 |
|
| GV *gvp; |
| 31 |
9 |
| CV *cv = sv_2cv (cb_sv, &st, &gvp, 0); |
| 32 |
|
| |
| 33 |
9 |
▶ | if (!cv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cv) |
0 |
9 |
9 |
|
| 34 |
0 |
| croak ("expected a CODE reference for guard"); |
| 35 |
|
| |
| 36 |
9 |
| return (SV *)cv; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
|
| static void |
| 40 |
8 |
| exec_guard_cb (pTHX_ SV *cb) |
| 41 |
|
| { |
| 42 |
8 |
| dSP; |
| 43 |
8 |
▶ | SV *saveerr = SvOK (ERRSV) ? sv_mortalcopy (ERRSV) : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| SV *saveerr = SvOK (ERRSV) ? sv_mortalcopy (ERRSV) : 0; |
8 |
0 |
8 |
| SV *saveerr = SvOK (ERRSV) ? sv_mortalcopy (ERRSV) : 0; |
8 |
0 |
8 |
| SV *saveerr = SvOK (ERRSV) ? sv_mortalcopy (ERRSV) : 0; |
8 |
0 |
8 |
|
| 44 |
8 |
| SV *savedie = PL_diehook; |
| 45 |
|
| |
| 46 |
8 |
| PL_diehook = 0; |
| 47 |
|
| |
| 48 |
8 |
| PUSHSTACKi (PERLSI_DESTROY); |
| 49 |
|
| |
| 50 |
8 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
8 |
8 |
|
| 51 |
8 |
| PUTBACK; |
| 52 |
8 |
| call_sv (cb, G_VOID | G_DISCARD | G_EVAL); |
| 53 |
|
| |
| 54 |
8 |
▶ | if (SvTRUE (ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE (ERRSV)) |
8 |
0 |
8 |
| if (SvTRUE (ERRSV)) |
3 |
5 |
8 |
|
| 55 |
|
| { |
| 56 |
3 |
| SPAGAIN; |
| 57 |
|
| |
| 58 |
3 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
3 |
3 |
|
| 59 |
3 |
| PUTBACK; |
| 60 |
3 |
| call_sv (get_sv ("Guard::DIED", 1), G_VOID | G_DISCARD | G_EVAL | G_KEEPERR); |
| 61 |
|
| |
| 62 |
3 |
▶ | sv_setpvn (ERRSV, "", 0); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setpvn (ERRSV, "", 0); |
3 |
0 |
3 |
|
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
8 |
▶ | if (saveerr) |
Branch: 1 branch
| Branch | True | False | Total |
| if (saveerr) |
8 |
0 |
8 |
|
| 66 |
8 |
▶ | sv_setsv (ERRSV, saveerr); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv (ERRSV, saveerr); |
8 |
0 |
8 |
|
| 67 |
|
| |
| 68 |
|
| { |
| 69 |
8 |
| SV *oldhook = PL_diehook; |
| 70 |
8 |
| PL_diehook = savedie; |
| 71 |
8 |
| SvREFCNT_dec (oldhook); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
8 |
| POPSTACK; |
| 75 |
8 |
| } |
| 76 |
|
| |
| 77 |
|
| static void |
| 78 |
6 |
| scope_guard_cb (pTHX_ void *cv) |
| 79 |
|
| { |
| 80 |
6 |
| exec_guard_cb (aTHX_ sv_2mortal ((SV *)cv)); |
| 81 |
6 |
| } |
| 82 |
|
| |
| 83 |
|
| static int |
| 84 |
2 |
| guard_free (pTHX_ SV *cv, MAGIC *mg) |
| 85 |
|
| { |
| 86 |
2 |
| exec_guard_cb (aTHX_ mg->mg_obj); |
| 87 |
|
| |
| 88 |
2 |
| return 0; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| static MGVTBL guard_vtbl = { |
| 92 |
|
| 0, 0, 0, 0, |
| 93 |
|
| guard_free |
| 94 |
|
| }; |
| 95 |
|
| |
| 96 |
|
| MODULE = Guard PACKAGE = Guard |
| 97 |
|
| |
| 98 |
|
| BOOT: |
| 99 |
4 |
| guard_stash = gv_stashpv ("Guard", 1); |
| 100 |
4 |
| CvNODEBUG_on (get_cv ("Guard::scope_guard", 0)); /* otherwise calling scope can be the debugger */ |
| 101 |
|
| |
| 102 |
|
| void |
| 103 |
|
| scope_guard (SV *block) |
| 104 |
|
| PROTOTYPE: & |
| 105 |
|
| CODE: |
| 106 |
6 |
| LEAVE; /* unfortunately, perl sandwiches XS calls into ENTER/LEAVE */ |
| 107 |
6 |
| SAVEDESTRUCTOR_X (scope_guard_cb, (void *)SvREFCNT_inc (guard_get_cv (aTHX_ block))); |
| 108 |
6 |
| ENTER; /* unfortunately, perl sandwiches XS calls into ENTER/LEAVE */ |
| 109 |
|
| |
| 110 |
|
| SV * |
| 111 |
|
| guard (SV *block) |
| 112 |
|
| PROTOTYPE: & |
| 113 |
|
| CODE: |
| 114 |
|
| { |
| 115 |
3 |
| SV *cv = guard_get_cv (aTHX_ block); |
| 116 |
3 |
| SV *guard = NEWSV (0, 0); |
| 117 |
3 |
▶ | SvUPGRADE (guard, SVt_PVMG); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE (guard, SVt_PVMG); |
3 |
0 |
3 |
|
| 118 |
3 |
| sv_magicext (guard, cv, PERL_MAGIC_ext, &guard_vtbl, 0, 0); |
| 119 |
3 |
| RETVAL = newRV_noinc (guard); |
| 120 |
3 |
| SvOBJECT_on (guard); |
| 121 |
|
| #if !PERL_VERSION_ATLEAST(5,18,0) |
| 122 |
|
| ++PL_sv_objcount; |
| 123 |
|
| #endif |
| 124 |
3 |
| SvSTASH_set (guard, (HV*)SvREFCNT_inc ((SV *)guard_stash)); |
| 125 |
|
| } |
| 126 |
|
| OUTPUT: |
| 127 |
|
| RETVAL |
| 128 |
|
| |
| 129 |
|
| void |
| 130 |
|
| cancel (SV *guard) |
| 131 |
|
| PROTOTYPE: $ |
| 132 |
|
| CODE: |
| 133 |
|
| { |
| 134 |
|
| MAGIC *mg; |
| 135 |
1 |
▶ | if (!SvROK (guard) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK (guard) |
1 |
0 |
1 |
|
| 136 |
1 |
▶ | || !(mg = mg_find (SvRV (guard), PERL_MAGIC_ext)) |
Branch: 1 branch
| Branch | True | False | Total |
| || !(mg = mg_find (SvRV (guard), PERL_MAGIC_ext)) |
1 |
0 |
1 |
|
| 137 |
1 |
▶ | || mg->mg_virtual != &guard_vtbl) |
Branch: 1 branch
| Branch | True | False | Total |
| || mg->mg_virtual != &guard_vtbl) |
0 |
1 |
1 |
|
| 138 |
0 |
| croak ("Guard::cancel called on a non-guard object"); |
| 139 |
|
| |
| 140 |
1 |
| SvREFCNT_dec (mg->mg_obj); |
| 141 |
1 |
| mg->mg_obj = 0; |
| 142 |
1 |
| mg->mg_virtual = 0; |
| 143 |
|
| } |