CallAsOp.xs

filter: Statementstmt 98.3% 60 / 61 Branchbran 44.4% 8 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.0% 68 / 79 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/B/Hooks/XSUB/CallAsOp/Install/Files.pm ↑ Summary blib/lib/B/Hooks/XSUB/CallAsOp.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include "hook_xsub_callasop.h"
8  
9  
10 #define MY_CXT_KEY "B::Hooks::XSUB::CallAsOp::_guts" XS_VERSION
11  
12 typedef struct {
13 OP fakeop;
14 UNOP trampoline;
15  
16 OP *saved_op;
17  
18 SV **saved_args;
19 I32 saved_items;
20  
21 /* the actual function to call as if it were pp_addr */
22  
23 TRAMPOLINE_HOOK((*hook));
24 } my_cxt_t;
25  
26 START_MY_CXT
27  
28 2 static void setup_trampoline_cb (pTHX_ void *ptr) {
29 dMY_CXT;
30 2 int flags = GIMME_V;
31  
32 assert(MY_CXT.hook);
33  
34 2 Zero(&MY_CXT.trampoline, 1, UNOP);
35  
36 /* this is modeled after call_sv and friends
37 *
38 * we recreate MY_CXT.trampoline to be an entersub-like opcode, fired at
39 * the same stack depth etc as the entersub that invoked the XSUB
40 *
41 * the difference is that the op_ppaddr points at the hook that was saved
42 * by the TRAMPOLINE macro */
43 2 ▶ if (!(flags & G_NOARGS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(flags & G_NOARGS)) 2 0 2
44 2 MY_CXT.trampoline.op_flags |= OPf_STACKED;
45  
46 2 ▶ MY_CXT.trampoline.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
Branch: 1 branch
BranchTrueFalseTotal
MY_CXT.trampoline.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID : 0 2 2
47 0 ▶ (flags & G_ARRAY) ? OPf_WANT_LIST : OPf_WANT_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
(flags & G_ARRAY) ? OPf_WANT_LIST : OPf_WANT_SCALAR); 0 0 0
48  
49 2 MY_CXT.trampoline.op_type = OP_ENTERSUB; /* we pretend it's an entersub, it's called in the same context */
50 2 MY_CXT.trampoline.op_next = PL_op->op_next; /* to return from this op we go to what entersub should have returned to */
51 2 MY_CXT.trampoline.op_ppaddr = MY_CXT.hook; /* the body of the op is the user's hook */
52  
53 2 MY_CXT.hook = NULL;
54  
55 /* ENTERSUB will return PL_op->op_next causing execution of the trampoline, fakeop is there only for its op_next */
56 2 MY_CXT.fakeop.op_next = (OP *)&MY_CXT.trampoline;
57 2 PL_op = &MY_CXT.fakeop;
58  
59 assert(PL_op->op_next);
60  
61 /* could be NULL in DESTROY */
62 /* assert(PL_op->op_next->op_next); */
63 2 }
64  
65 2 void b_hooks_xsub_callasop_setup_trampoline (pTHX_ b_hooks_xsub_callasop_hook_t hook) {
66 dMY_CXT;
67  
68  
69 assert(MY_CXT.hook == NULL);
70  
71 /* save the hook somewhere */
72 2 MY_CXT.hook = hook;
73  
74 /* SAVEDESTRUCTOR_X will fire inside LEAVE in the ENTERSUB for XSUBs */
75 2 SAVEDESTRUCTOR_X(setup_trampoline_cb, NULL);
76  
77  
78 2 ▶ SAVESTACK_POS(); /* Enforce some insanity in scalar context... we want to
Branch: 1 branch
BranchTrueFalseTotal
SAVESTACK_POS(); /* Enforce some insanity in scalar context... we want to 0 2 2
79 let the our fake op hook manipulate the stack and it's
80 their responsibility to respect GIMME_V. this disposes
81 of the extra null SV injected in scalar context when no
82 value was left on the stack */
83 2 }
84  
85 2 void b_hooks_xsub_callasop_trampoline_save_args (pTHX_ SV **start, I32 items) {
86 dMY_CXT;
87 2 dSP;
88  
89 assert(MY_CXT.saved_args == NULL);
90 assert(MY_CXT.saved_items == 0);
91  
92 2 ▶ if ( items != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( items != 0 ) { 2 0 2
93 2 MY_CXT.saved_items = items;
94 2 Newx(MY_CXT.saved_args, items, SV *);
95  
96 2 Copy(start, MY_CXT.saved_args, items, SV *);
97  
98 2 SP = start - 1;
99 2 PUTBACK;
100 }
101 2 }
102  
103 2 void b_hooks_xsub_callasop_trampoline_restore_args (pTHX) {
104 2 dSP;
105 dMY_CXT;
106 I32 i;
107  
108 2 ▶ if ( MY_CXT.saved_items > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( MY_CXT.saved_items > 0 ) { 2 0 2
109 assert(MY_CXT.saved_args != NULL);
110  
111 2 ▶ EXTEND(SP, MY_CXT.saved_items);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, MY_CXT.saved_items); 2 0 2
EXTEND(SP, MY_CXT.saved_items); 0 2 2
112  
113 2 Copy(MY_CXT.saved_args, SP + 1, MY_CXT.saved_items, SV *);
114  
115 2 SP += MY_CXT.saved_items;
116  
117 2 PUTBACK;
118  
119 2 Safefree(MY_CXT.saved_args);
120 2 MY_CXT.saved_args = NULL;
121 2 MY_CXT.saved_items = 0;
122 } else {
123 assert(MY_CXT.saved_args == NULL);
124 }
125 2 }
126  
127 /* stashes PL_op before the trampoline hack. this allows cont_reset,
128 * cont_invoke and cont_shift to have a proper value for PL_op */
129 2 void b_hooks_xsub_callasop_trampoline_save_op (pTHX) {
130 dMY_CXT;
131  
132 assert(MY_CXT.saved_op == NULL);
133  
134 assert(PL_op);
135 assert(PL_op->op_next);
136  
137 2 MY_CXT.saved_op = PL_op;
138 2 }
139  
140  
141 /* restore PL_op's state to what it was before the trampoline */
142 2 void b_hooks_xsub_callasop_trampoline_restore_op (pTHX) {
143 dMY_CXT;
144  
145 assert(MY_CXT.saved_op != NULL);
146 assert(MY_CXT.saved_op->op_next);
147  
148 2 PL_op = MY_CXT.saved_op;
149  
150 2 MY_CXT.saved_op = NULL;
151 2 }
152  
153  
154  
155  
156 2 static TRAMPOLINE_HOOK(test_hook)
157 {
158 2 dSP;
159 2 dMARK;
160  
161 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
162  
163 2 TRAMPOLINE_RESTORE_OP;
164 2 TRAMPOLINE_RESTORE_ARGS; /* should be magic */
165  
166 2 PUTBACK;
167  
168 2 return PL_ppaddr[OP_ENTERSUB](aTHX);
169 }
170  
171 MODULE = B::Hooks::XSUB::CallAsOp PACKAGE = B::Hooks::XSUB::CallAsOp
172 PROTOTYPES: DISABLE
173  
174 BOOT:
175 {
176 MY_CXT_INIT;
177 1 MY_CXT.hook = NULL;
178  
179 1 MY_CXT.saved_args = NULL;
180 1 MY_CXT.saved_items = 0;
181  
182 1 MY_CXT.saved_op = NULL;
183 }
184  
185 void __test (...)
186 PPCODE:
187 2 TRAMPOLINE_SAVE_OP;
188 2 TRAMPOLINE_SAVE_ARGS;
189 assert(SP == &ST(-1));
190 2 TRAMPOLINE(test_hook);
191  
« blib/arch/B/Hooks/XSUB/CallAsOp/Install/Files.pm ↑ Summary blib/lib/B/Hooks/XSUB/CallAsOp.pm »