6 * needs to be made into Moose::XS::Meta::Instance and Meta::Slot for the
7 * metadata, with a proper destructor. XSANY still points to this struct, but
8 * it is shared by all functions of the same type.
10 * Instance contains SvSTASH, and ATTR slots[]
12 * On recreation of the meta instance we refresh the ATTR value of all the CVs
15 * need a good way to handle time between invalidate and regeneration (just
16 * check XSANY and call get_meta_instance if null?)
20 * type constraints are already implemented by konobi
21 * should be trivial to do coercions for the core types, too
23 * TypeConstraint::Class can compare SvSTASH by ptr, and if it's neq *then*
24 * call ->isa (should handle vast majority of cases)
26 * base parametrized types are also trivial
28 * ClassName is get_stathpvn
32 * for a constructor we have ATTR *attrs, and iterate that, removing init_arg
33 * we can preallocate the structure to the right size (maybe even with the
34 * right HEs?), and do various other prehashing hacks to gain speed
38 * delegations and attribute helpers:
47 * I32 *type; // hash, array, whatever + vtable for operation
52 STATIC MGVTBL null_mg_vtbl = {
66 #endif /* MGf_LOCAL */
69 STATIC MAGIC *stash_in_mg (pTHX_ SV *sv, SV *obj) {
70 MAGIC *mg = sv_magicext(sv, obj, PERL_MAGIC_ext, &null_mg_vtbl, NULL, 0 );
71 mg->mg_flags |= MGf_REFCOUNTED;
76 STATIC SV *get_stashed_in_mg(pTHX_ SV *sv) {
77 MAGIC *mg, *moremagic;
79 if (SvTYPE(sv) >= SVt_PVMG) {
80 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
81 if ((mg->mg_type == PERL_MAGIC_ext) && (mg->mg_virtual == &null_mg_vtbl))
154 /* the meta instance struct */
157 U32 flags; /* slot type, TC behavior, coerce, weaken, (no default | default, builder + lazy), auto_deref */
159 /* slot access fields */
160 SV *slot_sv; /* value of the slot (slot name presumably) */
161 U32 slot_u32; /* for optimized access (precomputed hash or otherr) */
163 DEFAULT def; /* cv, value or other, depending on flags */
165 TC_CHECK tc_check; /* cv, value or other, dependidng on flags */
166 SV *type_constraint; /* meta attr */
171 SV *meta_attr; /* the meta attr object */
172 AV *cvs; /* CVs which use this attr */
176 * instance reading writing
177 * 00000000 00000000 00000000 00000000
185 * ^^^^^^^ if 0 then nothing special (just hash)? FIXME TBD
188 #define ATTR_INSTANCE_MASK 0xff000000
189 #define ATTR_READING_MASK 0x0000ff00
190 #define ATTR_WRITING_MASK 0x000000ff
192 #define ATTR_MASK_TYPE 0x7
194 #define ATTR_MASK_DEFAULT 0x700
195 #define ATTR_SHIFT_DEAFULT 8
197 #define ATTR_LAZY 0x800
199 #define ATTR_COERCE 0x08
200 #define ATTR_WEAK 0x10
201 #define ATTR_TRIGGER 0x10
203 #define ATTR_ISWEAK(attr) ( attr->flags & ATTR_WEAK )
204 #define ATTR_ISLAZY(attr) ( attr->flags & ATTR_LAZY )
205 #define ATTR_ISCOERCE(attr) ( attr->flags & ATTR_COERCE )
207 #define ATTR_TYPE(f) ( attr->flags & 0x7 )
208 #define ATTR_DEFAULT(f) ( ( attr->flags & ATTR_MASK_DEFAULT ) >> ATTR_SHIFT_DEFAULT )
210 #define ATTR_DUMB_READER(attr) !ATTR_IS_LAZY(attr)
211 #define ATTR_DUMB_WRITER(attr) ( ( attr->flags & ATTR_WRITING_MASK ) == 0 )
212 #define ATTR_DUMB_INSTANCE(attr) ( ( attr->flags & ATTR_INSTANCE_MASK ) == 0 )
214 #define dATTR ATTR *attr = (XSANY.any_i32 ? INT2PTR(ATTR *, (XSANY.any_i32)) : define_attr(aTHX_ cv))
217 /* FIXME define a vtable that does call_sv */
219 SV * (*get)(pTHX_ SV *self, ATTR *attr);
220 void (*set)(pTHX_ SV *self, ATTR *attr, SV *value);
221 bool * (*has)(pTHX_ SV *self, ATTR *attr);
222 SV * (*delete)(pTHX_ SV *self, ATTR *attr);
229 /* these are not yet implemented */
239 /* slot access method */
243 /* attr descriptors */
249 STATIC void init_attr (MI *mi, ATTR *attr, HV *desc) {
252 SV **key = hv_fetchs(desc, "key", 0);
253 SV **meta_attr = hv_fetchs(desc, "meta", 0);
256 if ( !meta_attr ) croak("'meta' is required");
258 attr->meta_attr = *meta_attr;
265 /* if type == hash */
266 /* prehash the key */
267 if ( !key ) croak("'key' is required");
269 pv = SvPV(*key, len);
271 PERL_HASH(hash, pv, len);
273 attr->slot_sv = newSVpvn_share(pv, len, hash);
274 attr->slot_u32 = hash;
278 attr->tc_check.type = 0;
279 attr->type_constraint = NULL;
282 attr->initializer = NULL;
283 attr->trigger = NULL;
285 /* cross refs to CVs which use this struct */
289 STATIC MI *new_mi (pTHX_ HV *stash, AV *attrs) {
292 const I32 num = av_len(attrs) + 1;
296 SvREFCNT_inc_simple(stash);
299 mi->type = 0; /* nothing else implemented yet */
301 /* initialize attributes */
303 Newx(mi->attrs, num, ATTR);
304 for ( ix = 0; ix < num; ix++ ) {
305 SV **desc = av_fetch(attrs, ix, 0);
307 if ( !desc || !*desc || !SvROK(*desc) || !(SvTYPE(SvRV(*desc)) == SVt_PVHV) ) {
308 croak("Attribute descriptor has to be a hash reference");
311 init_attr(mi, &mi->attrs[ix], (HV *)SvRV(*desc));
317 STATIC SV *new_mi_obj (pTHX_ MI *mi) {
318 return newRV_noinc(newSViv(PTR2IV(mi)));
321 STATIC SV *attr_to_meta_instance(pTHX_ SV *meta_attr) {
327 croak("No attr found in magic!");
334 count = call_pv("Moose::XS::attr_to_meta_instance", G_SCALAR);
337 croak("attr_to_meta_instance borked (%d args returned, expecting 1)", count);
351 STATIC SV *perl_mi_to_c_mi(pTHX_ SV *perl_mi) {
364 count = call_pv("Moose::XS::meta_instance_to_attr_descs", G_ARRAY);
367 croak("meta_instance_to_attr_descs borked (%d args returned, expecting 2)", count);
375 stash = gv_stashsv(class, 0);
377 mi = new_mi(aTHX_ stash, (AV *)SvRV(attrs));
382 return new_mi_obj(aTHX_ mi);
385 STATIC ATTR *mi_find_attr(MI *mi, SV *meta_attr) {
388 for ( ix = 0; ix <= mi->num_attrs; ix++ ) {
389 if ( SvRV(mi->attrs[ix].meta_attr) == SvRV(meta_attr) ) {
390 return &mi->attrs[ix];
395 croak("Attr not found");
399 STATIC ATTR *get_attr(pTHX_ CV *cv) {
400 SV *meta_attr = get_stashed_in_mg(aTHX_ (SV *)cv);
401 SV *perl_mi = attr_to_meta_instance(aTHX_ meta_attr);
402 SV *c_mi = get_stashed_in_mg(aTHX_ SvRV(perl_mi));
406 c_mi = perl_mi_to_c_mi(aTHX_ perl_mi);
407 stash_in_mg(aTHX_ perl_mi, c_mi);
412 mi = INT2PTR(MI *, SvIV(SvRV(c_mi)));
414 return mi_find_attr(mi, meta_attr);
417 STATIC ATTR *define_attr (pTHX_ CV *cv) {
418 ATTR *attr = get_attr(aTHX_ cv);
421 XSANY.any_i32 = PTR2IV(attr);
423 av_push( attr->cvs, (SV *)cv );
428 STATIC void weaken(pTHX_ SV *sv) {
430 sv_rvweaken(sv); /* FIXME i think this might warn when weakening an already weak ref */
432 croak("weak references are not implemented in this release of perl");
437 /* meta instance protocol */
439 STATIC SV *get_slot_value(pTHX_ SV *self, ATTR *attr) {
444 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
446 assert( ATTR_DUMB_INSTANCE(attr) );
448 if ((he = hv_fetch_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32)))
454 STATIC void set_slot_value(pTHX_ SV *self, ATTR *attr, SV *value) {
459 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
461 assert( ATTR_DUMB_INSTANCE(attr) );
465 he = hv_store_ent((HV*)SvRV(self), attr->slot_sv, value, attr->slot_u32);
467 if ( ATTR_ISWEAK(attr) )
468 weaken(aTHX_ HeVAL(he)); /* actually only needed once at HE creation time */
470 croak("Hash store failed.");
474 STATIC bool has_slot_value(pTHX_ SV *self, ATTR *attr) {
477 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
479 assert( ATTR_DUMB_INSTANCE(attr) );
481 return hv_exists_ent((HV *)SvRV(self), attr->slot_sv, attr->slot_u32);
484 STATIC SV *deinitialize_slot(pTHX_ SV *self, ATTR *attr) {
487 assert(SvTYPE(SvRV(self)) == SVt_PVHV);
489 assert( ATTR_DUMB_INSTANCE(attr) );
491 return hv_delete_ent((HV *)SvRV(self), attr->slot_sv, 0, attr->slot_u32);
495 /* simple high level api */
508 Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self");
512 assert( ATTR_DUMB_READER(attr) );
514 value = get_slot_value(aTHX_ ST(0), attr);
517 ST(0) = sv_mortalcopy(value); /* mortalcopy because $_ .= "blah" for $foo->bar */
534 Perl_croak(aTHX_ "Usage: %s(%s, %s)", GvNAME(CvGV(cv)), "self", "value");
538 assert( ATTR_DUMB_WRITER(attr) );
540 set_slot_value(aTHX_ ST(0), attr, ST(1));
542 ST(0) = ST(1); /* return value */
556 Perl_croak(aTHX_ "Usage: %s(%s, [ %s ])", GvNAME(CvGV(cv)), "self", "value");
561 assert( ATTR_DUMB_READER(attr) );
562 set_slot_value(aTHX_ ST(0), attr, ST(1));
563 ST(0) = ST(1); /* return value */
565 assert( ATTR_DUMB_WRITER(attr) );
566 SV *value = get_slot_value(aTHX_ ST(0), attr);
577 STATIC XS(predicate);
587 Perl_croak(aTHX_ "Usage: %s(%s)", GvNAME(CvGV(cv)), "self");
591 if ( has_slot_value(aTHX_ ST(0), attr) )
605 STATIC XSPROTO ((*xs_bodies[])) = {
612 MODULE = Moose PACKAGE = Moose::XS
620 new_getter = xs_body_getter
621 new_setter = xs_body_setter
622 new_accessor = xs_body_accessor
623 new_predicate = xs_body_predicate
627 if ( ix >= max_xs_body )
628 croak("Unknown Moose::XS body type");
630 if ( !sv_isobject(attr) )
631 croak("'attr' must be a Moose::Meta::Attribute");
633 cv = newXS(SvOK(name) ? SvPV_nolen(name) : NULL, xs_bodies[ix], __FILE__);
638 /* associate CV with meta attr */
639 stash_in_mg(aTHX_ (SV *)cv, attr);
641 /* this will be set on first call */
649 MODULE = Moose PACKAGE = Moose::XS::Meta::Instance
656 MI *mi = INT2PTR(MI *, SvIV(SvRV(self)));
658 /* foreach attr ( delete cvs XSANY ), free attrs free mi */