X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=xs-src%2FMouse.xs;h=075b8060ec6c862130bd7d9a3f36ec51810224a7;hb=855eff0e9edf07f4d7af3eb245a3139fa6c6bd05;hp=7c70a8498bccac5af949292cde71e7b05fdef4c3;hpb=9974d611a73bb21c9bdba9da2e2658c92d0ab695;p=gitmo%2FMouse.git diff --git a/xs-src/Mouse.xs b/xs-src/Mouse.xs index 7c70a84..075b806 100644 --- a/xs-src/Mouse.xs +++ b/xs-src/Mouse.xs @@ -30,8 +30,6 @@ enum mouse_xc_ix_t{ MOUSE_XC_GEN, /* class generation */ MOUSE_XC_STASH, /* symbol table hash */ - MOUSE_XC_BUILDARGS, /* Custom BUILDARGS */ - MOUSE_XC_ATTRALL, /* all the attributes */ MOUSE_XC_BUILDALL, /* all the BUILD methods */ MOUSE_XC_DEMOLISHALL, /* all the DEMOLISH methods */ @@ -74,7 +72,7 @@ mouse_class_has_custom_buildargs(pTHX_ HV* const stash) { GV* const buildargs = gv_fetchmeth_autoload(stash, "BUILDARGS", sizeof("BUILDARGS")-1, 0); - return buildargs && CvXSUB(GvCV(buildargs)) == XS_Mouse__Object_BUILDARGS; + return buildargs && CvXSUB(GvCV(buildargs)) != XS_Mouse__Object_BUILDARGS; } static void @@ -122,15 +120,17 @@ mouse_class_update_xc(pTHX_ SV* const metaclass PERL_UNUSED_DECL, HV* const stas for(i = 0; i < len; i++){ SV* const klass = MOUSE_av_at(linearized_isa, i); + HV* const st = gv_stashsv(klass, TRUE); SV* meta; GV* gv; - gv = stash_fetchs(stash, "BUILD", FALSE); + gv = stash_fetchs(st, "BUILD", FALSE); if(gv && GvCVu(gv)){ - av_push(buildall, newRV_inc((SV*)GvCV(gv))); + av_unshift(buildall, 1); + av_store(buildall, 0, newRV_inc((SV*)GvCV(gv))); } - gv = stash_fetchs(stash, "DEMOLISH", FALSE); + gv = stash_fetchs(st, "DEMOLISH", FALSE); if(gv && GvCVu(gv)){ av_push(demolishall, newRV_inc((SV*)GvCV(gv))); } @@ -175,7 +175,7 @@ mouse_get_xc(pTHX_ SV* const metaclass) { av_extend(xc, MOUSE_XC_last - 1); - av_store(xc, MOUSE_XC_GEN, newSViv(0)); + av_store(xc, MOUSE_XC_GEN, newSVuv(0U)); av_store(xc, MOUSE_XC_STASH, (SV*)stash); SvREFCNT_inc_simple_void_NN(stash); @@ -188,9 +188,14 @@ mouse_get_xc(pTHX_ SV* const metaclass) { } gen = MOUSE_xc_gen(xc); + + if(SvUVX(gen) != 0U && MOUSE_xc_flags(xc) & MOUSEf_XC_IS_IMMUTABLE){ + return xc; + } + stash = MOUSE_xc_stash(xc); - if(SvUV(gen) != mro_get_pkg_gen(stash)){ + if(SvUVX(gen) != mro_get_pkg_gen(stash)){ mouse_class_update_xc(aTHX_ metaclass, stash, xc); } @@ -198,10 +203,15 @@ mouse_get_xc(pTHX_ SV* const metaclass) { } HV* -mouse_build_args(pTHX_ SV* metaclass, SV* const klass, I32 const start, I32 const items, I32 const ax) { +mouse_buildargs(pTHX_ SV* metaclass, SV* const klass, I32 ax, I32 items) { HV* args; - if((items - start) == 1){ - SV* const args_ref = ST(start); + + /* shift @_ */ + ax++; + items--; + + if(items == 1){ + SV* const args_ref = ST(0); if(!IsHashRef(args_ref)){ if(!metaclass){ metaclass = get_metaclass(klass); } mouse_throw_error(metaclass, NULL, "Single parameters to new() must be a HASH ref"); @@ -214,12 +224,12 @@ mouse_build_args(pTHX_ SV* metaclass, SV* const klass, I32 const start, I32 cons args = newHV_mortal(); - if( ((items - start) % 2) != 0 ){ + if( (items % 2) != 0 ){ if(!metaclass){ metaclass = get_metaclass(klass); } mouse_throw_error(metaclass, NULL, "Odd number of parameters to new()"); } - for(i = start; i < items; i += 2){ + for(i = 0; i < items; i += 2){ (void)hv_store_ent(args, ST(i), newSVsv(ST(i+1)), 0U); } @@ -235,6 +245,10 @@ mouse_class_initialize_object(pTHX_ SV* const meta, SV* const object, HV* const I32 i; AV* triggers_queue = NULL; + assert(meta || object); + assert(args); + assert(SvTYPE(args) == SVt_PVHV); + ENTER; SAVETMPS; @@ -299,6 +313,28 @@ mouse_class_initialize_object(pTHX_ SV* const meta, SV* const object, HV* const LEAVE; } +static SV* +mouse_initialize_metaclass(pTHX_ SV* const klass) { + SV* meta = get_metaclass(klass); + + if(!SvOK(meta)){ + dSP; + PUSHMARK(SP); + + EXTEND(SP, 2); + mPUSHp("Mouse::Meta::Class", sizeof("Mouse::Meta::Class")-1); + PUSHs(klass); + PUTBACK; + + call_method("initialize", G_SCALAR); + SPAGAIN; + meta = POPs; + PUTBACK; + } + + return meta; +} + MODULE = Mouse PACKAGE = Mouse PROTOTYPES: DISABLE @@ -383,14 +419,16 @@ CODE: set_slot(methods, name, code); /* $self->{methods}{$name} = $code */ - /* TODO: name the CODE ref if it's anonymous */ - //code_entity = (CV*)SvRV(code_ref); - //if(CvANON(code_entity) - // && CvGV(code_entity) /* a cv under construction has no gv */ ){ + /* name the CODE ref if it's anonymous */ + { + CV* const code_entity = (CV*)SvRV(code_ref); + if(CvANON(code_entity) + && CvGV(code_entity) /* a cv under construction has no gv */ ){ - // CvGV(code_entity) = gv; - // CvANON_off(code_entity); - //} + CvGV(code_entity) = gv; + CvANON_off(code_entity); + } + } } MODULE = Mouse PACKAGE = Mouse::Meta::Class @@ -398,6 +436,17 @@ MODULE = Mouse PACKAGE = Mouse::Meta::Class BOOT: INSTALL_SIMPLE_READER(Class, roles); INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id); + newCONSTSUB(gv_stashpvs("Mouse::Meta::Class", TRUE), "constructor_class", + newSVpvs("Mouse::Meta::Method::Constructor::XS")); + newCONSTSUB(gv_stashpvs("Mouse::Meta::Class", TRUE), "destructor_class", + newSVpvs("Mouse::Meta::Method::Destructor::XS")); + + + newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Constructor::XS", TRUE), "_generate_constructor", + newRV_inc((SV*)get_cvs("Mouse::Object::new", TRUE))); + newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Destructor::XS", TRUE), "_generate_destructor", + newRV_inc((SV*)get_cvs("Mouse::Object::DESTROY", TRUE))); + void linearized_isa(SV* self) @@ -437,8 +486,8 @@ SV* new_object_(SV* meta, ...) CODE: { - HV* const args = mouse_build_args(aTHX_ meta, NULL, 1, items, ax); AV* const xc = mouse_get_xc(aTHX_ meta); + HV* const args = mouse_buildargs(aTHX_ meta, NULL, ax, items); RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc)); mouse_class_initialize_object(aTHX_ meta, RETVAL, args, FALSE); @@ -452,6 +501,16 @@ CODE: mouse_class_initialize_object(aTHX_ meta, object, args, ignore_triggers); } +void +__xc(SV* meta) +PPCODE: +{ + AV* const xc = mouse_get_xc(aTHX_ meta); + mXPUSHu(MOUSE_xc_flags(xc)); + mXPUSHs(newRV_inc((SV*)MOUSE_xc_buildall(xc))); + mXPUSHs(newRV_inc((SV*)MOUSE_xc_demolishall(xc))); +} + MODULE = Mouse PACKAGE = Mouse::Meta::Role BOOT: @@ -460,11 +519,135 @@ BOOT: MODULE = Mouse PACKAGE = Mouse::Object +SV* +new(SV* klass, ...) +CODE: +{ + SV* const meta = mouse_initialize_metaclass(aTHX_ klass); + AV* const xc = mouse_get_xc(aTHX_ meta); + UV const flags = MOUSE_xc_flags(xc); + SV* args; + AV* buildall; + I32 len, i; + + /* BUILDARGS */ + if(flags & MOUSEf_XC_HAS_BUILDARGS){ + SPAGAIN; + + PUSHMARK(SP); + EXTEND(SP, items); + for(i = 0; i < items; i++){ + PUSHs(ST(i)); + } + //SP += items; + PUTBACK; + call_method("BUILDARGS", G_SCALAR); + SPAGAIN; + args = POPs; + PUTBACK; + + if(!IsHashRef(args)){ + croak("BUILDARGS did not return a HASH reference"); + } + } + else{ + args = newRV_inc((SV*)mouse_buildargs(aTHX_ meta, klass, ax, items)); + sv_2mortal(args); + } + + /* new_object */ + RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc)); + mouse_class_initialize_object(aTHX_ meta, RETVAL, (HV*)SvRV(args), FALSE); + + /* BUILDALL */ + buildall = MOUSE_xc_buildall(xc); + len = AvFILLp(buildall) + 1; + for(i = 0; i < len; i++){ + dSP; + + PUSHMARK(SP); + EXTEND(SP, 2); + PUSHs(RETVAL); + PUSHs(args); + PUTBACK; + + call_sv(AvARRAY(buildall)[i], G_VOID | G_DISCARD); + } +} +OUTPUT: + RETVAL + +void +DESTROY(SV* object) +CODE: +{ + SV* const meta = get_metaclass(object); + AV* demolishall; + I32 len, i; + + if(!IsObject(object)){ + croak("You must not call DESTROY as a class method"); + } + + if(SvOK(meta)){ + AV* const xc = mouse_get_xc(aTHX_ meta); + + demolishall = MOUSE_xc_demolishall(xc); + } + else { + AV* const linearized_isa = mro_get_linear_isa(SvSTASH(SvRV(object))); + + len = AvFILLp(linearized_isa) + 1; + + demolishall = newAV_mortal(); + for(i = 0; i < len; i++){ + SV* const klass = MOUSE_av_at(linearized_isa, i); + HV* const st = gv_stashsv(klass, TRUE); + GV* const gv = stash_fetchs(st, "DEMOLISH", FALSE); + if(gv && GvCVu(gv)){ + av_push(demolishall, newRV_inc((SV*)GvCV(gv))); + } + } + } + + /* DEMOLISHALL */ + len = AvFILLp(demolishall) + 1; + if(len > 0){ + GV* const statusvalue = gv_fetchpvs("?", 0, SVt_PV); + SAVESPTR(GvSV(statusvalue)); /* local $? */ + SAVESPTR(ERRSV); /* local $@ */ + + GvSV(statusvalue) = sv_2mortal(newSViv(0)); + ERRSV = sv_2mortal(newSVpvs("")); + for(i = 0; i < len; i++){ + dSP; + + PUSHMARK(SP); + XPUSHs(object); + PUTBACK; + + call_sv(AvARRAY(demolishall)[i], G_VOID | G_DISCARD | G_EVAL); + if(SvTRUE(ERRSV)){ + SV* const e = newSVsv(ERRSV); + + FREETMPS; + LEAVE; + + sv_setsv(ERRSV, e); + SvREFCNT_dec(e); + croak(NULL); /* rethrow */ + } + } + } +} + HV* BUILDARGS(SV* klass, ...) CODE: { - RETVAL = mouse_build_args(aTHX_ NULL, klass, 1, items, ax); + RETVAL = mouse_buildargs(aTHX_ NULL, klass, ax, items); } OUTPUT: RETVAL + +