| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #define NEED_sv_2pv_flags |
| 6 |
|
| |
| 7 |
|
| #include "ppport.h" |
| 8 |
|
| |
| 9 |
|
| #include <stdio.h> |
| 10 |
|
| #include <stdlib.h> |
| 11 |
|
| #include <string.h> |
| 12 |
|
| |
| 13 |
|
| #include <ffi.h> |
| 14 |
|
| |
| 15 |
|
| #ifdef __MINGW32__ |
| 16 |
|
| # include <stdint.h> |
| 17 |
|
| #endif |
| 18 |
|
| |
| 19 |
|
| #ifdef _MSC_VER |
| 20 |
|
| #include <stdlib.h> |
| 21 |
|
| typedef __int64 int64_t; |
| 22 |
|
| typedef unsigned __int64 uint64_t; |
| 23 |
|
| #endif |
| 24 |
|
| |
| 25 |
|
| #include "math_int64/perl_math_int64.h" |
| 26 |
|
| #include "math_int64/perl_math_int64.c" |
| 27 |
|
| |
| 28 |
|
| #if defined(_WIN32) || defined(__CYGWIN__) |
| 29 |
|
| # include <windows.h> |
| 30 |
|
| # include <psapi.h> |
| 31 |
|
| #else |
| 32 |
|
| # include <dlfcn.h> |
| 33 |
|
| #endif |
| 34 |
|
| |
| 35 |
|
| #if defined(__CYGWIN__) |
| 36 |
|
| # include <sys/cygwin.h> |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
|
| typedef struct FFI_RAW { |
| 40 |
|
| void *fn; |
| 41 |
|
| void *handle; |
| 42 |
|
| |
| 43 |
|
| ffi_cif cif; |
| 44 |
|
| ffi_type *ret; |
| 45 |
|
| char ret_type; |
| 46 |
|
| ffi_type **args; |
| 47 |
|
| char *args_types; |
| 48 |
|
| unsigned int argc; |
| 49 |
|
| } FFI_Raw_t; |
| 50 |
|
| |
| 51 |
|
| typedef void FFI_Raw_MemPtr_t; |
| 52 |
|
| |
| 53 |
|
| typedef struct FFI_RAW_CALLBACK { |
| 54 |
|
| void *fn; |
| 55 |
|
| SV *coderef; |
| 56 |
|
| ffi_closure *closure; |
| 57 |
|
| |
| 58 |
|
| ffi_cif cif; |
| 59 |
|
| ffi_type *ret; |
| 60 |
|
| char ret_type; |
| 61 |
|
| void *ret_value; |
| 62 |
|
| ffi_type **args; |
| 63 |
|
| char *args_types; |
| 64 |
|
| unsigned int argc; |
| 65 |
|
| } FFI_Raw_Callback_t; |
| 66 |
|
| |
| 67 |
102 |
| void *_ffi_raw_get_type(char type) { |
| 68 |
102 |
| switch (type) { |
| 69 |
28 |
| case 'v': return &ffi_type_void; |
| 70 |
2 |
| case 'l': return &ffi_type_slong; |
| 71 |
2 |
| case 'L': return &ffi_type_ulong; |
| 72 |
2 |
| case 'x': return &ffi_type_sint64; |
| 73 |
2 |
| case 'X': return &ffi_type_uint64; |
| 74 |
18 |
| case 'i': return &ffi_type_sint32; |
| 75 |
2 |
| case 'I': return &ffi_type_uint32; |
| 76 |
5 |
| case 'z': return &ffi_type_sint16; |
| 77 |
2 |
| case 'Z': return &ffi_type_uint16; |
| 78 |
3 |
| case 'c': return &ffi_type_sint8; |
| 79 |
2 |
| case 'C': return &ffi_type_uint8; |
| 80 |
2 |
| case 'f': return &ffi_type_float; |
| 81 |
2 |
| case 'd': return &ffi_type_double; |
| 82 |
30 |
| case 's': |
| 83 |
30 |
| case 'p': return &ffi_type_pointer; |
| 84 |
0 |
| default: Perl_croak(aTHX_ "Invalid type '%c'", type); |
| 85 |
|
| } |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| #define PTR_TO_INT(ARG) \ |
| 89 |
|
| newSViv(PTR2IV(ARG)) |
| 90 |
|
| |
| 91 |
|
| #define INT_TO_PTR(ARG) \ |
| 92 |
|
| INT2PTR(void *, SvIV(ARG)) |
| 93 |
|
| |
| 94 |
|
| #define INIT_FFI_CIF(ARG, ARGC) \ |
| 95 |
|
| int i; \ |
| 96 |
|
| ARG -> ret = _ffi_raw_get_type(SvIV(ret_type)); \ |
| 97 |
|
| ARG -> ret_type = SvIV(ret_type); \ |
| 98 |
|
| ARG -> argc = items - ARGC; \ |
| 99 |
|
| \ |
| 100 |
|
| Newx(ARG -> args, ARG -> argc, ffi_type *); \ |
| 101 |
|
| Newx(ARG -> args_types, ARG -> argc, char); \ |
| 102 |
|
| \ |
| 103 |
|
| for (i = ARGC; i < items; i++) { \ |
| 104 |
|
| char type = SvIV(ST(i)); \ |
| 105 |
|
| \ |
| 106 |
|
| ARG -> args_types[i - ARGC] = type; \ |
| 107 |
|
| ARG -> args[i - ARGC] = _ffi_raw_get_type(type); \ |
| 108 |
|
| } \ |
| 109 |
|
| \ |
| 110 |
|
| status = ffi_prep_cif( \ |
| 111 |
|
| &ARG -> cif, FFI_DEFAULT_ABI, ARG -> argc, \ |
| 112 |
|
| ARG -> ret, ARG -> args \ |
| 113 |
|
| ); \ |
| 114 |
|
| \ |
| 115 |
|
| if (status != FFI_OK) \ |
| 116 |
|
| Perl_croak(aTHX_ "Error creating calling interface"); |
| 117 |
|
| |
| 118 |
|
| #define FFI_PUSH_PARAM(TYPE, FN) { \ |
| 119 |
|
| SV *arg = FN(*(TYPE *) args[i]); \ |
| 120 |
|
| XPUSHs(sv_2mortal(arg)); \ |
| 121 |
|
| break; \ |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| #if defined(__CYGWIN__) |
| 125 |
|
| void *_ffi_raw_win32_load_library(const char *posix_path) { |
| 126 |
|
| void *lib; |
| 127 |
|
| |
| 128 |
|
| ssize_t size; |
| 129 |
|
| char *win_path; |
| 130 |
|
| |
| 131 |
|
| size = cygwin_conv_path(CCP_POSIX_TO_WIN_A | CCP_RELATIVE, posix_path, NULL, 0); |
| 132 |
|
| if (size < 0) return NULL; |
| 133 |
|
| |
| 134 |
|
| Newx(win_path, size, char); |
| 135 |
|
| if (cygwin_conv_path(CCP_POSIX_TO_WIN_A | CCP_RELATIVE, posix_path, win_path, size)) { |
| 136 |
|
| Safefree(win_path); |
| 137 |
|
| return NULL; |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| lib = LoadLibrary(win_path); |
| 141 |
|
| Safefree(win_path); |
| 142 |
|
| |
| 143 |
|
| return lib; |
| 144 |
|
| } |
| 145 |
|
| #elif defined(_WIN32) |
| 146 |
|
| # define _ffi_raw_win32_load_library(fn) LoadLibrary(fn) |
| 147 |
|
| #endif |
| 148 |
|
| |
| 149 |
11 |
| void _ffi_raw_cb_wrap(ffi_cif *cif, void *ret, void *args[], void *argp) { |
| 150 |
11 |
| dSP; |
| 151 |
|
| |
| 152 |
|
| int i, retc; |
| 153 |
11 |
| FFI_Raw_Callback_t *self = argp; |
| 154 |
|
| |
| 155 |
11 |
| ENTER; |
| 156 |
11 |
| SAVETMPS; |
| 157 |
|
| |
| 158 |
11 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
11 |
11 |
|
| 159 |
17 |
▶ | for (i = 0; i < self -> argc; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < self -> argc; i++) { |
6 |
11 |
17 |
|
| 160 |
6 |
| switch (self -> args_types[i]) { |
| 161 |
0 |
| case 'v': break; |
| 162 |
0 |
▶ | case 'l': FFI_PUSH_PARAM(long, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'l': FFI_PUSH_PARAM(long, newSViv) |
0 |
0 |
0 |
|
| 163 |
0 |
▶ | case 'L': FFI_PUSH_PARAM(unsigned long, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'L': FFI_PUSH_PARAM(unsigned long, newSViv) |
0 |
0 |
0 |
|
| 164 |
0 |
▶ | case 'x': FFI_PUSH_PARAM(long long int, newSVi64) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'x': FFI_PUSH_PARAM(long long int, newSVi64) |
0 |
0 |
0 |
|
| 165 |
0 |
▶ | case 'X': FFI_PUSH_PARAM(unsigned long long int, newSVu64) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'X': FFI_PUSH_PARAM(unsigned long long int, newSVu64) |
0 |
0 |
0 |
|
| 166 |
6 |
▶ | case 'i': FFI_PUSH_PARAM(int, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'i': FFI_PUSH_PARAM(int, newSViv) |
0 |
6 |
6 |
|
| 167 |
0 |
▶ | case 'I': FFI_PUSH_PARAM(unsigned int, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'I': FFI_PUSH_PARAM(unsigned int, newSViv) |
0 |
0 |
0 |
|
| 168 |
0 |
▶ | case 'c': FFI_PUSH_PARAM(char, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'c': FFI_PUSH_PARAM(char, newSViv) |
0 |
0 |
0 |
|
| 169 |
0 |
▶ | case 'C': FFI_PUSH_PARAM(unsigned char, newSViv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'C': FFI_PUSH_PARAM(unsigned char, newSViv) |
0 |
0 |
0 |
|
| 170 |
0 |
▶ | case 'f': FFI_PUSH_PARAM(float, newSVuv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'f': FFI_PUSH_PARAM(float, newSVuv) |
0 |
0 |
0 |
|
| 171 |
0 |
▶ | case 'd': FFI_PUSH_PARAM(double, newSVuv) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'd': FFI_PUSH_PARAM(double, newSVuv) |
0 |
0 |
0 |
|
| 172 |
0 |
| case 's': { |
| 173 |
0 |
| SV *arg = newSVpv(*(char **) args[i], 0); |
| 174 |
0 |
▶ | XPUSHs(sv_2mortal(arg)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(arg)); |
0 |
0 |
0 |
|
| 175 |
0 |
| break; |
| 176 |
|
| } |
| 177 |
0 |
▶ | case 'p': FFI_PUSH_PARAM(void *, PTR_TO_INT) |
Branch: 1 branch
| Branch | True | False | Total |
| case 'p': FFI_PUSH_PARAM(void *, PTR_TO_INT) |
0 |
0 |
0 |
|
| 178 |
|
| } |
| 179 |
|
| } |
| 180 |
11 |
| PUTBACK; |
| 181 |
|
| |
| 182 |
11 |
| retc = call_sv(self -> coderef, G_SCALAR); |
| 183 |
|
| |
| 184 |
11 |
| SPAGAIN; |
| 185 |
|
| |
| 186 |
11 |
| switch (self -> ret_type) { |
| 187 |
2 |
| case 'v': break; |
| 188 |
0 |
| case 'l': *(long *) ret = POPi; break; |
| 189 |
0 |
| case 'L': *(unsigned long *) ret = POPi; break; |
| 190 |
0 |
| case 'x': *(long long int *) ret = POPi; break; |
| 191 |
0 |
| case 'X': *(unsigned long long int *) ret = POPi; break; |
| 192 |
4 |
| case 'i': *(int *) ret = POPi; break; |
| 193 |
0 |
| case 'I': *(unsigned int *) ret = POPi; break; |
| 194 |
0 |
| case 'z': *(short *) ret = POPi; break; |
| 195 |
0 |
| case 'Z': *(unsigned short *) ret = POPi; break; |
| 196 |
0 |
| case 'c': *(char *) ret = POPi; break; |
| 197 |
0 |
| case 'C': *(unsigned char *) ret = POPi; break; |
| 198 |
0 |
| case 'f': *(float *) ret = POPn; break; |
| 199 |
0 |
| case 'd': *(double *) ret = POPn; break; |
| 200 |
2 |
| case 's': { |
| 201 |
2 |
| SV *value = POPs; |
| 202 |
|
| |
| 203 |
2 |
▶ | if (self -> ret_value != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self -> ret_value != NULL) |
1 |
1 |
2 |
|
| 204 |
1 |
| Safefree(self -> ret_value); |
| 205 |
|
| |
| 206 |
2 |
▶ | if (SvOK(value)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(value)) |
1 |
1 |
2 |
|
| 207 |
1 |
| *(char **) ret = savepv(SvPV_nolen(value)); |
| 208 |
|
| else |
| 209 |
1 |
| *(char **) ret = NULL; |
| 210 |
|
| |
| 211 |
2 |
| self -> ret_value = *(void **) ret; |
| 212 |
|
| |
| 213 |
2 |
| break; |
| 214 |
|
| } |
| 215 |
3 |
| case 'p': { |
| 216 |
3 |
| SV *value = POPs; |
| 217 |
|
| |
| 218 |
3 |
▶ | if (!SvOK(value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(value)) { |
1 |
2 |
3 |
|
| 219 |
1 |
| *(void **) ret = NULL; |
| 220 |
1 |
| break; |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
3 |
| if (sv_derived_from(value, "FFI::Raw::Ptr") || |
| 224 |
1 |
| sv_derived_from(value, "FFI::Raw::Callback")) |
| 225 |
1 |
| value = SvRV(value); |
| 226 |
|
| |
| 227 |
2 |
| *(void **) ret = INT_TO_PTR(value); |
| 228 |
|
| |
| 229 |
2 |
| break; |
| 230 |
|
| } |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
11 |
| PUTBACK; |
| 234 |
11 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
11 |
0 |
11 |
|
| 235 |
11 |
| LEAVE; |
| 236 |
11 |
| } |
| 237 |
|
| |
| 238 |
|
| MODULE = FFI::Raw PACKAGE = FFI::Raw |
| 239 |
|
| |
| 240 |
|
| BOOT: |
| 241 |
13 |
| PERL_MATH_INT64_LOAD; |
| 242 |
|
| |
| 243 |
|
| FFI_Raw_t * |
| 244 |
|
| new(class, library, function, ret_type, ...) |
| 245 |
|
| SV *class |
| 246 |
|
| SV *library |
| 247 |
|
| SV *function |
| 248 |
|
| SV *ret_type |
| 249 |
|
| |
| 250 |
|
| PREINIT: |
| 251 |
|
| char *error; |
| 252 |
|
| ffi_status status; |
| 253 |
|
| |
| 254 |
|
| FFI_Raw_t *ffi_raw; |
| 255 |
|
| |
| 256 |
|
| const char *library_name, *function_name; |
| 257 |
|
| #if defined(_WIN32) || defined(__CYGWIN__) |
| 258 |
|
| int n; |
| 259 |
|
| DWORD needed; |
| 260 |
|
| |
| 261 |
|
| HANDLE process; |
| 262 |
|
| HMODULE mods[1024]; |
| 263 |
|
| TCHAR mod_name[MAX_PATH]; |
| 264 |
|
| #endif |
| 265 |
|
| |
| 266 |
|
| CODE: |
| 267 |
57 |
| Newx(ffi_raw, 1, FFI_Raw_t); |
| 268 |
|
| |
| 269 |
57 |
▶ | if (SvOK(library)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(library)) |
56 |
1 |
57 |
|
| 270 |
56 |
| library_name = SvPV_nolen(library); |
| 271 |
|
| else |
| 272 |
1 |
| library_name = NULL; |
| 273 |
|
| |
| 274 |
57 |
| function_name = SvPV_nolen(function); |
| 275 |
|
| #if defined(_WIN32) || defined(__CYGWIN__) |
| 276 |
|
| GetLastError(); |
| 277 |
|
| |
| 278 |
|
| if (library_name != NULL) { |
| 279 |
|
| ffi_raw -> handle = _ffi_raw_win32_load_library(library_name); |
| 280 |
|
| |
| 281 |
|
| if (ffi_raw->handle == NULL) |
| 282 |
|
| Perl_croak(aTHX_ "Library not found"); |
| 283 |
|
| |
| 284 |
|
| /*if ((error = GetLastError()) != NULL) |
| 285 |
|
| Perl_croak(aTHX_ error);*/ |
| 286 |
|
| |
| 287 |
|
| ffi_raw -> fn = GetProcAddress( |
| 288 |
|
| ffi_raw -> handle, function_name |
| 289 |
|
| ); |
| 290 |
|
| |
| 291 |
|
| if (ffi_raw -> fn == NULL) |
| 292 |
|
| Perl_croak(aTHX_ "Function not found"); |
| 293 |
|
| |
| 294 |
|
| /*if ((error = GetLastError()) != NULL) |
| 295 |
|
| Perl_croak(aTHX_ error);*/ |
| 296 |
|
| } else { |
| 297 |
|
| process = OpenProcess( |
| 298 |
|
| PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, |
| 299 |
|
| FALSE, GetCurrentProcessId() |
| 300 |
|
| ); |
| 301 |
|
| |
| 302 |
|
| if (process == NULL) |
| 303 |
|
| Perl_croak(aTHX_ "Process not found"); |
| 304 |
|
| |
| 305 |
|
| if (EnumProcessModules( |
| 306 |
|
| process, mods, sizeof(mods), &needed) |
| 307 |
|
| ) { |
| 308 |
|
| for (n = 0; n < (needed/sizeof(HMODULE)); n++) { |
| 309 |
|
| if (GetModuleFileNameEx( |
| 310 |
|
| process, mods[n], mod_name, |
| 311 |
|
| sizeof(mod_name)/sizeof(TCHAR)) |
| 312 |
|
| ) { |
| 313 |
|
| |
| 314 |
|
| ffi_raw -> handle = _ffi_raw_win32_load_library(mod_name); |
| 315 |
|
| |
| 316 |
|
| if (ffi_raw -> handle == NULL) |
| 317 |
|
| continue; |
| 318 |
|
| |
| 319 |
|
| ffi_raw -> fn = GetProcAddress( |
| 320 |
|
| ffi_raw -> handle, function_name |
| 321 |
|
| ); |
| 322 |
|
| |
| 323 |
|
| if (ffi_raw -> fn != NULL) |
| 324 |
|
| break; |
| 325 |
|
| |
| 326 |
|
| FreeLibrary(ffi_raw -> handle); |
| 327 |
|
| } |
| 328 |
|
| } |
| 329 |
|
| } |
| 330 |
|
| |
| 331 |
|
| if (ffi_raw -> fn == NULL) |
| 332 |
|
| Perl_croak(aTHX_ "Function not found"); |
| 333 |
|
| } |
| 334 |
|
| #else |
| 335 |
57 |
| dlerror(); |
| 336 |
|
| |
| 337 |
57 |
| ffi_raw -> handle = dlopen(library_name, RTLD_LAZY); |
| 338 |
|
| |
| 339 |
57 |
▶ | if ((error = dlerror()) != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = dlerror()) != NULL) |
1 |
56 |
57 |
|
| 340 |
1 |
| Perl_croak(aTHX_ "%s", error); |
| 341 |
|
| |
| 342 |
56 |
| ffi_raw -> fn = dlsym(ffi_raw -> handle, function_name); |
| 343 |
|
| |
| 344 |
56 |
▶ | if ((error = dlerror()) != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = dlerror()) != NULL) |
0 |
56 |
56 |
|
| 345 |
0 |
| Perl_croak(aTHX_ "%s", error); |
| 346 |
|
| #endif |
| 347 |
92 |
▶ | INIT_FFI_CIF(ffi_raw, 4) |
Branch: 2 branches
| Branch | True | False | Total |
| INIT_FFI_CIF(ffi_raw, 4) |
36 |
56 |
92 |
| INIT_FFI_CIF(ffi_raw, 4) |
0 |
56 |
56 |
|
| 348 |
|
| |
| 349 |
56 |
| RETVAL = ffi_raw; |
| 350 |
|
| |
| 351 |
|
| OUTPUT: RETVAL |
| 352 |
|
| |
| 353 |
|
| FFI_Raw_t * |
| 354 |
|
| new_from_ptr(class, function, ret_type, ...) |
| 355 |
|
| SV *class |
| 356 |
|
| SV *function |
| 357 |
|
| SV *ret_type |
| 358 |
|
| |
| 359 |
|
| PREINIT: |
| 360 |
|
| char *error; |
| 361 |
|
| ffi_status status; |
| 362 |
|
| |
| 363 |
|
| FFI_Raw_t *ffi_raw; |
| 364 |
|
| |
| 365 |
|
| CODE: |
| 366 |
0 |
| Newx(ffi_raw, 1, FFI_Raw_t); |
| 367 |
|
| |
| 368 |
0 |
| ffi_raw -> handle = NULL; |
| 369 |
0 |
| ffi_raw -> fn = INT_TO_PTR(function); |
| 370 |
|
| |
| 371 |
0 |
▶ | INIT_FFI_CIF(ffi_raw, 3) |
Branch: 2 branches
| Branch | True | False | Total |
| INIT_FFI_CIF(ffi_raw, 3) |
0 |
0 |
0 |
| INIT_FFI_CIF(ffi_raw, 3) |
0 |
0 |
0 |
|
| 372 |
|
| |
| 373 |
0 |
| RETVAL = ffi_raw; |
| 374 |
|
| |
| 375 |
|
| OUTPUT: RETVAL |
| 376 |
|
| |
| 377 |
|
| #define FFI_SET_ARG(TYPE, FN) { \ |
| 378 |
|
| TYPE *val; \ |
| 379 |
|
| Newx(val, 1, TYPE); \ |
| 380 |
|
| *val = FN(arg); \ |
| 381 |
|
| values[i] = val; \ |
| 382 |
|
| break; \ |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| #if defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN |
| 386 |
|
| # define FFI_CALL(TYPE, FN) { \ |
| 387 |
|
| void *result; \ |
| 388 |
|
| void *original; \ |
| 389 |
|
| ffi_type *rtype = self -> ret; \ |
| 390 |
|
| Newx(result, 1, TYPE); \ |
| 391 |
|
| original = result; \ |
| 392 |
|
| ffi_call(&self -> cif, self -> fn, result, values); \ |
| 393 |
|
| if (rtype -> type != FFI_TYPE_FLOAT && \ |
| 394 |
|
| rtype -> type != FFI_TYPE_STRUCT && \ |
| 395 |
|
| rtype -> size < sizeof(ffi_arg)) \ |
| 396 |
|
| result = (char *) result + sizeof(ffi_arg) - rtype -> size; \ |
| 397 |
|
| output = FN(*(TYPE *) result); \ |
| 398 |
|
| Safefree(original); \ |
| 399 |
|
| break; \ |
| 400 |
|
| } |
| 401 |
|
| #else |
| 402 |
|
| # define FFI_CALL(TYPE, FN) { \ |
| 403 |
|
| void *result; \ |
| 404 |
|
| ffi_type *rtype = self -> ret; \ |
| 405 |
|
| Newx(result, 1, TYPE); \ |
| 406 |
|
| ffi_call(&self -> cif, self -> fn, result, values); \ |
| 407 |
|
| output = FN(*(TYPE *) result); \ |
| 408 |
|
| Safefree(result); \ |
| 409 |
|
| break; \ |
| 410 |
|
| } |
| 411 |
|
| #endif |
| 412 |
|
| |
| 413 |
|
| SV * |
| 414 |
|
| call(self, ...) |
| 415 |
|
| FFI_Raw_t *self |
| 416 |
|
| |
| 417 |
|
| PREINIT: |
| 418 |
|
| int i; |
| 419 |
|
| |
| 420 |
|
| SV *output; |
| 421 |
|
| void **values; |
| 422 |
|
| |
| 423 |
|
| CODE: |
| 424 |
101 |
▶ | if (self -> argc != (items - 1)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self -> argc != (items - 1)) |
0 |
101 |
101 |
|
| 425 |
0 |
| Perl_croak(aTHX_ "Wrong number of arguments"); |
| 426 |
|
| |
| 427 |
101 |
| Newx(values, self -> argc, void *); |
| 428 |
|
| |
| 429 |
165 |
▶ | for (i = 0; i < self -> argc; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < self -> argc; i++) { |
64 |
101 |
165 |
|
| 430 |
64 |
| SV *arg = ST(i + 1); |
| 431 |
|
| |
| 432 |
64 |
| switch (self -> args_types[i]) { |
| 433 |
0 |
| case 'v': break; |
| 434 |
1 |
| case 'l': FFI_SET_ARG(long, SvIV) |
| 435 |
1 |
| case 'L': FFI_SET_ARG(unsigned long, SvUV) |
| 436 |
1 |
| case 'x': FFI_SET_ARG(long long int, SvI64) |
| 437 |
1 |
| case 'X': FFI_SET_ARG(unsigned long long int, SvU64) |
| 438 |
6 |
| case 'i': FFI_SET_ARG(int, SvIV) |
| 439 |
1 |
| case 'I': FFI_SET_ARG(unsigned int, SvUV) |
| 440 |
5 |
| case 'z': FFI_SET_ARG(short, SvIV) |
| 441 |
1 |
| case 'Z': FFI_SET_ARG(unsigned short, SvUV) |
| 442 |
3 |
| case 'c': FFI_SET_ARG(char, SvIV) |
| 443 |
1 |
| case 'C': FFI_SET_ARG(unsigned char, SvUV) |
| 444 |
2 |
| case 'f': FFI_SET_ARG(float, SvNV) |
| 445 |
2 |
| case 'd': FFI_SET_ARG(double, SvNV) |
| 446 |
5 |
| case 's': { |
| 447 |
|
| STRLEN l; |
| 448 |
|
| char **val; |
| 449 |
|
| |
| 450 |
5 |
| Newx(val, 1, char *); |
| 451 |
|
| |
| 452 |
5 |
▶ | if (SvOK(arg)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOK(arg)) |
4 |
1 |
5 |
|
| 453 |
4 |
| *val = SvPV(arg, l); |
| 454 |
|
| else |
| 455 |
1 |
| *val = NULL; |
| 456 |
|
| |
| 457 |
5 |
| values[i] = val; |
| 458 |
5 |
| break; |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
34 |
| case 'p': { |
| 462 |
|
| void **val; |
| 463 |
|
| |
| 464 |
34 |
| Newx(val, 1, void *); |
| 465 |
|
| |
| 466 |
34 |
▶ | if (!SvOK(arg)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(arg)) |
1 |
33 |
34 |
|
| 467 |
1 |
| *val = NULL; |
| 468 |
|
| else { |
| 469 |
33 |
▶ | if (sv_derived_from( |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from( |
18 |
15 |
33 |
|
| 470 |
|
| arg, "FFI::Raw::Ptr" |
| 471 |
|
| )) { |
| 472 |
18 |
| arg = SvRV(arg); |
| 473 |
|
| } |
| 474 |
|
| |
| 475 |
33 |
▶ | if (sv_derived_from( |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from( |
11 |
22 |
33 |
|
| 476 |
|
| arg, "FFI::Raw::Callback" |
| 477 |
|
| )) { |
| 478 |
11 |
| FFI_Raw_Callback_t *cb = |
| 479 |
11 |
| INT_TO_PTR(SvRV(arg)); |
| 480 |
11 |
| *val = cb -> fn; |
| 481 |
|
| } else |
| 482 |
22 |
| *val = INT_TO_PTR(arg); |
| 483 |
|
| } |
| 484 |
|
| |
| 485 |
34 |
| values[i] = val; |
| 486 |
34 |
| break; |
| 487 |
|
| } |
| 488 |
|
| } |
| 489 |
|
| } |
| 490 |
|
| |
| 491 |
101 |
| switch (self -> ret_type) { |
| 492 |
38 |
| case 'v': { |
| 493 |
38 |
| ffi_call( |
| 494 |
38 |
| &self -> cif, self -> fn, |
| 495 |
|
| NULL, values |
| 496 |
|
| ); |
| 497 |
|
| |
| 498 |
38 |
| output = newSV(0); |
| 499 |
38 |
| break; |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
2 |
| case 'l': FFI_CALL(long, newSViv) |
| 503 |
2 |
| case 'L': FFI_CALL(unsigned long, newSVuv) |
| 504 |
2 |
| case 'x': FFI_CALL(long long int, newSVi64) |
| 505 |
2 |
| case 'X': FFI_CALL(unsigned long long int, newSVu64) |
| 506 |
21 |
| case 'i': FFI_CALL(int, newSViv) |
| 507 |
2 |
| case 'I': FFI_CALL(unsigned int, newSVuv) |
| 508 |
2 |
| case 'z': FFI_CALL(short, newSViv) |
| 509 |
2 |
| case 'Z': FFI_CALL(unsigned short, newSVuv) |
| 510 |
2 |
| case 'c': FFI_CALL(char, newSViv) |
| 511 |
2 |
| case 'C': FFI_CALL(unsigned char, newSVuv) |
| 512 |
2 |
| case 'f': FFI_CALL(float, newSVnv) |
| 513 |
2 |
| case 'd': FFI_CALL(double, newSVnv) |
| 514 |
16 |
| case 's': { |
| 515 |
|
| char *result; |
| 516 |
|
| |
| 517 |
16 |
| ffi_call( |
| 518 |
16 |
| &self -> cif, self -> fn, |
| 519 |
|
| &result, values |
| 520 |
|
| ); |
| 521 |
|
| |
| 522 |
16 |
| output = newSVpv(result, 0); |
| 523 |
16 |
| break; |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
4 |
| case 'p': { |
| 527 |
|
| void *result; |
| 528 |
|
| |
| 529 |
4 |
| ffi_call( |
| 530 |
4 |
| &self -> cif, self -> fn, |
| 531 |
|
| &result, values |
| 532 |
|
| ); |
| 533 |
|
| |
| 534 |
4 |
▶ | if (result == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (result == NULL) |
2 |
2 |
4 |
|
| 535 |
2 |
| output = &PL_sv_undef; |
| 536 |
|
| else |
| 537 |
2 |
| output = PTR_TO_INT(result); |
| 538 |
4 |
| break; |
| 539 |
|
| } |
| 540 |
|
| } |
| 541 |
|
| |
| 542 |
165 |
▶ | for (i = 0; i < self -> argc; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < self -> argc; i++) |
64 |
101 |
165 |
|
| 543 |
64 |
| Safefree(values[i]); |
| 544 |
|
| |
| 545 |
101 |
| Safefree(values); |
| 546 |
|
| |
| 547 |
101 |
| RETVAL = output; |
| 548 |
|
| |
| 549 |
|
| OUTPUT: RETVAL |
| 550 |
|
| |
| 551 |
|
| void |
| 552 |
|
| DESTROY(self) |
| 553 |
|
| FFI_Raw_t *self |
| 554 |
|
| |
| 555 |
|
| CODE: |
| 556 |
56 |
▶ | if (self -> handle) |
Branch: 1 branch
| Branch | True | False | Total |
| if (self -> handle) |
56 |
0 |
56 |
|
| 557 |
|
| #if defined(_WIN32) || defined(__CYGWIN__) |
| 558 |
|
| FreeLibrary(self -> handle); |
| 559 |
|
| #else |
| 560 |
56 |
| dlclose(self -> handle); |
| 561 |
|
| #endif |
| 562 |
56 |
| Safefree(self -> args_types); |
| 563 |
56 |
| Safefree(self -> args); |
| 564 |
56 |
| Safefree(self); |
| 565 |
|
| |
| 566 |
|
| INCLUDE: xs/MemPtr.xs |
| 567 |
|
| INCLUDE: xs/Callback.xs |