-
Notifications
You must be signed in to change notification settings - Fork 1
/
Any.xs
210 lines (171 loc) · 5.1 KB
/
Any.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
/* Taken from XS::Object::Magic */
STATIC MGVTBL null_mg_vtbl = {
NULL, /* get */
NULL, /* set */
NULL, /* len */
NULL, /* clear */
NULL, /* free */
#if MGf_COPY
NULL, /* copy */
#endif /* MGf_COPY */
#if MGf_DUP
NULL, /* dup */
#endif /* MGf_DUP */
#if MGf_LOCAL
NULL, /* local */
#endif /* MGf_LOCAL */
};
typedef struct {
SV *name;
SV *data;
} private_sv_data;
MODULE = Simple::Class::Private::Data::Any PACKAGE = Simple::Class::Private::Data::Any
SV *
new(package, ...)
SV *package
PREINIT:
HV *self;
HV *stash;
SV *self_ref;
int i = 0;
CODE:
/* Make sure the first argument is a string */
if (!SvPOK(package)) {
croak("new() expects a package name");
}
/* Make sure we have an even number of arguments for our hash */
if ((items-1) % 2) {
croak("Odd number of elements in constructor arguments");
}
/* my %self; */
self = newHV();
/* Get our packages stash (needed for bless) */
stash = gv_stashpv(SvPV_nolen(package), 0);
if (!stash) {
croak("Failed to find our stash!");
}
/* my $ref = \%self; # Weakened, though... */
self_ref = newRV_noinc((SV*)self);
/* Take @_ and fill in our object with it
*
* ST(0) is the package name, ST(i) to ST(items-1) are the rest
* of the arguments */
for (i = 1; i < items; i+= 2) {
SV *obj = ST(i+1);
/* Storing '_private' string? Hide it from Perl land in magic */
if (!strcmp(SvPV_nolen(ST(i)), "_private")) {
private_sv_data *pdata;
Newx(pdata, sizeof(pdata), private_sv_data);
pdata->name = SvREFCNT_inc(ST(i));
pdata->data = SvREFCNT_inc(sv_mortalcopy(obj));
/* Make us magic! */
sv_magicext((SV*)self, NULL, PERL_MAGIC_ext, &null_mg_vtbl, pdata, 0);
} else {
/* if (ref $val) { # no refs allowed! } */
if (SvROK(obj)) {
croak("Hash value for '%s' must be a string", SvPV_nolen(ST(i)));
}
/* $self{$key} = $val */
/* obj has refcount of 0 here I guess. It needs 1 which our
hash will own */
SvREFCNT_inc(obj);
if (NULL == hv_store(self, SvPV_nolen(ST(i)), strlen(SvPV_nolen(ST(i))), obj, 0)) {
croak("panic: hv_store() failed to store element in hash");
}
}
}
/* return bless $self, __PACKAGE__ */
RETVAL = sv_bless(self_ref, stash);
OUTPUT:
RETVAL
void
display(self)
SV *self
PREINIT:
HV *obj;
SV *next;
char *key;
I32 rlen;
CODE:
if (!sv_isobject(self)) {
croak("Self is not an object");
}
obj = (HV*) SvRV(self);
/* if (Scalar::Util::reftype($obj) ne 'HASH') { ... } */
if (!(SvTYPE(obj) == SVt_PVHV)) {
croak("Reference is not a hashref");
}
PerlIO_printf(PerlIO_stdout(), "Self:\n");
/* Iterate through our hash and print out our keys/values.
*
* Similar to:
*
* while (my ($k, $v) = each %$self) {
* print "$k: $v\n";
* }
*/
hv_iterinit(obj);
while (next = hv_iternextsv(obj, &key, &rlen)) {
PerlIO_printf(PerlIO_stdout(), "\t%s: %s\n", key, SvPV_nolen(next));
}
/* Any private data? */
if (SvTYPE(obj) >= SVt_PVMG) {
MAGIC *mg;
/* Our object may have lots of magic, look through it all to
find ours */
for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic) {
if (
(mg->mg_type == PERL_MAGIC_ext)
&& (mg->mg_virtual == &null_mg_vtbl)
) {
private_sv_data *pdata = (private_sv_data*)mg->mg_ptr;
PerlIO_printf(PerlIO_stdout(), "\t%s: %s\n",
SvPV_nolen(pdata->name),
SvPV_nolen(pdata->data)
);
}
}
}
void
DESTROY(self)
SV *self
PREINIT:
HV *obj;
SV *next;
char *key;
I32 rlen;
CODE:
if (!sv_isobject(self)) {
croak("Self is not an object");
}
obj = (HV*) SvRV(self);
/* if (Scalar::Util::reftype($obj) ne 'HASH') { ... } */
if (!(SvTYPE(obj) == SVt_PVHV)) {
croak("Reference is not a hashref");
}
/* Any private data? */
if (SvTYPE(obj) >= SVt_PVMG) {
MAGIC *mg;
/* Our object may have lots of magic, look through it all to
find ours */
for (mg = SvMAGIC(obj); mg; mg = mg->mg_moremagic) {
if (
(mg->mg_type == PERL_MAGIC_ext)
&& (mg->mg_virtual == &null_mg_vtbl)
) {
private_sv_data *pdata = (private_sv_data*)mg->mg_ptr;
/* We're done with these SVs, release them */
SvREFCNT_dec(pdata->name);
SvREFCNT_dec(pdata->data);
pdata->name = NULL;
pdata->data = NULL;
/* And free our struct */
Safefree(pdata);
mg->mg_ptr = NULL;
}
}
}