return @attr;
}
-sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
+sub linearized_isa;
sub new_object {
my $self = shift;
return 1 if $self->{methods}{$method_name};
- my $code = do{
- no strict 'refs';
- no warnings 'once';
- *{ $self->{package} . '::' . $method_name }{CODE};
- };
+ my $code = $self->_get_code_ref($method_name);
return $code && $self->_code_is_mine($code);
}
or $self->throw_error('You must define a method name');
return $self->{methods}{$method_name} ||= do{
- my $code = do{
- no strict 'refs';
- no warnings 'once';
- *{$self->{package} . '::' . $method_name}{CODE};
- };
-
+ my $code = $self->_get_code_ref($method_name);
($code && $self->_code_is_mine($code)) ? $code : undef;
};
}
sub roles { $_[0]->{roles} }
+sub linearized_isa { @{ get_linear_isa($_[0]->{package}) } }
+
package
Mouse::Meta::Role;
#include "ppport.h"
+#ifndef newSVpvs_share
+#define newSVpvs_share(s) Perl_newSVpvn_share(aTHX_ s, sizeof(s)-1, 0U)
+#endif
+
+#ifndef mro_get_linear_isa
+#define no_mro_get_linear_isa
+#define mro_get_linear_isa(stash) mouse_mro_get_linear_isa(aTHX_ stash)
+AV* mouse_mro_get_linear_isa(pTHX_ HV* const stash)
+#endif /* !mro_get_linear_isa */
+
+#ifndef mro_get_pkg_gen
+#ifdef no_mro_get_linear_isa
+#define mro_get_pkg_gen(stash) ((void)stash, PL_sub_generation)
+#else
+#define mro_get_pkg_gen(stash) (HvAUX(stash)->xhv_mro_meta ? HvAUX(stash)->xhv_mro_meta->pkg_gen : (U32)0)
+#endif /* !no_mro_get_linear_isa */
+#endif /* mro_get_package_gen */
+
#define MOUSE_CALL_BOOT(name) STMT_START { \
EXTERN_C XS(CAT2(boot_, name)); \
PUSHMARK(SP); \
CALL_FPTR(CAT2(boot_, name))(aTHX_ cv); \
} STMT_END
+extern SV* mouse_package;
+extern SV* mouse_namespace;
#define is_class_loaded(sv) mouse_is_class_loaded(aTHX_ sv)
bool mouse_is_class_loaded(pTHX_ SV*);
#include "mouse.h"
+SV* mouse_package;
+SV* mouse_namespace;
+
MODULE = Mouse PACKAGE = Mouse::Util
PROTOTYPES: DISABLE
+BOOT:
+ mouse_package = newSVpvs_share("package");
+ mouse_namespace = newSVpvs_share("namespace");
+
+
bool
is_class_loaded(SV* sv = &PL_sv_undef)
INSTALL_SIMPLE_READER_WITH_KEY(Module, _attribute_map, attributes);
HV*
-namespace(HV* self)
+namespace(SV* self)
CODE:
{
- SV** svp = hv_fetchs(self, "package", FALSE);
- if(!(svp && SvOK(*svp))){
+ SV* const package = mouse_instance_get_slot(self, mouse_package);
+ if(!(package && SvOK(package))){
croak("No package name");
}
- RETVAL = gv_stashsv(*svp, GV_ADDMULTI);
+ RETVAL = gv_stashsv(package, GV_ADDMULTI);
+}
+OUTPUT:
+ RETVAL
+
+CV*
+_get_code_ref(SV* self, SV* name)
+CODE:
+{
+ SV* const stash_ref = mcall0(self, mouse_namespace); /* $self->namespace */
+ HV* stash;
+ HE* he;
+ if(!(SvROK(stash_ref) && SvTYPE(SvRV(stash_ref)) == SVt_PVHV)){
+ croak("namespace() didn't return a HASH reference");
+ }
+ stash = (HV*)SvRV(stash_ref);
+ he = hv_fetch_ent(stash, name, FALSE, 0U);
+ if(he){
+ GV* const gv = (GV*)hv_iterval(stash, he);
+ if(isGV(gv)){
+ RETVAL = GvCVu(gv);
+ }
+ else{ /* special constant or stub */
+ STRLEN len;
+ const char* const pv = SvPV_const(name, len);
+ gv_init(gv, stash, pv, len, GV_ADDMULTI);
+ RETVAL = GvCVu(gv);
+ }
+ }
+ else{
+ RETVAL = NULL;
+ }
+
+ if(!RETVAL){
+ XSRETURN_UNDEF;
+ }
}
OUTPUT:
RETVAL
+
MODULE = Mouse PACKAGE = Mouse::Meta::Class
BOOT:
INSTALL_SIMPLE_READER(Class, roles);
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
+void
+linearized_isa(SV* self)
+PPCODE:
+{
+ SV* const stash_ref = mcall0(self, mouse_namespace); /* $self->namespace */
+ AV* linearized_isa;
+ I32 len;
+ I32 i;
+ if(!(SvROK(stash_ref) && SvTYPE(SvRV(stash_ref)) == SVt_PVHV)){
+ croak("namespace() didn't return a HASH reference");
+ }
+ linearized_isa = mro_get_linear_isa((HV*)SvRV(stash_ref));
+ len = AvFILLp(linearized_isa) + 1;
+ EXTEND(SP, len);
+ for(i = 0; i < len; i++){
+ PUSHs(AvARRAY(linearized_isa)[i]);
+ }
+}
+
MODULE = Mouse PACKAGE = Mouse::Meta::Role
BOOT:
INSTALL_SIMPLE_READER_WITH_KEY(Role, get_roles, roles);
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Role, is_anon_role, anon_serial_id);
-
MODULE = Mouse PACKAGE = Mouse::Meta::Attribute
BOOT:
#include "mouse.h"
+#define ISA_CACHE "::LINEALIZED_ISA_CACHE::"
+
+#ifndef no_mro_get_linear_isa
+AV*
+mouse_mro_get_linear_isa(pTHX_ HV* const stash){
+ GV* const cachegv = *(GV**)hv_fetchs(stash, ISA_CACHE, TRUE);
+ AV* isa;
+ SV* gen;
+ CV* get_linear_isa;
+
+ if(!isGV(cachegv))
+ gv_init(cachegv, stash, ISA_CACHE, sizeof(ISA_CACHE)-1, TRUE);
+
+ isa = GvAVn(cachegv);
+ gen = GvSVn(cachegv);
+
+
+ if(SvIOK(gen) && SvIVX(gen) == (IV)mro_get_pkg_gen(stash)){
+ return isa; /* returns the cache if available */
+ }
+ else{
+ SvREADONLY_off(isa);
+ av_clear(isa);
+ }
+
+ get_linear_isa = get_cv("Mouse::Util::get_linear_isa", TRUE);
+
+ {
+ SV* avref;
+ dSP;
+
+ ENTER;
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ mXPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
+ PUTBACK;
+
+ call_sv((SV*)get_linear_isa, G_SCALAR);
+
+ SPAGAIN;
+ avref = POPs;
+ PUTBACK;
+
+ if(SvROK(avref) && SvTYPE(SvRV(avref)) == SVt_PVAV){
+ AV* const av = (AV*)SvRV(avref);
+ I32 const len = AvFILLp(av) + 1;
+ I32 i;
+
+ for(i = 0; i < len; i++){
+ HV* const stash = gv_stashsv(AvARRAY(av)[i], FALSE);
+ if(stash)
+ av_push(isa, newSVpv(HvNAME(stash), 0));
+ }
+ SvREADONLY_on(isa);
+ }
+ else{
+ Perl_croak(aTHX_ "Mouse:Util::get_linear_isa() didn't return an ARRAY reference");
+ }
+
+ FREETMPS;
+ LEAVE;
+ }
+
+ sv_setiv(gen, (IV)mro_get_pkg_gen(stash));
+ return GvAV(cachegv);
+}
+#endif /* !no_mor_get_linear_isa */
+
+
/* equivalent to "blessed($x) && $x->isa($klass)" */
bool
mouse_is_instance_of(pTHX_ SV* const sv, SV* const klass){