| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include <EXTERN.h> |
| 3 |
|
| #include <perl.h> |
| 4 |
|
| #include <XSUB.h> |
| 5 |
|
| |
| 6 |
|
| #include "ppport.h" |
| 7 |
|
| |
| 8 |
|
| #define NEED_mro_get_linear_isa |
| 9 |
|
| #include "mro_compat.h" |
| 10 |
|
| |
| 11 |
|
| #include "streq.h" |
| 12 |
|
| |
| 13 |
|
| #define MY_CXT_KEY "Scalar::Util::Instance::_guts" XS_VERSION |
| 14 |
|
| typedef struct sui_cxt{ |
| 15 |
|
| GV* universal_isa; |
| 16 |
|
| } my_cxt_t; |
| 17 |
|
| START_MY_CXT |
| 18 |
|
| |
| 19 |
|
| #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj) |
| 20 |
|
| #define MG_klass_pv(mg) ((mg)->mg_ptr) |
| 21 |
|
| #define MG_klass_len(mg) ((mg)->mg_len) |
| 22 |
|
| |
| 23 |
|
| static MGVTBL scalar_util_instance_vtbl; |
| 24 |
|
| |
| 25 |
|
| static const char* |
| 26 |
94 |
| canonicalize_package_name(const char* name){ |
| 27 |
|
| |
| 28 |
|
| /* "::Foo" -> "Foo" */ |
| 29 |
94 |
▶ | if(name[0] == ':' && name[1] == ':'){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(name[0] == ':' && name[1] == ':'){ |
7 |
87 |
94 |
| if(name[0] == ':' && name[1] == ':'){ |
7 |
0 |
7 |
|
| 30 |
7 |
| name += 2; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
|
| /* "main::main::main::Foo" -> "Foo" */ |
| 34 |
103 |
▶ | while(strnEQ(name, "main::", sizeof("main::")-1)){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(strnEQ(name, "main::", sizeof("main::")-1)){ |
9 |
94 |
103 |
|
| 35 |
9 |
| name += sizeof("main::")-1; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
94 |
| return name; |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| static int |
| 42 |
32 |
| lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){ |
| 43 |
32 |
| AV* const linearized_isa = mro_get_linear_isa(instance_stash); |
| 44 |
32 |
| SV** svp = AvARRAY(linearized_isa); |
| 45 |
32 |
| SV** const end = svp + AvFILLp(linearized_isa) + 1; |
| 46 |
|
| |
| 47 |
76 |
▶ | while(svp != end){ |
Branch: 1 branch
| Branch | True | False | Total |
| while(svp != end){ |
59 |
17 |
76 |
|
| 48 |
|
| assert(SvPVX(*svp)); |
| 49 |
59 |
▶ | if(strEQ(klass_pv, canonicalize_package_name(SvPVX(*svp)))){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(strEQ(klass_pv, canonicalize_package_name(SvPVX(*svp)))){ |
15 |
44 |
59 |
|
| 50 |
15 |
| return TRUE; |
| 51 |
|
| } |
| 52 |
44 |
| svp++; |
| 53 |
|
| } |
| 54 |
17 |
| return FALSE; |
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| static int |
| 58 |
47 |
| instance_isa(pTHX_ SV* const instance, const MAGIC* const mg){ |
| 59 |
|
| dMY_CXT; |
| 60 |
47 |
| HV* const instance_stash = SvSTASH(SvRV(instance)); |
| 61 |
47 |
| GV* const instance_isa = gv_fetchmeth_autoload(instance_stash, "isa", sizeof("isa")-1, 0); |
| 62 |
|
| |
| 63 |
|
| /* the instance has no own isa method */ |
| 64 |
47 |
▶ | if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){ |
Branch: 2 branches
| Branch | True | False | Total |
| if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){ |
47 |
0 |
47 |
| if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){ |
41 |
6 |
47 |
|
| 65 |
41 |
| return MG_klass_stash(mg) == instance_stash |
| 66 |
41 |
▶ | || lookup_isa(aTHX_ instance_stash, MG_klass_pv(mg)); |
Branch: 2 branches
| Branch | True | False | Total |
| || lookup_isa(aTHX_ instance_stash, MG_klass_pv(mg)); |
32 |
9 |
41 |
| || lookup_isa(aTHX_ instance_stash, MG_klass_pv(mg)); |
15 |
17 |
32 |
|
| 67 |
|
| } |
| 68 |
|
| /* the instance has its own isa method */ |
| 69 |
|
| else { |
| 70 |
|
| int retval; |
| 71 |
6 |
| dSP; |
| 72 |
|
| |
| 73 |
6 |
| ENTER; |
| 74 |
6 |
| SAVETMPS; |
| 75 |
|
| |
| 76 |
6 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
6 |
6 |
|
| 77 |
6 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
6 |
6 |
|
| 78 |
6 |
| PUSHs(instance); |
| 79 |
6 |
| mPUSHp(MG_klass_pv(mg), MG_klass_len(mg)); |
| 80 |
6 |
| PUTBACK; |
| 81 |
|
| |
| 82 |
6 |
| call_sv((SV*)instance_isa, G_SCALAR); |
| 83 |
|
| |
| 84 |
5 |
| SPAGAIN; |
| 85 |
|
| |
| 86 |
5 |
| retval = SvTRUEx(POPs); |
| 87 |
|
| |
| 88 |
5 |
| PUTBACK; |
| 89 |
|
| |
| 90 |
5 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
5 |
0 |
5 |
|
| 91 |
5 |
| LEAVE; |
| 92 |
|
| |
| 93 |
5 |
| return retval; |
| 94 |
|
| } |
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
|
| XS(XS_isa_check); /* -W */ |
| 98 |
54 |
| XS(XS_isa_check){ |
| 99 |
|
| dVAR; |
| 100 |
54 |
| dXSARGS; |
| 101 |
|
| SV* sv; |
| 102 |
|
| |
| 103 |
|
| assert(XSANY.any_ptr != NULL); |
| 104 |
|
| |
| 105 |
54 |
▶ | if(items != 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(items != 1){ |
2 |
52 |
54 |
|
| 106 |
2 |
▶ | if(items < 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(items < 1){ |
1 |
1 |
2 |
|
| 107 |
1 |
| croak("Not enough arguments for is-a predicate"); |
| 108 |
|
| } |
| 109 |
|
| else{ |
| 110 |
1 |
| croak("Too many arguments for is-a predicate"); |
| 111 |
|
| } |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
52 |
| sv = ST(0); |
| 115 |
52 |
▶ | SvGETMAGIC(sv); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(sv); |
2 |
50 |
52 |
| SvGETMAGIC(sv); |
0 |
2 |
2 |
|
| 116 |
|
| |
| 117 |
52 |
▶ | ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) && instance_isa(aTHX_ sv, (MAGIC*)XSANY.any_ptr) ); |
Branch: 3 branches
| Branch | True | False | Total |
| ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) && instance_isa(aTHX_ sv, (MAGIC*)XSANY.any_ptr) ); |
49 |
3 |
52 |
| ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) && instance_isa(aTHX_ sv, (MAGIC*)XSANY.any_ptr) ); |
47 |
2 |
49 |
| ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) && instance_isa(aTHX_ sv, (MAGIC*)XSANY.any_ptr) ); |
27 |
19 |
46 |
|
| 118 |
51 |
| XSRETURN(1); |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| XS(XS_isa_check_for_universal); /* -W */ |
| 122 |
6 |
| XS(XS_isa_check_for_universal){ |
| 123 |
|
| dVAR; |
| 124 |
6 |
| dXSARGS; |
| 125 |
|
| SV* sv; |
| 126 |
|
| PERL_UNUSED_VAR(cv); |
| 127 |
|
| |
| 128 |
6 |
▶ | if(items != 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(items != 1){ |
2 |
4 |
6 |
|
| 129 |
2 |
▶ | if(items < 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(items < 1){ |
1 |
1 |
2 |
|
| 130 |
1 |
| croak("Not enough arguments for is-a predicate"); |
| 131 |
|
| } |
| 132 |
|
| else{ |
| 133 |
1 |
| croak("Too many arguments for is-a predicate"); |
| 134 |
|
| } |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
4 |
| sv = ST(0); |
| 138 |
4 |
▶ | SvGETMAGIC(sv); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(sv); |
0 |
4 |
4 |
| SvGETMAGIC(sv); |
0 |
0 |
0 |
|
| 139 |
|
| |
| 140 |
4 |
▶ | ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) ); |
Branch: 2 branches
| Branch | True | False | Total |
| ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) ); |
4 |
0 |
4 |
| ST(0) = boolSV( SvROK(sv) && SvOBJECT(SvRV(sv)) ); |
4 |
0 |
4 |
|
| 141 |
4 |
| XSRETURN(1); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| static void |
| 145 |
5 |
| setup_my_cxt(pTHX_ pMY_CXT){ |
| 146 |
5 |
| MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV); |
| 147 |
5 |
| SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa); |
| 148 |
5 |
| } |
| 149 |
|
| |
| 150 |
|
| MODULE = Scalar::Util::Instance PACKAGE = Scalar::Util::Instance |
| 151 |
|
| |
| 152 |
|
| PROTOTYPES: DISABLE |
| 153 |
|
| |
| 154 |
|
| BOOT: |
| 155 |
|
| { |
| 156 |
|
| MY_CXT_INIT; |
| 157 |
5 |
| setup_my_cxt(aTHX_ aMY_CXT); |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| #ifdef USE_ITHREADS |
| 161 |
|
| |
| 162 |
|
| void |
| 163 |
|
| CLONE(...) |
| 164 |
|
| CODE: |
| 165 |
|
| { |
| 166 |
|
| MY_CXT_CLONE; |
| 167 |
|
| setup_my_cxt(aTHX_ aMY_CXT); |
| 168 |
|
| PERL_UNUSED_VAR(items); |
| 169 |
|
| } |
| 170 |
|
| |
| 171 |
|
| #endif /* !USE_ITHREADS */ |
| 172 |
|
| |
| 173 |
|
| void |
| 174 |
|
| generate_for(self, SV* klass, const char* predicate_name = NULL) |
| 175 |
|
| PPCODE: |
| 176 |
|
| { |
| 177 |
|
| STRLEN klass_len; |
| 178 |
|
| const char* klass_pv; |
| 179 |
|
| HV* stash; |
| 180 |
|
| CV* xsub; |
| 181 |
|
| |
| 182 |
36 |
▶ | if(!SvOK(klass)){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(klass)){ |
1 |
35 |
36 |
|
| 183 |
1 |
| croak("You must define a class name for generate_for"); |
| 184 |
|
| } |
| 185 |
35 |
| klass_pv = SvPV_const(klass, klass_len); |
| 186 |
35 |
| klass_pv = canonicalize_package_name(klass_pv); |
| 187 |
|
| |
| 188 |
35 |
▶ | if(strNE(klass_pv, "UNIVERSAL")){ |
Branch: 1 branch
| Branch | True | False | Total |
| if(strNE(klass_pv, "UNIVERSAL")){ |
32 |
3 |
35 |
|
| 189 |
32 |
| xsub = newXS(predicate_name, XS_isa_check, __FILE__); |
| 190 |
|
| |
| 191 |
32 |
| stash = gv_stashpvn(klass_pv, klass_len, GV_ADD); |
| 192 |
|
| |
| 193 |
32 |
| CvXSUBANY(xsub).any_ptr = sv_magicext( |
| 194 |
|
| (SV*)xsub, |
| 195 |
|
| (SV*)stash, /* mg_obj */ |
| 196 |
|
| PERL_MAGIC_ext, |
| 197 |
|
| &scalar_util_instance_vtbl, |
| 198 |
|
| klass_pv, /* mg_ptr */ |
| 199 |
|
| klass_len /* mg_len */ |
| 200 |
|
| ); |
| 201 |
|
| } |
| 202 |
|
| else{ |
| 203 |
3 |
| xsub = newXS(predicate_name, XS_isa_check_for_universal, __FILE__); |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
35 |
▶ | if(predicate_name == NULL){ /* anonymous predicate */ |
Branch: 1 branch
| Branch | True | False | Total |
| if(predicate_name == NULL){ /* anonymous predicate */ |
25 |
10 |
35 |
|
| 207 |
25 |
▶ | XPUSHs( newRV_noinc((SV*)xsub) ); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs( newRV_noinc((SV*)xsub) ); |
0 |
25 |
25 |
|
| 208 |
|
| } |
| 209 |
|
| } |
| 210 |
|
| |