Debian Codegraph @

xs/record_opaque.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
8 XS(ffi_pl_record_accessor_opaque)
9 {
10 ffi_pl_record_member *member;
11 SV *self;
12 SV *arg;
13 char *ptr1;
14 void **ptr2;
15
16 dVAR; dXSARGS;
17
18 if(items == 0)
19 croak("This is a method, you must provide at least the object");
20
21 member = (ffi_pl_record_member*) CvXSUBANY(cv).any_ptr;
22
23 self = ST(0);
24 if(SvROK(self))
25 self = SvRV(self);
26
27 if(!SvOK(self))
28 croak("Null record error");
29
30 ptr1 = (char*) SvPV_nolen(self);
31 ptr2 = (void**) &ptr1[member->offset];
32
33 if(items > 1)
34 {
35 if(SvREADONLY(self))
36 {
37 croak("record is read-only");
38 }
39 else
40 {
41 arg = ST(1);
42 *ptr2 = SvOK(arg) ? INT2PTR(void*, SvIV(arg)) : NULL;
43 }
44 }
45
46 if(GIMME_V == G_VOID)
47 XSRETURN_EMPTY;
48
49 if(*ptr2 != NULL)
50 XSRETURN_IV( PTR2IV( *ptr2 ));
51 else
52 XSRETURN_EMPTY;
53 }
54
55 XS(ffi_pl_record_accessor_opaque_array)
56 {
57 ffi_pl_record_member *member;
58 SV *self;
59 SV *arg;
60 SV **item;
61 AV *av;
62 char *ptr1;
63 void **ptr2;
64 int i;
65
66 dVAR; dXSARGS;
67
68 if(items == 0)
69 croak("This is a method, you must provide at least the object");
70
71 member = (ffi_pl_record_member*) CvXSUBANY(cv).any_ptr;
72
73 self = ST(0);
74 if(SvROK(self))
75 self = SvRV(self);
76
77 if(!SvOK(self))
78 croak("Null record error");
79
80 ptr1 = (char*) SvPV_nolen(self);
81 ptr2 = (void**) &ptr1[member->offset];
82
83 if(items > 1 && SvREADONLY(self))
84 {
85 croak("record is read-only");
86 }
87
88 if(items > 2)
89 {
90 i = SvIV(ST(1));
91 if(i >= 0 && i < member->count)
92 {
93 arg = ST(2);
94 ptr2[i] = SvOK(arg) ? INT2PTR(void*, SvIV(arg)) : NULL;
95 }
96 else
97 {
98 warn("illegal index %d", i);
99 }
100 }
101 else if(items > 1)
102 {
103 arg = ST(1);
104 if(SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVAV)
105 {
106 av = (AV*) SvRV(arg);
107 for(i=0; i < member->count; i++)
108 {
109 item = av_fetch(av, i, 0);
110 if(item != NULL && SvOK(*item))
111 {
112 ptr2[i] = INT2PTR(void*, SvIV(*item));
113 }
114 else
115 {
116 ptr2[i] = NULL;
117 }
118 }
119 }
120 else
121 {
122 i = SvIV(ST(1));
123 if(i < 0 && i >= member->count)
124 {
125 warn("illegal index %d", i);
126 XSRETURN_EMPTY;
127 }
128 else if(ptr2[i] == NULL)
129 {
130 XSRETURN_EMPTY;
131 }
132 else
133 {
134 XSRETURN_IV(PTR2IV(ptr2[i]));
135 }
136 warn("passing non array reference into ffi/platypus array argument type");
137 }
138 }
139
140 if(GIMME_V == G_VOID)
141 XSRETURN_EMPTY;
142
143 av = newAV();
144 av_fill(av, member->count-1);
145 for(i=0; i < member->count; i++)
146 {
147 if(ptr2[i] != NULL)
148 sv_setiv(*av_fetch(av, i, 1), PTR2IV(ptr2[i]));
149 }
150 ST(0) = newRV_inc((SV*)av);
151 XSRETURN(1);
152 }