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);
27 if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */
28 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
30 assert(CvXSUBANY(cv).any_ptr != NULL);
31 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) {
126 else if(SvNOKp(sv)) {
127 NV const nv = SvNVX(sv);
128 NV mod = Perl_fmod( nv, 1 );
132 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
134 return !(num_type & IS_NUMBER_NOT_INT);
141 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
143 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
147 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
149 return is_class_loaded(sv);
153 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
155 if(is_class_loaded(sv)){
161 ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
172 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
178 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
182 return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
188 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
190 return IsArrayRef(sv);
194 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
196 return IsHashRef(sv);
200 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
202 return IsCodeRef(sv);
206 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
212 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
214 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
218 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
222 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
224 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
225 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
226 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
228 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
233 return is_an_instance_of("IO::Handle", sv);
237 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
239 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
242 /* Parameterized type constraints */
245 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
247 AV* const av = (AV*)SvRV(sv);
248 I32 const len = av_len(av) + 1;
250 for(i = 0; i < len; i++){
251 SV* const value = *av_fetch(av, i, TRUE);
252 if(!mouse_tc_check(aTHX_ param, value)){
262 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
263 if(mouse_tc_HashRef(aTHX_ NULL, sv)){
264 HV* const hv = (HV*)SvRV(sv);
268 while((he = hv_iternext(hv))){
269 SV* const value = hv_iterval(hv, he);
270 if(!mouse_tc_check(aTHX_ param, value)){
271 hv_iterinit(hv); /* reset */
281 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
283 return mouse_tc_check(aTHX_ param, sv);
289 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
290 I32 const len = AvFILLp(types) + 1;
293 for(i = 0; i < len; i++){
294 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
303 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
304 I32 const len = AvFILLp(types) + 1;
311 for(i = 0; i < len; i++){
312 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
324 * This class_type generator is taken from Scalar::Util::Instance
327 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
328 typedef struct sui_cxt{
334 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
335 #define MG_klass_pv(mg) ((mg)->mg_ptr)
336 #define MG_klass_len(mg) ((mg)->mg_len)
339 mouse_canonicalize_package_name(const char* name){
341 /* "::Foo" -> "Foo" */
342 if(name[0] == ':' && name[1] == ':'){
346 /* "main::main::main::Foo" -> "Foo" */
347 while(strnEQ(name, "main::", sizeof("main::")-1)){
348 name += sizeof("main::")-1;
355 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
356 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
357 SV** svp = AvARRAY(linearized_isa);
358 SV** const end = svp + AvFILLp(linearized_isa) + 1;
362 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
370 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
371 #define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
374 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
375 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
376 if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
380 return gv_fetchmeth_autoload(stash, name, namelen, 0);
384 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
386 assert(SvTYPE(stash) == SVt_PVHV);
388 if(IsObject(instance)){
390 HV* const instance_stash = SvSTASH(SvRV(instance));
391 GV* const myisa = find_method_pvs(instance_stash, "isa");
393 /* the instance has no own isa method */
394 if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){
395 return stash == instance_stash
396 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
398 /* the instance has its own isa method */
406 package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
407 ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package)));
419 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
420 PERL_UNUSED_ARG(data);
421 return SvROK(sv) && SvOBJECT(SvRV(sv));
425 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
426 if(IsObject(instance)){
428 HV* const mystash = SvSTASH(SvRV(instance));
429 GV* const mycan = find_method_pvs(mystash, "can");
430 bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
431 I32 const len = AvFILLp(methods) + 1;
433 for(i = 0; i < len; i++){
434 SV* const name = MOUSE_av_at(methods, i);
437 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
447 ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name)));
462 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
465 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
468 xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
469 CvXSUBANY(xsub).any_ptr = sv_magicext(
471 param, /* mg_obj: refcnt will be increased */
473 &mouse_util_type_constraints_vtbl,
474 (char*)fptr, /* mg_ptr */
475 0 /* mg_len: 0 for static data */
479 sv_2mortal((SV*)xsub);
486 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
488 const char* klass_pv = SvPV_const(klass, klass_len);
492 klass_pv = mouse_canonicalize_package_name(klass_pv);
494 if(strNE(klass_pv, "UNIVERSAL")){
495 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
496 fptr = (check_fptr_t)mouse_is_an_instance_of;
501 fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
504 return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
508 mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
510 AV* const param = newAV_mortal();
514 must_ref(methods, "an ARRAY ref for method names", SVt_PVAV);
515 av = (AV*)SvRV(methods);
517 len = av_len(av) + 1;
518 for(i = 0; i < len; i++){
519 SV* const name = *av_fetch(av, i, TRUE);
521 const char* const pv = SvPV_const(name, pvlen);
523 av_push(param, newSVpvn_share(pv, pvlen, 0U));
526 return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
530 XS(XS_Mouse_constraint_check) {
533 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
536 croak("Too few arguments for type constraint check functions");
540 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
545 setup_my_cxt(pTHX_ pMY_CXT){
546 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
547 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
549 MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
550 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
553 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
555 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
558 VERSIONCHECK: DISABLE
563 setup_my_cxt(aTHX_ aMY_CXT);
565 /* setup built-in type constraints */
575 DEFINE_TC(ScalarRef);
580 DEFINE_TC(FileHandle);
581 DEFINE_TC(RegexpRef);
583 DEFINE_TC(ClassName);
594 setup_my_cxt(aTHX_ aMY_CXT);
595 PERL_UNUSED_VAR(items);
598 #endif /* !USE_ITHREADS */
600 #define MOUSE_TC_MAYBE 0
601 #define MOUSE_TC_ARRAY_REF 1
602 #define MOUSE_TC_HASH_REF 2
605 _parameterize_ArrayRef_for(SV* param)
607 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
608 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
609 _parameterize_Maybe_for = MOUSE_TC_MAYBE
613 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
614 if(!IsCodeRef(tc_code)){
615 croak("_compiled_type_constraint didn't return a CODE reference");
619 case MOUSE_TC_ARRAY_REF:
620 fptr = mouse_parameterized_ArrayRef;
622 case MOUSE_TC_HASH_REF:
623 fptr = mouse_parameterized_HashRef;
625 default: /* Maybe type */
626 fptr = mouse_parameterized_Maybe;
628 RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
633 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
636 INSTALL_SIMPLE_READER(TypeConstraint, name);
637 INSTALL_SIMPLE_READER(TypeConstraint, parent);
638 INSTALL_SIMPLE_READER(TypeConstraint, message);
640 INSTALL_SIMPLE_READER(TypeConstraint, type_parameter);
642 INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
643 INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
645 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
646 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
649 compile_type_constraint(SV* self)
652 AV* const checks = newAV_mortal();
653 SV* check; /* check function */
657 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
658 check = get_slots(parent, "hand_optimized_type_constraint");
659 if(check && SvOK(check)){
660 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
661 croak("Not a CODE reference");
663 av_unshift(checks, 1);
664 av_store(checks, 0, newSVsv(check));
665 break; /* a hand optimized constraint must include all the parent */
668 check = get_slots(parent, "constraint");
669 if(check && SvOK(check)){
670 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
671 croak("Not a CODE reference");
673 av_unshift(checks, 1);
674 av_store(checks, 0, newSVsv(check));
678 check = get_slots(self, "constraint");
679 if(check && SvOK(check)){
680 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
681 croak("Not a CODE reference");
683 av_push(checks, newSVsv(check));
686 types_ref = get_slots(self, "type_constraints");
687 if(types_ref && SvOK(types_ref)){ /* union type */
694 if(!IsArrayRef(types_ref)){
695 croak("Not an ARRAY reference");
697 types = (AV*)SvRV(types_ref);
698 len = av_len(types) + 1;
700 union_checks = newAV_mortal();
702 for(i = 0; i < len; i++){
703 SV* const tc = *av_fetch(types, i, TRUE);
704 SV* const c = get_slots(tc, "compiled_type_constraint");
705 if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
707 croak("'%"SVf"' has no compiled type constraint", self);
709 av_push(union_checks, newSVsv(c));
712 union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
713 av_push(checks, newRV_inc((SV*)union_check));
716 if(AvFILLp(checks) < 0){
717 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
720 check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
722 (void)set_slots(self, "compiled_type_constraint", check);