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