Use bit-or, not simple assignment
[gitmo/Mouse.git] / xs-src / Mouse.xs
index 12bcf89..137e5a0 100644 (file)
@@ -2,6 +2,8 @@
 
 SV* mouse_package;
 SV* mouse_namespace;
+SV* mouse_methods;
+SV* mouse_name;
 
 MODULE = Mouse  PACKAGE = Mouse::Util
 
@@ -10,10 +12,14 @@ PROTOTYPES: DISABLE
 BOOT:
     mouse_package   = newSVpvs_share("package");
     mouse_namespace = newSVpvs_share("namespace");
+    mouse_methods   = newSVpvs_share("methods");
+    mouse_name      = newSVpvs_share("name");
+
+    MOUSE_CALL_BOOT(Mouse__Util__TypeConstraints);
 
 
 bool
-is_class_loaded(SV* sv = &PL_sv_undef)
+is_class_loaded(SV* sv)
 
 void
 get_code_info(CV* code)
@@ -80,36 +86,45 @@ CODE:
 OUTPUT:
     RETVAL
 
+void
+generate_isa_predicate_for(SV* klass, const char* predicate_name = NULL)
+PPCODE:
+{
+    STRLEN klass_len;
+    const char* klass_pv;
+    HV* stash;
+    CV* xsub;
+
+    if(!SvOK(klass)){
+        croak("You must define a class name for generate_for");
+    }
+    klass_pv = SvPV_const(klass, klass_len);
+    klass_pv = mouse_canonicalize_package_name(klass_pv);
 
-MODULE = Mouse  PACKAGE = Mouse::Util::TypeConstraints
+    if(strNE(klass_pv, "UNIVERSAL")){
+        static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
 
-void
-Item(SV* sv = &PL_sv_undef)
-ALIAS:
-    Any        = MOUSE_TC_ANY
-    Item       = MOUSE_TC_ITEM
-    Undef      = MOUSE_TC_UNDEF
-    Defined    = MOUSE_TC_DEFINED
-    Bool       = MOUSE_TC_BOOL
-    Value      = MOUSE_TC_VALUE
-    Ref        = MOUSE_TC_REF
-    Str        = MOUSE_TC_STR
-    Num        = MOUSE_TC_NUM
-    Int        = MOUSE_TC_INT
-    ScalarRef  = MOUSE_TC_SCALAR_REF
-    ArrayRef   = MOUSE_TC_ARRAY_REF
-    HashRef    = MOUSE_TC_HASH_REF
-    CodeRef    = MOUSE_TC_CODE_REF
-    GlobRef    = MOUSE_TC_GLOB_REF
-    FileHandle = MOUSE_TC_FILEHANDLE
-    RegexpRef  = MOUSE_TC_REGEXP_REF
-    Object     = MOUSE_TC_OBJECT
-    ClassName  = MOUSE_TC_CLASS_NAME
-    RoleName   = MOUSE_TC_ROLE_NAME
-CODE:
-    SvGETMAGIC(sv);
-    ST(0) = boolSV( mouse_tc_check(aTHX_ ix, sv) );
-    XSRETURN(1);
+        xsub = newXS(predicate_name, XS_isa_check, __FILE__);
+
+        stash = gv_stashpvn(klass_pv, klass_len, GV_ADD);
+
+        CvXSUBANY(xsub).any_ptr = sv_magicext(
+            (SV*)xsub,
+            (SV*)stash, /* mg_obj */
+            PERL_MAGIC_ext,
+            &mouse_util_type_constraints_vtbl,
+            klass_pv,   /* mg_ptr */
+            klass_len   /* mg_len */
+        );
+    }
+    else{
+        xsub = newXS(predicate_name, XS_isa_check_for_universal, __FILE__);
+    }
+
+    if(predicate_name == NULL){ /* anonymous predicate */
+        XPUSHs( newRV_noinc((SV*)xsub) );
+    }
+}
 
 
 MODULE = Mouse  PACKAGE = Mouse::Meta::Module
@@ -123,15 +138,70 @@ HV*
 namespace(SV* self)
 CODE:
 {
-    SV* const package = mouse_instance_get_slot(self, mouse_package);
+    SV* const package = mouse_instance_get_slot(aTHX_ self, mouse_package);
     if(!(package && SvOK(package))){
-        croak("No package name");
+        croak("No package name defined");
     }
     RETVAL = gv_stashsv(package, GV_ADDMULTI);
 }
 OUTPUT:
     RETVAL
 
+# ignore extra arguments for extensibility
+void
+add_method(SV* self, SV* name, SV* code, ...)
+CODE:
+{
+    SV* const package = mouse_instance_get_slot(aTHX_ self, mouse_package); /* $self->{package} */
+    SV* const methods = mouse_instance_get_slot(aTHX_ self, mouse_methods); /* $self->{methods} */
+    GV* gv;
+    SV* code_ref;
+
+    if(!(package && SvOK(package))){
+        croak("No package name defined");
+    }
+
+    SvGETMAGIC(name);
+    SvGETMAGIC(code);
+
+    if(!SvOK(name)){
+        mouse_throw_error(self, NULL, "You must define a method name");
+    }
+    if(!SvROK(code)){
+        mouse_throw_error(self, NULL, "You must define a CODE reference");
+    }
+
+    code_ref = code;
+    if(SvTYPE(SvRV(code_ref)) != SVt_PVCV){
+        SV*  sv = code_ref;  /* used in tryAMAGICunDEREF */
+        SV** sp = &sv;       /* used in tryAMAGICunDEREF */
+        tryAMAGICunDEREF(to_cv); /* try \&{$code} */
+        if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV){
+            mouse_throw_error(self, NULL, "Not a CODE reference");
+        }
+        code_ref = sv;
+    }
+
+    /*  *{$package . '::' . $name} -> *gv */
+    gv = gv_fetchpv(form("%"SVf"::%"SVf, package, name), GV_ADDMULTI, SVt_PVCV);
+    if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
+        SvREFCNT_dec(GvCV(gv));
+        GvCV(gv) = NULL;
+    }
+    sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
+
+    mouse_instance_set_slot(aTHX_ 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 */ ){
+
+    //    CvGV(code_entity) = gv;
+    //    CvANON_off(code_entity);
+    //}
+}
+
 MODULE = Mouse  PACKAGE = Mouse::Meta::Class
 
 BOOT:
@@ -204,6 +274,9 @@ BOOT:
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
 
+    newCONSTSUB(gv_stashpvs("Mouse::Meta::Attribute", TRUE), "accessor_metaclass",
+        newSVpvs("Mouse::Meta::Method::Accessor::XS"));
+
 MODULE = Mouse  PACKAGE = Mouse::Meta::TypeConstraint
 
 BOOT:
@@ -215,3 +288,58 @@ BOOT:
     INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
 
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
+
+
+MODULE = Mouse  PACKAGE = Mouse::Meta::Method::Accessor::XS
+
+CV*
+_generate_accessor(klass, SV* attr, metaclass)
+CODE:
+{
+    RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_accessor);
+}
+OUTPUT:
+    RETVAL
+
+CV*
+_generate_reader(klass, SV* attr, metaclass)
+CODE:
+{
+    RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_reader);
+}
+OUTPUT:
+    RETVAL
+
+CV*
+_generate_writer(klass, SV* attr, metaclass)
+CODE:
+{
+    RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_writer);
+}
+OUTPUT:
+    RETVAL
+
+CV*
+_generate_clearer(klass, SV* attr, metaclass)
+CODE:
+{
+    SV* const slot = mcall0s(attr, "name");
+    STRLEN len;
+    const char* const pv = SvPV_const(slot, len);
+    RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_clearer);
+}
+OUTPUT:
+    RETVAL
+
+CV*
+_generate_predicate(klass, SV* attr, metaclass)
+CODE:
+{
+    SV* const slot = mcall0s(attr, "name");
+    STRLEN len;
+    const char* const pv = SvPV_const(slot, len);
+    RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_predicate);
+}
+OUTPUT:
+    RETVAL
+