Commit | Line | Data |
1ea12c91 |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
4 | |
035fd0c4 |
5 | #define NEED_newRV_noinc |
6 | #define NEED_newSVpvn_share |
7 | #define NEED_sv_2pv_flags |
8 | #include "ppport.h" |
9 | |
10 | #ifndef XSPROTO |
11 | #define XSPROTO(name) void name(pTHX_ CV* cv) |
12 | #endif |
13 | |
14 | #ifndef gv_stashpvs |
70a91f79 |
15 | #define gv_stashpvs(x, y) Perl_gv_stashpvn(aTHX_ STR_WITH_LEN(x), y) |
035fd0c4 |
16 | #endif |
17 | |
1ea12c91 |
18 | /* FIXME |
1ea12c91 |
19 | * delegations and attribute helpers: |
20 | * |
21 | * typedef struct { |
de2f2e97 |
22 | * ATTR *attr; |
1ea12c91 |
23 | * pv *method; |
24 | * } delegation; |
25 | * |
26 | * typedef struct { |
de2f2e97 |
27 | * ATTR *attr; |
1ea12c91 |
28 | * I32 *type; // hash, array, whatever + vtable for operation |
29 | * } attributehelper; |
30 | */ |
31 | |
de2f2e97 |
32 | |
2cd9d2ba |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | /* These two functions attach magic with no behavior to an SV. |
39 | * |
40 | * The stashed value is reference counted, and is destroyed when it's parent |
41 | * object is destroyed. |
42 | * |
43 | * This is used to keep a reference the the meta attribute from a generated |
44 | * method, and to cache the C struct based wrapper attached to the meta |
45 | * instance. |
46 | */ |
47 | |
de2f2e97 |
48 | STATIC MGVTBL null_mg_vtbl = { |
49 | NULL, /* get */ |
50 | NULL, /* set */ |
51 | NULL, /* len */ |
52 | NULL, /* clear */ |
53 | NULL, /* free */ |
54 | #if MGf_COPY |
55 | NULL, /* copy */ |
56 | #endif /* MGf_COPY */ |
57 | #if MGf_DUP |
58 | NULL, /* dup */ |
59 | #endif /* MGf_DUP */ |
60 | #if MGf_LOCAL |
61 | NULL, /* local */ |
62 | #endif /* MGf_LOCAL */ |
63 | }; |
64 | |
f253044f |
65 | STATIC MAGIC *stash_in_mg (pTHX_ SV *sv, SV *obj) { |
66 | MAGIC *mg = sv_magicext(sv, obj, PERL_MAGIC_ext, &null_mg_vtbl, NULL, 0 ); |
67 | mg->mg_flags |= MGf_REFCOUNTED; |
68 | |
69 | return mg; |
70 | } |
71 | |
72 | STATIC SV *get_stashed_in_mg(pTHX_ SV *sv) { |
85ddc685 |
73 | MAGIC *mg; |
f253044f |
74 | |
75 | if (SvTYPE(sv) >= SVt_PVMG) { |
76 | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) { |
77 | if ((mg->mg_type == PERL_MAGIC_ext) && (mg->mg_virtual == &null_mg_vtbl)) |
78 | break; |
79 | } |
80 | if (mg) |
81 | return mg->mg_obj; |
82 | } |
83 | |
84 | return NULL; |
85 | } |
de2f2e97 |
86 | |
2cd9d2ba |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | /* The folloing data structures deal with type constraints */ |
96 | |
97 | /* this is an enum of the various kinds of constraint checking an attribute can |
98 | * have. |
99 | * |
100 | * tc_cv is the fallback behavior (simply applying the |
101 | * ->_compiled_type_constraint to the value, but other more optimal checks are |
102 | * implemented too. */ |
103 | |
104 | typedef enum { |
105 | tc_none = 0, /* no type checking */ |
106 | tc_type, /* a builtin type to be checked by check_sv_type */ |
107 | tc_stash, /* a stash for a class, implements TypeConstraint::Class by comparing SvSTASH and then invoking C<isa> if necessary */ |
108 | tc_cv, /* applies a code reference to the value and checks for truth */ |
109 | tc_fptr, /* apply a C function pointer */ |
5b264806 |
110 | tc_enum /* TODO check that the value is in an allowed set of values (strings) */ |
2cd9d2ba |
111 | } tc_kind; |
112 | |
113 | /* this is a enum of builtin type check. They are handled in a switch statement |
114 | * in check_sv_type */ |
de2f2e97 |
115 | typedef enum { |
4c6fbfb1 |
116 | Any, /* or item, or bool */ |
117 | Undef, |
118 | Defined, |
119 | Str, /* or value */ |
120 | Num, |
121 | Int, |
122 | GlobRef, /* SVt_PVGV */ |
123 | ArrayRef, /* SVt_PVAV */ |
124 | HashRef, /* SVt_PVHV */ |
125 | CodeRef, /* SVt_PVCV */ |
126 | Ref, |
127 | ScalarRef, |
2cd9d2ba |
128 | FileHandle, /* TODO */ |
4c6fbfb1 |
129 | RegexpRef, |
130 | Object, |
2cd9d2ba |
131 | Role, /* TODO */ |
5b264806 |
132 | ClassName |
de2f2e97 |
133 | } TC; |
134 | |
2cd9d2ba |
135 | /* auxillary pointer/int union used for constraint checking */ |
de2f2e97 |
136 | typedef union { |
2cd9d2ba |
137 | TC type; /* the builtin type number for tc_type */ |
138 | SV *sv; /* the cv for tc_cv, or the stash for tc_stash */ |
139 | OP *op; /* TODO not used */ |
140 | bool (*fptr)(pTHX_ SV *type_constraint, SV *sv); /* the function pointer for tc_fptr FIXME aux data? */ |
4c6fbfb1 |
141 | } TC_CHECK; |
142 | |
2cd9d2ba |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | /* The folloing data structures deal with type default value generation */ |
149 | |
150 | /* This is an enum for the various types of default value behaviors an |
151 | * attribute can have */ |
de2f2e97 |
152 | |
153 | typedef enum { |
2cd9d2ba |
154 | default_none = 0, /* no default value */ |
155 | default_normal, /* code reference or scalar */ |
156 | default_builder, /* builder method */ |
5b264806 |
157 | default_type /* TODO enumerated type optimization (will call newHV, newAV etc to avoid calling a code ref for these simple cases) */ |
de2f2e97 |
158 | } default_kind; |
159 | |
2cd9d2ba |
160 | typedef union { |
161 | SV *sv; /* The default value, or a code ref to generate one. If builder then this sv is applied as a method (stringified) */ |
162 | U32 type; /* TODO for default_type, should probably be one of SVt_PVAV/SVt_PVHV */ |
163 | } DEFAULT; |
164 | |
165 | |
166 | |
167 | |
168 | |
169 | |
170 | /* the ATTR struct contains all the meta data for a Moose::Meta::Attribute for |
171 | * a given meta instance |
172 | * |
173 | * flags determines the various behaviors |
174 | * |
175 | * This supports only one slot per attribute in the current implementation, but |
176 | * slot_sv could contain an array |
177 | * |
178 | * A list of XSUBs that rely on this attr struct are cross indexed in the cvs |
179 | * array, so that when the meta instance is destroyed the XSANY field will be |
180 | * cleared. This is done in delete_mi |
181 | * */ |
182 | |
1ea12c91 |
183 | typedef struct { |
2cd9d2ba |
184 | /* pointer to the MI this attribute is a part of the meta instance struct */ |
de2f2e97 |
185 | struct mi *mi; |
186 | |
187 | U32 flags; /* slot type, TC behavior, coerce, weaken, (no default | default, builder + lazy), auto_deref */ |
188 | |
189 | /* slot access fields */ |
2cd9d2ba |
190 | SV *slot_sv; /* value of the slot (currently always slot name) */ |
191 | U32 slot_u32; /* for optimized access (precomputed hash, possibly something else) */ |
de2f2e97 |
192 | |
f55aeea0 |
193 | SV *init_arg_sv; |
194 | U32 init_arg_u32; |
195 | |
de2f2e97 |
196 | DEFAULT def; /* cv, value or other, depending on flags */ |
197 | |
2cd9d2ba |
198 | TC_CHECK tc_check; /* see TC_CHECK*/ |
199 | SV *type_constraint; /* Moose::Meta::TypeConstraint object */ |
de2f2e97 |
200 | |
2cd9d2ba |
201 | CV *initializer; /* TODO */ |
202 | CV *trigger; /* TODO */ |
de2f2e97 |
203 | |
2cd9d2ba |
204 | SV *meta_attr; /* the Moose::Meta::Attribute */ |
205 | AV *cvs; /* an array of CVs which use this attr, see delete_mi */ |
de2f2e97 |
206 | } ATTR; |
207 | |
2cd9d2ba |
208 | /* the flags integer is mapped as follows |
209 | * instance misc reading writing |
de2f2e97 |
210 | * 00000000 00000000 00000000 00000000 |
2cd9d2ba |
211 | * writing |
7ce1a351 |
212 | * ^ trigger |
213 | * ^ weak |
45922f54 |
214 | * ^ tc.sv is refcounted |
de2f2e97 |
215 | * ^^^ tc_kind |
216 | * ^ coerce |
2cd9d2ba |
217 | * |
218 | * reading |
de2f2e97 |
219 | * ^^^ default_kind |
220 | * ^ lazy |
45922f54 |
221 | * ^ def.sv is refcounted |
2cd9d2ba |
222 | * |
223 | * misc |
224 | * ^ attr is required TODO |
225 | * |
226 | * flags having to do with the instance layout (TODO, only hash supported for now) |
de2f2e97 |
227 | * ^^^^^^^ if 0 then nothing special (just hash)? FIXME TBD |
228 | */ |
229 | |
230 | #define ATTR_INSTANCE_MASK 0xff000000 |
231 | #define ATTR_READING_MASK 0x0000ff00 |
232 | #define ATTR_WRITING_MASK 0x000000ff |
233 | |
234 | #define ATTR_MASK_TYPE 0x7 |
235 | |
236 | #define ATTR_MASK_DEFAULT 0x700 |
fe0194bf |
237 | #define ATTR_SHIFT_DEFAULT 8 |
de2f2e97 |
238 | |
239 | #define ATTR_LAZY 0x800 |
fe0194bf |
240 | #define ATTR_DEFREFCNT 0x1000 |
de2f2e97 |
241 | |
7ce1a351 |
242 | #define ATTR_COERCE 0x8 |
243 | #define ATTR_TCREFCNT 0x10 |
244 | #define ATTR_WEAK 0x20 |
245 | #define ATTR_TRIGGER 0x40 |
de2f2e97 |
246 | |
247 | #define ATTR_ISWEAK(attr) ( attr->flags & ATTR_WEAK ) |
248 | #define ATTR_ISLAZY(attr) ( attr->flags & ATTR_LAZY ) |
249 | #define ATTR_ISCOERCE(attr) ( attr->flags & ATTR_COERCE ) |
1ea12c91 |
250 | |
de2f2e97 |
251 | #define ATTR_TYPE(f) ( attr->flags & 0x7 ) |
252 | #define ATTR_DEFAULT(f) ( ( attr->flags & ATTR_MASK_DEFAULT ) >> ATTR_SHIFT_DEFAULT ) |
1ea12c91 |
253 | |
de2f2e97 |
254 | #define ATTR_DUMB_READER(attr) !ATTR_IS_LAZY(attr) |
255 | #define ATTR_DUMB_WRITER(attr) ( ( attr->flags & ATTR_WRITING_MASK ) == 0 ) |
256 | #define ATTR_DUMB_INSTANCE(attr) ( ( attr->flags & ATTR_INSTANCE_MASK ) == 0 ) |
1ea12c91 |
257 | |
de2f2e97 |
258 | |
259 | |
2cd9d2ba |
260 | /* This unused (TODO) vtable will implement the meta instance protocol in terms |
261 | * of function pointers to allow the XS accessors to be used with custom meta |
262 | * instances in the future. |
263 | * |
264 | * We'll need to define a default instance of this vtable that uses call_sv, |
265 | * too. */ |
266 | |
267 | /* FIXME define a vtable that does call_sv for fallback meta instance protocol */ |
de2f2e97 |
268 | typedef struct { |
269 | SV * (*get)(pTHX_ SV *self, ATTR *attr); |
270 | void (*set)(pTHX_ SV *self, ATTR *attr, SV *value); |
271 | bool * (*has)(pTHX_ SV *self, ATTR *attr); |
272 | SV * (*delete)(pTHX_ SV *self, ATTR *attr); |
273 | } instance_vtbl; |
274 | |
2cd9d2ba |
275 | /* TODO this table describes the instance layout of the object. Not yet |
276 | * implemented */ |
de2f2e97 |
277 | typedef enum { |
278 | hash = 0, |
279 | |
280 | /* these are not yet implemented */ |
281 | array, |
282 | fptr, |
283 | cv, |
5b264806 |
284 | judy |
de2f2e97 |
285 | } instance_types; |
286 | |
2cd9d2ba |
287 | |
288 | /* this struct models the meta instance *and* meta attributes simultaneously. |
289 | * It is a cache of the meta attribute behaviors for a given class or subclass |
290 | * and can be parametrized on that level |
291 | * |
292 | * |
293 | * An object pointing to this structure is kept in a refcounted magic inside |
294 | * the meta instance it corresponds to. On C<invalidate_meta_instance> the meta |
295 | * instance is destroyed, causing the proxy object to be destroyed, deleting |
296 | * this structure, clearing the XSANY of all dependent attribute methods. |
297 | * |
298 | * The next invocation of an attribute method will eventually call get_attr, |
299 | * which will call C<get_meta_instance> on the metaclass (recreating it in the |
300 | * Class::MOP level), and cache a new MI struct inside it. Subsequent |
301 | * invocations of get_attr will then search the MI for an ATTR matching the |
302 | * meta_attribute of the attribute method */ |
de2f2e97 |
303 | typedef struct mi { |
de2f2e97 |
304 | HV *stash; |
305 | |
306 | /* slot access method */ |
2cd9d2ba |
307 | instance_types type; /* TODO only hashes supported currently */ |
308 | instance_vtbl *vtbl; /* TODO */ |
de2f2e97 |
309 | |
310 | /* attr descriptors */ |
311 | I32 num_attrs; |
312 | ATTR *attrs; |
313 | } MI; |
314 | |
315 | |
4c6fbfb1 |
316 | |
317 | |
2cd9d2ba |
318 | |
319 | |
320 | |
321 | |
322 | /* these functions implement type constraint checking */ |
323 | |
324 | /* checks that the SV is a scalar ref */ |
4c6fbfb1 |
325 | STATIC bool check_is_scalar_ref(SV *sv) { |
326 | if( SvROK(sv) ) { |
327 | switch (SvTYPE(SvRV(sv))) { |
328 | case SVt_IV: |
329 | case SVt_NV: |
330 | case SVt_PV: |
331 | case SVt_NULL: |
332 | return 1; |
333 | break; |
334 | default: |
335 | return 0; |
336 | } |
337 | } |
338 | return 0; |
339 | } |
340 | |
2cd9d2ba |
341 | /* checks that the SV is a ref to a certain SvTYPE, where type is in the table |
342 | * above */ |
4c6fbfb1 |
343 | STATIC bool check_reftype(TC type, SV *sv) { |
344 | int svt; |
345 | |
346 | if ( !SvROK(sv) ) |
347 | return 0; |
348 | |
349 | switch (type) { |
350 | case GlobRef: |
351 | svt = SVt_PVGV; |
352 | break; |
353 | case ArrayRef: |
354 | svt = SVt_PVAV; |
355 | break; |
356 | case HashRef: |
357 | svt = SVt_PVHV; |
358 | break; |
359 | case CodeRef: |
360 | svt = SVt_PVCV; |
361 | break; |
85ddc685 |
362 | default: |
363 | croak("not a reftype %d\n", type); |
4c6fbfb1 |
364 | } |
365 | |
160f9ca7 |
366 | return SvTYPE(SvRV(sv)) == svt; |
4c6fbfb1 |
367 | } |
368 | |
2cd9d2ba |
369 | /* checks whether an SV is of a certain class |
370 | * SvSTASH is first compared by pointer for efficiency */ |
7ce1a351 |
371 | STATIC bool check_sv_class(pTHX_ HV *stash, SV *sv) { |
4c6fbfb1 |
372 | dSP; |
373 | bool ret; |
7ce1a351 |
374 | SV *rv; |
4c6fbfb1 |
375 | |
376 | if (!sv) |
377 | return 0; |
378 | SvGETMAGIC(sv); |
379 | if (!SvROK(sv)) |
380 | return 0; |
7ce1a351 |
381 | rv = (SV*)SvRV(sv); |
382 | if (!SvOBJECT(rv)) |
4c6fbfb1 |
383 | return 0; |
7ce1a351 |
384 | if (SvSTASH(rv) == stash) |
4c6fbfb1 |
385 | return 1; |
386 | |
387 | ENTER; |
388 | SAVETMPS; |
389 | PUSHMARK(SP); |
390 | XPUSHs(sv); |
7ce1a351 |
391 | XPUSHs(sv_2mortal(newSVpv(HvNAME_get(stash), 0))); |
4c6fbfb1 |
392 | PUTBACK; |
393 | |
394 | call_method("isa", G_SCALAR); |
395 | |
396 | SPAGAIN; |
397 | ret = SvTRUE(TOPs); |
398 | |
399 | FREETMPS; |
400 | LEAVE; |
401 | |
402 | return ret; |
403 | } |
404 | |
2cd9d2ba |
405 | /* checks whether SV of of a known simple type. Most of the non parametrized |
406 | * Moose core types are implemented here */ |
4c6fbfb1 |
407 | STATIC bool check_sv_type (TC type, SV *sv) { |
408 | if (!sv) |
409 | return 0; |
160f9ca7 |
410 | |
4c6fbfb1 |
411 | switch (type) { |
412 | case Any: |
413 | return 1; |
414 | break; |
415 | case Undef: |
416 | return !SvOK(sv); |
417 | break; |
418 | case Defined: |
419 | return SvOK(sv); |
420 | break; |
421 | case Str: |
422 | return (SvOK(sv) && !SvROK(sv)); |
160f9ca7 |
423 | case Num: |
424 | #if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION <5) |
425 | if (!SvPOK(sv) && !SvPOKp(sv)) |
426 | return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK); |
427 | else |
428 | #endif |
429 | return looks_like_number(sv); |
430 | break; |
431 | case Int: |
432 | if ( SvIOK(sv) ) { |
433 | return 1; |
434 | } else if ( SvPOK(sv) ) { |
0be3b17f |
435 | /* FIXME i really don't like this */ |
160f9ca7 |
436 | int i; |
437 | STRLEN len; |
438 | char *pv = SvPV(sv, len); |
439 | char *end = pv + len; |
0be3b17f |
440 | char *tail = end; |
160f9ca7 |
441 | |
442 | errno = 0; |
0be3b17f |
443 | i = strtol(pv, &tail, 0); |
444 | |
445 | if ( errno ) return 0; |
446 | |
447 | while ( tail != end ) { |
448 | if ( !isspace(*tail++) ) return 0; |
449 | } |
450 | |
451 | return 1; |
160f9ca7 |
452 | } |
453 | return 0; |
454 | break; |
4c6fbfb1 |
455 | case Ref: |
456 | return SvROK(sv); |
457 | break; |
458 | case ScalarRef: |
459 | return check_is_scalar_ref(sv); |
460 | break; |
461 | case ArrayRef: |
462 | case HashRef: |
463 | case CodeRef: |
464 | case GlobRef: |
465 | return check_reftype(type, sv); |
466 | break; |
3c63e75d |
467 | case RegexpRef: |
4c6fbfb1 |
468 | case Object: |
9fad6c09 |
469 | if ( sv_isobject(sv) ) { |
470 | char *name = HvNAME_get(SvSTASH(SvRV(sv))); |
471 | bool is_regexp = strEQ("Regexp", name); |
472 | return ( type != RegexpRef ? is_regexp : !is_regexp ); |
473 | } |
474 | return 0; |
4c6fbfb1 |
475 | break; |
160f9ca7 |
476 | case ClassName: |
7ce1a351 |
477 | if ( SvOK(sv) && !SvROK(sv) ) { |
160f9ca7 |
478 | STRLEN len; |
479 | char *pv; |
480 | pv = SvPV(sv, len); |
481 | return ( gv_stashpvn(pv, len, 0) != NULL ); |
160f9ca7 |
482 | } |
7ce1a351 |
483 | return 0; |
484 | break; |
4c6fbfb1 |
485 | case FileHandle: |
486 | croak("todo"); |
487 | break; |
488 | default: |
489 | croak("todo"); |
490 | } |
491 | |
492 | return 0; |
493 | } |
494 | |
2cd9d2ba |
495 | /* invoke a CV on an SV and return SvTRUE of the result */ |
45922f54 |
496 | STATIC bool check_sv_cv (pTHX_ SV *cv, SV *sv) { |
497 | bool ret; |
498 | dSP; |
499 | |
500 | ENTER; |
501 | SAVETMPS; |
502 | PUSHMARK(SP); |
503 | XPUSHs(sv); |
504 | PUTBACK; |
505 | |
506 | call_sv(cv, G_SCALAR); |
507 | |
508 | SPAGAIN; |
509 | ret = SvTRUE(POPs); |
510 | |
511 | PUTBACK; |
512 | FREETMPS; |
513 | LEAVE; |
514 | |
515 | return ret; |
516 | } |
517 | |
2cd9d2ba |
518 | /* checks the type constraint for an SV based on the type constraint kind */ |
160f9ca7 |
519 | STATIC bool check_type_constraint(pTHX_ tc_kind kind, TC_CHECK tc_check, SV *type_constraint, SV *sv) { |
4c6fbfb1 |
520 | switch (kind) { |
521 | case tc_none: |
522 | return 1; |
523 | break; |
524 | case tc_type: |
525 | return check_sv_type(tc_check.type, sv); |
526 | break; |
527 | case tc_stash: |
7ce1a351 |
528 | return check_sv_class(aTHX_ (HV *)tc_check.sv, sv); |
4c6fbfb1 |
529 | break; |
4c6fbfb1 |
530 | case tc_fptr: |
531 | return tc_check.fptr(aTHX_ type_constraint, sv); |
532 | break; |
533 | case tc_cv: |
45922f54 |
534 | return check_sv_cv(aTHX_ tc_check.sv, sv); |
535 | break; |
85ddc685 |
536 | case tc_enum: |
537 | croak("todo\n"); |
538 | break; |
4c6fbfb1 |
539 | } |
540 | |
541 | croak("todo"); |
542 | return 0; |
543 | } |
544 | |
545 | |
2cd9d2ba |
546 | /* end of type constraint checking functions */ |
547 | |
548 | |
549 | |
550 | |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | /* Initialize the ATTR structure using positional arguments from Perl space. */ |
557 | |
160f9ca7 |
558 | STATIC void init_attr (MI *mi, ATTR *attr, AV *desc) { |
559 | U32 flags = 0; |
f55aeea0 |
560 | U32 slot_hash, init_arg_hash; |
561 | STRLEN slot_len, init_arg_len; |
562 | char *slot_pv, *init_arg_pv; |
160f9ca7 |
563 | I32 ix = av_len(desc); |
564 | SV **params = AvARRAY(desc); |
565 | SV *tc; |
f55aeea0 |
566 | SV *slot_sv; |
567 | SV *init_arg_sv; |
de2f2e97 |
568 | |
569 | attr->mi = mi; |
570 | |
f55aeea0 |
571 | if ( ix != 13 ) |
9fad6c09 |
572 | croak("wrong number of args (%d != 14)", (int)ix + 1); |
de2f2e97 |
573 | |
160f9ca7 |
574 | for ( ; ix >= 0; ix-- ) { |
575 | if ( !params[ix] || params[ix] == &PL_sv_undef ) |
576 | croak("bad params"); |
577 | } |
578 | |
2cd9d2ba |
579 | |
580 | |
581 | /* handle attribute slot array */ |
582 | |
160f9ca7 |
583 | if ( !SvROK(params[1]) || SvTYPE(SvRV(params[1])) != SVt_PVAV ) |
584 | croak("slots is not an array"); |
de2f2e97 |
585 | |
160f9ca7 |
586 | if ( av_len((AV *)SvRV(params[1])) != 0 ) |
587 | croak("Only unary slots are supported at the moment"); |
1ea12c91 |
588 | |
160f9ca7 |
589 | /* calculate a hash from the slot */ |
590 | /* FIXME arrays etc should also be supported */ |
f55aeea0 |
591 | slot_sv = *av_fetch((AV *)SvRV(params[1]), 0, 0); |
592 | slot_pv = SvPV(slot_sv, slot_len); |
593 | PERL_HASH(slot_hash, slot_pv, slot_len); |
1ea12c91 |
594 | |
de2f2e97 |
595 | |
f55aeea0 |
596 | init_arg_sv = params[13]; |
597 | init_arg_pv = SvPV(init_arg_sv, init_arg_len); |
598 | PERL_HASH(init_arg_hash, init_arg_pv, init_arg_len); |
2cd9d2ba |
599 | |
600 | |
601 | /* FIXME better organize these, positionals suck */ |
160f9ca7 |
602 | if ( SvTRUE(params[2]) ) |
603 | flags |= ATTR_WEAK; |
604 | |
605 | if ( SvTRUE(params[3]) ) |
606 | flags |= ATTR_COERCE; |
de2f2e97 |
607 | |
160f9ca7 |
608 | if ( SvTRUE(params[4]) ) |
609 | flags |= ATTR_LAZY; |
de2f2e97 |
610 | |
2cd9d2ba |
611 | |
612 | |
613 | /* type constraint data */ |
614 | |
160f9ca7 |
615 | tc = params[5]; |
de2f2e97 |
616 | |
160f9ca7 |
617 | if ( SvOK(tc) ) { |
618 | int tc_kind = SvIV(params[6]); |
619 | SV *data = params[7]; |
620 | |
621 | switch (tc_kind) { |
160f9ca7 |
622 | case tc_type: |
623 | attr->tc_check.type = SvIV(data); |
624 | break; |
7ce1a351 |
625 | case tc_stash: |
626 | flags |= ATTR_TCREFCNT; |
627 | attr->tc_check.sv = (SV *)gv_stashsv(data, 0); |
628 | break; |
160f9ca7 |
629 | case tc_cv: |
7ce1a351 |
630 | flags |= ATTR_TCREFCNT; |
631 | attr->tc_check.sv = SvRV(data); |
632 | if ( SvTYPE(attr->tc_check.sv) != SVt_PVCV ) |
160f9ca7 |
633 | croak("compiled type constraint is not a coderef"); |
634 | break; |
635 | default: |
636 | croak("todo"); |
637 | } |
638 | |
639 | flags |= tc_kind; |
640 | } |
641 | |
2cd9d2ba |
642 | |
643 | |
644 | /* default/builder data */ |
fe0194bf |
645 | |
646 | if ( SvTRUE(params[10]) ) { /* has default */ |
647 | SV *sv = params[11]; |
648 | |
649 | if ( SvROK(sv) ) { |
650 | attr->def.sv = SvRV(sv); |
651 | if ( SvTYPE(attr->def.sv) != SVt_PVCV ) |
652 | croak("compiled type constraint is not a coderef"); |
653 | } else { |
654 | attr->def.sv = newSVsv(sv); |
655 | sv_2mortal(attr->def.sv); /* in case of error soon, we refcnt inc it later after we're done checking params */ |
656 | } |
657 | |
658 | flags |= ( ATTR_DEFREFCNT | ( default_normal << ATTR_SHIFT_DEFAULT ) ); |
659 | } else if ( SvOK(params[12]) ) { /* builder */ |
660 | attr->def.sv = newSVsv(params[12]); |
661 | flags |= ( ATTR_DEFREFCNT | ( default_builder << ATTR_SHIFT_DEFAULT ) ); |
662 | } |
663 | |
2cd9d2ba |
664 | |
160f9ca7 |
665 | |
666 | attr->trigger = SvROK(params[6]) ? (CV *)SvRV(params[6]) : NULL; |
667 | if ( attr->trigger && SvTYPE(attr->trigger) != SVt_PVCV ) |
668 | croak("trigger is not a coderef"); |
669 | |
670 | attr->initializer = SvROK(params[7]) ? (CV *)SvRV(params[7]) : NULL; |
671 | if ( attr->initializer && SvTYPE(attr->initializer) != SVt_PVCV ) |
672 | croak("initializer is not a coderef"); |
673 | |
2cd9d2ba |
674 | |
675 | |
676 | /* now that we're done preparing/checking args and shit, so we finalize the |
677 | * attr, increasing refcounts for any referenced data, and creating the CV |
678 | * array */ |
679 | |
680 | attr->flags = flags; |
681 | |
682 | /* copy the outer ref SV */ |
160f9ca7 |
683 | attr->meta_attr = newSVsv(params[0]); |
684 | attr->type_constraint = newSVsv(tc); |
2cd9d2ba |
685 | |
686 | /* increase the refcount for auxillary structures */ |
7ce1a351 |
687 | SvREFCNT_inc(attr->trigger); |
688 | SvREFCNT_inc(attr->initializer); |
9be0171f |
689 | if ( flags & ATTR_TCREFCNT ) SvREFCNT_inc(attr->tc_check.sv); |
fe0194bf |
690 | if ( flags & ATTR_DEFREFCNT ) SvREFCNT_inc(attr->def.sv); |
160f9ca7 |
691 | |
f55aeea0 |
692 | attr->slot_sv = newSVpvn_share(slot_pv, slot_len, slot_hash); |
693 | attr->slot_u32 = slot_hash; |
694 | |
695 | attr->init_arg_sv = newSVpvn_share(init_arg_pv, init_arg_len, init_arg_hash); |
696 | attr->init_arg_u32 = init_arg_hash; |
160f9ca7 |
697 | |
de2f2e97 |
698 | /* cross refs to CVs which use this struct */ |
699 | attr->cvs = newAV(); |
700 | } |
701 | |
2cd9d2ba |
702 | STATIC SV *new_mi (pTHX_ HV *stash, AV *attrs) { |
703 | HV *mi_stash = gv_stashpvs("Moose::XS::Meta::Instance",0); |
704 | SV *sv_ptr = newSViv(0); |
705 | SV *obj = sv_2mortal(sv_bless(newRV_noinc(sv_ptr), mi_stash)); |
de2f2e97 |
706 | MI *mi; |
2cd9d2ba |
707 | const I32 num_attrs = av_len(attrs) + 1; |
de2f2e97 |
708 | |
687453c6 |
709 | Newxz(mi, 1, MI); |
2cd9d2ba |
710 | |
711 | /* set the pointer now, if we have any initialization errors it'll get |
712 | * cleaned up because obj is mortal */ |
713 | sv_setiv(sv_ptr, PTR2IV(mi)); |
714 | |
687453c6 |
715 | Newxz(mi->attrs, num_attrs, ATTR); |
2cd9d2ba |
716 | |
de2f2e97 |
717 | SvREFCNT_inc_simple(stash); |
718 | mi->stash = stash; |
719 | |
de2f2e97 |
720 | mi->type = 0; /* nothing else implemented yet */ |
721 | |
722 | /* initialize attributes */ |
2cd9d2ba |
723 | for ( ; mi->num_attrs < num_attrs; mi->num_attrs++ ) { |
724 | SV **desc = av_fetch(attrs, mi->num_attrs, 0); |
de2f2e97 |
725 | |
160f9ca7 |
726 | if ( !desc || !*desc || !SvROK(*desc) || !(SvTYPE(SvRV(*desc)) == SVt_PVAV) ) { |
de2f2e97 |
727 | croak("Attribute descriptor has to be a hash reference"); |
f253044f |
728 | } |
de2f2e97 |
729 | |
2cd9d2ba |
730 | init_attr(mi, &mi->attrs[mi->num_attrs], (AV *)SvRV(*desc)); |
de2f2e97 |
731 | } |
732 | |
2cd9d2ba |
733 | return obj; |
734 | } |
735 | |
736 | STATIC void delete_attr (pTHX_ ATTR *attr) { |
737 | I32 i; |
738 | SV **cvs = AvARRAY(attr->cvs); |
739 | |
740 | /* remove the pointers to this ATTR struct from all the the dependent CVs */ |
741 | for ( i = av_len(attr->cvs); i >= 0; i-- ) { |
742 | CV *cv = (CV *)cvs[i]; |
743 | XSANY.any_i32 = 0; |
744 | } |
745 | |
746 | SvREFCNT_dec(attr->cvs); |
747 | SvREFCNT_dec(attr->slot_sv); |
748 | SvREFCNT_dec(attr->type_constraint); |
749 | if ( attr->flags & ATTR_TCREFCNT ) SvREFCNT_dec(attr->tc_check.sv); |
750 | if ( attr->flags & ATTR_DEFREFCNT ) SvREFCNT_dec(attr->def.sv); |
751 | SvREFCNT_dec(attr->initializer); |
752 | SvREFCNT_dec(attr->trigger); |
753 | SvREFCNT_dec(attr->meta_attr); |
de2f2e97 |
754 | } |
755 | |
7ce1a351 |
756 | STATIC void delete_mi (pTHX_ MI *mi) { |
2cd9d2ba |
757 | SvREFCNT_dec(mi->stash); |
7ce1a351 |
758 | |
2cd9d2ba |
759 | while ( mi->num_attrs--) { |
760 | ATTR *attr = &mi->attrs[mi->num_attrs]; |
761 | delete_attr(aTHX_ attr); |
7ce1a351 |
762 | } |
763 | |
2cd9d2ba |
764 | if ( mi->attrs ) Safefree(mi->attrs); |
7ce1a351 |
765 | Safefree(mi); |
766 | } |
767 | |
de2f2e97 |
768 | |
2cd9d2ba |
769 | |
770 | |
771 | /* these functions call Perl-space for MOP methods, helpers etc */ |
772 | |
773 | |
774 | /* wow, so much code for the equivalent of |
775 | * $attr->associated_class->get_meta_instance */ |
f253044f |
776 | STATIC SV *attr_to_meta_instance(pTHX_ SV *meta_attr) { |
777 | dSP; |
778 | I32 count; |
779 | SV *mi; |
780 | |
781 | if ( !meta_attr ) |
782 | croak("No attr found in magic!"); |
783 | |
784 | ENTER; |
785 | SAVETMPS; |
786 | PUSHMARK(SP); |
2cd9d2ba |
787 | |
f253044f |
788 | XPUSHs(meta_attr); |
2cd9d2ba |
789 | |
f253044f |
790 | PUTBACK; |
791 | count = call_pv("Moose::XS::attr_to_meta_instance", G_SCALAR); |
792 | |
793 | if ( count != 1 ) |
9fad6c09 |
794 | croak("attr_to_meta_instance borked (%d args returned, expecting 1)", (int)count); |
f253044f |
795 | |
796 | SPAGAIN; |
797 | mi = POPs; |
798 | |
799 | SvREFCNT_inc(mi); |
800 | |
801 | PUTBACK; |
802 | FREETMPS; |
803 | LEAVE; |
804 | |
fe0194bf |
805 | return sv_2mortal(mi); |
f253044f |
806 | } |
807 | |
2cd9d2ba |
808 | /* gets a class and an array of attr parameters */ |
f253044f |
809 | STATIC SV *perl_mi_to_c_mi(pTHX_ SV *perl_mi) { |
810 | dSP; |
811 | I32 count; |
2cd9d2ba |
812 | SV *mi; |
f253044f |
813 | SV *class; |
814 | SV *attrs; |
815 | HV *stash; |
816 | |
817 | ENTER; |
818 | SAVETMPS; |
819 | PUSHMARK(SP); |
2cd9d2ba |
820 | |
f253044f |
821 | XPUSHs(perl_mi); |
2cd9d2ba |
822 | |
f253044f |
823 | PUTBACK; |
824 | count = call_pv("Moose::XS::meta_instance_to_attr_descs", G_ARRAY); |
825 | |
826 | if ( count != 2 ) |
9fad6c09 |
827 | croak("meta_instance_to_attr_descs borked (%d args returned, expecting 2)", (int)count); |
f253044f |
828 | |
829 | SPAGAIN; |
830 | attrs = POPs; |
831 | class = POPs; |
832 | |
833 | PUTBACK; |
834 | |
835 | stash = gv_stashsv(class, 0); |
836 | |
837 | mi = new_mi(aTHX_ stash, (AV *)SvRV(attrs)); |
2cd9d2ba |
838 | SvREFCNT_inc(mi); |
f253044f |
839 | |
840 | FREETMPS; |
841 | LEAVE; |
842 | |
2cd9d2ba |
843 | return sv_2mortal(mi); |
f253044f |
844 | } |
845 | |
2cd9d2ba |
846 | |
847 | |
848 | /* locate an ATTR for a MOP level attribute inside an MI */ |
849 | STATIC ATTR *mi_find_attr(SV *mi_obj, SV *meta_attr) { |
f253044f |
850 | I32 ix; |
2cd9d2ba |
851 | MI *mi = INT2PTR(MI *, SvIV(SvRV(mi_obj))); |
f253044f |
852 | |
035fd0c4 |
853 | for ( ix = 0; ix < mi->num_attrs; ix++ ) { |
f253044f |
854 | if ( SvRV(mi->attrs[ix].meta_attr) == SvRV(meta_attr) ) { |
855 | return &mi->attrs[ix]; |
de2f2e97 |
856 | } |
de2f2e97 |
857 | } |
858 | |
9fad6c09 |
859 | croak("Attr %x not found in meta instance of %s", (unsigned int)PTR2UV(SvRV(meta_attr)) /* SvPV_force_nomg(sv_2mortal(newSVsv(meta_attr))) */, HvNAME_get(mi->stash) ); |
de2f2e97 |
860 | return NULL; |
861 | } |
862 | |
2cd9d2ba |
863 | /* returns the ATTR for a CV: |
864 | * |
865 | * 1. get the Moose::Meta::Attribute using get_stashed_in_mg from the CV itself |
866 | * 2. get the meta instance by calling $attr->associated_class->get_meta_instance |
867 | * 3. get the MI by using get_stashed_in_mg from the meta instance, creating it if necessary |
868 | * 4. search for the appropriate ATTR in the MI using mi_find_attr |
869 | */ |
de2f2e97 |
870 | STATIC ATTR *get_attr(pTHX_ CV *cv) { |
f253044f |
871 | SV *meta_attr = get_stashed_in_mg(aTHX_ (SV *)cv); |
872 | SV *perl_mi = attr_to_meta_instance(aTHX_ meta_attr); |
2cd9d2ba |
873 | SV *mi_obj = get_stashed_in_mg(aTHX_ SvRV(perl_mi)); |
de2f2e97 |
874 | |
2cd9d2ba |
875 | if (!mi_obj) { |
876 | mi_obj = perl_mi_to_c_mi(aTHX_ perl_mi); |
877 | stash_in_mg(aTHX_ SvRV(perl_mi), mi_obj); |
f253044f |
878 | } |
879 | |
2cd9d2ba |
880 | return mi_find_attr(mi_obj, meta_attr); |
1ea12c91 |
881 | } |
882 | |
2cd9d2ba |
883 | /* Cache a pointer to the appropriate ATTR in the XSANY of the CV, using |
884 | * get_attr */ |
de2f2e97 |
885 | STATIC ATTR *define_attr (pTHX_ CV *cv) { |
886 | ATTR *attr = get_attr(aTHX_ cv); |
887 | assert(attr); |
888 | |
889 | XSANY.any_i32 = PTR2IV(attr); |
f253044f |
890 | |
7ce1a351 |
891 | SvREFCNT_inc(cv); |
f253044f |
892 | av_push( attr->cvs, (SV *)cv ); |
de2f2e97 |
893 | |
894 | return attr; |
895 | } |
896 | |
2cd9d2ba |
897 | |
898 | |
899 | |
900 | |
901 | |
902 | |
de2f2e97 |
903 | STATIC void weaken(pTHX_ SV *sv) { |
1ea12c91 |
904 | #ifdef SvWEAKREF |
de2f2e97 |
905 | sv_rvweaken(sv); /* FIXME i think this might warn when weakening an already weak ref */ |
1ea12c91 |
906 | #else |
907 | croak("weak references are not implemented in this release of perl"); |
908 | #endif |
909 | } |
910 | |
911 | |
2cd9d2ba |
912 | |
913 | |
914 | |
915 | |
1ea12c91 |
916 | /* meta instance protocol */ |
917 | |
de2f2e97 |
918 | STATIC SV *get_slot_value(pTHX_ SV *self, ATTR *attr) { |
1ea12c91 |
919 | HE *he; |
920 | |
921 | assert(self); |
922 | assert(SvROK(self)); |
923 | assert(SvTYPE(SvRV(self)) == SVt_PVHV); |
924 | |
de2f2e97 |
925 | assert( ATTR_DUMB_INSTANCE(attr) ); |
926 | |
927 | if ((he = hv_fetch_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32))) |
1ea12c91 |
928 | return HeVAL(he); |
929 | else |
930 | return NULL; |
931 | } |
932 | |
de2f2e97 |
933 | STATIC void set_slot_value(pTHX_ SV *self, ATTR *attr, SV *value) { |
1ea12c91 |
934 | HE *he; |
4c6fbfb1 |
935 | SV *copy; |
1ea12c91 |
936 | |
937 | assert(self); |
938 | assert(SvROK(self)); |
939 | assert(SvTYPE(SvRV(self)) == SVt_PVHV); |
940 | |
de2f2e97 |
941 | assert( ATTR_DUMB_INSTANCE(attr) ); |
942 | |
4c6fbfb1 |
943 | copy = newSVsv(value); |
944 | |
945 | he = hv_store_ent((HV*)SvRV(self), attr->slot_sv, copy, attr->slot_u32); |
1ea12c91 |
946 | |
1ea12c91 |
947 | if (he != NULL) { |
de2f2e97 |
948 | if ( ATTR_ISWEAK(attr) ) |
4c6fbfb1 |
949 | weaken(aTHX_ HeVAL(he)); |
1ea12c91 |
950 | } else { |
4c6fbfb1 |
951 | SvREFCNT_dec(copy); |
1ea12c91 |
952 | croak("Hash store failed."); |
953 | } |
954 | } |
955 | |
de2f2e97 |
956 | STATIC bool has_slot_value(pTHX_ SV *self, ATTR *attr) { |
1ea12c91 |
957 | assert(self); |
958 | assert(SvROK(self)); |
959 | assert(SvTYPE(SvRV(self)) == SVt_PVHV); |
960 | |
de2f2e97 |
961 | assert( ATTR_DUMB_INSTANCE(attr) ); |
962 | |
963 | return hv_exists_ent((HV *)SvRV(self), attr->slot_sv, attr->slot_u32); |
964 | } |
965 | |
966 | STATIC SV *deinitialize_slot(pTHX_ SV *self, ATTR *attr) { |
967 | assert(self); |
968 | assert(SvROK(self)); |
969 | assert(SvTYPE(SvRV(self)) == SVt_PVHV); |
970 | |
971 | assert( ATTR_DUMB_INSTANCE(attr) ); |
972 | |
973 | return hv_delete_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32); |
1ea12c91 |
974 | } |
975 | |
af0842cb |
976 | STATIC SV *create_instance(pTHX_ MI *mi) { |
e315cce6 |
977 | return sv_bless(sv_2mortal(newRV_noinc((SV *)newHV())), mi->stash); |
af0842cb |
978 | } |
fe0194bf |
979 | |
fe0194bf |
980 | |
fe0194bf |
981 | |
fe0194bf |
982 | |
2cd9d2ba |
983 | /* Shared functionality for readers/writers/accessors, this roughly corresponds |
984 | * to the methods of Moose::Meta::Attribute on the instance |
985 | * (get_value/set_value, default value handling, etc) */ |
fe0194bf |
986 | |
2cd9d2ba |
987 | STATIC void attr_set_value(pTHX_ SV *self, ATTR *attr, SV *value); |
fe0194bf |
988 | |
2cd9d2ba |
989 | STATIC SV *call_builder (pTHX_ SV *self, ATTR *attr) { |
990 | SV *sv; |
991 | dSP; |
fe0194bf |
992 | |
2cd9d2ba |
993 | ENTER; |
994 | SAVETMPS; |
995 | PUSHMARK(SP); |
996 | |
997 | XPUSHs(self); |
998 | |
999 | /* we invoke the builder as a stringified method. This will not work for |
1000 | * $obj->$coderef etc, for that we need to use 'default' */ |
1001 | PUTBACK; |
1002 | call_method(SvPV_nolen(attr->def.sv), G_SCALAR); |
1003 | SPAGAIN; |
1004 | |
1005 | /* the value is a mortal with a refcount of 1, so we need to keep it around */ |
1006 | sv = POPs; |
1007 | SvREFCNT_inc(sv); |
1008 | |
1009 | PUTBACK; |
1010 | FREETMPS; |
1011 | LEAVE; |
1012 | |
1013 | return sv_2mortal(sv); |
1014 | } |
1015 | |
1016 | |
1017 | /* Returns an SV for the default value. Should be copied by the caller because |
1018 | * it's either an alias for a simple value, or a mortal from cv/builder */ |
1019 | STATIC SV *get_default(pTHX_ SV *self, ATTR *attr) { |
1020 | switch ( ATTR_DEFAULT(attr) ) { |
1021 | case default_none: |
1022 | return NULL; |
1023 | break; |
1024 | case default_builder: |
1025 | return call_builder(aTHX_ self, attr); |
fe0194bf |
1026 | break; |
1027 | case default_normal: |
1028 | if ( SvROK(attr->def.sv) ) { |
1029 | printf("CV default\n"); |
2cd9d2ba |
1030 | croak("todo"); |
fe0194bf |
1031 | } else { |
1032 | printf("simple value\n"); |
1033 | return attr->def.sv; /* will be copied by set for lazy, and by reader for both cases */ |
1034 | } |
1035 | break; |
fe0194bf |
1036 | case default_type: |
1037 | croak("todo"); |
1038 | break; |
1039 | } |
1040 | |
1041 | return NULL; |
1042 | } |
1043 | |
2cd9d2ba |
1044 | /* $attr->get_value($self), will vivify lazy values if needed |
1045 | * returns an alias to the sv that is copied in the reader/writer/accessor code |
1046 | * */ |
1047 | STATIC SV *attr_get_value(pTHX_ SV *self, ATTR *attr) { |
fe0194bf |
1048 | SV *value = get_slot_value(aTHX_ self, attr); |
1049 | |
1050 | if ( value ) { |
1051 | return value; |
1052 | } else if ( ATTR_ISLAZY(attr) ) { |
1053 | value = get_default(aTHX_ self, attr); |
2cd9d2ba |
1054 | attr_set_value(aTHX_ self, attr, value); |
fe0194bf |
1055 | return value; |
1056 | } |
1057 | |
1058 | return NULL; |
160f9ca7 |
1059 | } |
1060 | |
2cd9d2ba |
1061 | /* $attr->set_value($self) */ |
1062 | STATIC void attr_set_value(pTHX_ SV *self, ATTR *attr, SV *value) { |
fe0194bf |
1063 | if ( ATTR_TYPE(attr) ) { |
1064 | if ( !check_type_constraint(aTHX_ ATTR_TYPE(attr), attr->tc_check, attr->type_constraint, value) ) |
160f9ca7 |
1065 | croak("Bad param"); |
1066 | } |
1067 | |
1068 | set_slot_value(aTHX_ self, attr, value); |
1069 | } |
1ea12c91 |
1070 | |
2cd9d2ba |
1071 | |
1072 | |
1073 | |
1074 | |
1075 | |
1076 | |
1077 | /* Perl-space level functionality |
1078 | * |
1079 | * These subs are installed by new_sub's various aliases as the bodies of the |
1080 | * new XSUBs |
1081 | * */ |
1082 | |
1083 | |
1084 | |
1085 | /* This macro is used in the XS subs to set up the 'attr' variable. |
1086 | * |
1087 | * if XSANY is NULL then define_attr is called on the CV, to set the pointer |
1088 | * to the ATTR struct. |
1089 | * */ |
1090 | #define dATTR ATTR *attr = (XSANY.any_i32 ? INT2PTR(ATTR *, (XSANY.any_i32)) : define_attr(aTHX_ cv)) |
1091 | |
1ea12c91 |
1092 | |
24a7a8c5 |
1093 | STATIC XS(reader); |
1094 | STATIC XS(reader) |
1ea12c91 |
1095 | { |
1096 | #ifdef dVAR |
1097 | dVAR; |
1098 | #endif |
1099 | dXSARGS; |
de2f2e97 |
1100 | dATTR; |
1ea12c91 |
1101 | SV *value; |
1102 | |
1103 | if (items != 1) |
1104 | Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self"); |
1105 | |
1106 | SP -= items; |
1107 | |
2cd9d2ba |
1108 | value = attr_get_value(aTHX_ ST(0), attr); |
1ea12c91 |
1109 | |
1110 | if (value) { |
1111 | ST(0) = sv_mortalcopy(value); /* mortalcopy because $_ .= "blah" for $foo->bar */ |
1112 | XSRETURN(1); |
1113 | } else { |
1114 | XSRETURN_UNDEF; |
1115 | } |
1116 | } |
1117 | |
24a7a8c5 |
1118 | STATIC XS(writer); |
1119 | STATIC XS(writer) |
1ea12c91 |
1120 | { |
1121 | #ifdef dVAR |
1122 | dVAR; |
1123 | #endif |
1124 | dXSARGS; |
de2f2e97 |
1125 | dATTR; |
1ea12c91 |
1126 | |
1127 | if (items != 2) |
1128 | Perl_croak(aTHX_ "Usage: %s(%s, %s)", GvNAME(CvGV(cv)), "self", "value"); |
1129 | |
1130 | SP -= items; |
1131 | |
2cd9d2ba |
1132 | attr_set_value(aTHX_ ST(0), attr, ST(1)); |
1ea12c91 |
1133 | |
1134 | ST(0) = ST(1); /* return value */ |
1135 | XSRETURN(1); |
1136 | } |
1137 | |
de2f2e97 |
1138 | STATIC XS(accessor); |
1139 | STATIC XS(accessor) |
1ea12c91 |
1140 | { |
1141 | #ifdef dVAR |
1142 | dVAR; |
1143 | #endif |
1144 | dXSARGS; |
de2f2e97 |
1145 | dATTR; |
1ea12c91 |
1146 | |
1147 | if (items < 1) |
1148 | Perl_croak(aTHX_ "Usage: %s(%s, [ %s ])", GvNAME(CvGV(cv)), "self", "value"); |
1149 | |
1150 | SP -= items; |
1151 | |
1152 | if (items > 1) { |
2cd9d2ba |
1153 | attr_set_value(aTHX_ ST(0), attr, ST(1)); |
1ea12c91 |
1154 | ST(0) = ST(1); /* return value */ |
1155 | } else { |
2cd9d2ba |
1156 | SV *value = attr_get_value(aTHX_ ST(0), attr); |
1ea12c91 |
1157 | if ( value ) { |
1158 | ST(0) = value; |
1159 | } else { |
1160 | XSRETURN_UNDEF; |
1161 | } |
1162 | } |
1163 | |
1164 | XSRETURN(1); |
1165 | } |
1166 | |
1167 | STATIC XS(predicate); |
1168 | STATIC XS(predicate) |
1169 | { |
1170 | #ifdef dVAR |
1171 | dVAR; |
1172 | #endif |
1173 | dXSARGS; |
de2f2e97 |
1174 | dATTR; |
1ea12c91 |
1175 | |
1176 | if (items != 1) |
1177 | Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self"); |
1178 | |
1179 | SP -= items; |
1180 | |
de2f2e97 |
1181 | if ( has_slot_value(aTHX_ ST(0), attr) ) |
1ea12c91 |
1182 | XSRETURN_YES; |
1183 | else |
1184 | XSRETURN_NO; |
1185 | } |
1186 | |
1187 | enum xs_body { |
24a7a8c5 |
1188 | xs_body_reader = 0, |
1189 | xs_body_writer, |
de2f2e97 |
1190 | xs_body_accessor, |
1ea12c91 |
1191 | xs_body_predicate, |
1192 | max_xs_body |
1193 | }; |
1194 | |
1195 | STATIC XSPROTO ((*xs_bodies[])) = { |
24a7a8c5 |
1196 | reader, |
1197 | writer, |
de2f2e97 |
1198 | accessor, |
1ea12c91 |
1199 | predicate, |
1200 | }; |
1201 | |
1202 | MODULE = Moose PACKAGE = Moose::XS |
4e783f63 |
1203 | PROTOTYPES: ENABLE |
1ea12c91 |
1204 | |
1205 | CV * |
de2f2e97 |
1206 | new_sub(attr, name) |
1ea12c91 |
1207 | INPUT: |
de2f2e97 |
1208 | SV *attr; |
1209 | SV *name; |
4e783f63 |
1210 | PROTOTYPE: $;$ |
1ea12c91 |
1211 | ALIAS: |
24a7a8c5 |
1212 | new_reader = xs_body_reader |
1213 | new_writer = xs_body_writer |
de2f2e97 |
1214 | new_accessor = xs_body_accessor |
1215 | new_predicate = xs_body_predicate |
1ea12c91 |
1216 | PREINIT: |
1217 | CV * cv; |
1218 | CODE: |
1219 | if ( ix >= max_xs_body ) |
1220 | croak("Unknown Moose::XS body type"); |
1221 | |
de2f2e97 |
1222 | if ( !sv_isobject(attr) ) |
1223 | croak("'attr' must be a Moose::Meta::Attribute"); |
1224 | |
1225 | cv = newXS(SvOK(name) ? SvPV_nolen(name) : NULL, xs_bodies[ix], __FILE__); |
1ea12c91 |
1226 | |
1227 | if (cv == NULL) |
1228 | croak("Oi vey!"); |
1229 | |
de2f2e97 |
1230 | /* associate CV with meta attr */ |
f253044f |
1231 | stash_in_mg(aTHX_ (SV *)cv, attr); |
de2f2e97 |
1232 | |
1233 | /* this will be set on first call */ |
1234 | XSANY.any_i32 = 0; |
1ea12c91 |
1235 | |
1236 | RETVAL = cv; |
1237 | OUTPUT: |
1238 | RETVAL |
1239 | |
1240 | |
f253044f |
1241 | MODULE = Moose PACKAGE = Moose::XS::Meta::Instance |
4e783f63 |
1242 | PROTOTYPES: DISABLE |
f253044f |
1243 | |
1244 | void |
1245 | DESTROY(self) |
1246 | INPUT: |
1247 | SV *self; |
1248 | PREINIT: |
1249 | MI *mi = INT2PTR(MI *, SvIV(SvRV(self))); |
1250 | CODE: |
2cd9d2ba |
1251 | if ( mi ) |
1252 | delete_mi(aTHX_ mi); |
1253 | |
3c63e75d |
1254 | |
1255 | MODULE = Moose PACKAGE = Moose::XS::TypeConstraints |
1256 | PROTOTYPES: ENABLE |
1257 | |
1258 | bool |
1259 | _check_type(sv) |
1260 | INPUT: |
1261 | SV* sv |
1262 | ALIAS: |
1263 | Any = Any |
1264 | Item = Any |
1265 | Bool = Any |
1266 | Undef = Undef |
1267 | Defined = Defined |
1268 | Str = Str |
1269 | Value = Str |
1270 | Num = Num |
1271 | Int = Int |
1272 | GlobRef = GlobRef |
1273 | ArrayRef = ArrayRef |
1274 | HashRef = HashRef |
1275 | CodeRef = CodeRef |
1276 | Ref = Ref |
1277 | ScalarRef = ScalarRef |
1278 | FileHandle = FileHandle |
1279 | RegexpRef = RegexpRef |
1280 | Object = Object |
1281 | Role = Role |
1282 | ClassName = ClassName |
1283 | CODE: |
1284 | RETVAL = check_sv_type(ix, sv); |
1285 | OUTPUT: |
1286 | RETVAL |
1287 | |
1288 | bool |
1289 | ObjectOfType(sv, class) |
1290 | INPUT: |
1291 | SV* sv |
1292 | SV* class |
1293 | PREINIT: |
1294 | HV *stash = gv_stashsv(class, 0); |
1295 | CODE: |
1296 | RETVAL = check_sv_class(aTHX_ stash, sv); |
1297 | OUTPUT: |
1298 | RETVAL |