| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| #include "BUtils.h" |
| 5 |
|
| |
| 6 |
|
| #ifndef Newx |
| 7 |
|
| # define Newx(v,n,t) New(0,v,n,t) |
| 8 |
|
| #endif /* !Newx */ |
| 9 |
|
| |
| 10 |
|
| #include "hook_op_check_entersubforcv.h" |
| 11 |
|
| |
| 12 |
|
| typedef struct userdata_St { |
| 13 |
|
| CV *cv; |
| 14 |
|
| hook_op_check_entersubforcv_cb cb; |
| 15 |
|
| void *ud; |
| 16 |
|
| } userdata_t; |
| 17 |
|
| |
| 18 |
|
| STATIC OP * |
| 19 |
2967 |
| entersub_cb (pTHX_ OP *op, void *user_data) { |
| 20 |
|
| OP *kid, *last; |
| 21 |
|
| GV *gv; |
| 22 |
|
| CV *cv; |
| 23 |
2967 |
| userdata_t *ud = (userdata_t *)user_data; |
| 24 |
|
| |
| 25 |
2967 |
▶ | if (op->op_type != OP_ENTERSUB) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_type != OP_ENTERSUB) { |
42 |
2925 |
2967 |
|
| 26 |
42 |
| return op; |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
|
| /* we currently ignore anything caused by &foo because \&foo also creates |
| 30 |
|
| * an entersub op before ck_doref removes it again */ |
| 31 |
2925 |
▶ | if (op->op_private & OPpENTERSUB_AMPER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_private & OPpENTERSUB_AMPER) { |
1073 |
1852 |
2925 |
|
| 32 |
1073 |
| return op; |
| 33 |
|
| } |
| 34 |
|
| |
| 35 |
1852 |
▶ | if (op->op_type == OP_NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_type == OP_NULL) { |
0 |
1852 |
1852 |
|
| 36 |
0 |
| return op; |
| 37 |
|
| } |
| 38 |
|
| |
| 39 |
1852 |
| kid = cUNOPx (op)->op_first; |
| 40 |
|
| |
| 41 |
1852 |
▶ | if (!kid) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!kid) { |
0 |
1852 |
1852 |
|
| 42 |
0 |
| return op; |
| 43 |
|
| } |
| 44 |
|
| |
| 45 |
|
| /* pushmark for method call */ |
| 46 |
1852 |
▶ | if (kid->op_type != OP_NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kid->op_type != OP_NULL) { |
957 |
895 |
1852 |
|
| 47 |
957 |
| return op; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
895 |
| last = cLISTOPx (kid)->op_last; |
| 51 |
|
| |
| 52 |
|
| /* not what we expected */ |
| 53 |
895 |
▶ | if (last->op_type != OP_NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last->op_type != OP_NULL) { |
0 |
895 |
895 |
|
| 54 |
0 |
| return op; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
895 |
| kid = cUNOPx (last)->op_first; |
| 58 |
|
| |
| 59 |
|
| /* not a GV */ |
| 60 |
895 |
▶ | if (kid->op_type != OP_GV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kid->op_type != OP_GV) { |
26 |
869 |
895 |
|
| 61 |
26 |
| return op; |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
869 |
| gv = cGVOPx_gv (kid); |
| 65 |
876 |
▶ | cv = SvTYPE (gv) == SVt_PVGV ? GvCV (gv) : |
Branch: 1 branch
| Branch | True | False | Total |
| cv = SvTYPE (gv) == SVt_PVGV ? GvCV (gv) : |
862 |
7 |
869 |
|
| 66 |
7 |
▶ | SvROK ((SV*)gv) ? (CV*) SvRV ((SV*)gv) : (CV*)NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SvROK ((SV*)gv) ? (CV*) SvRV ((SV*)gv) : (CV*)NULL; |
7 |
0 |
7 |
|
| 67 |
|
| |
| 68 |
869 |
▶ | if (ud->cv == cv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ud->cv == cv) { |
11 |
858 |
869 |
|
| 69 |
11 |
| op = ud->cb (aTHX_ op, cv, ud->ud); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
869 |
| return op; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| hook_op_check_id |
| 76 |
5 |
| hook_op_check_entersubforcv (CV *cv, hook_op_check_entersubforcv_cb cb, void *user_data) { |
| 77 |
|
| userdata_t *ud; |
| 78 |
|
| |
| 79 |
5 |
| Newx (ud, 1, userdata_t); |
| 80 |
5 |
| ud->cv = cv; |
| 81 |
5 |
| ud->cb = cb; |
| 82 |
5 |
| ud->ud = user_data; |
| 83 |
|
| |
| 84 |
5 |
| return hook_op_check (OP_ENTERSUB, entersub_cb, ud); |
| 85 |
|
| } |
| 86 |
|
| |
| 87 |
|
| void * |
| 88 |
3 |
| hook_op_check_entersubforcv_remove (hook_op_check_id id) { |
| 89 |
|
| void *ret; |
| 90 |
3 |
| userdata_t *ud = hook_op_check_remove (OP_ENTERSUB, id); |
| 91 |
|
| |
| 92 |
3 |
▶ | if (!ud) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ud) { |
0 |
3 |
3 |
|
| 93 |
0 |
| return NULL; |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
3 |
| ret = ud->ud; |
| 97 |
|
| |
| 98 |
3 |
| Safefree (ud); |
| 99 |
|
| |
| 100 |
3 |
| return ret; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
|
| STATIC OP * |
| 104 |
11 |
| perl_cb (pTHX_ OP *op, CV *cv, void *ud) { |
| 105 |
|
| SV *opsv; |
| 106 |
11 |
| dSP; |
| 107 |
|
| |
| 108 |
11 |
| ENTER; |
| 109 |
11 |
| SAVETMPS; |
| 110 |
|
| |
| 111 |
11 |
| opsv = sv_newmortal (); |
| 112 |
11 |
| sv_setiv (newSVrv (opsv, BUtils_cc_opclassname (aTHX_ op)), PTR2IV (op)); |
| 113 |
|
| |
| 114 |
11 |
▶ | PUSHMARK (SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK (SP); |
0 |
11 |
11 |
|
| 115 |
11 |
▶ | EXTEND (SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND (SP, 2); |
0 |
11 |
11 |
|
| 116 |
11 |
| PUSHs (sv_2mortal (newRV ((SV *)cv))); |
| 117 |
11 |
| PUSHs (opsv); |
| 118 |
11 |
| PUTBACK; |
| 119 |
|
| |
| 120 |
11 |
| call_sv ((SV *)ud, G_VOID|G_DISCARD); |
| 121 |
|
| |
| 122 |
11 |
| SPAGAIN; |
| 123 |
|
| |
| 124 |
11 |
| PUTBACK; |
| 125 |
11 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
11 |
0 |
11 |
|
| 126 |
11 |
| LEAVE; |
| 127 |
|
| |
| 128 |
11 |
| return op; |
| 129 |
|
| } |
| 130 |
|
| |
| 131 |
|
| MODULE = B::Hooks::OP::Check::EntersubForCV PACKAGE = B::Hooks::OP::Check::EntersubForCV |
| 132 |
|
| |
| 133 |
|
| PROTOTYPES: DISABLE |
| 134 |
|
| |
| 135 |
|
| UV |
| 136 |
|
| register (cv, cb) |
| 137 |
|
| CV *cv |
| 138 |
|
| SV *cb |
| 139 |
|
| CODE: |
| 140 |
5 |
| RETVAL = (UV)hook_op_check_entersubforcv (cv, perl_cb, newSVsv (cb)); |
| 141 |
|
| OUTPUT: |
| 142 |
|
| RETVAL |
| 143 |
|
| |
| 144 |
|
| void |
| 145 |
|
| unregister (id) |
| 146 |
|
| UV id |
| 147 |
|
| PREINIT: |
| 148 |
|
| SV *ud; |
| 149 |
|
| CODE: |
| 150 |
3 |
| ud = hook_op_check_entersubforcv_remove ((hook_op_check_id)id); |
| 151 |
|
| |
| 152 |
3 |
▶ | if (ud) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ud) { |
3 |
0 |
3 |
|
| 153 |
3 |
| SvREFCNT_dec (ud); |
| 154 |
|
| } |