/*
- * full definition of built-in type constraints (ware in Moose::Util::TypeConstraints::OptimizedConstraints)
+ * TypeConstraint stuff
+ * - Mouse::Util::TypeConstraints (including OptimizedConstraionts)
+ * - Mouse::Meta::TypeConstraint
*/
#include "mouse.h"
-#if PERL_BCDVERSION >= 0x5008005
-#define LooksLikeNumber(sv) looks_like_number(sv)
-#else
-#define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : SvNIOKp(sv) )
-#endif
-
#ifndef SvRXOK
#define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr))
#endif
typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
+/*
+ NOTE: mouse_tc_check() handles GETMAGIC
+*/
int
mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
CV* const cv = (CV*)SvRV(tc_code);
assert(CvXSUBANY(cv).any_ptr != NULL);
assert(mg->mg_ptr != NULL);
+ SvGETMAGIC(sv);
/* call the check function directly, skipping call_sv() */
return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
}
call_sv(tc_code, G_SCALAR);
SPAGAIN;
- ok = SvTRUEx(POPs);
+ ok = sv_true(POPs);
PUTBACK;
FREETMPS;
mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
- if(SvTRUE(sv)){
+ if(sv_true(sv)){
if(SvIOKp(sv)){
return SvIVX(sv) == 1;
}
}
}
else{
- /* false must be boolean */
+ /* any false value must be boolean */
return TRUE;
}
}
return LooksLikeNumber(sv);
}
+static int
+S_nv_is_integer(pTHX_ NV const nv) {
+ if(nv == (NV)(IV)nv){
+ return TRUE;
+ }
+ else {
+ char buf[64]; /* Must fit sprintf/Gconvert of longest NV */
+ char* p;
+ (void)Gconvert(nv, NV_DIG, 0, buf);
+ p = &buf[0];
+
+ /* -?[0-9]+ */
+ if(*p == '-') p++;
+
+ while(*p){
+ if(!isDIGIT(*p)){
+ return FALSE;
+ }
+ p++;
+ }
+ return TRUE;
+ }
+}
+
int
mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
if(SvIOKp(sv)){
return TRUE;
}
- else if(SvNOKp(sv)){
- NV const nv = SvNVX(sv);
- return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
+ else if(SvNOKp(sv)) {
+ return S_nv_is_integer(aTHX_ SvNVX(sv));
}
else if(SvPOKp(sv)){
int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
assert(sv);
if(is_class_loaded(sv)){
int ok;
- SV* meta;
- dSP;
ENTER;
SAVETMPS;
- PUSHMARK(SP);
- XPUSHs(sv);
- PUTBACK;
- call_pv("Mouse::Util::get_metaclass_by_name", G_SCALAR);
- SPAGAIN;
- meta = POPs;
- PUTBACK;
-
- ok = is_an_instance_of("Mouse::Meta::Role", meta);
+ ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
FREETMPS;
LEAVE;
}
int
-mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
+mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
assert(sv);
- return SvROK(sv) && !SvOBJECT(SvRV(sv)) && (SvTYPE(SvRV(sv)) <= SVt_PVLV && !isGV(SvRV(sv)));
+ if(SvROK(sv)){
+ sv = SvRV(sv);
+ return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
+ }
+ return FALSE;
}
int
mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
- return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV;
+ return IsArrayRef(sv);
}
int
mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
- return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV;
+ return IsHashRef(sv);
}
int
mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
assert(sv);
- return SvROK(sv) && !SvOBJECT(SvRV(sv))&& SvTYPE(SvRV(sv)) == SVt_PVCV;
+ return IsCodeRef(sv);
}
int
static int
mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
- if(mouse_tc_ArrayRef(aTHX_ NULL, sv)){
+ if(IsArrayRef(sv)){
AV* const av = (AV*)SvRV(sv);
I32 const len = av_len(av) + 1;
I32 i;
for(i = 0; i < len; i++){
SV* const value = *av_fetch(av, i, TRUE);
- SvGETMAGIC(value);
if(!mouse_tc_check(aTHX_ param, value)){
return FALSE;
}
hv_iterinit(hv);
while((he = hv_iternext(hv))){
SV* const value = hv_iterval(hv, he);
- SvGETMAGIC(value);
if(!mouse_tc_check(aTHX_ param, value)){
+ hv_iterinit(hv); /* reset */
return FALSE;
}
}
#define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
typedef struct sui_cxt{
GV* universal_isa;
+ GV* universal_can;
} my_cxt_t;
START_MY_CXT
return FALSE;
}
+#define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
+#define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
+
+STATIC_INLINE GV*
+mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
+ GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
+ if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
+ return *gvp;
+ }
+
+ return gv_fetchmeth_autoload(stash, name, namelen, 0);
+}
+
int
mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
assert(stash);
if(IsObject(instance)){
dMY_CXT;
HV* const instance_stash = SvSTASH(SvRV(instance));
- GV* const instance_isa = gv_fetchmeth_autoload(instance_stash, "isa", sizeof("isa")-1, 0);
+ GV* const myisa = find_method_pvs(instance_stash, "isa");
/* the instance has no own isa method */
- if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
+ if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){
return stash == instance_stash
|| mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
}
/* the instance has its own isa method */
else {
- int retval;
- dSP;
+ SV* package;
+ int ok;
ENTER;
SAVETMPS;
- PUSHMARK(SP);
- EXTEND(SP, 2);
- PUSHs(instance);
- mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
- PUTBACK;
-
- call_sv((SV*)instance_isa, G_SCALAR);
-
- SPAGAIN;
-
- retval = SvTRUEx(POPs);
-
- PUTBACK;
+ package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
+ ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package)));
FREETMPS;
LEAVE;
- return retval;
+ return ok;
}
}
return FALSE;
return SvROK(sv) && SvOBJECT(SvRV(sv));
}
+static int
+mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
+ if(IsObject(instance)){
+ dMY_CXT;
+ HV* const mystash = SvSTASH(SvRV(instance));
+ GV* const mycan = find_method_pvs(mystash, "can");
+ bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
+ I32 const len = AvFILLp(methods) + 1;
+ I32 i;
+ for(i = 0; i < len; i++){
+ SV* const name = MOUSE_av_at(methods, i);
+
+ if(use_builtin){
+ if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
+ return FALSE;
+ }
+ }
+ else{
+ bool ok;
+
+ ENTER;
+ SAVETMPS;
+
+ ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name)));
+
+ FREETMPS;
+ LEAVE;
+
+ if(!ok){
+ return FALSE;
+ }
+ }
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+
static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
static CV*
param, /* mg_obj: refcnt will be increased */
PERL_MAGIC_ext,
&mouse_util_type_constraints_vtbl,
- (void*)fptr, /* mg_ptr */
+ (char*)fptr, /* mg_ptr */
0 /* mg_len: 0 for static data */
);
STRLEN klass_len;
const char* klass_pv = SvPV_const(klass, klass_len);
SV* param;
- void* fptr;
+ check_fptr_t fptr;
klass_pv = mouse_canonicalize_package_name(klass_pv);
if(strNE(klass_pv, "UNIVERSAL")){
param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
- fptr = (void*)mouse_is_an_instance_of;
+ fptr = (check_fptr_t)mouse_is_an_instance_of;
}
else{
param = NULL;
- fptr = (void*)mouse_is_an_instance_of_universal;
+ fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
}
return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
}
+CV*
+mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
+ AV* av;
+ AV* const param = newAV_mortal();
+ I32 len;
+ I32 i;
+
+ must_ref(methods, "an ARRAY ref for method names", SVt_PVAV);
+ av = (AV*)SvRV(methods);
+
+ len = av_len(av) + 1;
+ for(i = 0; i < len; i++){
+ SV* const name = *av_fetch(av, i, TRUE);
+ STRLEN pvlen;
+ const char* const pv = SvPV_const(name, pvlen);
+
+ av_push(param, newSVpvn_share(pv, pvlen, 0U));
+ }
+
+ return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
+}
+
+
XS(XS_Mouse_constraint_check) {
dVAR;
dXSARGS;
MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
+ SV* sv;
if(items < 1){
croak("Too few arguments for type constraint check functions");
}
- SvGETMAGIC( ST(0) );
- ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
+ sv = ST(0);
+ SvGETMAGIC(sv);
+ ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv) );
XSRETURN(1);
}
+XS(XS_Mouse_TypeConstraint_fallback); /* -Wmissing-prototypes */
+XS(XS_Mouse_TypeConstraint_fallback) {
+ dXSARGS;
+ PERL_UNUSED_VAR(cv);
+ PERL_UNUSED_VAR(items);
+ XSRETURN_EMPTY;
+}
+
static void
setup_my_cxt(pTHX_ pMY_CXT){
MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
+
+ MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
+ SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
}
#define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
+#define MTC_CLASS "Mouse::Meta::TypeConstraint"
+
MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
PROTOTYPES: DISABLE
{
check_fptr_t fptr;
SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
- if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){
+ if(!IsCodeRef(tc_code)){
croak("_compiled_type_constraint didn't return a CODE reference");
}
MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
+BOOT:
+ INSTALL_SIMPLE_READER(TypeConstraint, name);
+ INSTALL_SIMPLE_READER(TypeConstraint, parent);
+ INSTALL_SIMPLE_READER(TypeConstraint, message);
+
+ INSTALL_SIMPLE_READER(TypeConstraint, type_parameter);
+
+ INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
+ INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
+
+ INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
+ INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
+
+ /* overload stuff */
+ PL_amagic_generation++;
+ (void)newXS( MTC_CLASS "::()",
+ XS_Mouse_TypeConstraint_fallback, file);
+
+ /* fallback => 1 */
+ sv_setsv(
+ get_sv( MTC_CLASS "::()", GV_ADD ),
+ &PL_sv_yes
+ );
+
+ /* '""' => '_as_string' */
+ {
+ SV* const code_ref = sv_2mortal(newRV_inc(
+ (SV*)get_cv( MTC_CLASS "::_as_string", GV_ADD )));
+ sv_setsv_mg(
+ (SV*)gv_fetchpvs( MTC_CLASS "::(\"\"", GV_ADDMULTI, SVt_PVCV ),
+ code_ref );
+ }
+
+ /* '0+' => '_identity' */
+ {
+ SV* const code_ref = sv_2mortal(newRV_inc(
+ (SV*)get_cv( MTC_CLASS "::_identity", GV_ADD )));
+ sv_setsv_mg(
+ (SV*)gv_fetchpvs( MTC_CLASS "::(0+", GV_ADDMULTI, SVt_PVCV ),
+ code_ref );
+ }
+
+ /* '|' => '_unite' */
+ {
+ SV* const code_ref = sv_2mortal(newRV_inc(
+ (SV*)get_cv( MTC_CLASS "::_unite", GV_ADD )));
+ sv_setsv_mg(
+ (SV*)gv_fetchpvs( MTC_CLASS "::(|", GV_ADDMULTI, SVt_PVCV ),
+ code_ref );
+ }
+
+
void
compile_type_constraint(SV* self)
CODE:
{
- AV* const checks = newAV();
+ AV* const checks = newAV_mortal();
SV* check; /* check function */
SV* parent;
SV* types_ref;
- sv_2mortal((SV*)checks);
-
for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
check = get_slots(parent, "hand_optimized_type_constraint");
if(check && SvOK(check)){
I32 len;
I32 i;
- if(!mouse_tc_ArrayRef(aTHX_ NULL, types_ref)){
+ if(!IsArrayRef(types_ref)){
croak("Not an ARRAY reference");
}
types = (AV*)SvRV(types_ref);
len = av_len(types) + 1;
- union_checks = newAV();
- sv_2mortal((SV*)union_checks);
+ union_checks = newAV_mortal();
for(i = 0; i < len; i++){
SV* const tc = *av_fetch(types, i, TRUE);
SV* const c = get_slots(tc, "compiled_type_constraint");
if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
- sv_dump(self);
- croak("'%"SVf"' has no compiled type constraint", self);
+ mouse_throw_error(self, c, "'%"SVf"' has no compiled type constraint", self);
}
av_push(union_checks, newSVsv(c));
}
else{
check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
}
- set_slots(self, "compiled_type_constraint", check);
+ (void)set_slots(self, "compiled_type_constraint", check);
}
+bool
+check(SV* self, SV* sv)
+CODE:
+{
+ SV* const check = get_slots(self, "compiled_type_constraint");
+ if(!(check && mouse_tc_CodeRef(aTHX_ NULL, check))){
+ mouse_throw_error(self, check, "'%"SVf"' has no compiled type constraint", self);
+ }
+ RETVAL = mouse_tc_check(aTHX_ check, sv) ? TRUE : FALSE;
+}
+OUTPUT:
+ RETVAL
+