| 1 |
|
| #include "xs_version.h" |
| 2 |
|
| #include "xslate.h" |
| 3 |
|
| |
| 4 |
|
| #define TXBM_DECL(name) void name \ |
| 5 |
|
| (pTHX_ tx_state_t* const st PERL_UNUSED_DECL, SV* const retval, SV* const method PERL_UNUSED_DECL, SV** MARK) |
| 6 |
|
| |
| 7 |
|
| /* tx_bm _ TYPE _ MONIKER */ |
| 8 |
|
| #define TXBM_NAME(t, n) CAT2( CAT2(tx_bm, _), CAT2(t, CAT2(_, n))) |
| 9 |
|
| #define TXBM(t, moniker) static TXBM_DECL( TXBM_NAME(t, moniker)) |
| 10 |
|
| |
| 11 |
|
| #define TXBM_SETUP(t, name, nargs_min, nargs_max) \ |
| 12 |
|
| { STRINGIFY(t) "::" STRINGIFY(name), TXBM_NAME(t, name), nargs_min, nargs_max } |
| 13 |
|
| |
| 14 |
|
| typedef struct { |
| 15 |
|
| const char* const name; |
| 16 |
|
| |
| 17 |
|
| TXBM_DECL( (*body) ); |
| 18 |
|
| |
| 19 |
|
| U8 nargs_min; |
| 20 |
|
| U8 nargs_max; |
| 21 |
|
| } tx_builtin_method_t; |
| 22 |
|
| |
| 23 |
|
| #define MY_CXT_KEY "Text::Xslate::Methods::_guts" XS_VERSION |
| 24 |
|
| typedef struct { |
| 25 |
|
| tx_state_t* cmparg_st; |
| 26 |
|
| SV* cmparg_proc; |
| 27 |
|
| |
| 28 |
|
| HV* pair_stash; |
| 29 |
|
| } my_cxt_t; |
| 30 |
|
| START_MY_CXT; |
| 31 |
|
| |
| 32 |
|
| static SV* |
| 33 |
23 |
| tx_make_pair(pTHX_ HV* const stash, SV* const key, SV* const val) { |
| 34 |
|
| AV* av; |
| 35 |
|
| SV* pair[2]; |
| 36 |
23 |
| pair[0] = key; |
| 37 |
23 |
| pair[1] = val; |
| 38 |
|
| |
| 39 |
23 |
| av = av_make(2, pair); |
| 40 |
23 |
| return sv_bless( sv_2mortal( newRV_noinc((SV*)av) ), stash ); |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| static I32 |
| 44 |
16 |
| tx_pair_cmp(pTHX_ SV* const a, SV* const b) { |
| 45 |
|
| assert(SvROK(a)); |
| 46 |
|
| assert(SvTYPE(SvRV(a)) == SVt_PVAV); |
| 47 |
|
| assert(SvROK(b)); |
| 48 |
|
| assert(SvTYPE(SvRV(b)) == SVt_PVAV); |
| 49 |
|
| |
| 50 |
16 |
| return sv_cmp( |
| 51 |
|
| *av_fetch((AV*)SvRV(a), 0, TRUE), |
| 52 |
|
| *av_fetch((AV*)SvRV(b), 0, TRUE) |
| 53 |
|
| ); |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| static SV* |
| 57 |
26 |
| tx_keys(pTHX_ SV* const hvref) { |
| 58 |
26 |
| HV* const hv = (HV*)SvRV(hvref); |
| 59 |
26 |
| AV* const av = newAV(); |
| 60 |
26 |
| SV* const avref = sv_2mortal(newRV_noinc((SV*)av)); |
| 61 |
|
| HE* he; |
| 62 |
|
| I32 i; |
| 63 |
|
| |
| 64 |
|
| assert(SvROK(hvref)); |
| 65 |
|
| assert(SvTYPE(hv) == SVt_PVHV); |
| 66 |
|
| |
| 67 |
26 |
▶ | if(HvKEYS(hv) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(HvKEYS(hv) > 0) { |
2 |
24 |
26 |
| if(HvKEYS(hv) > 0) { |
22 |
4 |
26 |
|
| 68 |
22 |
▶ | av_extend(av, HvKEYS(hv) - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| av_extend(av, HvKEYS(hv) - 1); |
0 |
22 |
22 |
|
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
26 |
| hv_iterinit(hv); |
| 72 |
26 |
| i = 0; |
| 73 |
74 |
▶ | while((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while((he = hv_iternext(hv))) { |
48 |
26 |
74 |
|
| 74 |
48 |
| SV* const key = hv_iterkeysv(he); |
| 75 |
48 |
| (void)av_store(av, i++, key); |
| 76 |
48 |
| SvREFCNT_inc_simple_void_NN(key); |
| 77 |
|
| } |
| 78 |
26 |
| sortsv(AvARRAY(av), i, Perl_sv_cmp); |
| 79 |
26 |
| return avref; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
|
| /* NIL */ |
| 83 |
|
| |
| 84 |
|
| /* SCALAR */ |
| 85 |
|
| |
| 86 |
|
| /* ARRAY */ |
| 87 |
2 |
| TXBM(array, first) { |
| 88 |
2 |
| SV **svp = av_fetch((AV*)SvRV(*MARK), 0, FALSE); |
| 89 |
2 |
▶ | sv_setsv(retval, svp ? *svp : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv(retval, svp ? *svp : &PL_sv_undef); |
1 |
1 |
2 |
|
| 90 |
2 |
| } |
| 91 |
|
| |
| 92 |
2 |
| TXBM(array, last) { |
| 93 |
2 |
| AV* const av = (AV*)SvRV(*MARK); |
| 94 |
2 |
| SV **svp = av_fetch(av, av_len(av), FALSE); |
| 95 |
2 |
▶ | sv_setsv(retval, svp ? *svp : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv(retval, svp ? *svp : &PL_sv_undef); |
1 |
1 |
2 |
|
| 96 |
2 |
| } |
| 97 |
|
| |
| 98 |
6 |
| TXBM(array, size) { |
| 99 |
6 |
| sv_setiv(retval, av_len((AV*)SvRV(*MARK)) + 1); |
| 100 |
6 |
| } |
| 101 |
|
| |
| 102 |
62 |
| TXBM(array, join) { |
| 103 |
62 |
| dSP; |
| 104 |
62 |
| dORIGMARK; |
| 105 |
62 |
| AV* const av = (AV*)SvRV(*MARK); |
| 106 |
62 |
| I32 const len = av_len(av) + 1; |
| 107 |
|
| I32 i; |
| 108 |
|
| |
| 109 |
62 |
▶ | EXTEND(SP, len); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, len); |
62 |
0 |
62 |
| EXTEND(SP, len); |
0 |
62 |
62 |
|
| 110 |
62 |
| MARK = ORIGMARK; |
| 111 |
216 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
154 |
62 |
216 |
|
| 112 |
154 |
| SV** const svp = av_fetch(av, i, FALSE); |
| 113 |
154 |
▶ | PUSHs(svp ? *svp : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(svp ? *svp : &PL_sv_undef); |
154 |
0 |
154 |
|
| 114 |
|
| } |
| 115 |
|
| /* don't do PUTBACK */ |
| 116 |
|
| |
| 117 |
62 |
| MARK++; |
| 118 |
62 |
| sv_setpvs(retval, ""); |
| 119 |
62 |
| do_join(retval, *MARK, MARK, SP); |
| 120 |
62 |
| } |
| 121 |
|
| |
| 122 |
15 |
| TXBM(array, reverse) { |
| 123 |
15 |
| AV* const av = (AV*)SvRV(*MARK); |
| 124 |
15 |
| I32 const len = av_len(av) + 1; |
| 125 |
15 |
| AV* const result = newAV(); |
| 126 |
15 |
| SV* const resultref = sv_2mortal(newRV_noinc((SV*)result)); |
| 127 |
|
| I32 i; |
| 128 |
|
| |
| 129 |
15 |
| av_fill(result, len - 1); |
| 130 |
75 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
60 |
15 |
75 |
|
| 131 |
60 |
| SV** const svp = av_fetch(av, i, FALSE); |
| 132 |
60 |
▶ | (void)av_store(result, -(i+1), newSVsv(svp ? *svp : &PL_sv_undef)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)av_store(result, -(i+1), newSVsv(svp ? *svp : &PL_sv_undef)); |
60 |
0 |
60 |
|
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
15 |
| sv_setsv(retval, resultref); |
| 136 |
15 |
| } |
| 137 |
|
| |
| 138 |
|
| static I32 |
| 139 |
83 |
| tx_sv_cmp(pTHX_ SV* const x, SV* const y) { |
| 140 |
|
| dMY_CXT; |
| 141 |
83 |
| dSP; |
| 142 |
83 |
| tx_state_t* const st = MY_CXT.cmparg_st; |
| 143 |
83 |
| SV* const proc = MY_CXT.cmparg_proc; |
| 144 |
|
| SV* result; |
| 145 |
|
| |
| 146 |
|
| assert(st); |
| 147 |
|
| assert(proc); |
| 148 |
|
| |
| 149 |
83 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
83 |
83 |
|
| 150 |
|
| /* no need to extend SP because of the args of the method (sort) is >= 2 */ |
| 151 |
83 |
| PUSHs(x); |
| 152 |
83 |
| PUSHs(y); |
| 153 |
83 |
| PUTBACK; |
| 154 |
83 |
| result = tx_unmark_raw(aTHX_ tx_proccall(aTHX_ st, proc, "sort callback")); |
| 155 |
83 |
| return SvIV(result); |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| static SVCOMPARE_t |
| 159 |
15 |
| tx_prepare_compare_func(pTHX_ tx_state_t* const st, I32 const items, SV** const MARK) { |
| 160 |
|
| assert(items == 0 || items == 1); |
| 161 |
15 |
▶ | if(items == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(items == 0) { |
5 |
10 |
15 |
|
| 162 |
5 |
| return Perl_sv_cmp; |
| 163 |
|
| } |
| 164 |
|
| else { |
| 165 |
|
| dMY_CXT; |
| 166 |
10 |
| SAVEVPTR(MY_CXT.cmparg_st); |
| 167 |
10 |
| SAVESPTR(MY_CXT.cmparg_proc); |
| 168 |
|
| |
| 169 |
10 |
| MY_CXT.cmparg_st = st; |
| 170 |
10 |
| MY_CXT.cmparg_proc = *(MARK + 1); |
| 171 |
10 |
| return tx_sv_cmp; |
| 172 |
|
| } |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
15 |
| TXBM(array, sort) { |
| 176 |
15 |
| dSP; |
| 177 |
15 |
| I32 const items = SP - MARK; |
| 178 |
15 |
| AV* const av = (AV*)SvRV(*MARK); |
| 179 |
15 |
| I32 const len = av_len(av) + 1; |
| 180 |
15 |
| AV* const result = newAV(); |
| 181 |
15 |
| SV* const resultref = newRV_noinc((SV*)result); |
| 182 |
|
| SVCOMPARE_t cmpfunc; |
| 183 |
|
| I32 i; |
| 184 |
|
| |
| 185 |
15 |
| ENTER; |
| 186 |
15 |
| SAVETMPS; |
| 187 |
15 |
| sv_2mortal(resultref); |
| 188 |
|
| |
| 189 |
15 |
| cmpfunc = tx_prepare_compare_func(aTHX_ st, items, MARK); |
| 190 |
|
| |
| 191 |
15 |
| av_extend(result, len - 1); |
| 192 |
86 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
71 |
15 |
86 |
|
| 193 |
71 |
| SV** const svp = av_fetch(av, i, FALSE); |
| 194 |
71 |
▶ | (void)av_store(result, i, newSVsv(svp ? *svp : &PL_sv_undef)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)av_store(result, i, newSVsv(svp ? *svp : &PL_sv_undef)); |
71 |
0 |
71 |
|
| 195 |
|
| } |
| 196 |
15 |
| sortsv(AvARRAY(result), len, cmpfunc); |
| 197 |
|
| |
| 198 |
15 |
| sv_setsv(retval, resultref); |
| 199 |
|
| |
| 200 |
15 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
15 |
0 |
15 |
|
| 201 |
15 |
| LEAVE; |
| 202 |
15 |
| } |
| 203 |
|
| |
| 204 |
22 |
| TXBM(array, map) { |
| 205 |
22 |
| AV* const av = (AV*)SvRV(*MARK); |
| 206 |
22 |
| SV* const proc = *(++MARK); |
| 207 |
22 |
| I32 const len = av_len(av) + 1; |
| 208 |
22 |
| AV* const result = newAV(); |
| 209 |
22 |
| SV* const resultref = newRV_noinc((SV*)result); |
| 210 |
|
| I32 i; |
| 211 |
|
| |
| 212 |
22 |
| ENTER; |
| 213 |
22 |
| SAVETMPS; |
| 214 |
22 |
| sv_2mortal(resultref); |
| 215 |
22 |
| av_extend(result, len - 1); |
| 216 |
83 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
61 |
22 |
83 |
|
| 217 |
61 |
| dSP; |
| 218 |
61 |
| SV** const svp = av_fetch(av, i, FALSE); |
| 219 |
|
| SV* sv; |
| 220 |
|
| |
| 221 |
61 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
61 |
61 |
|
| 222 |
|
| /* no need to extend SP because of the args of the method is > 0 */ |
| 223 |
61 |
▶ | PUSHs(svp ? *svp : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(svp ? *svp : &PL_sv_undef); |
61 |
0 |
61 |
|
| 224 |
61 |
| PUTBACK; |
| 225 |
61 |
| sv = tx_proccall(aTHX_ st, proc, "map callback"); |
| 226 |
61 |
| (void)av_store(result, i, newSVsv(sv)); |
| 227 |
|
| } |
| 228 |
|
| /* setting retval must be here because retval is actually st->targ */ |
| 229 |
22 |
| sv_setsv(retval, resultref); |
| 230 |
22 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
22 |
0 |
22 |
|
| 231 |
22 |
| LEAVE; |
| 232 |
22 |
| } |
| 233 |
|
| |
| 234 |
10 |
| TXBM(array, reduce) { |
| 235 |
10 |
| AV* const av = (AV*)SvRV(*MARK); |
| 236 |
10 |
| SV* const proc = *(++MARK); |
| 237 |
10 |
| I32 const len = av_len(av) + 1; |
| 238 |
|
| SV** svp; |
| 239 |
|
| SV* a; |
| 240 |
|
| I32 i; |
| 241 |
|
| |
| 242 |
10 |
▶ | if(len < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(len < 2) { |
2 |
8 |
10 |
|
| 243 |
2 |
| svp = av_fetch(av, 0, FALSE); |
| 244 |
2 |
▶ | sv_setsv(retval, svp ? *svp : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| sv_setsv(retval, svp ? *svp : NULL); |
1 |
1 |
2 |
|
| 245 |
2 |
| return; |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
8 |
| ENTER; |
| 249 |
8 |
| SAVETMPS; |
| 250 |
8 |
| svp = av_fetch(av, 0, FALSE); |
| 251 |
8 |
▶ | a = svp ? *svp : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| a = svp ? *svp : &PL_sv_undef; |
8 |
0 |
8 |
|
| 252 |
|
| |
| 253 |
72 |
▶ | for(i = 1; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 1; i < len; i++) { |
64 |
8 |
72 |
|
| 254 |
64 |
| dSP; |
| 255 |
|
| SV* b; |
| 256 |
64 |
| svp = av_fetch(av, i, FALSE); |
| 257 |
64 |
▶ | b = svp ? *svp : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| b = svp ? *svp : &PL_sv_undef; |
64 |
0 |
64 |
|
| 258 |
|
| |
| 259 |
64 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
64 |
64 |
|
| 260 |
64 |
| PUSHs(a); |
| 261 |
64 |
| PUSHs(b); |
| 262 |
64 |
| PUTBACK; |
| 263 |
64 |
| a = tx_proccall(aTHX_ st, proc, "reduce callback"); |
| 264 |
|
| } |
| 265 |
|
| /* setting retval must be here because retval is actually st->targ */ |
| 266 |
8 |
| sv_setsv(retval, a); |
| 267 |
8 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
8 |
0 |
8 |
|
| 268 |
8 |
| LEAVE; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
5 |
| TXBM(array, merge) { |
| 272 |
5 |
| AV* const av = (AV*)SvRV(*MARK); |
| 273 |
5 |
| SV* const value = *(++MARK); |
| 274 |
5 |
| I32 const len = av_len(av) + 1; |
| 275 |
5 |
| AV* const result = newAV(); |
| 276 |
5 |
| SV* const resultref = newRV_noinc((SV*)result); |
| 277 |
5 |
| AV* m = NULL; |
| 278 |
|
| I32 mlen; |
| 279 |
|
| I32 i; |
| 280 |
|
| |
| 281 |
5 |
| ENTER; |
| 282 |
5 |
| SAVETMPS; |
| 283 |
5 |
| sv_2mortal(resultref); |
| 284 |
|
| |
| 285 |
5 |
▶ | if(tx_sv_is_array_ref(aTHX_ value)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(tx_sv_is_array_ref(aTHX_ value)) { |
2 |
3 |
5 |
|
| 286 |
2 |
| m = (AV*)SvRV(value); |
| 287 |
2 |
| mlen = av_len(m) + 1; |
| 288 |
|
| } |
| 289 |
|
| else { |
| 290 |
3 |
| mlen = 1; |
| 291 |
|
| } |
| 292 |
5 |
| av_extend(result, len + mlen - 1); |
| 293 |
|
| |
| 294 |
|
| /* copy */ |
| 295 |
14 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
9 |
5 |
14 |
|
| 296 |
9 |
| SV** const svp = av_fetch(av, i, FALSE); |
| 297 |
9 |
▶ | SV* const sv = svp ? *svp : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV* const sv = svp ? *svp : &PL_sv_undef; |
9 |
0 |
9 |
|
| 298 |
9 |
| (void)av_store(result, i, newSVsv(sv)); |
| 299 |
|
| } |
| 300 |
|
| /* merge */ |
| 301 |
5 |
▶ | if(m) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(m) { |
2 |
3 |
5 |
|
| 302 |
8 |
▶ | for(i = 0; i < mlen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < mlen; i++) { |
6 |
2 |
8 |
|
| 303 |
6 |
| SV** const svp = av_fetch(m, i, FALSE); |
| 304 |
6 |
▶ | SV* const sv = svp ? *svp : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| SV* const sv = svp ? *svp : &PL_sv_undef; |
6 |
0 |
6 |
|
| 305 |
6 |
| av_push(result, newSVsv(sv)); |
| 306 |
|
| } |
| 307 |
|
| } |
| 308 |
|
| else { |
| 309 |
3 |
| av_push(result, newSVsv(value)); |
| 310 |
|
| } |
| 311 |
|
| |
| 312 |
|
| /* setting retval must be here because retval is actually st->targ */ |
| 313 |
5 |
| sv_setsv(retval, resultref); |
| 314 |
5 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
5 |
0 |
5 |
|
| 315 |
5 |
| LEAVE; |
| 316 |
5 |
| } |
| 317 |
|
| |
| 318 |
|
| /* HASH */ |
| 319 |
|
| |
| 320 |
3 |
| TXBM(hash, size) { |
| 321 |
3 |
| HV* const hv = (HV*)SvRV(*MARK); |
| 322 |
3 |
| IV i = 0; |
| 323 |
3 |
| hv_iterinit(hv); |
| 324 |
6 |
▶ | while(hv_iternext(hv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(hv_iternext(hv)) { |
3 |
3 |
6 |
|
| 325 |
3 |
| i++; |
| 326 |
|
| } |
| 327 |
3 |
| sv_setiv(retval, i); |
| 328 |
3 |
| } |
| 329 |
|
| |
| 330 |
20 |
| TXBM(hash, keys) { |
| 331 |
20 |
| sv_setsv(retval, tx_keys(aTHX_ *MARK)); |
| 332 |
20 |
| } |
| 333 |
|
| |
| 334 |
6 |
| TXBM(hash, values) { |
| 335 |
6 |
| SV* const avref = tx_keys(aTHX_ *MARK); |
| 336 |
6 |
| HV* const hv = (HV*)SvRV(*MARK); |
| 337 |
6 |
| AV* const av = (AV*)SvRV(avref); |
| 338 |
6 |
| I32 const len = AvFILLp(av) + 1; |
| 339 |
|
| I32 i; |
| 340 |
|
| |
| 341 |
|
| /* replace keys with values */ |
| 342 |
|
| /* map { $hv->{$_} } @{$keys} */ |
| 343 |
14 |
▶ | for(i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < len; i++) { |
8 |
6 |
14 |
|
| 344 |
8 |
| SV* const key = AvARRAY(av)[i]; |
| 345 |
8 |
| HE* const he = hv_fetch_ent(hv, key, TRUE, 0U); |
| 346 |
8 |
| SV* const val = hv_iterval(hv, he); |
| 347 |
8 |
| SvREFCNT_dec(key); |
| 348 |
8 |
| AvARRAY(av)[i] = newSVsv(val); |
| 349 |
|
| } |
| 350 |
|
| |
| 351 |
6 |
| sv_setsv(retval, avref); |
| 352 |
6 |
| } |
| 353 |
|
| |
| 354 |
11 |
| TXBM(hash, kv) { |
| 355 |
|
| dMY_CXT; |
| 356 |
11 |
| SV* const hvref = *MARK; |
| 357 |
11 |
| HV* const hv = (HV*)SvRV(hvref); |
| 358 |
11 |
| AV* const av = newAV(); |
| 359 |
11 |
| SV* const avref = newRV_noinc((SV*)av); |
| 360 |
|
| HE* he; |
| 361 |
|
| I32 i; |
| 362 |
|
| |
| 363 |
11 |
| ENTER; |
| 364 |
11 |
| SAVETMPS; |
| 365 |
11 |
| sv_2mortal(avref); |
| 366 |
|
| |
| 367 |
11 |
▶ | if(HvKEYS(hv) > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(HvKEYS(hv) > 0) { |
1 |
10 |
11 |
| if(HvKEYS(hv) > 0) { |
9 |
2 |
11 |
|
| 368 |
9 |
▶ | av_extend(av, HvKEYS(hv) - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| av_extend(av, HvKEYS(hv) - 1); |
0 |
9 |
9 |
|
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
11 |
| hv_iterinit(hv); |
| 372 |
11 |
| i = 0; |
| 373 |
34 |
▶ | while((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while((he = hv_iternext(hv))) { |
23 |
11 |
34 |
|
| 374 |
23 |
| SV* const pair = tx_make_pair(aTHX_ MY_CXT.pair_stash, |
| 375 |
|
| hv_iterkeysv(he), |
| 376 |
|
| hv_iterval(hv, he)); |
| 377 |
|
| |
| 378 |
23 |
| (void)av_store(av, i++, pair); |
| 379 |
23 |
| SvREFCNT_inc_simple_void_NN(pair); |
| 380 |
|
| } |
| 381 |
11 |
| sortsv(AvARRAY(av), i, tx_pair_cmp); |
| 382 |
11 |
| sv_setsv(retval, avref); |
| 383 |
|
| |
| 384 |
11 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
11 |
0 |
11 |
|
| 385 |
11 |
| LEAVE; |
| 386 |
11 |
| } |
| 387 |
|
| |
| 388 |
4 |
| TXBM(hash, merge) { |
| 389 |
4 |
| sv_setsv(retval, tx_merge_hash(aTHX_ st, *MARK, *(MARK + 1))); |
| 390 |
4 |
| } |
| 391 |
|
| |
| 392 |
|
| static const tx_builtin_method_t tx_builtin_method[] = { |
| 393 |
|
| TXBM_SETUP(array, first, 0, 0), |
| 394 |
|
| TXBM_SETUP(array, last, 0, 0), |
| 395 |
|
| TXBM_SETUP(array, size, 0, 0), |
| 396 |
|
| TXBM_SETUP(array, join, 1, 1), |
| 397 |
|
| TXBM_SETUP(array, reverse, 0, 0), |
| 398 |
|
| TXBM_SETUP(array, sort, 0, 1), /* can take a compare function */ |
| 399 |
|
| TXBM_SETUP(array, map, 1, 1), |
| 400 |
|
| TXBM_SETUP(array, reduce, 1, 1), |
| 401 |
|
| TXBM_SETUP(array, merge, 1, 1), |
| 402 |
|
| |
| 403 |
|
| TXBM_SETUP(hash, size, 0, 0), |
| 404 |
|
| TXBM_SETUP(hash, keys, 0, 0), /* TODO: can take a compare function */ |
| 405 |
|
| TXBM_SETUP(hash, values, 0, 0), /* TODO: can take a compare function */ |
| 406 |
|
| TXBM_SETUP(hash, kv, 0, 0), /* TODO: can take a compare function */ |
| 407 |
|
| TXBM_SETUP(hash, merge, 1, 1), |
| 408 |
|
| }; |
| 409 |
|
| |
| 410 |
|
| static const size_t tx_num_builtin_method |
| 411 |
|
| = sizeof(tx_builtin_method) / sizeof(tx_builtin_method[0]); |
| 412 |
|
| |
| 413 |
|
| SV* |
| 414 |
281 |
| tx_methodcall(pTHX_ tx_state_t* const st, SV* const method) { |
| 415 |
|
| /* PUSHMARK must be done */ |
| 416 |
281 |
| dSP; |
| 417 |
281 |
| dMARK; |
| 418 |
281 |
| dORIGMARK; |
| 419 |
281 |
| SV* const invocant = *(++MARK); |
| 420 |
|
| const char* type_name; |
| 421 |
|
| SV* fq_name; |
| 422 |
|
| HE* he; |
| 423 |
|
| SV* retval; |
| 424 |
|
| |
| 425 |
281 |
▶ | if(sv_isobject(invocant)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(sv_isobject(invocant)) { |
41 |
240 |
281 |
|
| 426 |
41 |
▶ | PUSHMARK(ORIGMARK); /* re-pushmark */ |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(ORIGMARK); /* re-pushmark */ |
0 |
41 |
41 |
|
| 427 |
41 |
| return tx_call_sv(aTHX_ st, method, G_METHOD, "method call"); |
| 428 |
|
| } |
| 429 |
|
| |
| 430 |
240 |
| retval = NULL; |
| 431 |
240 |
▶ | if(SvROK(invocant)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvROK(invocant)) { |
213 |
27 |
240 |
|
| 432 |
213 |
| SV* const referent = SvRV(invocant); |
| 433 |
213 |
▶ | if(SvTYPE(referent) == SVt_PVAV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvTYPE(referent) == SVt_PVAV) { |
163 |
50 |
213 |
|
| 434 |
163 |
| type_name = "array::"; |
| 435 |
|
| } |
| 436 |
50 |
▶ | else if(SvTYPE(referent) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if(SvTYPE(referent) == SVt_PVHV) { |
50 |
0 |
50 |
|
| 437 |
50 |
| type_name = "hash::"; |
| 438 |
|
| } |
| 439 |
|
| else { |
| 440 |
0 |
| type_name = "scalar::"; |
| 441 |
|
| } |
| 442 |
|
| } |
| 443 |
|
| else { |
| 444 |
27 |
▶ | if(SvOK(invocant)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvOK(invocant)) { |
20 |
7 |
27 |
|
| 445 |
20 |
| type_name = "scalar::"; |
| 446 |
|
| } |
| 447 |
|
| else { |
| 448 |
7 |
| type_name = "nil::"; |
| 449 |
|
| } |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
|
| /* make type::method */ |
| 453 |
240 |
| fq_name = st->targ; |
| 454 |
240 |
| sv_setpv(fq_name, type_name); |
| 455 |
240 |
| sv_catsv(fq_name, method); |
| 456 |
|
| |
| 457 |
240 |
| he = hv_fetch_ent(st->symbol, fq_name, FALSE, 0U); |
| 458 |
240 |
▶ | if(he) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(he) { |
231 |
9 |
240 |
|
| 459 |
231 |
| SV* const entity = HeVAL(he); |
| 460 |
|
| |
| 461 |
231 |
▶ | if(SvIOK(entity)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvIOK(entity)) { |
184 |
47 |
231 |
|
| 462 |
184 |
| I32 const items = SP - MARK; |
| 463 |
|
| const tx_builtin_method_t* bm; |
| 464 |
|
| |
| 465 |
184 |
▶ | if(SvUVX(entity) >= tx_num_builtin_method) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvUVX(entity) >= tx_num_builtin_method) { |
0 |
184 |
184 |
|
| 466 |
0 |
| croak("Oops: Builtin method index of %"SVf" is out of range", |
| 467 |
|
| fq_name); |
| 468 |
|
| } |
| 469 |
|
| |
| 470 |
184 |
| bm = &tx_builtin_method[SvUVX(entity)]; |
| 471 |
|
| |
| 472 |
184 |
▶ | if(!(items >= bm->nargs_min && items <= bm->nargs_max)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if(!(items >= bm->nargs_min && items <= bm->nargs_max)) { |
184 |
0 |
184 |
| if(!(items >= bm->nargs_min && items <= bm->nargs_max)) { |
1 |
183 |
184 |
|
| 473 |
1 |
| tx_error(aTHX_ st, "Wrong number of arguments for %"SVf, |
| 474 |
|
| method); |
| 475 |
1 |
| goto finish; |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
183 |
| retval = st->targ; |
| 479 |
183 |
| bm->body(aTHX_ st, retval, method, MARK); |
| 480 |
183 |
| goto finish; |
| 481 |
|
| } |
| 482 |
|
| else { /* user defined methods */ |
| 483 |
47 |
▶ | PUSHMARK(ORIGMARK); /* re-pushmark */ |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(ORIGMARK); /* re-pushmark */ |
0 |
47 |
47 |
|
| 484 |
47 |
| return tx_proccall(aTHX_ st, entity, "method call"); |
| 485 |
|
| } |
| 486 |
|
| } |
| 487 |
9 |
▶ | if(!SvOK(invocant)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(invocant)) { |
7 |
2 |
9 |
|
| 488 |
7 |
| tx_warn(aTHX_ st, "Use of nil to invoke method %"SVf, method); |
| 489 |
7 |
| goto finish; |
| 490 |
|
| } |
| 491 |
2 |
| tx_error(aTHX_ st, "Undefined method %"SVf" called for %s", method, |
| 492 |
|
| tx_neat(aTHX_ invocant)); |
| 493 |
|
| |
| 494 |
193 |
| finish: |
| 495 |
193 |
| SP = ORIGMARK; |
| 496 |
193 |
| PUTBACK; |
| 497 |
193 |
▶ | return retval ? retval : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| return retval ? retval : &PL_sv_undef; |
183 |
10 |
193 |
|
| 498 |
|
| } |
| 499 |
|
| |
| 500 |
|
| void |
| 501 |
297 |
| tx_register_builtin_methods(pTHX_ HV* const hv) { |
| 502 |
|
| U32 i; |
| 503 |
|
| assert(hv); |
| 504 |
4455 |
▶ | for(i = 0; i < tx_num_builtin_method; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < tx_num_builtin_method; i++) { |
4158 |
297 |
4455 |
|
| 505 |
4158 |
| const tx_builtin_method_t* const bm = &tx_builtin_method[i]; |
| 506 |
4158 |
| SV* const sv = *hv_fetch(hv, bm->name, strlen(bm->name), TRUE); |
| 507 |
|
| |
| 508 |
4158 |
▶ | if(!SvOK(sv)) { /* users can override it */ |
Branch: 1 branch
| Branch | True | False | Total |
| if(!SvOK(sv)) { /* users can override it */ |
4158 |
0 |
4158 |
|
| 509 |
4158 |
| TAINT_NOT; |
| 510 |
4158 |
| sv_setiv(sv, i); |
| 511 |
|
| } |
| 512 |
|
| } |
| 513 |
297 |
| } |
| 514 |
|
| |
| 515 |
|
| MODULE = Text::Xslate::Methods PACKAGE = Text::Xslate::Type::Pair |
| 516 |
|
| |
| 517 |
|
| PROTOTYPES: DISABLE |
| 518 |
|
| VERSIONCHECK: DISABLE |
| 519 |
|
| |
| 520 |
|
| BOOT: |
| 521 |
|
| { |
| 522 |
|
| MY_CXT_INIT; |
| 523 |
188 |
| MY_CXT.pair_stash = gv_stashpvs(TX_PAIR_CLASS, GV_ADDMULTI); |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
|
| #ifdef USE_ITHREADS |
| 527 |
|
| |
| 528 |
|
| void |
| 529 |
|
| CLONE(...) |
| 530 |
|
| CODE: |
| 531 |
|
| { |
| 532 |
|
| MY_CXT_CLONE; |
| 533 |
|
| MY_CXT.pair_stash = gv_stashpvs(TX_PAIR_CLASS, GV_ADDMULTI); |
| 534 |
|
| PERL_UNUSED_VAR(items); |
| 535 |
|
| } |
| 536 |
|
| |
| 537 |
|
| #endif |
| 538 |
|
| |
| 539 |
|
| void |
| 540 |
|
| key(AV* pair) |
| 541 |
|
| ALIAS: |
| 542 |
|
| key = 0 |
| 543 |
|
| value = 1 |
| 544 |
|
| CODE: |
| 545 |
|
| { |
| 546 |
44 |
| ST(0) = *av_fetch(pair, ix, TRUE); |
| 547 |
|
| } |
| 548 |
|
| |