From: Florian Ragwitz Date: Mon, 16 Mar 2009 17:54:27 +0000 (+0100) Subject: Add a helper function to make calling predicate methods from c easy. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=705f9e7b47ba6a281240436fbbd71ab00612f2cd;p=gitmo%2FClass-MOP.git Add a helper function to make calling predicate methods from c easy. --- diff --git a/cmop/mop.c b/cmop/mop.c index 126568d..b921879 100644 --- a/cmop/mop.c +++ b/cmop/mop.c @@ -75,6 +75,39 @@ mop_call0 (pTHX_ SV *const self, SV *const method) return ret; } +bool +mop_call_predicate (SV *self, const char *method) +{ + dSP; + I32 count; + SV *sv; + bool ret; + + ENTER; + SAVETMPS; + + PUSHMARK (SP); + XPUSHs (self); + PUTBACK; + + count = call_method (method, G_SCALAR); + + if (count != 1) { + croak ("predicate method %s didn't return exactly one value", method); + } + + SPAGAIN; + + sv = POPs; + ret = SvTRUE (sv); + + PUTBACK; + FREETMPS; + LEAVE; + + return ret; +} + int mop_get_code_info (SV *coderef, char **pkg, char **name) { diff --git a/include/mop.h b/include/mop.h index 7566652..f6ea76f 100644 --- a/include/mop.h +++ b/include/mop.h @@ -49,6 +49,7 @@ extern SV *mop_wrap; UV mop_check_package_cache_flag(pTHX_ HV *stash); int mop_get_code_info (SV *coderef, char **pkg, char **name); SV *mop_call0(pTHX_ SV *const self, SV *const method); +bool mop_call_predicate (SV *self, const char *method); typedef enum { TYPE_FILTER_NONE,