xs/closure.c

filter: Statementstmt 93.3% 182 / 195 Branchbran 64.7% 57 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.4% 239 / 283 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/custom.c ↑ Summary xs/names.c »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5 #include "ffi_platypus.h"
6 #include "ffi_platypus_guts.h"
7 #include "perl_math_int64.h"
8  
9 void
10 89 ffi_pl_closure_add_data(SV *closure, ffi_pl_closure *closure_data)
11 {
12 89 dSP;
13 89 ENTER;
14 89 SAVETMPS;
15 89 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 89 89
16 89 ▶ XPUSHs(closure);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(closure); 0 89 89
17 89 ▶ XPUSHs(sv_2mortal(newSViv(PTR2IV(closure_data))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(PTR2IV(closure_data)))); 0 89 89
18 89 ▶ XPUSHs(sv_2mortal(newSViv(PTR2IV(closure_data->type))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(PTR2IV(closure_data->type)))); 0 89 89
19 89 PUTBACK;
20 89 call_pv("FFI::Platypus::Closure::add_data", G_DISCARD);
21 89 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 89 0 89
22 89 LEAVE;
23 89 }
24  
25 ffi_pl_closure *
26 90 ffi_pl_closure_get_data(SV *closure, ffi_pl_type *type)
27 {
28 90 dSP;
29 int count;
30 ffi_pl_closure *ret;
31  
32 90 ENTER;
33 90 SAVETMPS;
34 90 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 90 90
35 90 ▶ XPUSHs(closure);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(closure); 0 90 90
36 90 ▶ XPUSHs(sv_2mortal(newSViv(PTR2IV(type))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSViv(PTR2IV(type)))); 0 90 90
37 90 PUTBACK;
38 90 count = call_pv("FFI::Platypus::Closure::get_data", G_SCALAR);
39 90 SPAGAIN;
40  
41 90 ▶ if (count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (count != 1) 0 90 90
42 0 ret = NULL;
43 else
44 90 ret = INT2PTR(void*, POPi);
45  
46 90 PUTBACK;
47 90 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 90 0 90
48 90 LEAVE;
49  
50 90 return ret;
51 }
52  
53 void
54 103 ffi_pl_closure_call(ffi_cif *ffi_cif, void *result, void **arguments, void *user)
55 {
56 103 dSP;
57  
58 103 ffi_pl_closure *closure = (ffi_pl_closure*) user;
59 103 ffi_pl_type_extra_closure *extra = &closure->type->extra[0].closure;
60 103 int flags = extra->flags;
61 int i;
62 int count;
63 SV *sv,*ref;
64  
65 103 ▶ if(!(flags & G_NOARGS))
Branch: 1 branch
BranchTrueFalseTotal
if(!(flags & G_NOARGS)) 98 5 103
66 {
67 98 ENTER;
68 98 SAVETMPS;
69 }
70  
71 103 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 103 103
72  
73 103 ▶ if(!(flags & G_NOARGS))
Branch: 1 branch
BranchTrueFalseTotal
if(!(flags & G_NOARGS)) 98 5 103
74 {
75 205 ▶ for(i=0; i< ffi_cif->nargs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for(i=0; i< ffi_cif->nargs; i++) 107 98 205
76 {
77 107 switch(extra->argument_types[i]->type_code)
78 {
79 0 case FFI_PL_TYPE_VOID:
80 0 break;
81 6 case FFI_PL_TYPE_SINT8:
82 6 sv = sv_newmortal();
83 6 sv_setiv(sv, *((int8_t*)arguments[i]));
84 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
85 6 break;
86 6 case FFI_PL_TYPE_SINT16:
87 6 sv = sv_newmortal();
88 6 sv_setiv(sv, *((int16_t*)arguments[i]));
89 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
90 6 break;
91 10 case FFI_PL_TYPE_SINT32:
92 10 sv = sv_newmortal();
93 10 sv_setiv(sv, *((int32_t*)arguments[i]));
94 10 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 10 10
95 10 break;
96 7 case FFI_PL_TYPE_SINT64:
97 7 sv = sv_newmortal();
98 7 sv_seti64(sv, *((int64_t*)arguments[i]));
99 7 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 7 7
100 7 break;
101 6 case FFI_PL_TYPE_UINT8:
102 6 sv = sv_newmortal();
103 6 sv_setuv(sv, *((uint8_t*)arguments[i]));
104 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
105 6 break;
106 6 case FFI_PL_TYPE_UINT16:
107 6 sv = sv_newmortal();
108 6 sv_setuv(sv, *((uint16_t*)arguments[i]));
109 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
110 6 break;
111 6 case FFI_PL_TYPE_UINT32:
112 6 sv = sv_newmortal();
113 6 sv_setuv(sv, *((uint32_t*)arguments[i]));
114 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
115 6 break;
116 7 case FFI_PL_TYPE_UINT64:
117 7 sv = sv_newmortal();
118 7 sv_setu64(sv, *((uint64_t*)arguments[i]));
119 7 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 7 7
120 7 break;
121 6 case FFI_PL_TYPE_FLOAT:
122 6 sv = sv_newmortal();
123 6 sv_setnv(sv, *((float*)arguments[i]));
124 6 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 6 6
125 6 break;
126 7 case FFI_PL_TYPE_DOUBLE:
127 7 sv = sv_newmortal();
128 7 sv_setnv(sv, *((double*)arguments[i]));
129 7 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 7 7
130 7 break;
131 7 case FFI_PL_TYPE_OPAQUE:
132 7 sv = sv_newmortal();
133 7 ▶ if( *((void**)arguments[i]) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if( *((void**)arguments[i]) != NULL) 3 4 7
134 3 sv_setiv(sv, PTR2IV( *((void**)arguments[i]) ));
135 7 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 7 7
136 7 break;
137 27 case FFI_PL_TYPE_STRING:
138 27 sv = sv_newmortal();
139 27 ▶ if( *((char**)arguments[i]) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if( *((char**)arguments[i]) != NULL) 24 3 27
140 {
141 24 sv_setpv(sv, *((char**)arguments[i]));
142 }
143 27 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 27 27
144 27 break;
145 5 case FFI_PL_TYPE_RECORD:
146 5 sv = sv_newmortal();
147 5 ▶ if( *((char**)arguments[i]) != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if( *((char**)arguments[i]) != NULL) 4 1 5
148 {
149 4 sv_setpvn(sv, *((char**)arguments[i]), extra->argument_types[i]->extra[0].record.size);
150 4 ▶ if(extra->argument_types[i]->extra[0].record.class != NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(extra->argument_types[i]->extra[0].record.class != NULL) 1 3 4
151 {
152 1 ref = newRV_inc(sv);
153 1 sv_bless(ref, gv_stashpv(extra->argument_types[i]->extra[0].record.class, GV_ADD));
154 1 SvREADONLY_on(sv);
155 1 sv = ref;
156 }
157 else
158 {
159 3 SvREADONLY_on(sv);
160 }
161 }
162 5 ▶ XPUSHs(sv);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv); 0 5 5
163 5 break;
164 1 case FFI_PL_TYPE_RECORD_VALUE:
165 1 sv = sv_newmortal();
166 1 sv_setpvn(sv, (char*)arguments[i], extra->argument_types[i]->extra[0].record.size);
167 1 ref = newRV_inc(sv);
168 1 sv_bless(ref, gv_stashpv(extra->argument_types[i]->extra[0].record.class, GV_ADD));
169 1 SvREADONLY_on(sv);
170 1 ▶ XPUSHs(ref);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ref); 0 1 1
171 1 break;
172 0 default:
173 0 warn("bad type");
174 0 break;
175 }
176 }
177 98 PUTBACK;
178 }
179  
180 103 count = call_sv(closure->coderef, flags | G_EVAL);
181  
182 103 ▶ if(SvTRUE(ERRSV))
Branch: 2 branches
BranchTrueFalseTotal
if(SvTRUE(ERRSV)) 103 0 103
if(SvTRUE(ERRSV)) 1 102 103
183 {
184 #ifdef warn_sv
185 1 ▶ warn_sv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
warn_sv(ERRSV); 1 0 1
186 #else
187 warn("%s", SvPV_nolen(ERRSV));
188 #endif
189 }
190  
191 103 ▶ if(!(flags & G_DISCARD))
Branch: 1 branch
BranchTrueFalseTotal
if(!(flags & G_DISCARD)) 73 30 103
192 {
193 73 SPAGAIN;
194  
195 73 ▶ if(count != 1)
Branch: 1 branch
BranchTrueFalseTotal
if(count != 1) 0 73 73
196 0 sv = &PL_sv_undef;
197 else
198 73 sv = POPs;
199  
200 73 switch(extra->return_type->type_code)
201 {
202 0 case FFI_PL_TYPE_VOID:
203 0 break;
204 6 case FFI_PL_TYPE_UINT8:
205 #if defined FFI_PL_PROBE_BIGENDIAN
206 ((uint8_t*)result)[3] = SvUV(sv);
207 #elif defined FFI_PL_PROBE_BIGENDIAN64
208 ((uint8_t*)result)[7] = SvUV(sv);
209 #else
210 6 *((uint8_t*)result) = SvUV(sv);
211 #endif
212 6 break;
213 6 case FFI_PL_TYPE_SINT8:
214 #if defined FFI_PL_PROBE_BIGENDIAN
215 ((int8_t*)result)[3] = SvIV(sv);
216 #elif defined FFI_PL_PROBE_BIGENDIAN64
217 ((int8_t*)result)[7] = SvIV(sv);
218 #else
219 6 *((int8_t*)result) = SvIV(sv);
220 #endif
221 6 break;
222 6 case FFI_PL_TYPE_UINT16:
223 #if defined FFI_PL_PROBE_BIGENDIAN
224 ((uint16_t*)result)[1] = SvUV(sv);
225 #elif defined FFI_PL_PROBE_BIGENDIAN64
226 ((uint16_t*)result)[3] = SvUV(sv);
227 #else
228 6 *((uint16_t*)result) = SvUV(sv);
229 #endif
230 6 break;
231 6 case FFI_PL_TYPE_SINT16:
232 #if defined FFI_PL_PROBE_BIGENDIAN
233 ((int16_t*)result)[1] = SvIV(sv);
234 #elif defined FFI_PL_PROBE_BIGENDIAN64
235 ((int16_t*)result)[3] = SvIV(sv);
236 #else
237 6 *((int16_t*)result) = SvIV(sv);
238 #endif
239 6 break;
240 6 case FFI_PL_TYPE_UINT32:
241 #if defined FFI_PL_PROBE_BIGENDIAN64
242 ((uint32_t*)result)[1] = SvUV(sv);
243 #else
244 6 *((uint32_t*)result) = SvUV(sv);
245 #endif
246 6 break;
247 9 case FFI_PL_TYPE_SINT32:
248 #if defined FFI_PL_PROBE_BIGENDIAN64
249 ((int32_t*)result)[1] = SvIV(sv);
250 #else
251 9 *((int32_t*)result) = SvIV(sv);
252 #endif
253 9 break;
254 6 case FFI_PL_TYPE_UINT64:
255 6 *((uint64_t*)result) = SvU64(sv);
256 6 break;
257 6 case FFI_PL_TYPE_SINT64:
258 6 *((int64_t*)result) = SvI64(sv);
259 6 break;
260 6 case FFI_PL_TYPE_FLOAT:
261 6 *((float*)result) = SvNV(sv);
262 6 break;
263 6 case FFI_PL_TYPE_DOUBLE:
264 6 *((double*)result) = SvNV(sv);
265 6 break;
266 7 case FFI_PL_TYPE_OPAQUE:
267 7 ▶ *((void**)result) = SvOK(sv) ? INT2PTR(void*, SvIV(sv)) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
*((void**)result) = SvOK(sv) ? INT2PTR(void*, SvIV(sv)) : NULL; 3 4 7
268 7 break;
269 3 case FFI_PL_TYPE_RECORD_VALUE:
270 3 ▶ if(sv_isobject(sv) && sv_derived_from(sv, extra->return_type->extra[0].record.class))
Branch: 2 branches
BranchTrueFalseTotal
if(sv_isobject(sv) && sv_derived_from(sv, extra->return_type->extra[0].record.class)) 2 1 3
if(sv_isobject(sv) && sv_derived_from(sv, extra->return_type->extra[0].record.class)) 2 0 2
271 {
272 char *ptr;
273 STRLEN len;
274 2 ptr = SvPV(SvRV(sv), len);
275 2 ▶ if(len > extra->return_type->extra[0].record.size)
Branch: 1 branch
BranchTrueFalseTotal
if(len > extra->return_type->extra[0].record.size) 0 2 2
276 0 len = extra->return_type->extra[0].record.size;
277 2 ▶ else if(len < extra->return_type->extra[0].record.size)
Branch: 1 branch
BranchTrueFalseTotal
else if(len < extra->return_type->extra[0].record.size) 1 1 2
278 {
279 1 warn("Return record from closure is wrong size!");
280 1 memset(result, 0, extra->return_type->extra[0].record.size);
281 }
282 2 memcpy(result, ptr, len);
283 2 break;
284 }
285 1 warn("Return record from closure is wrong type!");
286 1 memset(result, 0, extra->return_type->extra[0].record.size);
287 1 break;
288 0 default:
289 0 warn("bad type");
290 0 break;
291 }
292  
293 73 PUTBACK;
294 }
295  
296 103 ▶ if(!(flags & G_NOARGS))
Branch: 1 branch
BranchTrueFalseTotal
if(!(flags & G_NOARGS)) 98 5 103
297 {
298 98 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 98 0 98
299 98 LEAVE;
300 }
301 103 }
302  
« xs/custom.c ↑ Summary xs/names.c »