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