2 * full definition of built-in type constraints (ware in Moose::Util::TypeConstraints::OptimizedConstraints)
7 #if PERL_BCDVERSION >= 0x5008005
8 #define LooksLikeNumber(sv) looks_like_number(sv)
10 #define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : SvNIOKp(sv) )
14 #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr))
17 typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
20 NOTE: mouse_tc_check() handles GETMAGIC
23 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
24 CV* const cv = (CV*)SvRV(tc_code);
25 assert(SvTYPE(cv) == SVt_PVCV);
28 if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */
29 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
31 assert(CvXSUBANY(cv).any_ptr != NULL);
32 assert(mg->mg_ptr != NULL);
34 /* call the check function directly, skipping call_sv() */
35 return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
48 call_sv(tc_code, G_SCALAR);
62 The following type check functions return an integer, not a bool, to keep them simple,
63 so if you assign these return value to bool variable, you must use "expr ? TRUE : FALSE".
67 mouse_tc_Any(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv PERL_UNUSED_DECL) {
73 mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
78 return SvIVX(sv) == 1;
81 return SvNVX(sv) == 1.0;
83 else if(SvPOKp(sv)){ /* "1" */
84 return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
91 /* any false value must be boolean */
97 mouse_tc_Undef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
103 mouse_tc_Defined(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
109 mouse_tc_Value(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
111 return SvOK(sv) && !SvROK(sv);
115 mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
117 return LooksLikeNumber(sv);
121 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
127 NV const nv = SvNVX(sv);
128 return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
131 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
133 return !(num_type & IS_NUMBER_NOT_INT);
140 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
142 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
146 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
148 return is_class_loaded(sv);
152 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
154 if(is_class_loaded(sv)){
160 ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
171 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
177 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
181 return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
187 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
189 return IsArrayRef(sv);
193 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
195 return IsHashRef(sv);
199 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
201 return IsCodeRef(sv);
205 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
211 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
213 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
217 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
221 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
223 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
224 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
225 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
227 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
232 return is_an_instance_of("IO::Handle", sv);
236 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
238 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
241 /* Parameterized type constraints */
244 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
246 AV* const av = (AV*)SvRV(sv);
247 I32 const len = av_len(av) + 1;
249 for(i = 0; i < len; i++){
250 SV* const value = *av_fetch(av, i, TRUE);
251 if(!mouse_tc_check(aTHX_ param, value)){
261 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
262 if(mouse_tc_HashRef(aTHX_ NULL, sv)){
263 HV* const hv = (HV*)SvRV(sv);
267 while((he = hv_iternext(hv))){
268 SV* const value = hv_iterval(hv, he);
269 if(!mouse_tc_check(aTHX_ param, value)){
270 hv_iterinit(hv); /* reset */
280 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
282 return mouse_tc_check(aTHX_ param, sv);
288 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
289 I32 const len = AvFILLp(types) + 1;
292 for(i = 0; i < len; i++){
293 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
302 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
303 I32 const len = AvFILLp(types) + 1;
310 for(i = 0; i < len; i++){
311 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
323 * This class_type generator is taken from Scalar::Util::Instance
326 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
327 typedef struct sui_cxt{
333 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
334 #define MG_klass_pv(mg) ((mg)->mg_ptr)
335 #define MG_klass_len(mg) ((mg)->mg_len)
338 mouse_canonicalize_package_name(const char* name){
340 /* "::Foo" -> "Foo" */
341 if(name[0] == ':' && name[1] == ':'){
345 /* "main::main::main::Foo" -> "Foo" */
346 while(strnEQ(name, "main::", sizeof("main::")-1)){
347 name += sizeof("main::")-1;
354 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
355 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
356 SV** svp = AvARRAY(linearized_isa);
357 SV** const end = svp + AvFILLp(linearized_isa) + 1;
361 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
369 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
370 #define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
373 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
374 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
375 if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
379 return gv_fetchmeth_autoload(stash, name, namelen, 0);
383 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
385 assert(SvTYPE(stash) == SVt_PVHV);
387 if(IsObject(instance)){
389 HV* const instance_stash = SvSTASH(SvRV(instance));
390 GV* const instance_isa = find_method_pvs(instance_stash, "isa");
392 /* the instance has no own isa method */
393 if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
394 return stash == instance_stash
395 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
397 /* the instance has its own isa method */
408 mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
411 call_sv((SV*)instance_isa, G_SCALAR);
414 retval = sv_true(POPs);
427 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
428 PERL_UNUSED_ARG(data);
429 return SvROK(sv) && SvOBJECT(SvRV(sv));
433 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
434 if(IsObject(instance)){
436 HV* const mystash = SvSTASH(SvRV(instance));
437 GV* const mycan = find_method_pvs(mystash, "can");
438 bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
439 I32 const len = AvFILLp(methods) + 1;
441 for(i = 0; i < len; i++){
442 SV* const name = MOUSE_av_at(methods, i);
445 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
459 PUSHs(sv_mortalcopy(name));
462 call_method("can", G_SCALAR);
481 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
484 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
487 xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
488 CvXSUBANY(xsub).any_ptr = sv_magicext(
490 param, /* mg_obj: refcnt will be increased */
492 &mouse_util_type_constraints_vtbl,
493 (char*)fptr, /* mg_ptr */
494 0 /* mg_len: 0 for static data */
498 sv_2mortal((SV*)xsub);
505 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
507 const char* klass_pv = SvPV_const(klass, klass_len);
511 klass_pv = mouse_canonicalize_package_name(klass_pv);
513 if(strNE(klass_pv, "UNIVERSAL")){
514 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
515 fptr = (check_fptr_t)mouse_is_an_instance_of;
520 fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
523 return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
527 mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
529 AV* const param = newAV_mortal();
534 if(!IsArrayRef(methods)){
535 croak("You must pass an ARRAY ref method names");
537 av = (AV*)SvRV(methods);
539 len = av_len(av) + 1;
540 for(i = 0; i < len; i++){
541 SV* const name = *av_fetch(av, i, TRUE);
543 const char* const pv = SvPV_const(name, pvlen);
545 av_push(param, newSVpvn_share(pv, pvlen, 0U));
548 return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
552 XS(XS_Mouse_constraint_check) {
555 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
558 croak("Too few arguments for type constraint check functions");
562 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
567 setup_my_cxt(pTHX_ pMY_CXT){
568 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
569 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
571 MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
572 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
575 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
577 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
580 VERSIONCHECK: DISABLE
585 setup_my_cxt(aTHX_ aMY_CXT);
587 /* setup built-in type constraints */
597 DEFINE_TC(ScalarRef);
602 DEFINE_TC(FileHandle);
603 DEFINE_TC(RegexpRef);
605 DEFINE_TC(ClassName);
616 setup_my_cxt(aTHX_ aMY_CXT);
617 PERL_UNUSED_VAR(items);
620 #endif /* !USE_ITHREADS */
622 #define MOUSE_TC_MAYBE 0
623 #define MOUSE_TC_ARRAY_REF 1
624 #define MOUSE_TC_HASH_REF 2
627 _parameterize_ArrayRef_for(SV* param)
629 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
630 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
631 _parameterize_Maybe_for = MOUSE_TC_MAYBE
635 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
636 if(!IsCodeRef(tc_code)){
637 croak("_compiled_type_constraint didn't return a CODE reference");
641 case MOUSE_TC_ARRAY_REF:
642 fptr = mouse_parameterized_ArrayRef;
644 case MOUSE_TC_HASH_REF:
645 fptr = mouse_parameterized_HashRef;
647 default: /* Maybe type */
648 fptr = mouse_parameterized_Maybe;
650 RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
655 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
658 INSTALL_SIMPLE_READER(TypeConstraint, name);
659 INSTALL_SIMPLE_READER(TypeConstraint, parent);
660 INSTALL_SIMPLE_READER(TypeConstraint, message);
662 INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
663 INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
665 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
668 compile_type_constraint(SV* self)
671 AV* const checks = newAV_mortal();
672 SV* check; /* check function */
676 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
677 check = get_slots(parent, "hand_optimized_type_constraint");
678 if(check && SvOK(check)){
679 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
680 croak("Not a CODE reference");
682 av_unshift(checks, 1);
683 av_store(checks, 0, newSVsv(check));
684 break; /* a hand optimized constraint must include all the parent */
687 check = get_slots(parent, "constraint");
688 if(check && SvOK(check)){
689 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
690 croak("Not a CODE reference");
692 av_unshift(checks, 1);
693 av_store(checks, 0, newSVsv(check));
697 check = get_slots(self, "constraint");
698 if(check && SvOK(check)){
699 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
700 croak("Not a CODE reference");
702 av_push(checks, newSVsv(check));
705 types_ref = get_slots(self, "type_constraints");
706 if(types_ref && SvOK(types_ref)){ /* union type */
713 if(!IsArrayRef(types_ref)){
714 croak("Not an ARRAY reference");
716 types = (AV*)SvRV(types_ref);
717 len = av_len(types) + 1;
719 union_checks = newAV_mortal();
721 for(i = 0; i < len; i++){
722 SV* const tc = *av_fetch(types, i, TRUE);
723 SV* const c = get_slots(tc, "compiled_type_constraint");
724 if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
726 croak("'%"SVf"' has no compiled type constraint", self);
728 av_push(union_checks, newSVsv(c));
731 union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
732 av_push(checks, newRV_inc((SV*)union_check));
735 if(AvFILLp(checks) < 0){
736 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
739 check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
741 (void)set_slots(self, "compiled_type_constraint", check);