create_instance
[gitmo/Moose.git] / Moose.xs
CommitLineData
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
15#define gv_stashpvs(x, y) gv_stashpvn(STR_WITH_LEN(x), y)
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 48STATIC 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 65STATIC 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
72STATIC SV *get_stashed_in_mg(pTHX_ SV *sv) {
73 MAGIC *mg, *moremagic;
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
104typedef 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 */
110 tc_enum, /* TODO check that the value is in an allowed set of values (strings) */
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 115typedef 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 */
160f9ca7 132 ClassName,
de2f2e97 133} TC;
134
2cd9d2ba 135/* auxillary pointer/int union used for constraint checking */
de2f2e97 136typedef 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
153typedef enum {
2cd9d2ba 154 default_none = 0, /* no default value */
155 default_normal, /* code reference or scalar */
156 default_builder, /* builder method */
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 160typedef 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 183typedef 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 268typedef 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 277typedef enum {
278 hash = 0,
279
280 /* these are not yet implemented */
281 array,
282 fptr,
283 cv,
284 judy,
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 303typedef 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 325STATIC 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 343STATIC 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;
362 }
363
160f9ca7 364 return SvTYPE(SvRV(sv)) == svt;
4c6fbfb1 365}
366
2cd9d2ba 367/* checks whether an SV is of a certain class
368 * SvSTASH is first compared by pointer for efficiency */
7ce1a351 369STATIC bool check_sv_class(pTHX_ HV *stash, SV *sv) {
4c6fbfb1 370 dSP;
371 bool ret;
7ce1a351 372 SV *rv;
4c6fbfb1 373
374 if (!sv)
375 return 0;
376 SvGETMAGIC(sv);
377 if (!SvROK(sv))
378 return 0;
7ce1a351 379 rv = (SV*)SvRV(sv);
380 if (!SvOBJECT(rv))
4c6fbfb1 381 return 0;
7ce1a351 382 if (SvSTASH(rv) == stash)
4c6fbfb1 383 return 1;
384
385 ENTER;
386 SAVETMPS;
387 PUSHMARK(SP);
388 XPUSHs(sv);
7ce1a351 389 XPUSHs(sv_2mortal(newSVpv(HvNAME_get(stash), 0)));
4c6fbfb1 390 PUTBACK;
391
392 call_method("isa", G_SCALAR);
393
394 SPAGAIN;
395 ret = SvTRUE(TOPs);
396
397 FREETMPS;
398 LEAVE;
399
400 return ret;
401}
402
2cd9d2ba 403/* checks whether SV of of a known simple type. Most of the non parametrized
404 * Moose core types are implemented here */
4c6fbfb1 405STATIC bool check_sv_type (TC type, SV *sv) {
406 if (!sv)
407 return 0;
160f9ca7 408
4c6fbfb1 409 switch (type) {
410 case Any:
411 return 1;
412 break;
413 case Undef:
414 return !SvOK(sv);
415 break;
416 case Defined:
417 return SvOK(sv);
418 break;
419 case Str:
420 return (SvOK(sv) && !SvROK(sv));
160f9ca7 421 case Num:
422#if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION <5)
423 if (!SvPOK(sv) && !SvPOKp(sv))
424 return SvFLAGS(sv) & (SVf_NOK|SVp_NOK|SVf_IOK|SVp_IOK);
425 else
426#endif
427 return looks_like_number(sv);
428 break;
429 case Int:
430 if ( SvIOK(sv) ) {
431 return 1;
432 } else if ( SvPOK(sv) ) {
0be3b17f 433 /* FIXME i really don't like this */
160f9ca7 434 int i;
435 STRLEN len;
436 char *pv = SvPV(sv, len);
437 char *end = pv + len;
0be3b17f 438 char *tail = end;
160f9ca7 439
440 errno = 0;
0be3b17f 441 i = strtol(pv, &tail, 0);
442
443 if ( errno ) return 0;
444
445 while ( tail != end ) {
446 if ( !isspace(*tail++) ) return 0;
447 }
448
449 return 1;
160f9ca7 450 }
451 return 0;
452 break;
4c6fbfb1 453 case Ref:
454 return SvROK(sv);
455 break;
456 case ScalarRef:
457 return check_is_scalar_ref(sv);
458 break;
459 case ArrayRef:
460 case HashRef:
461 case CodeRef:
462 case GlobRef:
463 return check_reftype(type, sv);
464 break;
3c63e75d 465 case RegexpRef:
466 return ( sv_isobject(sv) && ( strEQ("Regexp", HvNAME_get(SvSTASH(SvRV(sv)))) ) );
467 break;
4c6fbfb1 468 case Object:
3c63e75d 469 return ( sv_isobject(sv) && ( strNE("Regexp", HvNAME_get(SvSTASH(SvRV(sv)))) ) );
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 491STATIC 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 514STATIC 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;
4c6fbfb1 531 }
532
533 croak("todo");
534 return 0;
535}
536
537
2cd9d2ba 538/* end of type constraint checking functions */
539
540
541
542
543
544
545
546
547
548/* Initialize the ATTR structure using positional arguments from Perl space. */
549
160f9ca7 550STATIC void init_attr (MI *mi, ATTR *attr, AV *desc) {
551 U32 flags = 0;
f55aeea0 552 U32 slot_hash, init_arg_hash;
553 STRLEN slot_len, init_arg_len;
554 char *slot_pv, *init_arg_pv;
160f9ca7 555 I32 ix = av_len(desc);
556 SV **params = AvARRAY(desc);
557 SV *tc;
f55aeea0 558 SV *slot_sv;
559 SV *init_arg_sv;
de2f2e97 560
561 attr->mi = mi;
562
f55aeea0 563 if ( ix != 13 )
564 croak("wrong number of args (%d != 14)", ix + 1);
de2f2e97 565
160f9ca7 566 for ( ; ix >= 0; ix-- ) {
567 if ( !params[ix] || params[ix] == &PL_sv_undef )
568 croak("bad params");
569 }
570
2cd9d2ba 571
572
573 /* handle attribute slot array */
574
160f9ca7 575 if ( !SvROK(params[1]) || SvTYPE(SvRV(params[1])) != SVt_PVAV )
576 croak("slots is not an array");
de2f2e97 577
160f9ca7 578 if ( av_len((AV *)SvRV(params[1])) != 0 )
579 croak("Only unary slots are supported at the moment");
1ea12c91 580
160f9ca7 581 /* calculate a hash from the slot */
582 /* FIXME arrays etc should also be supported */
f55aeea0 583 slot_sv = *av_fetch((AV *)SvRV(params[1]), 0, 0);
584 slot_pv = SvPV(slot_sv, slot_len);
585 PERL_HASH(slot_hash, slot_pv, slot_len);
1ea12c91 586
de2f2e97 587
f55aeea0 588 init_arg_sv = params[13];
589 init_arg_pv = SvPV(init_arg_sv, init_arg_len);
590 PERL_HASH(init_arg_hash, init_arg_pv, init_arg_len);
2cd9d2ba 591
592
593 /* FIXME better organize these, positionals suck */
160f9ca7 594 if ( SvTRUE(params[2]) )
595 flags |= ATTR_WEAK;
596
597 if ( SvTRUE(params[3]) )
598 flags |= ATTR_COERCE;
de2f2e97 599
160f9ca7 600 if ( SvTRUE(params[4]) )
601 flags |= ATTR_LAZY;
de2f2e97 602
2cd9d2ba 603
604
605 /* type constraint data */
606
160f9ca7 607 tc = params[5];
de2f2e97 608
160f9ca7 609 if ( SvOK(tc) ) {
610 int tc_kind = SvIV(params[6]);
611 SV *data = params[7];
612
613 switch (tc_kind) {
160f9ca7 614 case tc_type:
615 attr->tc_check.type = SvIV(data);
616 break;
7ce1a351 617 case tc_stash:
618 flags |= ATTR_TCREFCNT;
619 attr->tc_check.sv = (SV *)gv_stashsv(data, 0);
620 break;
160f9ca7 621 case tc_cv:
7ce1a351 622 flags |= ATTR_TCREFCNT;
623 attr->tc_check.sv = SvRV(data);
624 if ( SvTYPE(attr->tc_check.sv) != SVt_PVCV )
160f9ca7 625 croak("compiled type constraint is not a coderef");
626 break;
627 default:
628 croak("todo");
629 }
630
631 flags |= tc_kind;
632 }
633
2cd9d2ba 634
635
636 /* default/builder data */
fe0194bf 637
638 if ( SvTRUE(params[10]) ) { /* has default */
639 SV *sv = params[11];
640
641 if ( SvROK(sv) ) {
642 attr->def.sv = SvRV(sv);
643 if ( SvTYPE(attr->def.sv) != SVt_PVCV )
644 croak("compiled type constraint is not a coderef");
645 } else {
646 attr->def.sv = newSVsv(sv);
647 sv_2mortal(attr->def.sv); /* in case of error soon, we refcnt inc it later after we're done checking params */
648 }
649
650 flags |= ( ATTR_DEFREFCNT | ( default_normal << ATTR_SHIFT_DEFAULT ) );
651 } else if ( SvOK(params[12]) ) { /* builder */
652 attr->def.sv = newSVsv(params[12]);
653 flags |= ( ATTR_DEFREFCNT | ( default_builder << ATTR_SHIFT_DEFAULT ) );
654 }
655
2cd9d2ba 656
160f9ca7 657
658 attr->trigger = SvROK(params[6]) ? (CV *)SvRV(params[6]) : NULL;
659 if ( attr->trigger && SvTYPE(attr->trigger) != SVt_PVCV )
660 croak("trigger is not a coderef");
661
662 attr->initializer = SvROK(params[7]) ? (CV *)SvRV(params[7]) : NULL;
663 if ( attr->initializer && SvTYPE(attr->initializer) != SVt_PVCV )
664 croak("initializer is not a coderef");
665
2cd9d2ba 666
667
668 /* now that we're done preparing/checking args and shit, so we finalize the
669 * attr, increasing refcounts for any referenced data, and creating the CV
670 * array */
671
672 attr->flags = flags;
673
674 /* copy the outer ref SV */
160f9ca7 675 attr->meta_attr = newSVsv(params[0]);
676 attr->type_constraint = newSVsv(tc);
2cd9d2ba 677
678 /* increase the refcount for auxillary structures */
7ce1a351 679 SvREFCNT_inc(attr->trigger);
680 SvREFCNT_inc(attr->initializer);
9be0171f 681 if ( flags & ATTR_TCREFCNT ) SvREFCNT_inc(attr->tc_check.sv);
fe0194bf 682 if ( flags & ATTR_DEFREFCNT ) SvREFCNT_inc(attr->def.sv);
160f9ca7 683
f55aeea0 684 attr->slot_sv = newSVpvn_share(slot_pv, slot_len, slot_hash);
685 attr->slot_u32 = slot_hash;
686
687 attr->init_arg_sv = newSVpvn_share(init_arg_pv, init_arg_len, init_arg_hash);
688 attr->init_arg_u32 = init_arg_hash;
160f9ca7 689
de2f2e97 690 /* cross refs to CVs which use this struct */
691 attr->cvs = newAV();
692}
693
2cd9d2ba 694STATIC SV *new_mi (pTHX_ HV *stash, AV *attrs) {
695 HV *mi_stash = gv_stashpvs("Moose::XS::Meta::Instance",0);
696 SV *sv_ptr = newSViv(0);
697 SV *obj = sv_2mortal(sv_bless(newRV_noinc(sv_ptr), mi_stash));
de2f2e97 698 MI *mi;
2cd9d2ba 699 const I32 num_attrs = av_len(attrs) + 1;
de2f2e97 700
687453c6 701 Newxz(mi, 1, MI);
2cd9d2ba 702
703 /* set the pointer now, if we have any initialization errors it'll get
704 * cleaned up because obj is mortal */
705 sv_setiv(sv_ptr, PTR2IV(mi));
706
687453c6 707 Newxz(mi->attrs, num_attrs, ATTR);
2cd9d2ba 708
de2f2e97 709 SvREFCNT_inc_simple(stash);
710 mi->stash = stash;
711
de2f2e97 712 mi->type = 0; /* nothing else implemented yet */
713
714 /* initialize attributes */
2cd9d2ba 715 for ( ; mi->num_attrs < num_attrs; mi->num_attrs++ ) {
716 SV **desc = av_fetch(attrs, mi->num_attrs, 0);
de2f2e97 717
160f9ca7 718 if ( !desc || !*desc || !SvROK(*desc) || !(SvTYPE(SvRV(*desc)) == SVt_PVAV) ) {
de2f2e97 719 croak("Attribute descriptor has to be a hash reference");
f253044f 720 }
de2f2e97 721
2cd9d2ba 722 init_attr(mi, &mi->attrs[mi->num_attrs], (AV *)SvRV(*desc));
de2f2e97 723 }
724
2cd9d2ba 725 return obj;
726}
727
728STATIC void delete_attr (pTHX_ ATTR *attr) {
729 I32 i;
730 SV **cvs = AvARRAY(attr->cvs);
731
732 /* remove the pointers to this ATTR struct from all the the dependent CVs */
733 for ( i = av_len(attr->cvs); i >= 0; i-- ) {
734 CV *cv = (CV *)cvs[i];
735 XSANY.any_i32 = 0;
736 }
737
738 SvREFCNT_dec(attr->cvs);
739 SvREFCNT_dec(attr->slot_sv);
740 SvREFCNT_dec(attr->type_constraint);
741 if ( attr->flags & ATTR_TCREFCNT ) SvREFCNT_dec(attr->tc_check.sv);
742 if ( attr->flags & ATTR_DEFREFCNT ) SvREFCNT_dec(attr->def.sv);
743 SvREFCNT_dec(attr->initializer);
744 SvREFCNT_dec(attr->trigger);
745 SvREFCNT_dec(attr->meta_attr);
de2f2e97 746}
747
7ce1a351 748STATIC void delete_mi (pTHX_ MI *mi) {
2cd9d2ba 749 SvREFCNT_dec(mi->stash);
7ce1a351 750
2cd9d2ba 751 while ( mi->num_attrs--) {
752 ATTR *attr = &mi->attrs[mi->num_attrs];
753 delete_attr(aTHX_ attr);
7ce1a351 754 }
755
2cd9d2ba 756 if ( mi->attrs ) Safefree(mi->attrs);
7ce1a351 757 Safefree(mi);
758}
759
de2f2e97 760
2cd9d2ba 761
762
763/* these functions call Perl-space for MOP methods, helpers etc */
764
765
766/* wow, so much code for the equivalent of
767 * $attr->associated_class->get_meta_instance */
f253044f 768STATIC SV *attr_to_meta_instance(pTHX_ SV *meta_attr) {
769 dSP;
770 I32 count;
771 SV *mi;
772
773 if ( !meta_attr )
774 croak("No attr found in magic!");
775
776 ENTER;
777 SAVETMPS;
778 PUSHMARK(SP);
2cd9d2ba 779
f253044f 780 XPUSHs(meta_attr);
2cd9d2ba 781
f253044f 782 PUTBACK;
783 count = call_pv("Moose::XS::attr_to_meta_instance", G_SCALAR);
784
785 if ( count != 1 )
786 croak("attr_to_meta_instance borked (%d args returned, expecting 1)", count);
787
788 SPAGAIN;
789 mi = POPs;
790
791 SvREFCNT_inc(mi);
792
793 PUTBACK;
794 FREETMPS;
795 LEAVE;
796
fe0194bf 797 return sv_2mortal(mi);
f253044f 798}
799
2cd9d2ba 800/* gets a class and an array of attr parameters */
f253044f 801STATIC SV *perl_mi_to_c_mi(pTHX_ SV *perl_mi) {
802 dSP;
803 I32 count;
2cd9d2ba 804 SV *mi;
f253044f 805 SV *class;
806 SV *attrs;
807 HV *stash;
808
809 ENTER;
810 SAVETMPS;
811 PUSHMARK(SP);
2cd9d2ba 812
f253044f 813 XPUSHs(perl_mi);
2cd9d2ba 814
f253044f 815 PUTBACK;
816 count = call_pv("Moose::XS::meta_instance_to_attr_descs", G_ARRAY);
817
818 if ( count != 2 )
819 croak("meta_instance_to_attr_descs borked (%d args returned, expecting 2)", count);
820
821 SPAGAIN;
822 attrs = POPs;
823 class = POPs;
824
825 PUTBACK;
826
827 stash = gv_stashsv(class, 0);
828
829 mi = new_mi(aTHX_ stash, (AV *)SvRV(attrs));
2cd9d2ba 830 SvREFCNT_inc(mi);
f253044f 831
832 FREETMPS;
833 LEAVE;
834
2cd9d2ba 835 return sv_2mortal(mi);
f253044f 836}
837
2cd9d2ba 838
839
840/* locate an ATTR for a MOP level attribute inside an MI */
841STATIC ATTR *mi_find_attr(SV *mi_obj, SV *meta_attr) {
f253044f 842 I32 ix;
2cd9d2ba 843 MI *mi = INT2PTR(MI *, SvIV(SvRV(mi_obj)));
f253044f 844
035fd0c4 845 for ( ix = 0; ix < mi->num_attrs; ix++ ) {
f253044f 846 if ( SvRV(mi->attrs[ix].meta_attr) == SvRV(meta_attr) ) {
847 return &mi->attrs[ix];
de2f2e97 848 }
de2f2e97 849 }
850
2cd9d2ba 851 croak("Attr %x not found in meta instance of %s", SvRV(meta_attr) /* SvPV_force_nomg(sv_2mortal(newSVsv(meta_attr))) */, HvNAME_get(mi->stash) );
de2f2e97 852 return NULL;
853}
854
2cd9d2ba 855/* returns the ATTR for a CV:
856 *
857 * 1. get the Moose::Meta::Attribute using get_stashed_in_mg from the CV itself
858 * 2. get the meta instance by calling $attr->associated_class->get_meta_instance
859 * 3. get the MI by using get_stashed_in_mg from the meta instance, creating it if necessary
860 * 4. search for the appropriate ATTR in the MI using mi_find_attr
861 */
de2f2e97 862STATIC ATTR *get_attr(pTHX_ CV *cv) {
f253044f 863 SV *meta_attr = get_stashed_in_mg(aTHX_ (SV *)cv);
864 SV *perl_mi = attr_to_meta_instance(aTHX_ meta_attr);
2cd9d2ba 865 SV *mi_obj = get_stashed_in_mg(aTHX_ SvRV(perl_mi));
de2f2e97 866
2cd9d2ba 867 if (!mi_obj) {
868 mi_obj = perl_mi_to_c_mi(aTHX_ perl_mi);
869 stash_in_mg(aTHX_ SvRV(perl_mi), mi_obj);
f253044f 870 }
871
2cd9d2ba 872 return mi_find_attr(mi_obj, meta_attr);
1ea12c91 873}
874
2cd9d2ba 875/* Cache a pointer to the appropriate ATTR in the XSANY of the CV, using
876 * get_attr */
de2f2e97 877STATIC ATTR *define_attr (pTHX_ CV *cv) {
878 ATTR *attr = get_attr(aTHX_ cv);
879 assert(attr);
880
881 XSANY.any_i32 = PTR2IV(attr);
f253044f 882
7ce1a351 883 SvREFCNT_inc(cv);
f253044f 884 av_push( attr->cvs, (SV *)cv );
de2f2e97 885
886 return attr;
887}
888
2cd9d2ba 889
890
891
892
893
894
de2f2e97 895STATIC void weaken(pTHX_ SV *sv) {
1ea12c91 896#ifdef SvWEAKREF
de2f2e97 897 sv_rvweaken(sv); /* FIXME i think this might warn when weakening an already weak ref */
1ea12c91 898#else
899 croak("weak references are not implemented in this release of perl");
900#endif
901}
902
903
2cd9d2ba 904
905
906
907
1ea12c91 908/* meta instance protocol */
909
de2f2e97 910STATIC SV *get_slot_value(pTHX_ SV *self, ATTR *attr) {
1ea12c91 911 HE *he;
912
913 assert(self);
914 assert(SvROK(self));
915 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
916
de2f2e97 917 assert( ATTR_DUMB_INSTANCE(attr) );
918
919 if ((he = hv_fetch_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32)))
1ea12c91 920 return HeVAL(he);
921 else
922 return NULL;
923}
924
de2f2e97 925STATIC void set_slot_value(pTHX_ SV *self, ATTR *attr, SV *value) {
1ea12c91 926 HE *he;
4c6fbfb1 927 SV *copy;
1ea12c91 928
929 assert(self);
930 assert(SvROK(self));
931 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
932
de2f2e97 933 assert( ATTR_DUMB_INSTANCE(attr) );
934
4c6fbfb1 935 copy = newSVsv(value);
936
937 he = hv_store_ent((HV*)SvRV(self), attr->slot_sv, copy, attr->slot_u32);
1ea12c91 938
1ea12c91 939 if (he != NULL) {
de2f2e97 940 if ( ATTR_ISWEAK(attr) )
4c6fbfb1 941 weaken(aTHX_ HeVAL(he));
1ea12c91 942 } else {
4c6fbfb1 943 SvREFCNT_dec(copy);
1ea12c91 944 croak("Hash store failed.");
945 }
946}
947
de2f2e97 948STATIC bool has_slot_value(pTHX_ SV *self, ATTR *attr) {
1ea12c91 949 assert(self);
950 assert(SvROK(self));
951 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
952
de2f2e97 953 assert( ATTR_DUMB_INSTANCE(attr) );
954
955 return hv_exists_ent((HV *)SvRV(self), attr->slot_sv, attr->slot_u32);
956}
957
958STATIC SV *deinitialize_slot(pTHX_ SV *self, ATTR *attr) {
959 assert(self);
960 assert(SvROK(self));
961 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
962
963 assert( ATTR_DUMB_INSTANCE(attr) );
964
965 return hv_delete_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32);
1ea12c91 966}
967
af0842cb 968STATIC SV *create_instance(pTHX_ MI *mi) {
969 return sv_bless(sv_2mortal(newRV_noinc(newHV())), mi->stash);
970}
fe0194bf 971
fe0194bf 972
fe0194bf 973
fe0194bf 974
2cd9d2ba 975/* Shared functionality for readers/writers/accessors, this roughly corresponds
976 * to the methods of Moose::Meta::Attribute on the instance
977 * (get_value/set_value, default value handling, etc) */
fe0194bf 978
2cd9d2ba 979STATIC void attr_set_value(pTHX_ SV *self, ATTR *attr, SV *value);
fe0194bf 980
2cd9d2ba 981STATIC 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
1009/* Returns an SV for the default value. Should be copied by the caller because
1010 * it's either an alias for a simple value, or a mortal from cv/builder */
1011STATIC SV *get_default(pTHX_ SV *self, ATTR *attr) {
1012 switch ( ATTR_DEFAULT(attr) ) {
1013 case default_none:
1014 return NULL;
1015 break;
1016 case default_builder:
1017 return call_builder(aTHX_ self, attr);
fe0194bf 1018 break;
1019 case default_normal:
1020 if ( SvROK(attr->def.sv) ) {
1021 printf("CV default\n");
2cd9d2ba 1022 croak("todo");
fe0194bf 1023 } else {
1024 printf("simple value\n");
1025 return attr->def.sv; /* will be copied by set for lazy, and by reader for both cases */
1026 }
1027 break;
fe0194bf 1028 case default_type:
1029 croak("todo");
1030 break;
1031 }
1032
1033 return NULL;
1034}
1035
2cd9d2ba 1036/* $attr->get_value($self), will vivify lazy values if needed
1037 * returns an alias to the sv that is copied in the reader/writer/accessor code
1038 * */
1039STATIC SV *attr_get_value(pTHX_ SV *self, ATTR *attr) {
fe0194bf 1040 SV *value = get_slot_value(aTHX_ self, attr);
1041
1042 if ( value ) {
1043 return value;
1044 } else if ( ATTR_ISLAZY(attr) ) {
1045 value = get_default(aTHX_ self, attr);
2cd9d2ba 1046 attr_set_value(aTHX_ self, attr, value);
fe0194bf 1047 return value;
1048 }
1049
1050 return NULL;
160f9ca7 1051}
1052
2cd9d2ba 1053/* $attr->set_value($self) */
1054STATIC void attr_set_value(pTHX_ SV *self, ATTR *attr, SV *value) {
fe0194bf 1055 if ( ATTR_TYPE(attr) ) {
1056 if ( !check_type_constraint(aTHX_ ATTR_TYPE(attr), attr->tc_check, attr->type_constraint, value) )
160f9ca7 1057 croak("Bad param");
1058 }
1059
1060 set_slot_value(aTHX_ self, attr, value);
1061}
1ea12c91 1062
2cd9d2ba 1063
1064
1065
1066
1067
1068
1069/* Perl-space level functionality
1070 *
1071 * These subs are installed by new_sub's various aliases as the bodies of the
1072 * new XSUBs
1073 * */
1074
1075
1076
1077/* This macro is used in the XS subs to set up the 'attr' variable.
1078 *
1079 * if XSANY is NULL then define_attr is called on the CV, to set the pointer
1080 * to the ATTR struct.
1081 * */
1082#define dATTR ATTR *attr = (XSANY.any_i32 ? INT2PTR(ATTR *, (XSANY.any_i32)) : define_attr(aTHX_ cv))
1083
1ea12c91 1084
24a7a8c5 1085STATIC XS(reader);
1086STATIC XS(reader)
1ea12c91 1087{
1088#ifdef dVAR
1089 dVAR;
1090#endif
1091 dXSARGS;
de2f2e97 1092 dATTR;
1ea12c91 1093 SV *value;
1094
1095 if (items != 1)
1096 Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self");
1097
1098 SP -= items;
1099
2cd9d2ba 1100 value = attr_get_value(aTHX_ ST(0), attr);
1ea12c91 1101
1102 if (value) {
1103 ST(0) = sv_mortalcopy(value); /* mortalcopy because $_ .= "blah" for $foo->bar */
1104 XSRETURN(1);
1105 } else {
1106 XSRETURN_UNDEF;
1107 }
1108}
1109
24a7a8c5 1110STATIC XS(writer);
1111STATIC XS(writer)
1ea12c91 1112{
1113#ifdef dVAR
1114 dVAR;
1115#endif
1116 dXSARGS;
de2f2e97 1117 dATTR;
1ea12c91 1118
1119 if (items != 2)
1120 Perl_croak(aTHX_ "Usage: %s(%s, %s)", GvNAME(CvGV(cv)), "self", "value");
1121
1122 SP -= items;
1123
2cd9d2ba 1124 attr_set_value(aTHX_ ST(0), attr, ST(1));
1ea12c91 1125
1126 ST(0) = ST(1); /* return value */
1127 XSRETURN(1);
1128}
1129
de2f2e97 1130STATIC XS(accessor);
1131STATIC XS(accessor)
1ea12c91 1132{
1133#ifdef dVAR
1134 dVAR;
1135#endif
1136 dXSARGS;
de2f2e97 1137 dATTR;
1ea12c91 1138
1139 if (items < 1)
1140 Perl_croak(aTHX_ "Usage: %s(%s, [ %s ])", GvNAME(CvGV(cv)), "self", "value");
1141
1142 SP -= items;
1143
1144 if (items > 1) {
2cd9d2ba 1145 attr_set_value(aTHX_ ST(0), attr, ST(1));
1ea12c91 1146 ST(0) = ST(1); /* return value */
1147 } else {
2cd9d2ba 1148 SV *value = attr_get_value(aTHX_ ST(0), attr);
1ea12c91 1149 if ( value ) {
1150 ST(0) = value;
1151 } else {
1152 XSRETURN_UNDEF;
1153 }
1154 }
1155
1156 XSRETURN(1);
1157}
1158
1159STATIC XS(predicate);
1160STATIC XS(predicate)
1161{
1162#ifdef dVAR
1163 dVAR;
1164#endif
1165 dXSARGS;
de2f2e97 1166 dATTR;
1ea12c91 1167
1168 if (items != 1)
1169 Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self");
1170
1171 SP -= items;
1172
de2f2e97 1173 if ( has_slot_value(aTHX_ ST(0), attr) )
1ea12c91 1174 XSRETURN_YES;
1175 else
1176 XSRETURN_NO;
1177}
1178
1179enum xs_body {
24a7a8c5 1180 xs_body_reader = 0,
1181 xs_body_writer,
de2f2e97 1182 xs_body_accessor,
1ea12c91 1183 xs_body_predicate,
1184 max_xs_body
1185};
1186
1187STATIC XSPROTO ((*xs_bodies[])) = {
24a7a8c5 1188 reader,
1189 writer,
de2f2e97 1190 accessor,
1ea12c91 1191 predicate,
1192};
1193
1194MODULE = Moose PACKAGE = Moose::XS
4e783f63 1195PROTOTYPES: ENABLE
1ea12c91 1196
1197CV *
de2f2e97 1198new_sub(attr, name)
1ea12c91 1199 INPUT:
de2f2e97 1200 SV *attr;
1201 SV *name;
4e783f63 1202 PROTOTYPE: $;$
1ea12c91 1203 ALIAS:
24a7a8c5 1204 new_reader = xs_body_reader
1205 new_writer = xs_body_writer
de2f2e97 1206 new_accessor = xs_body_accessor
1207 new_predicate = xs_body_predicate
1ea12c91 1208 PREINIT:
1209 CV * cv;
1210 CODE:
1211 if ( ix >= max_xs_body )
1212 croak("Unknown Moose::XS body type");
1213
de2f2e97 1214 if ( !sv_isobject(attr) )
1215 croak("'attr' must be a Moose::Meta::Attribute");
1216
1217 cv = newXS(SvOK(name) ? SvPV_nolen(name) : NULL, xs_bodies[ix], __FILE__);
1ea12c91 1218
1219 if (cv == NULL)
1220 croak("Oi vey!");
1221
de2f2e97 1222 /* associate CV with meta attr */
f253044f 1223 stash_in_mg(aTHX_ (SV *)cv, attr);
de2f2e97 1224
1225 /* this will be set on first call */
1226 XSANY.any_i32 = 0;
1ea12c91 1227
1228 RETVAL = cv;
1229 OUTPUT:
1230 RETVAL
1231
1232
f253044f 1233MODULE = Moose PACKAGE = Moose::XS::Meta::Instance
4e783f63 1234PROTOTYPES: DISABLE
f253044f 1235
1236void
1237DESTROY(self)
1238 INPUT:
1239 SV *self;
1240 PREINIT:
1241 MI *mi = INT2PTR(MI *, SvIV(SvRV(self)));
1242 CODE:
2cd9d2ba 1243 if ( mi )
1244 delete_mi(aTHX_ mi);
1245
3c63e75d 1246
1247MODULE = Moose PACKAGE = Moose::XS::TypeConstraints
1248PROTOTYPES: ENABLE
1249
1250bool
1251_check_type(sv)
1252 INPUT:
1253 SV* sv
1254 ALIAS:
1255 Any = Any
1256 Item = Any
1257 Bool = Any
1258 Undef = Undef
1259 Defined = Defined
1260 Str = Str
1261 Value = Str
1262 Num = Num
1263 Int = Int
1264 GlobRef = GlobRef
1265 ArrayRef = ArrayRef
1266 HashRef = HashRef
1267 CodeRef = CodeRef
1268 Ref = Ref
1269 ScalarRef = ScalarRef
1270 FileHandle = FileHandle
1271 RegexpRef = RegexpRef
1272 Object = Object
1273 Role = Role
1274 ClassName = ClassName
1275 CODE:
1276 RETVAL = check_sv_type(ix, sv);
1277 OUTPUT:
1278 RETVAL
1279
1280bool
1281ObjectOfType(sv, class)
1282 INPUT:
1283 SV* sv
1284 SV* class
1285 PREINIT:
1286 HV *stash = gv_stashsv(class, 0);
1287 CODE:
1288 RETVAL = check_sv_class(aTHX_ stash, sv);
1289 OUTPUT:
1290 RETVAL