Debian Codegraph @

xs/closure.c

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