3 * - Mouse::Util::TypeConstraints (including OptimizedConstraionts)
4 * - Mouse::Meta::TypeConstraint
10 #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr))
13 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
14 typedef struct sui_cxt{
21 typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
24 XSPROTO(XS_Mouse_constraint_check);
27 NOTE: mouse_tc_check() handles GETMAGIC
30 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
31 CV* const cv = (CV*)SvRV(tc_code);
32 assert(SvTYPE(cv) == SVt_PVCV);
34 if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */
35 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
37 assert(CvXSUBANY(cv).any_ptr != NULL);
38 assert(mg->mg_ptr != NULL);
41 /* call the check function directly, skipping call_sv() */
42 return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
54 if( MY_CXT.tc_extra_args ) {
55 AV* const av = MY_CXT.tc_extra_args;
56 I32 const len = AvFILLp(av) + 1;
58 for(i = 0; i < len; i++) {
59 XPUSHs( AvARRAY(av)[i] );
64 call_sv(tc_code, G_SCALAR);
78 The following type check functions return an integer, not a bool, to keep
80 so if you assign these return value to a bool variable, you must use
81 "expr ? TRUE : FALSE".
85 mouse_tc_Any(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv PERL_UNUSED_DECL) {
91 mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
95 if(SvPOKp(sv)){ /* "1" */
96 return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
99 return SvIVX(sv) == 1;
102 return SvNVX(sv) == 1.0;
109 /* any false value is a boolean */
115 mouse_tc_Undef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
121 mouse_tc_Defined(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
127 mouse_tc_Value(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
129 return SvOK(sv) && !SvROK(sv);
133 mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
135 return LooksLikeNumber(sv);
139 S_nv_is_integer(pTHX_ NV const nv) {
140 if(nv == (NV)(IV)nv){
144 char buf[64]; /* Must fit sprintf/Gconvert of longest NV */
146 (void)Gconvert(nv, NV_DIG, 0, buf);
163 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
166 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
167 return num_type && !(num_type & IS_NUMBER_NOT_INT);
172 else if(SvNOKp(sv)) {
173 return S_nv_is_integer(aTHX_ SvNVX(sv));
179 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
181 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
185 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
187 return is_class_loaded(sv);
191 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
193 if(is_class_loaded(sv)){
199 ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
210 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
216 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
220 return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
226 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
228 return IsArrayRef(sv);
232 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
234 return IsHashRef(sv);
238 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
240 return IsCodeRef(sv);
244 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
250 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
252 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
256 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
260 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
262 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
263 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
264 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
266 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
271 return is_an_instance_of("IO::Handle", sv);
275 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
277 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
280 /* Parameterized type constraints */
283 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
285 AV* const av = (AV*)SvRV(sv);
286 I32 const len = av_len(av) + 1;
288 for(i = 0; i < len; i++){
289 SV* const value = *av_fetch(av, i, TRUE);
290 if(!mouse_tc_check(aTHX_ param, value)){
300 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
302 HV* const hv = (HV*)SvRV(sv);
306 while((he = hv_iternext(hv))){
307 SV* const value = hv_iterval(hv, he);
308 if(!mouse_tc_check(aTHX_ param, value)){
309 hv_iterinit(hv); /* reset */
319 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
321 return mouse_tc_check(aTHX_ param, sv);
327 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
328 I32 const len = AvFILLp(types) + 1;
331 for(i = 0; i < len; i++){
332 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
341 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
342 I32 const len = AvFILLp(types) + 1;
349 for(i = 0; i < len; i++){
350 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
362 * This class_type generator is taken from Scalar::Util::Instance
366 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
367 #define MG_klass_pv(mg) ((mg)->mg_ptr)
368 #define MG_klass_len(mg) ((mg)->mg_len)
371 mouse_canonicalize_package_name(const char* name){
373 /* "::Foo" -> "Foo" */
374 if(name[0] == ':' && name[1] == ':'){
378 /* "main::main::main::Foo" -> "Foo" */
379 while(strnEQ(name, "main::", sizeof("main::")-1)){
380 name += sizeof("main::")-1;
387 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
388 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
389 SV** svp = AvARRAY(linearized_isa);
390 SV** const end = svp + AvFILLp(linearized_isa) + 1;
394 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
402 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
403 #define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
406 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
407 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
408 if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
412 return gv_fetchmeth_autoload(stash, name, namelen, 0);
416 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
418 assert(SvTYPE(stash) == SVt_PVHV);
420 if(IsObject(instance)){
422 HV* const instance_stash = SvSTASH(SvRV(instance));
423 GV* const myisa = find_method_pvs(instance_stash, "isa");
425 /* the instance has no own isa method */
426 if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){
427 return stash == instance_stash
428 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
430 /* the instance has its own isa method */
438 package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
439 ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package)));
451 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
452 PERL_UNUSED_ARG(data);
453 return SvROK(sv) && SvOBJECT(SvRV(sv));
457 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
458 if(IsObject(instance)){
460 HV* const mystash = SvSTASH(SvRV(instance));
461 GV* const mycan = find_method_pvs(mystash, "can");
462 bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
463 I32 const len = AvFILLp(methods) + 1;
465 for(i = 0; i < len; i++){
466 SV* const name = MOUSE_av_at(methods, i);
469 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
479 ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name)));
494 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
497 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
500 xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
501 CvXSUBANY(xsub).any_ptr = sv_magicext(
503 param, /* mg_obj: refcnt will be increased */
505 &mouse_util_type_constraints_vtbl,
506 (char*)fptr, /* mg_ptr */
507 0 /* mg_len: 0 for static data */
511 sv_2mortal((SV*)xsub);
518 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
520 const char* klass_pv = SvPV_const(klass, klass_len);
524 klass_pv = mouse_canonicalize_package_name(klass_pv);
526 if(strNE(klass_pv, "UNIVERSAL")){
527 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
528 fptr = (check_fptr_t)mouse_is_an_instance_of;
533 fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
536 return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
540 mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
542 AV* const param = newAV_mortal();
546 must_ref(methods, "an ARRAY ref for method names", SVt_PVAV);
547 av = (AV*)SvRV(methods);
549 len = av_len(av) + 1;
550 for(i = 0; i < len; i++){
551 SV* const name = *av_fetch(av, i, TRUE);
553 const char* const pv = SvPV_const(name, pvlen);
555 av_push(param, newSVpvn_share(pv, pvlen, 0U));
558 return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
562 XSPROTO(XS_Mouse_constraint_check) {
565 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
569 croak("Too few arguments for type constraint check functions");
574 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv) );
579 XSPROTO(XS_Mouse_TypeConstraint_fallback) {
582 PERL_UNUSED_VAR(items);
587 setup_my_cxt(pTHX_ pMY_CXT){
588 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
589 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
591 MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
592 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
594 MY_CXT.tc_extra_args = NULL;
597 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
599 #define MTC_CLASS "Mouse::Meta::TypeConstraint"
601 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
604 VERSIONCHECK: DISABLE
609 setup_my_cxt(aTHX_ aMY_CXT);
611 /* setup built-in type constraints */
621 DEFINE_TC(ScalarRef);
626 DEFINE_TC(FileHandle);
627 DEFINE_TC(RegexpRef);
629 DEFINE_TC(ClassName);
640 setup_my_cxt(aTHX_ aMY_CXT);
641 PERL_UNUSED_VAR(items);
644 #endif /* !USE_ITHREADS */
646 #define MOUSE_TC_MAYBE 0
647 #define MOUSE_TC_ARRAY_REF 1
648 #define MOUSE_TC_HASH_REF 2
651 _parameterize_ArrayRef_for(SV* param)
653 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
654 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
655 _parameterize_Maybe_for = MOUSE_TC_MAYBE
659 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
660 if(!IsCodeRef(tc_code)){
661 croak("_compiled_type_constraint didn't return a CODE reference");
665 case MOUSE_TC_ARRAY_REF:
666 fptr = mouse_parameterized_ArrayRef;
668 case MOUSE_TC_HASH_REF:
669 fptr = mouse_parameterized_HashRef;
671 default: /* Maybe type */
672 fptr = mouse_parameterized_Maybe;
674 RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
679 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
682 INSTALL_SIMPLE_READER(TypeConstraint, name);
683 INSTALL_SIMPLE_READER(TypeConstraint, parent);
684 INSTALL_SIMPLE_READER(TypeConstraint, message);
686 INSTALL_SIMPLE_READER(TypeConstraint, type_parameter);
688 INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
690 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
691 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
694 PL_amagic_generation++;
695 (void)newXS( MTC_CLASS "::()",
696 XS_Mouse_TypeConstraint_fallback, file);
700 get_sv( MTC_CLASS "::()", GV_ADD ),
704 /* '""' => '_as_string' */
706 SV* const code_ref = sv_2mortal(newRV_inc(
707 (SV*)get_cv( MTC_CLASS "::_as_string", GV_ADD )));
709 (SV*)gv_fetchpvs( MTC_CLASS "::(\"\"", GV_ADDMULTI, SVt_PVCV ),
713 /* '0+' => '_identity' */
715 SV* const code_ref = sv_2mortal(newRV_inc(
716 (SV*)get_cv( MTC_CLASS "::_identity", GV_ADD )));
718 (SV*)gv_fetchpvs( MTC_CLASS "::(0+", GV_ADDMULTI, SVt_PVCV ),
722 /* '|' => '_unite' */
724 SV* const code_ref = sv_2mortal(newRV_inc(
725 (SV*)get_cv( MTC_CLASS "::_unite", GV_ADD )));
727 (SV*)gv_fetchpvs( MTC_CLASS "::(|", GV_ADDMULTI, SVt_PVCV ),
732 _identity(SV* self, ...)
736 croak("Invalid object instance: '%"SVf"'", self);
738 RETVAL = PTR2UV(SvRV(self));
744 compile_type_constraint(SV* self)
747 AV* const checks = newAV_mortal();
748 SV* check; /* check function */
752 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
753 check = get_slots(parent, "hand_optimized_type_constraint");
754 if(check && SvOK(check)){
755 if(!IsCodeRef(check)){
756 croak("Not a CODE reference");
758 av_unshift(checks, 1);
759 av_store(checks, 0, newSVsv(check));
760 break; /* a hand optimized constraint must include all the parent */
763 check = get_slots(parent, "constraint");
764 if(check && SvOK(check)){
765 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
766 croak("Not a CODE reference");
768 av_unshift(checks, 1);
769 av_store(checks, 0, newSVsv(check));
773 check = get_slots(self, "constraint");
774 if(check && SvOK(check)){
775 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
776 croak("Not a CODE reference");
778 av_push(checks, newSVsv(check));
781 types_ref = get_slots(self, "type_constraints");
782 if(types_ref && SvOK(types_ref)){ /* union type */
789 if(!IsArrayRef(types_ref)){
790 croak("Not an ARRAY reference");
792 types = (AV*)SvRV(types_ref);
793 len = av_len(types) + 1;
795 union_checks = newAV_mortal();
797 for(i = 0; i < len; i++){
798 SV* const tc = *av_fetch(types, i, TRUE);
799 SV* const c = get_slots(tc, "compiled_type_constraint");
800 if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
801 mouse_throw_error(self, c, "'%"SVf"' has no compiled type constraint", self);
803 av_push(union_checks, newSVsv(c));
806 union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
807 av_push(checks, newRV_inc((SV*)union_check));
810 if(AvFILLp(checks) < 0){
811 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
814 check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
816 (void)set_slots(self, "compiled_type_constraint", check);
820 check(SV* self, SV* sv, ...)
823 SV* const check = get_slots(self, "compiled_type_constraint");
824 if(!(check && IsCodeRef(check))){
825 mouse_throw_error(self, check,
826 "'%"SVf"' has no compiled type constraint", self);
832 SAVESPTR(MY_CXT.tc_extra_args);
833 av = MY_CXT.tc_extra_args = newAV_mortal();
834 av_extend(av, items - 3);
835 for(i = 2; i < items; i++) {
836 av_push(av, SvREFCNT_inc_NN( ST(i) ) );
839 RETVAL = mouse_tc_check(aTHX_ check, sv) ? TRUE : FALSE;