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 typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
16 NOTE: mouse_tc_check() handles GETMAGIC
19 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
20 CV* const cv = (CV*)SvRV(tc_code);
21 assert(SvTYPE(cv) == SVt_PVCV);
23 if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */
24 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
26 assert(CvXSUBANY(cv).any_ptr != NULL);
27 assert(mg->mg_ptr != NULL);
30 /* call the check function directly, skipping call_sv() */
31 return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
44 call_sv(tc_code, G_SCALAR);
58 The following type check functions return an integer, not a bool, to keep them simple,
59 so if you assign these return value to bool variable, you must use "expr ? TRUE : FALSE".
63 mouse_tc_Any(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv PERL_UNUSED_DECL) {
69 mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
73 if(SvPOKp(sv)){ /* "1" */
74 return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
77 return SvIVX(sv) == 1;
80 return SvNVX(sv) == 1.0;
87 /* any false value is a boolean */
93 mouse_tc_Undef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
99 mouse_tc_Defined(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
105 mouse_tc_Value(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
107 return SvOK(sv) && !SvROK(sv);
111 mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
113 return LooksLikeNumber(sv);
117 S_nv_is_integer(pTHX_ NV const nv) {
118 if(nv == (NV)(IV)nv){
122 char buf[64]; /* Must fit sprintf/Gconvert of longest NV */
124 (void)Gconvert(nv, NV_DIG, 0, buf);
141 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
144 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
145 return num_type && !(num_type & IS_NUMBER_NOT_INT);
150 else if(SvNOKp(sv)) {
151 return S_nv_is_integer(aTHX_ SvNVX(sv));
157 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
159 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
163 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
165 return is_class_loaded(sv);
169 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
171 if(is_class_loaded(sv)){
177 ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
188 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
194 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
198 return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
204 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
206 return IsArrayRef(sv);
210 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
212 return IsHashRef(sv);
216 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
218 return IsCodeRef(sv);
222 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
228 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
230 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
234 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
238 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
240 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
241 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
242 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
244 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
249 return is_an_instance_of("IO::Handle", sv);
253 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
255 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
258 /* Parameterized type constraints */
261 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
263 AV* const av = (AV*)SvRV(sv);
264 I32 const len = av_len(av) + 1;
266 for(i = 0; i < len; i++){
267 SV* const value = *av_fetch(av, i, TRUE);
268 if(!mouse_tc_check(aTHX_ param, value)){
278 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
279 if(mouse_tc_HashRef(aTHX_ NULL, sv)){
280 HV* const hv = (HV*)SvRV(sv);
284 while((he = hv_iternext(hv))){
285 SV* const value = hv_iterval(hv, he);
286 if(!mouse_tc_check(aTHX_ param, value)){
287 hv_iterinit(hv); /* reset */
297 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
299 return mouse_tc_check(aTHX_ param, sv);
305 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
306 I32 const len = AvFILLp(types) + 1;
309 for(i = 0; i < len; i++){
310 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
319 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
320 I32 const len = AvFILLp(types) + 1;
327 for(i = 0; i < len; i++){
328 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
340 * This class_type generator is taken from Scalar::Util::Instance
343 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
344 typedef struct sui_cxt{
350 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
351 #define MG_klass_pv(mg) ((mg)->mg_ptr)
352 #define MG_klass_len(mg) ((mg)->mg_len)
355 mouse_canonicalize_package_name(const char* name){
357 /* "::Foo" -> "Foo" */
358 if(name[0] == ':' && name[1] == ':'){
362 /* "main::main::main::Foo" -> "Foo" */
363 while(strnEQ(name, "main::", sizeof("main::")-1)){
364 name += sizeof("main::")-1;
371 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
372 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
373 SV** svp = AvARRAY(linearized_isa);
374 SV** const end = svp + AvFILLp(linearized_isa) + 1;
378 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
386 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
387 #define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
390 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
391 GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
392 if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
396 return gv_fetchmeth_autoload(stash, name, namelen, 0);
400 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
402 assert(SvTYPE(stash) == SVt_PVHV);
404 if(IsObject(instance)){
406 HV* const instance_stash = SvSTASH(SvRV(instance));
407 GV* const myisa = find_method_pvs(instance_stash, "isa");
409 /* the instance has no own isa method */
410 if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){
411 return stash == instance_stash
412 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
414 /* the instance has its own isa method */
422 package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
423 ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package)));
435 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
436 PERL_UNUSED_ARG(data);
437 return SvROK(sv) && SvOBJECT(SvRV(sv));
441 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
442 if(IsObject(instance)){
444 HV* const mystash = SvSTASH(SvRV(instance));
445 GV* const mycan = find_method_pvs(mystash, "can");
446 bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
447 I32 const len = AvFILLp(methods) + 1;
449 for(i = 0; i < len; i++){
450 SV* const name = MOUSE_av_at(methods, i);
453 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
463 ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name)));
478 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
481 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
484 xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
485 CvXSUBANY(xsub).any_ptr = sv_magicext(
487 param, /* mg_obj: refcnt will be increased */
489 &mouse_util_type_constraints_vtbl,
490 (char*)fptr, /* mg_ptr */
491 0 /* mg_len: 0 for static data */
495 sv_2mortal((SV*)xsub);
502 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
504 const char* klass_pv = SvPV_const(klass, klass_len);
508 klass_pv = mouse_canonicalize_package_name(klass_pv);
510 if(strNE(klass_pv, "UNIVERSAL")){
511 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
512 fptr = (check_fptr_t)mouse_is_an_instance_of;
517 fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
520 return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
524 mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
526 AV* const param = newAV_mortal();
530 must_ref(methods, "an ARRAY ref for method names", SVt_PVAV);
531 av = (AV*)SvRV(methods);
533 len = av_len(av) + 1;
534 for(i = 0; i < len; i++){
535 SV* const name = *av_fetch(av, i, TRUE);
537 const char* const pv = SvPV_const(name, pvlen);
539 av_push(param, newSVpvn_share(pv, pvlen, 0U));
542 return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
546 XS(XS_Mouse_constraint_check) {
549 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
553 croak("Too few arguments for type constraint check functions");
558 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv) );
562 XS(XS_Mouse_TypeConstraint_fallback); /* -Wmissing-prototypes */
563 XS(XS_Mouse_TypeConstraint_fallback) {
566 PERL_UNUSED_VAR(items);
571 setup_my_cxt(pTHX_ pMY_CXT){
572 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
573 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
575 MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
576 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
579 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
581 #define MTC_CLASS "Mouse::Meta::TypeConstraint"
583 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
586 VERSIONCHECK: DISABLE
591 setup_my_cxt(aTHX_ aMY_CXT);
593 /* setup built-in type constraints */
603 DEFINE_TC(ScalarRef);
608 DEFINE_TC(FileHandle);
609 DEFINE_TC(RegexpRef);
611 DEFINE_TC(ClassName);
622 setup_my_cxt(aTHX_ aMY_CXT);
623 PERL_UNUSED_VAR(items);
626 #endif /* !USE_ITHREADS */
628 #define MOUSE_TC_MAYBE 0
629 #define MOUSE_TC_ARRAY_REF 1
630 #define MOUSE_TC_HASH_REF 2
633 _parameterize_ArrayRef_for(SV* param)
635 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
636 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
637 _parameterize_Maybe_for = MOUSE_TC_MAYBE
641 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
642 if(!IsCodeRef(tc_code)){
643 croak("_compiled_type_constraint didn't return a CODE reference");
647 case MOUSE_TC_ARRAY_REF:
648 fptr = mouse_parameterized_ArrayRef;
650 case MOUSE_TC_HASH_REF:
651 fptr = mouse_parameterized_HashRef;
653 default: /* Maybe type */
654 fptr = mouse_parameterized_Maybe;
656 RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
661 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
664 INSTALL_SIMPLE_READER(TypeConstraint, name);
665 INSTALL_SIMPLE_READER(TypeConstraint, parent);
666 INSTALL_SIMPLE_READER(TypeConstraint, message);
668 INSTALL_SIMPLE_READER(TypeConstraint, type_parameter);
670 INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
671 INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
673 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
674 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
677 PL_amagic_generation++;
678 (void)newXS( MTC_CLASS "::()",
679 XS_Mouse_TypeConstraint_fallback, file);
683 get_sv( MTC_CLASS "::()", GV_ADD ),
687 /* '""' => '_as_string' */
689 SV* const code_ref = sv_2mortal(newRV_inc(
690 (SV*)get_cv( MTC_CLASS "::_as_string", GV_ADD )));
692 (SV*)gv_fetchpvs( MTC_CLASS "::(\"\"", GV_ADDMULTI, SVt_PVCV ),
696 /* '0+' => '_identity' */
698 SV* const code_ref = sv_2mortal(newRV_inc(
699 (SV*)get_cv( MTC_CLASS "::_identity", GV_ADD )));
701 (SV*)gv_fetchpvs( MTC_CLASS "::(0+", GV_ADDMULTI, SVt_PVCV ),
705 /* '|' => '_unite' */
707 SV* const code_ref = sv_2mortal(newRV_inc(
708 (SV*)get_cv( MTC_CLASS "::_unite", GV_ADD )));
710 (SV*)gv_fetchpvs( MTC_CLASS "::(|", GV_ADDMULTI, SVt_PVCV ),
715 _identity(SV* self, ...)
719 croak("Invalid object instance: '%"SVf"'", self);
721 RETVAL = PTR2UV(SvRV(self));
727 compile_type_constraint(SV* self)
730 AV* const checks = newAV_mortal();
731 SV* check; /* check function */
735 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
736 check = get_slots(parent, "hand_optimized_type_constraint");
737 if(check && SvOK(check)){
738 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
739 croak("Not a CODE reference");
741 av_unshift(checks, 1);
742 av_store(checks, 0, newSVsv(check));
743 break; /* a hand optimized constraint must include all the parent */
746 check = get_slots(parent, "constraint");
747 if(check && SvOK(check)){
748 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
749 croak("Not a CODE reference");
751 av_unshift(checks, 1);
752 av_store(checks, 0, newSVsv(check));
756 check = get_slots(self, "constraint");
757 if(check && SvOK(check)){
758 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
759 croak("Not a CODE reference");
761 av_push(checks, newSVsv(check));
764 types_ref = get_slots(self, "type_constraints");
765 if(types_ref && SvOK(types_ref)){ /* union type */
772 if(!IsArrayRef(types_ref)){
773 croak("Not an ARRAY reference");
775 types = (AV*)SvRV(types_ref);
776 len = av_len(types) + 1;
778 union_checks = newAV_mortal();
780 for(i = 0; i < len; i++){
781 SV* const tc = *av_fetch(types, i, TRUE);
782 SV* const c = get_slots(tc, "compiled_type_constraint");
783 if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
784 mouse_throw_error(self, c, "'%"SVf"' has no compiled type constraint", self);
786 av_push(union_checks, newSVsv(c));
789 union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
790 av_push(checks, newRV_inc((SV*)union_check));
793 if(AvFILLp(checks) < 0){
794 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
797 check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
799 (void)set_slots(self, "compiled_type_constraint", check);
803 check(SV* self, SV* sv)
806 SV* const check = get_slots(self, "compiled_type_constraint");
807 if(!(check && mouse_tc_CodeRef(aTHX_ NULL, check))){
808 mouse_throw_error(self, check, "'%"SVf"' has no compiled type constraint", self);
810 RETVAL = mouse_tc_check(aTHX_ check, sv) ? TRUE : FALSE;