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_constraint_check){ /* built-in type constraints */
25 MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
27 assert(CvXSUBANY(cv).any_ptr != NULL);
28 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) {
74 return SvIVX(sv) == 1;
77 return SvNVX(sv) == 1.0;
79 else if(SvPOKp(sv)){ /* "1" */
80 return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
87 /* any false value must be 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 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
123 NV const nv = SvNVX(sv);
124 return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
127 int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
129 return !(num_type & IS_NUMBER_NOT_INT);
136 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
138 return SvOK(sv) && !SvROK(sv) && !isGV(sv);
142 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
144 return is_class_loaded(sv);
148 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
150 if(is_class_loaded(sv)){
156 ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
167 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
173 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
175 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && (SvTYPE(SvRV(sv)) <= SVt_PVLV && !isGV(SvRV(sv)));
179 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
181 return IsArrayRef(sv);
185 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
187 return IsHashRef(sv);
191 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
193 return IsCodeRef(sv);
197 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
203 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
205 return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
209 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
213 /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
215 gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
216 if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
217 IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
219 if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
224 return is_an_instance_of("IO::Handle", sv);
228 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
230 return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
233 /* Parameterized type constraints */
236 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
238 AV* const av = (AV*)SvRV(sv);
239 I32 const len = av_len(av) + 1;
241 for(i = 0; i < len; i++){
242 SV* const value = *av_fetch(av, i, TRUE);
244 if(!mouse_tc_check(aTHX_ param, value)){
254 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
255 if(mouse_tc_HashRef(aTHX_ NULL, sv)){
256 HV* const hv = (HV*)SvRV(sv);
260 while((he = hv_iternext(hv))){
261 SV* const value = hv_iterval(hv, he);
263 if(!mouse_tc_check(aTHX_ param, value)){
273 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
275 return mouse_tc_check(aTHX_ param, sv);
281 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
282 I32 const len = AvFILLp(types) + 1;
285 for(i = 0; i < len; i++){
286 if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
295 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
296 I32 const len = AvFILLp(types) + 1;
303 for(i = 0; i < len; i++){
304 if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
316 * This class_type generator is taken from Scalar::Util::Instance
319 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
320 typedef struct sui_cxt{
325 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
326 #define MG_klass_pv(mg) ((mg)->mg_ptr)
327 #define MG_klass_len(mg) ((mg)->mg_len)
330 mouse_canonicalize_package_name(const char* name){
332 /* "::Foo" -> "Foo" */
333 if(name[0] == ':' && name[1] == ':'){
337 /* "main::main::main::Foo" -> "Foo" */
338 while(strnEQ(name, "main::", sizeof("main::")-1)){
339 name += sizeof("main::")-1;
346 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
347 AV* const linearized_isa = mro_get_linear_isa(instance_stash);
348 SV** svp = AvARRAY(linearized_isa);
349 SV** const end = svp + AvFILLp(linearized_isa) + 1;
353 if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
362 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
364 assert(SvTYPE(stash) == SVt_PVHV);
366 if(IsObject(instance)){
368 HV* const instance_stash = SvSTASH(SvRV(instance));
369 GV* const instance_isa = gv_fetchmeth_autoload(instance_stash, "isa", sizeof("isa")-1, 0);
371 /* the instance has no own isa method */
372 if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
373 return stash == instance_stash
374 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
376 /* the instance has its own isa method */
387 mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
390 call_sv((SV*)instance_isa, G_SCALAR);
394 retval = SvTRUEx(POPs);
408 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
409 PERL_UNUSED_ARG(data);
410 return SvROK(sv) && SvOBJECT(SvRV(sv));
413 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
416 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
419 xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
420 CvXSUBANY(xsub).any_ptr = sv_magicext(
422 param, /* mg_obj: refcnt will be increased */
424 &mouse_util_type_constraints_vtbl,
425 (void*)fptr, /* mg_ptr */
426 0 /* mg_len: 0 for static data */
430 sv_2mortal((SV*)xsub);
437 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
439 const char* klass_pv = SvPV_const(klass, klass_len);
443 klass_pv = mouse_canonicalize_package_name(klass_pv);
445 if(strNE(klass_pv, "UNIVERSAL")){
446 param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
447 fptr = (void*)mouse_is_an_instance_of;
452 fptr = (void*)mouse_is_an_instance_of_universal;
455 return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
458 XS(XS_Mouse_constraint_check) {
461 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
464 croak("Too few arguments for type constraint check functions");
468 ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
473 setup_my_cxt(pTHX_ pMY_CXT){
474 MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
475 SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
478 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
480 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints
483 VERSIONCHECK: DISABLE
488 setup_my_cxt(aTHX_ aMY_CXT);
490 /* setup built-in type constraints */
500 DEFINE_TC(ScalarRef);
505 DEFINE_TC(FileHandle);
506 DEFINE_TC(RegexpRef);
508 DEFINE_TC(ClassName);
519 setup_my_cxt(aTHX_ aMY_CXT);
520 PERL_UNUSED_VAR(items);
523 #endif /* !USE_ITHREADS */
525 #define MOUSE_TC_MAYBE 0
526 #define MOUSE_TC_ARRAY_REF 1
527 #define MOUSE_TC_HASH_REF 2
530 _parameterize_ArrayRef_for(SV* param)
532 _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
533 _parameterize_HashRef_for = MOUSE_TC_HASH_REF
534 _parameterize_Maybe_for = MOUSE_TC_MAYBE
538 SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
539 if(!IsCodeRef(tc_code)){
540 croak("_compiled_type_constraint didn't return a CODE reference");
544 case MOUSE_TC_ARRAY_REF:
545 fptr = mouse_parameterized_ArrayRef;
547 case MOUSE_TC_HASH_REF:
548 fptr = mouse_parameterized_HashRef;
550 default: /* Maybe type */
551 fptr = mouse_parameterized_Maybe;
553 RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
558 MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint
561 INSTALL_SIMPLE_READER(TypeConstraint, name);
562 INSTALL_SIMPLE_READER(TypeConstraint, parent);
563 INSTALL_SIMPLE_READER(TypeConstraint, message);
565 INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
566 INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
568 INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
571 compile_type_constraint(SV* self)
574 AV* const checks = newAV_mortal();
575 SV* check; /* check function */
579 for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
580 check = get_slots(parent, "hand_optimized_type_constraint");
581 if(check && SvOK(check)){
582 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
583 croak("Not a CODE reference");
585 av_unshift(checks, 1);
586 av_store(checks, 0, newSVsv(check));
587 break; /* a hand optimized constraint must include all the parent */
590 check = get_slots(parent, "constraint");
591 if(check && SvOK(check)){
592 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
593 croak("Not a CODE reference");
595 av_unshift(checks, 1);
596 av_store(checks, 0, newSVsv(check));
600 check = get_slots(self, "constraint");
601 if(check && SvOK(check)){
602 if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
603 croak("Not a CODE reference");
605 av_push(checks, newSVsv(check));
608 types_ref = get_slots(self, "type_constraints");
609 if(types_ref && SvOK(types_ref)){ /* union type */
616 if(!IsArrayRef(types_ref)){
617 croak("Not an ARRAY reference");
619 types = (AV*)SvRV(types_ref);
620 len = av_len(types) + 1;
622 union_checks = newAV_mortal();
624 for(i = 0; i < len; i++){
625 SV* const tc = *av_fetch(types, i, TRUE);
626 SV* const c = get_slots(tc, "compiled_type_constraint");
627 if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
629 croak("'%"SVf"' has no compiled type constraint", self);
631 av_push(union_checks, newSVsv(c));
634 union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
635 av_push(checks, newRV_inc((SV*)union_check));
638 if(AvFILLp(checks) < 0){
639 check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
642 check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
644 set_slots(self, "compiled_type_constraint", check);