Fixed the example showing parameter type constraints
[p5sagit/Function-Parameters.git] / Parameters.xs
index 8f77a63..4a44068 100644 (file)
@@ -85,11 +85,10 @@ WARNINGS_ENABLE
 #define HINTK_FLAGS_   MY_PKG "/flags:"
 #define HINTK_SHIFT_   MY_PKG "/shift:"
 #define HINTK_ATTRS_   MY_PKG "/attrs:"
+#define HINTK_REIFY_   MY_PKG "/reify:"
 
 #define DEFSTRUCT(T) typedef struct T T; struct T
 
-#define UV_BITS (sizeof (UV) * CHAR_BIT)
-
 enum {
        FLAG_NAME_OK      = 0x01,
        FLAG_ANON_OK      = 0x02,
@@ -103,6 +102,7 @@ enum {
 
 DEFSTRUCT(KWSpec) {
        unsigned flags;
+       I32 reify_type;
        SV *shift;
        SV *attrs;
 };
@@ -220,15 +220,6 @@ static int my_sv_eq_pvn(pTHX_ SV *sv, const char *p, STRLEN n) {
 #include "padop_on_crack.c.inc"
 
 
-static void my_require(pTHX_ const char *file) {
-       SV *err;
-       require_pv(file);
-       err = ERRSV;
-       if (SvTRUE(err)) {
-               croak_sv(err);
-       }
-}
-
 enum {
        MY_ATTR_LVALUE = 0x01,
        MY_ATTR_METHOD = 0x02,
@@ -506,22 +497,33 @@ static SV *parse_type(pTHX_ Sentinel sen, const SV *declarator) {
        return t;
 }
 
-static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, SV *name) {
-       SV *t;
+static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, const KWSpec *spec, SV *name) {
+       AV *type_reifiers;
+       SV *t, *sv, **psv;
        int n;
        dSP;
 
-       my_require(aTHX_ "Moose/Util/TypeConstraints.pm");
+       type_reifiers = get_av(MY_PKG "::type_reifiers", 0);
+       assert(type_reifiers != NULL);
+
+       if (spec->reify_type < 0 || spec->reify_type > av_len(type_reifiers)) {
+               croak("In %"SVf": internal error: reify_type [%ld] out of range [%ld]", SVfARG(declarator), (long)spec->reify_type, (long)(av_len(type_reifiers) + 1));
+       }
+
+       psv = av_fetch(type_reifiers, spec->reify_type, 0);
+       assert(psv != NULL);
+       sv = *psv;
 
        ENTER;
        SAVETMPS;
 
        PUSHMARK(SP);
-       EXTEND(SP, 1);
+       EXTEND(SP, 2);
        PUSHs(name);
+       PUSHs(PL_curstname);
        PUTBACK;
 
-       n = call_pv("Moose::Util::TypeConstraints::find_or_create_isa_type_constraint", G_SCALAR);
+       n = call_sv(sv, G_SCALAR);
        SPAGAIN;
 
        assert(n == 1);
@@ -801,7 +803,7 @@ static PADOFFSET parse_param(
                        }
                        *ptype = my_eval(aTHX_ sen, floor, expr);
                        if (!SvROK(*ptype)) {
-                               *ptype = reify_type(aTHX_ sen, declarator, *ptype);
+                               *ptype = reify_type(aTHX_ sen, declarator, spec, *ptype);
                        }
                        if (!sv_isobject(*ptype)) {
                                croak("In %"SVf": (%"SVf") doesn't look like a type object", SVfARG(declarator), SVfARG(*ptype));
@@ -810,7 +812,7 @@ static PADOFFSET parse_param(
                        c = lex_peek_unichar(0);
                } else if (MY_UNI_IDFIRST(c)) {
                        *ptype = parse_type(aTHX_ sen, declarator);
-                       *ptype = reify_type(aTHX_ sen, declarator, *ptype);
+                       *ptype = reify_type(aTHX_ sen, declarator, spec, *ptype);
 
                        c = lex_peek_unichar(0);
                }
@@ -979,10 +981,11 @@ static void register_info(pTHX_ UV key, SV *declarator, const KWSpec *kws, const
                mPUSHu(key);
        }
        /* 1 */ {
-               size_t n;
+               STRLEN n;
                char *p = SvPV(declarator, n);
                char *q = memchr(p, ' ', n);
-               mPUSHp(p, q ? (size_t)(q - p) : n);
+               SV *tmp = newSVpvn_utf8(p, q ? (size_t)(q - p) : n, SvUTF8(declarator));
+               mPUSHs(tmp);
        }
        if (!ps) {
                if (SvTRUE(kws->shift)) {
@@ -1119,6 +1122,9 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        I32 c;
 
        declarator = sentinel_mortalize(sen, newSVpvn(keyword_ptr, keyword_len));
+       if (lex_bufutf8()) {
+               SvUTF8_on(declarator);
+       }
 
        lex_read_space(0);
 
@@ -1950,6 +1956,7 @@ static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len,
        SV *sv, **psv;
        const char *p, *kw_active;
        STRLEN kw_active_len;
+       bool kw_is_utf8;
 
        if (!(hints = GvHV(PL_hintgv))) {
                return FALSE;
@@ -1962,6 +1969,9 @@ static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len,
        if (kw_active_len <= kw_len) {
                return FALSE;
        }
+
+       kw_is_utf8 = lex_bufutf8();
+
        for (
                p = kw_active;
                (p = strchr(p, *kw_ptr)) &&
@@ -1979,17 +1989,23 @@ static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len,
                        SAVEDESTRUCTOR_X(sentinel_clear_void, sen);
 
                        spec->flags = 0;
+                       spec->reify_type = 0;
                        spec->shift = sentinel_mortalize(sen, newSVpvs(""));
                        spec->attrs = sentinel_mortalize(sen, newSVpvs(""));
 
 #define FETCH_HINTK_INTO(NAME, PTR, LEN, X) STMT_START { \
        const char *fk_ptr_; \
        STRLEN fk_len_; \
+       I32 fk_xlen_; \
        SV *fk_sv_; \
        fk_sv_ = sentinel_mortalize(sen, newSVpvs(HINTK_ ## NAME)); \
        sv_catpvn(fk_sv_, PTR, LEN); \
        fk_ptr_ = SvPV(fk_sv_, fk_len_); \
-       if (!((X) = hv_fetch(hints, fk_ptr_, fk_len_, 0))) { \
+       fk_xlen_ = fk_len_; \
+       if (kw_is_utf8) { \
+               fk_xlen_ = -fk_xlen_; \
+       } \
+       if (!((X) = hv_fetch(hints, fk_ptr_, fk_xlen_, 0))) { \
                croak("%s: internal error: $^H{'%.*s'} not set", MY_PKG, (int)fk_len_, fk_ptr_); \
        } \
 } STMT_END
@@ -1997,6 +2013,9 @@ static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len,
                        FETCH_HINTK_INTO(FLAGS_, kw_ptr, kw_len, psv);
                        spec->flags = SvIV(*psv);
 
+                       FETCH_HINTK_INTO(REIFY_, kw_ptr, kw_len, psv);
+                       spec->reify_type = SvIV(*psv);
+
                        FETCH_HINTK_INTO(SHIFT_, kw_ptr, kw_len, psv);
                        SvSetSV(spec->shift, *psv);
 
@@ -2062,6 +2081,7 @@ WARNINGS_ENABLE {
        newCONSTSUB(stash, "HINTK_FLAGS_",   newSVpvs(HINTK_FLAGS_));
        newCONSTSUB(stash, "HINTK_SHIFT_",   newSVpvs(HINTK_SHIFT_));
        newCONSTSUB(stash, "HINTK_ATTRS_",   newSVpvs(HINTK_ATTRS_));
+       newCONSTSUB(stash, "HINTK_REIFY_",   newSVpvs(HINTK_REIFY_));
        /**/
        next_keyword_plugin = PL_keyword_plugin;
        PL_keyword_plugin = my_keyword_plugin;