| 1 |
|
| #ifdef __cplusplus |
| 2 |
|
| extern "C" { |
| 3 |
|
| #endif |
| 4 |
|
| |
| 5 |
|
| #define PERL_NO_GET_CONTEXT /* we want efficiency */ |
| 6 |
|
| #include <EXTERN.h> |
| 7 |
|
| #include <perl.h> |
| 8 |
|
| #include <XSUB.h> |
| 9 |
|
| |
| 10 |
|
| #ifdef __cplusplus |
| 11 |
|
| } /* extern "C" */ |
| 12 |
|
| #endif |
| 13 |
|
| |
| 14 |
|
| #define NEED_mg_findext |
| 15 |
|
| #define NEED_newSVpvn_flags |
| 16 |
|
| #include "ppport.h" |
| 17 |
|
| |
| 18 |
|
| #ifndef GvCV_set |
| 19 |
|
| # define GvCV_set(gv,cv) (GvGP(gv)->gp_cv = (cv)) |
| 20 |
|
| #endif |
| 21 |
|
| |
| 22 |
|
| #ifndef gv_init_pvn |
| 23 |
|
| # define gv_init_pvn gv_init |
| 24 |
|
| #endif |
| 25 |
|
| |
| 26 |
|
| #define IsArrayRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV) |
| 27 |
|
| #define IsHashRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV) |
| 28 |
|
| #define IsCodeRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVCV) |
| 29 |
|
| #define WANT_ARRAY GIMME_V == G_ARRAY |
| 30 |
|
| |
| 31 |
|
| XS(XS_prototype_method); |
| 32 |
|
| XS(XS_prototype_getter); |
| 33 |
|
| |
| 34 |
|
| static MGVTBL getter_vtbl = { 0 }; |
| 35 |
|
| |
| 36 |
|
| static GV * |
| 37 |
240 |
| prototype_gv_pvn(pTHX_ HV *stash, const char *name, STRLEN len, U32 flags) |
| 38 |
|
| { |
| 39 |
240 |
| GV *gv = (GV *)newSV(0); |
| 40 |
240 |
| gv_init_pvn(gv, stash, name, len, flags); |
| 41 |
240 |
| return gv; |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| static GV * |
| 45 |
188 |
| prototype_gv_sv(pTHX_ HV *stash, SV *namesv) |
| 46 |
|
| { |
| 47 |
188 |
| U32 flag = 0; |
| 48 |
|
| char *namepv; |
| 49 |
|
| STRLEN namelen; |
| 50 |
188 |
| namepv = SvPV(namesv, namelen); |
| 51 |
188 |
▶ | if (SvUTF8(namesv)) flag = SVf_UTF8; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvUTF8(namesv)) flag = SVf_UTF8; |
7 |
181 |
188 |
|
| 52 |
188 |
| return prototype_gv_pvn(aTHX_ stash, namepv, namelen, flag); |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
|
| static void |
| 56 |
188 |
| add_method_sv(pTHX_ HV *stash, SV *method, CV *code) |
| 57 |
|
| { |
| 58 |
|
| GV *gv; |
| 59 |
188 |
| gv = prototype_gv_sv(aTHX_ stash, method); |
| 60 |
188 |
| GvCV_set(gv, code); |
| 61 |
188 |
| hv_store_ent(stash, method, (SV *)gv, 0); |
| 62 |
|
| #if PERL_VERSION >= 10 |
| 63 |
188 |
| mro_method_changed_in(stash); |
| 64 |
|
| #else |
| 65 |
|
| PL_sub_generation++; |
| 66 |
|
| #endif |
| 67 |
188 |
| } |
| 68 |
|
| |
| 69 |
|
| static CV * |
| 70 |
144 |
| make_closure(pTHX_ SV *retval) |
| 71 |
|
| { |
| 72 |
|
| /* Release the destination even when fetching magic throws on older Perls. */ |
| 73 |
144 |
| SV *value = sv_newmortal(); |
| 74 |
144 |
| sv_setsv(value, retval); |
| 75 |
141 |
| CV *xsub = newXS(NULL /* anonymous */, XS_prototype_getter, __FILE__); |
| 76 |
|
| /* Magic owns the scalar for exactly as long as the getter CV. */ |
| 77 |
141 |
| sv_magicext((SV *)xsub, value, PERL_MAGIC_ext, &getter_vtbl, NULL, 0); |
| 78 |
141 |
| return xsub; |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| static void |
| 82 |
48 |
| push_values(pTHX_ SV *retval) |
| 83 |
|
| { |
| 84 |
48 |
| dSP; |
| 85 |
|
| /* A later argument can replace the getter and release its owned values. */ |
| 86 |
50 |
▶ | if (WANT_ARRAY && IsArrayRef(retval)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (WANT_ARRAY && IsArrayRef(retval)) { |
6 |
42 |
48 |
| if (WANT_ARRAY && IsArrayRef(retval)) { |
4 |
2 |
6 |
| if (WANT_ARRAY && IsArrayRef(retval)) { |
4 |
0 |
4 |
| if (WANT_ARRAY && IsArrayRef(retval)) { |
2 |
2 |
4 |
|
| 87 |
2 |
| AV *av = (AV *)SvRV(retval); |
| 88 |
2 |
| I32 len = av_len(av) + 1; |
| 89 |
2 |
▶ | EXTEND(SP, len); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, len); |
2 |
0 |
2 |
| EXTEND(SP, len); |
0 |
2 |
2 |
|
| 90 |
13 |
▶ | for (I32 i = 0; i < len; i++){ |
Branch: 1 branch
| Branch | True | False | Total |
| for (I32 i = 0; i < len; i++){ |
11 |
2 |
13 |
|
| 91 |
11 |
| SV **const svp = av_fetch(av, i, FALSE); |
| 92 |
11 |
▶ | PUSHs(svp ? sv_2mortal(SvREFCNT_inc(*svp)) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(svp ? sv_2mortal(SvREFCNT_inc(*svp)) : &PL_sv_undef); |
11 |
0 |
11 |
|
| 93 |
|
| } |
| 94 |
48 |
▶ | } else if (WANT_ARRAY && IsHashRef(retval)) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (WANT_ARRAY && IsHashRef(retval)) { |
4 |
42 |
46 |
| } else if (WANT_ARRAY && IsHashRef(retval)) { |
2 |
2 |
4 |
| } else if (WANT_ARRAY && IsHashRef(retval)) { |
2 |
0 |
2 |
| } else if (WANT_ARRAY && IsHashRef(retval)) { |
2 |
0 |
2 |
|
| 95 |
2 |
| HV *hv = (HV *)SvRV(retval); |
| 96 |
|
| HE *he; |
| 97 |
2 |
| hv_iterinit(hv); |
| 98 |
5 |
▶ | while ((he = hv_iternext(hv)) != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv)) != NULL){ |
3 |
2 |
5 |
|
| 99 |
3 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
3 |
3 |
|
| 100 |
3 |
| PUSHs(hv_iterkeysv(he)); |
| 101 |
3 |
| PUSHs(sv_2mortal(SvREFCNT_inc(hv_iterval(hv, he)))); |
| 102 |
|
| } |
| 103 |
|
| } else { |
| 104 |
44 |
▶ | XPUSHs(retval ? sv_2mortal(SvREFCNT_inc(retval)) : &PL_sv_undef); |
Branch: 2 branches
| Branch | True | False | Total |
| XPUSHs(retval ? sv_2mortal(SvREFCNT_inc(retval)) : &PL_sv_undef); |
0 |
44 |
44 |
| XPUSHs(retval ? sv_2mortal(SvREFCNT_inc(retval)) : &PL_sv_undef); |
44 |
0 |
44 |
|
| 105 |
|
| } |
| 106 |
48 |
| PUTBACK; |
| 107 |
48 |
| } |
| 108 |
|
| |
| 109 |
|
| static CV * |
| 110 |
52 |
| make_prototype_method(pTHX) |
| 111 |
|
| { |
| 112 |
|
| CV *xsub; |
| 113 |
52 |
| xsub = newXS(NULL /* anonymous */, XS_prototype_method, __FILE__); |
| 114 |
52 |
| return xsub; |
| 115 |
|
| } |
| 116 |
|
| |
| 117 |
|
| static void |
| 118 |
52 |
| install_prototype_method(pTHX_ HV *stash) |
| 119 |
|
| { |
| 120 |
52 |
| char *prototype = "prototype"; |
| 121 |
52 |
| CV *prototype_cv = make_prototype_method(aTHX); |
| 122 |
52 |
| GV *prototype_glob = prototype_gv_pvn(aTHX_ stash, prototype, 9, 0); |
| 123 |
52 |
| GvCV_set(prototype_glob, prototype_cv); |
| 124 |
52 |
| hv_store(stash, prototype, 9, (SV *)prototype_glob, 0); |
| 125 |
52 |
| } |
| 126 |
|
| |
| 127 |
48 |
| XS(XS_prototype_getter) |
| 128 |
|
| { |
| 129 |
48 |
| dVAR; dXSARGS; |
| 130 |
48 |
| SV *retval = mg_findext((SV *)cv, PERL_MAGIC_ext, &getter_vtbl)->mg_obj; |
| 131 |
48 |
| SP -= items; /* PPCODE */ |
| 132 |
48 |
| PUTBACK; |
| 133 |
48 |
| push_values(aTHX_ retval); |
| 134 |
48 |
| } |
| 135 |
|
| |
| 136 |
38 |
| XS(XS_prototype_method) |
| 137 |
|
| { |
| 138 |
38 |
| dVAR; dXSARGS; |
| 139 |
38 |
▶ | if ((items - 1) % 2 != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((items - 1) % 2 != 0) |
0 |
38 |
38 |
|
| 140 |
0 |
| Perl_croak(aTHX_ "Argument isn't hash type"); |
| 141 |
|
| |
| 142 |
38 |
▶ | if (items < 1 || !SvROK(ST(0)) || !SvOBJECT(SvRV(ST(0)))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (items < 1 || !SvROK(ST(0)) || !SvOBJECT(SvRV(ST(0)))) |
38 |
0 |
38 |
| if (items < 1 || !SvROK(ST(0)) || !SvOBJECT(SvRV(ST(0)))) |
37 |
1 |
38 |
| if (items < 1 || !SvROK(ST(0)) || !SvOBJECT(SvRV(ST(0)))) |
0 |
37 |
37 |
|
| 143 |
1 |
| Perl_croak(aTHX_ "prototype requires an object invocant"); |
| 144 |
37 |
| HV *stash = SvSTASH(SvRV(ST(0))); |
| 145 |
37 |
| I32 i = 1; /* First argument is skip: `my $self = shift;` */ |
| 146 |
90 |
▶ | while (i < items) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < items) { |
60 |
30 |
90 |
|
| 147 |
60 |
| SV *method = ST(i++); |
| 148 |
|
| STRLEN namelen; |
| 149 |
56 |
| const char *name = SvPV(method, namelen); |
| 150 |
56 |
| method = sv_2mortal(newSVpvn_flags(name, namelen, SvUTF8(method) ? SVf_UTF8 : 0)); |
| 151 |
56 |
| SV *val = ST(i++); |
| 152 |
56 |
▶ | CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
Branch: 3 branches
| Branch | True | False | Total |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
38 |
18 |
56 |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
38 |
0 |
38 |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
36 |
2 |
38 |
|
| 153 |
53 |
| add_method_sv(aTHX_ stash, method, cv); |
| 154 |
|
| } |
| 155 |
30 |
| XSRETURN(0); |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| MODULE = Package::Prototype PACKAGE = Package::Prototype |
| 159 |
|
| PROTOTYPES: DISABLE |
| 160 |
|
| |
| 161 |
|
| void * |
| 162 |
|
| bless(klass, ref, pkgsv=NULL) |
| 163 |
|
| SV *klass; |
| 164 |
|
| SV *ref; |
| 165 |
|
| SV *pkgsv; |
| 166 |
|
| PREINIT: |
| 167 |
|
| char *pkg; |
| 168 |
|
| STRLEN pkglen; |
| 169 |
|
| HE* entry; |
| 170 |
|
| HV *stash; |
| 171 |
|
| PPCODE: |
| 172 |
|
| { |
| 173 |
52 |
▶ | if (!IsHashRef(ref)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!IsHashRef(ref)) |
52 |
0 |
52 |
| if (!IsHashRef(ref)) |
52 |
0 |
52 |
| if (!IsHashRef(ref)) |
0 |
52 |
52 |
|
| 174 |
0 |
| Perl_croak(aTHX_ "Please pass an hash reference to the first argument"); |
| 175 |
|
| |
| 176 |
52 |
▶ | if (pkgsv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pkgsv) { |
5 |
47 |
52 |
|
| 177 |
5 |
| pkg = SvPV(pkgsv, pkglen); |
| 178 |
|
| } else { |
| 179 |
47 |
| pkg = "__ANON__"; |
| 180 |
47 |
| pkglen = 8; |
| 181 |
|
| } |
| 182 |
|
| |
| 183 |
52 |
| stash = (HV *)sv_2mortal((SV *)newHV()); |
| 184 |
52 |
▶ | hv_name_set(stash, pkg, pkglen, pkgsv && SvUTF8(pkgsv) ? SVf_UTF8 : 0); |
Branch: 2 branches
| Branch | True | False | Total |
| hv_name_set(stash, pkg, pkglen, pkgsv && SvUTF8(pkgsv) ? SVf_UTF8 : 0); |
5 |
47 |
52 |
| hv_name_set(stash, pkg, pkglen, pkgsv && SvUTF8(pkgsv) ? SVf_UTF8 : 0); |
1 |
4 |
5 |
|
| 185 |
|
| |
| 186 |
52 |
| install_prototype_method(aTHX_ stash); |
| 187 |
|
| |
| 188 |
52 |
| HV *hv = (HV *)SvRV(ref); |
| 189 |
52 |
| hv_iterinit(hv); |
| 190 |
187 |
▶ | while ((entry = hv_iternext(hv)) != NULL){ |
Branch: 1 branch
| Branch | True | False | Total |
| while ((entry = hv_iternext(hv)) != NULL){ |
135 |
52 |
187 |
|
| 191 |
|
| I32 keylen; |
| 192 |
135 |
| char* key = hv_iterkey(entry, &keylen); |
| 193 |
135 |
▶ | if (0 < keylen && key[0] != '_') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (0 < keylen && key[0] != '_') { |
135 |
0 |
135 |
| if (0 < keylen && key[0] != '_') { |
135 |
0 |
135 |
|
| 194 |
135 |
| SV *method = hv_iterkeysv(entry); |
| 195 |
135 |
| SV *val = hv_delete_ent(hv, method, 0, 0); |
| 196 |
135 |
▶ | CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
Branch: 3 branches
| Branch | True | False | Total |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
53 |
82 |
135 |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
45 |
8 |
53 |
| CV *cv = IsCodeRef(val) ? (CV *)SvREFCNT_inc(SvRV(val)) : make_closure(aTHX_ val); |
11 |
34 |
45 |
|
| 197 |
135 |
| add_method_sv(aTHX_ stash, method, cv); |
| 198 |
|
| } |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
52 |
| ST(0) = sv_bless(ref, stash); |
| 202 |
52 |
| XSRETURN(1); |
| 203 |
|
| } |