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 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
21 CV* const cv = (CV*)SvRV(tc_code);
22 assert(SvTYPE(cv) == SVt_PVCV);
24 if(CvXSUB(cv) == XS_Mouse__Util__TypeConstraints_Item){ /* built-in */
25 return mouse_builtin_tc_check(aTHX_ CvXSUBANY(cv).any_iv, sv);
27 else if(CvXSUB(cv) == XS_Mouse_parameterized_check){ /* complex type constraints */
28 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
30 assert(CvXSUBANY(cv).any_ptr != NULL);
32 /* call the check function directly, skipping call_sv() */
33 return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
46 call_sv(tc_code, G_SCALAR);
60 mouse_builtin_tc_check(pTHX_ mouse_tc const tc, SV* const sv) {
62 case MOUSE_TC_ANY: return mouse_tc_Any(aTHX_ sv);
63 case MOUSE_TC_ITEM: return mouse_tc_Any(aTHX_ sv);
64 case MOUSE_TC_MAYBE: return mouse_tc_Any(aTHX_ sv);
65 case MOUSE_TC_UNDEF: return mouse_tc_Undef(aTHX_ sv);
66 case MOUSE_TC_DEFINED: return mouse_tc_Defined(aTHX_ sv);
67 case MOUSE_TC_BOOL: return mouse_tc_Bool(aTHX_ sv);
68 case MOUSE_TC_VALUE: return mouse_tc_Value(aTHX_ sv);
69 case MOUSE_TC_REF: return mouse_tc_Ref(aTHX_ sv);
70 case MOUSE_TC_STR: return mouse_tc_Str(aTHX_ sv);
71 case MOUSE_TC_NUM: return mouse_tc_Num(aTHX_ sv);
72 case MOUSE_TC_INT: return mouse_tc_Int(aTHX_ sv);
73 case MOUSE_TC_SCALAR_REF: return mouse_tc_ScalarRef(aTHX_ sv);
74 case MOUSE_TC_ARRAY_REF: return mouse_tc_ArrayRef(aTHX_ sv);
75 case MOUSE_TC_HASH_REF: return mouse_tc_HashRef(aTHX_ sv);
76 case MOUSE_TC_CODE_REF: return mouse_tc_CodeRef(aTHX_ sv);
77 case MOUSE_TC_GLOB_REF: return mouse_tc_GlobRef(aTHX_ sv);
78 case MOUSE_TC_FILEHANDLE: return mouse_tc_FileHandle(aTHX_ sv);
79 case MOUSE_TC_REGEXP_REF: return mouse_tc_RegexpRef(aTHX_ sv);
80 case MOUSE_TC_OBJECT: return mouse_tc_Object(aTHX_ sv);
81 case MOUSE_TC_CLASS_NAME: return mouse_tc_ClassName(aTHX_ sv);
82 case MOUSE_TC_ROLE_NAME: return mouse_tc_RoleName(aTHX_ sv);
87 croak("Mouse-panic: unrecognized type constraint id: %d", (int)tc);
88 return FALSE; /* not reached */
93 The following type check functions return an integer, not a bool, to keep them simple,
94 so if you assign these return value to bool variable, you must use "expr ? TRUE : FALSE".
98 mouse_tc_Any(pTHX_ SV* const sv PERL_UNUSED_DECL) {
104 mouse_tc_Bool(pTHX_ SV* const sv) {
109 return SvIVX(sv) == 1;
112 return SvNVX(sv) == 1.0;
114 else if(SvPOKp(sv)){ /* "1" */
115 return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
122 /* false must be boolean */
128 mouse_tc_Undef(pTHX_ SV* const sv) {
134 mouse_tc_Defined(pTHX_ SV* const sv) {
140 mouse_tc_Value(pTHX_ SV* const sv) {
142 return SvOK(sv) && !SvROK(sv);
146 mouse_tc_Num(pTHX_ SV* const sv) {
148 return LooksLikeNumber(sv);
152 mouse_tc_Int(pTHX_ SV* const sv) {
158 NV const nv = SvNVX(sv);
159 return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
162 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
164 return !(num_type & IS_NUMBER_NOT_INT);
171 mouse_tc_Str(pTHX_ SV* const sv) {
173 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
177 mouse_tc_ClassName(pTHX_ SV* const sv){
179 return is_class_loaded(sv);
183 mouse_tc_RoleName(pTHX_ SV* const sv) {
185 if(is_class_loaded(sv)){
196 call_pv("Mouse::Util::get_metaclass_by_name", G_SCALAR);
201 ok = is_an_instance_of("Mouse::Meta::Role", meta);
212 mouse_tc_Ref(pTHX_ SV* const sv) {
218 mouse_tc_ScalarRef(pTHX_ SV* const sv) {
220 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && (SvTYPE(SvRV(sv)) <= SVt_PVLV && !isGV(SvRV(sv)));
224 mouse_tc_ArrayRef(pTHX_ SV* const sv) {
226 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV;
230 mouse_tc_HashRef(pTHX_ SV* const sv) {
232 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV;
236 mouse_tc_CodeRef(pTHX_ SV* const sv) {
238 return SvROK(sv) && !SvOBJECT(SvRV(sv))&& SvTYPE(SvRV(sv)) == SVt_PVCV;
242 mouse_tc_RegexpRef(pTHX_ SV* const sv) {
248 mouse_tc_GlobRef(pTHX_ SV* const sv) {
250 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
254 mouse_tc_FileHandle(pTHX_ SV* const sv) {
258 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
260 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
261 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
262 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
264 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
269 return is_an_instance_of("IO::Handle", sv);
273 mouse_tc_Object(pTHX_ SV* const sv) {
275 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
278 /* Parameterized type constraints */
281 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
282 if(mouse_tc_ArrayRef(aTHX_ sv)){
283 AV* const av = (AV*)SvRV(sv);
284 I32 const len = av_len(av) + 1;
286 for(i = 0; i < len; i++){
287 SV* const value = *av_fetch(av, i, TRUE);
289 if(!mouse_tc_check(aTHX_ param, value)){
299 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
300 if(mouse_tc_HashRef(aTHX_ sv)){
301 HV* const hv = (HV*)SvRV(sv);
305 while((he = hv_iternext(hv))){
306 SV* const value = hv_iterval(hv, he);
308 if(!mouse_tc_check(aTHX_ param, value)){
318 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
320 return mouse_tc_check(aTHX_ param, sv);
326 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
327 I32 const len = AvFILLp(types) + 1;
330 for(i = 0; i < len; i++){
331 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
340 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
341 I32 const len = AvFILLp(types) + 1;
348 for(i = 0; i < len; i++){
349 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
361 * This class_type generator is taken from Scalar::Util::Instance
364 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
365 typedef struct sui_cxt{
370 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
371 #define MG_klass_pv(mg) ((mg)->mg_ptr)
372 #define MG_klass_len(mg) ((mg)->mg_len)
375 mouse_canonicalize_package_name(const char* name){
377 /* "::Foo" -> "Foo" */
378 if(name[0] == ':' && name[1] == ':'){
382 /* "main::main::main::Foo" -> "Foo" */
383 while(strnEQ(name, "main::", sizeof("main::")-1)){
384 name += sizeof("main::")-1;
391 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
392 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
393 SV** svp = AvARRAY(linearized_isa);
394 SV** const end = svp + AvFILLp(linearized_isa) + 1;
398 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
407 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
409 assert(SvTYPE(stash) == SVt_PVHV);
411 if(IsObject(instance)){
413 HV* const instance_stash = SvSTASH(SvRV(instance));
414 GV* const instance_isa = gv_fetchmeth_autoload(instance_stash, "isa", sizeof("isa")-1, 0);
416 /* the instance has no own isa method */
417 if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
418 return stash == instance_stash
419 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
421 /* the instance has its own isa method */
432 mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
435 call_sv((SV*)instance_isa, G_SCALAR);
439 retval = SvTRUEx(POPs);
453 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
454 PERL_UNUSED_ARG(data);
455 return SvROK(sv) && SvOBJECT(SvRV(sv));
458 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
461 mouse_tc_parameterize(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
464 xsub = newXS(name, XS_Mouse_parameterized_check, __FILE__);
465 CvXSUBANY(xsub).any_ptr = sv_magicext(
467 param, /* mg_obj: refcnt will be increased */
469 &mouse_util_type_constraints_vtbl,
470 (void*)fptr, /* mg_ptr */
471 0 /* mg_len: 0 for static data */
475 sv_2mortal((SV*)xsub);
482 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
484 const char* klass_pv = SvPV_const(klass, klass_len);
488 klass_pv = mouse_canonicalize_package_name(klass_pv);
490 if(strNE(klass_pv, "UNIVERSAL")){
491 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
492 fptr = (void*)mouse_is_an_instance_of;
497 fptr = (void*)mouse_is_an_instance_of_universal;
500 return mouse_tc_parameterize(aTHX_ predicate_name, fptr, param);
503 XS(XS_Mouse_parameterized_check) {
506 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
509 croak("Too few arguments for parameterized check functions");
513 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
518 setup_my_cxt(pTHX_ pMY_CXT){
519 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
520 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
523 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
526 VERSIONCHECK: DISABLE
531 setup_my_cxt(aTHX_ aMY_CXT);
541 setup_my_cxt(aTHX_ aMY_CXT);
542 PERL_UNUSED_VAR(items);
545 #endif /* !USE_ITHREADS */
548 Item(SV* sv = &PL_sv_undef)
552 Undef = MOUSE_TC_UNDEF
553 Defined = MOUSE_TC_DEFINED
555 Value = MOUSE_TC_VALUE
560 ScalarRef = MOUSE_TC_SCALAR_REF
561 ArrayRef = MOUSE_TC_ARRAY_REF
562 HashRef = MOUSE_TC_HASH_REF
563 CodeRef = MOUSE_TC_CODE_REF
564 GlobRef = MOUSE_TC_GLOB_REF
565 FileHandle = MOUSE_TC_FILEHANDLE
566 RegexpRef = MOUSE_TC_REGEXP_REF
567 Object = MOUSE_TC_OBJECT
568 ClassName = MOUSE_TC_CLASS_NAME
569 RoleName = MOUSE_TC_ROLE_NAME
572 ST(0) = boolSV( mouse_builtin_tc_check(aTHX_ ix, sv) );
577 _parameterize_ArrayRef_for(SV* param)
579 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
580 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
581 _parameterize_Maybe_for = MOUSE_TC_MAYBE
585 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
586 if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){
587 croak("_compiled_type_constraint didn't return a CODE reference");
591 case MOUSE_TC_ARRAY_REF:
592 fptr = mouse_parameterized_ArrayRef;
594 case MOUSE_TC_HASH_REF:
595 fptr = mouse_parameterized_HashRef;
597 default: /* Maybe type */
598 fptr = mouse_parameterized_Maybe;
600 RETVAL = mouse_tc_parameterize(aTHX_ NULL, fptr, tc_code);
605 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
608 compile_type_constraint(SV* self)
611 AV* const checks = newAV();
612 SV* check; /* check function */
616 sv_2mortal((SV*)checks);
618 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
619 check = get_slots(parent, "hand_optimized_type_constraint");
620 if(check && SvOK(check)){
621 if(!mouse_tc_CodeRef(aTHX_ check)){
622 croak("Not a CODE reference");
624 av_unshift(checks, 1);
625 av_store(checks, 0, newSVsv(check));
626 break; /* a hand optimized constraint must include all the parent */
629 check = get_slots(parent, "constraint");
630 if(check && SvOK(check)){
631 if(!mouse_tc_CodeRef(aTHX_ check)){
632 croak("Not a CODE reference");
634 av_unshift(checks, 1);
635 av_store(checks, 0, newSVsv(check));
639 check = get_slots(self, "constraint");
640 if(check && SvOK(check)){
641 if(!mouse_tc_CodeRef(aTHX_ check)){
642 croak("Not a CODE reference");
644 av_push(checks, newSVsv(check));
647 types_ref = get_slots(self, "type_constraints");
648 if(types_ref && SvOK(types_ref)){ /* union type */
655 if(!mouse_tc_ArrayRef(aTHX_ types_ref)){
656 croak("Not an ARRAY reference");
658 types = (AV*)SvRV(types_ref);
659 len = av_len(types) + 1;
661 union_checks = newAV();
662 sv_2mortal((SV*)union_checks);
664 for(i = 0; i < len; i++){
665 SV* const tc = *av_fetch(types, i, TRUE);
666 SV* const c = get_slots(tc, "compiled_type_constraint");
667 if(!(c && mouse_tc_CodeRef(aTHX_ c))){
669 croak("'%"SVf"' has no compiled type constraint", self);
671 av_push(union_checks, newSVsv(c));
674 union_check = mouse_tc_parameterize(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
675 av_push(checks, newRV_inc((SV*)union_check));
678 if(AvFILLp(checks) < 0){
679 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
682 check = newRV_inc((SV*)mouse_tc_parameterize(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
684 set_slots(self, "compiled_type_constraint", check);