lib/Text/Xslate.xs

filter: Statementstmt 92.6% 759 / 819 Branchbran 71.6% 417 / 582 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.9% 1176 / 1401 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Text/Xslate/Symbol.pm ↑ Summary src/xslate_opcode.inc »
1 #define NEED_newSVpvn_flags_GLOBAL
2 #define NEED_newSVpvn_share
3 #define NEED_newSV_type
4 #include "xslate.h"
5  
6 #include "uri_unsafe.h"
7  
8 /* aliases */
9 #define TXCODE_literal_i TXCODE_literal
10 #define TXCODE_depend TXCODE_noop
11 #define TXCODE_macro_begin TXCODE_noop
12 #define TXCODE_macro_nargs TXCODE_noop
13 #define TXCODE_macro_outer TXCODE_noop
14 #define TXCODE_set_opinfo TXCODE_noop
15 #define TXCODE_super TXCODE_noop
16  
17 #include "xslate_ops.h"
18  
19 static bool dump_load = FALSE;
20  
21 #ifdef DEBUGGING
22 #define TX_st_sa *tx_sv_safe(aTHX_ &(TX_st->sa), "TX_st->sa", __FILE__, __LINE__)
23 #define TX_st_sb *tx_sv_safe(aTHX_ &(TX_st->sb), "TX_st->sb", __FILE__, __LINE__)
24 static SV**
25 tx_sv_safe(pTHX_ SV** const svp, const char* const name, const char* const f, int const l) {
26 if(*svp == NULL) {
27 croak("[BUG] %s is NULL at %s line %d.\n", name, f, l);
28 }
29 return svp;
30 }
31  
32 #define TX_lvarx_get(st, ix) tx_lvar_get_safe(aTHX_ st, ix)
33  
34 static SV*
35 tx_lvar_get_safe(pTHX_ tx_state_t* const st, I32 const lvar_ix) {
36 AV* const cframe = TX_current_framex(st);
37 I32 const real_ix = lvar_ix + TXframe_START_LVAR;
38  
39 assert(SvTYPE(cframe) == SVt_PVAV);
40 if(AvFILLp(cframe) < real_ix) {
41 croak("[BUG] Refers to unallocated local variable %d (> %d)",
42 (int)lvar_ix, (int)(AvFILLp(cframe) - TXframe_START_LVAR));
43 }
44  
45 if(!st->pad) {
46 croak("[BUG] Refers to local variable %d before initialization",
47 (int)lvar_ix);
48 }
49 return st->pad[lvar_ix];
50 }
51  
52  
53 #else /* DEBUGGING */
54 #define TX_st_sa (TX_st->sa)
55 #define TX_st_sb (TX_st->sb)
56  
57 #define TX_lvarx_get(st, ix) ((st)->pad[ix])
58 #endif /* DEBUGGING */
59  
60 #define TX_op_arg (TX_op->u_arg)
61 #define TX_op_arg_sv (TX_op_arg.sv)
62 #define TX_op_arg_iv (TX_op_arg.iv)
63 #define TX_op_arg_pc (TX_op_arg.pc)
64  
65 #define TX_lvarx(st, ix) tx_load_lvar(aTHX_ st, ix)
66  
67 #define TX_lvar(ix) TX_lvarx(TX_st, ix) /* init if uninitialized */
68 #define TX_lvar_get(ix) TX_lvarx_get(TX_st, ix)
69  
70 #define TX_ckuuv_lhs(x) tx_sv_check_uuv(aTHX_ (x), "lhs")
71 #define TX_ckuuv_rhs(x) tx_sv_check_uuv(aTHX_ (x), "rhs")
72  
73 #define TX_UNMARK_RAW(sv) SvRV(sv)
74  
75 #define MY_CXT_KEY "Text::Xslate::_guts" XS_VERSION
76 typedef struct {
77 I32 depth;
78 HV* raw_stash;
79 HV* macro_stash;
80  
81 tx_state_t* current_st; /* set while tx_execute(), othewise NULL */
82  
83 /* those handlers are just \&_warn and \&_die,
84 but stored here for performance */
85 SV* warn_handler;
86 SV* die_handler;
87  
88 /* original error handlers */
89 SV* orig_warn_handler;
90 SV* orig_die_handler;
91 SV* make_error;
92 } my_cxt_t;
93 START_MY_CXT
94  
95 static void
96 594 tx_sv_clear(pTHX_ SV* const sv) {
97 594 sv_unmagic(sv, PERL_MAGIC_taint);
98 594 sv_setsv(sv, NULL);
99 594 }
100  
101 const char*
102 tx_neat(pTHX_ SV* const sv);
103  
104 static SV*
105 tx_load_lvar(pTHX_ tx_state_t* const st, I32 const lvar_ix);
106  
107 static AV*
108 tx_push_frame(pTHX_ tx_state_t* const st);
109  
110 static void
111 tx_pop_frame(pTHX_ tx_state_t* const st, bool const replace_output);
112  
113 static SV*
114 tx_funcall(pTHX_ tx_state_t* const st, SV* const func, const char* const name);
115  
116 static SV*
117 tx_fetch(pTHX_ tx_state_t* const st, SV* const var, SV* const key);
118  
119 static SV*
120 tx_sv_to_ref(pTHX_ SV* const sv, svtype const svt, int const amg_id);
121  
122 int
123 10 tx_sv_is_array_ref(pTHX_ SV* const sv) {
124 assert(sv);
125 10 ▶ return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && !SvOBJECT(SvRV(sv));
Branch: 3 branches
BranchTrueFalseTotal
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && !SvOBJECT(SvRV(sv)); 6 4 10
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && !SvOBJECT(SvRV(sv)); 5 1 6
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV && !SvOBJECT(SvRV(sv)); 4 1 5
126 }
127  
128 int
129 15 tx_sv_is_hash_ref(pTHX_ SV* const sv) {
130 assert(sv);
131 15 ▶ return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV && !SvOBJECT(SvRV(sv));
Branch: 3 branches
BranchTrueFalseTotal
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV && !SvOBJECT(SvRV(sv)); 14 1 15
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV && !SvOBJECT(SvRV(sv)); 12 2 14
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV && !SvOBJECT(SvRV(sv)); 12 0 12
132 }
133  
134 int
135 81 tx_sv_is_code_ref(pTHX_ SV* const sv) {
136 assert(sv);
137 81 ▶ return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV && !SvOBJECT(SvRV(sv));
Branch: 3 branches
BranchTrueFalseTotal
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV && !SvOBJECT(SvRV(sv)); 80 1 81
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV && !SvOBJECT(SvRV(sv)); 80 0 80
return SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV && !SvOBJECT(SvRV(sv)); 80 0 80
138 }
139  
140 SV*
141 10 tx_merge_hash(pTHX_ tx_state_t* const st, SV* base, SV* value) {
142 10 HV* const hv = (HV*)SvRV(base);
143 10 HV* const result = newHVhv(hv);
144 10 SV* const resultref = newRV_noinc((SV*)result);
145 HE* he;
146 HV* m;
147 10 sv_2mortal((SV*)resultref);
148  
149 10 ▶ SvGETMAGIC(base);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(base); 0 10 10
SvGETMAGIC(base); 0 0 0
150 10 ▶ SvGETMAGIC(value);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(value); 0 10 10
SvGETMAGIC(value); 0 0 0
151  
152 10 ▶ if(!tx_sv_is_hash_ref(aTHX_ value)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!tx_sv_is_hash_ref(aTHX_ value)) { 0 10 10
153 0 ▶ if (st) {
Branch: 1 branch
BranchTrueFalseTotal
if (st) { 0 0 0
154 0 tx_error(aTHX_ st, "Merging value is not a HASH reference");
155 }
156 else {
157 0 Perl_croak(aTHX_ "Merging value is not a HASH reference");
158 }
159 0 return resultref;
160 }
161  
162 10 m = (HV*)SvRV(value);
163  
164 10 hv_iterinit(m);
165 27 ▶ while((he = hv_iternext(m))) {
Branch: 1 branch
BranchTrueFalseTotal
while((he = hv_iternext(m))) { 17 10 27
166 17 (void)hv_store_ent(result,
167 hv_iterkeysv(he),
168 newSVsv(hv_iterval(hv, he)),
169 0U);
170 }
171  
172 10 return resultref;
173 }
174  
175 STATIC_INLINE bool
176 tx_str_is_raw(pTHX_ pMY_CXT_ SV* const sv); /* doesn't handle magics */
177  
178 STATIC_INLINE void
179 tx_sv_cat(pTHX_ SV* const dest, SV* const src);
180  
181 static void
182 tx_sv_cat_with_html_escape_force(pTHX_ SV* const dest, SV* const src);
183  
184 STATIC_INLINE void
185 tx_print(pTHX_ tx_state_t* const st, SV* const sv);
186  
187 static SV*
188 tx_html_escape(pTHX_ SV* const str);
189  
190 static SV*
191 tx_uri_escape(pTHX_ SV* const src);
192  
193 STATIC_INLINE I32
194 tx_sv_eq(pTHX_ SV* const a, SV* const b);
195  
196 static SV*
197 tx_sv_check_uuv(pTHX_ SV* const sv, const char* const name);
198  
199 static I32
200 tx_sv_match(pTHX_ SV* const a, SV* const b);
201  
202 static bool
203 tx_sv_is_macro(pTHX_ SV* const sv);
204  
205 static void
206 tx_macro_enter(pTHX_ tx_state_t* const txst, AV* const macro, tx_pc_t const retaddr);
207  
208 static void
209 tx_execute(pTHX_ pMY_CXT_ tx_state_t* const base, SV* const output, HV* const hv);
210  
211 static tx_state_t*
212 tx_load_template(pTHX_ SV* const self, SV* const name, bool const from_include);
213  
214 #ifndef save_op
215 #define save_op() my_save_op(aTHX)
216 static void
217 my_save_op(pTHX) { /* copied from scope.c */
218 SSCHECK(2);
219 SSPUSHPTR(PL_op);
220 SSPUSHINT(SAVEt_OP);
221 }
222 #endif
223  
224 #include "xslate_opcode.inc"
225  
226 const char*
227 24 tx_neat(pTHX_ SV* const sv) {
228 24 ▶ if(SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(sv)) { 22 2 24
229 22 ▶ if(SvROK(sv) || looks_like_number(sv) || isGV(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if(SvROK(sv) || looks_like_number(sv) || isGV(sv)) { 19 3 22
if(SvROK(sv) || looks_like_number(sv) || isGV(sv)) { 14 5 19
if(SvROK(sv) || looks_like_number(sv) || isGV(sv)) { 0 14 14
230 8 return form("%"SVf, sv);
231 }
232 else {
233 14 return form("'%"SVf"'", sv);
234 }
235 }
236 2 return "nil";
237 }
238  
239 static IV
240 210 tx_verbose(pTHX_ tx_state_t* const st) {
241 210 HV* const hv = (HV*)SvRV(st->engine);
242 210 SV* const sv = *hv_fetchs(hv, "verbose", TRUE);
243 210 return SvIV(sv);
244 }
245  
246  
247 static void
248 71 tx_call_error_handler(pTHX_ SV* const handler, SV* const msg) {
249 71 dSP;
250 71 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 71 71
251 71 ▶ XPUSHs(msg);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(msg); 0 71 71
252 71 PUTBACK;
253 71 call_sv(handler, G_VOID | G_DISCARD);
254 56 }
255  
256 /* for trivial errors, ignored by default */
257 void
258 82 tx_warn(pTHX_ tx_state_t* const st, const char* const fmt, ...) {
259 assert(st);
260 assert(fmt);
261 82 ▶ if(tx_verbose(aTHX_ st) > TX_VERBOSE_DEFAULT) { /* stronger than the default */
Branch: 1 branch
BranchTrueFalseTotal
if(tx_verbose(aTHX_ st) > TX_VERBOSE_DEFAULT) { /* stronger than the default */ 40 42 82
262 dMY_CXT;
263 SV* msg;
264 va_list args;
265 40 va_start(args, fmt);
266  
267 40 ENTER;
268 40 SAVETMPS;
269 40 msg = sv_2mortal( vnewSVpvf(fmt, &args) );
270 40 tx_call_error_handler(aTHX_ MY_CXT.warn_handler, msg);
271 38 va_end(args);
272 38 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 38 0 38
273 38 LEAVE;
274 }
275 80 }
276  
277 /* for severe errors, warned by default */
278 void
279 28 tx_error(pTHX_ tx_state_t* const st, const char* const fmt, ...) {
280 assert(st);
281 assert(fmt);
282 28 ▶ if(tx_verbose(aTHX_ st) >= TX_VERBOSE_DEFAULT) { /* equal or stronger than the default */
Branch: 1 branch
BranchTrueFalseTotal
if(tx_verbose(aTHX_ st) >= TX_VERBOSE_DEFAULT) { /* equal or stronger than the default */ 22 6 28
283 dMY_CXT;
284 SV* msg;
285 va_list args;
286 22 va_start(args, fmt);
287 22 msg = sv_2mortal( vnewSVpvf(fmt, &args) );
288 22 tx_call_error_handler(aTHX_ MY_CXT.warn_handler, msg);
289 /* not reached */
290 18 va_end(args);
291 }
292 24 }
293  
294 static SV* /* allocate and load a lexcal variable */
295 2171 tx_load_lvar(pTHX_ tx_state_t* const st, I32 const lvar_ix) { /* the guts of TX_lvar() */
296 2171 AV* const cframe = TX_current_framex(st);
297 2171 I32 const real_ix = lvar_ix + TXframe_START_LVAR;
298  
299 assert(SvTYPE(cframe) == SVt_PVAV);
300  
301 2171 ▶ if(AvFILLp(cframe) < real_ix
Branch: 1 branch
BranchTrueFalseTotal
if(AvFILLp(cframe) < real_ix 239 1932 2171
302 239 ▶ || AvARRAY(cframe)[real_ix] == NULL
Branch: 1 branch
BranchTrueFalseTotal
|| AvARRAY(cframe)[real_ix] == NULL 238 1 239
303 238 ▶ || SvREADONLY(AvARRAY(cframe)[real_ix])) {
Branch: 1 branch
BranchTrueFalseTotal
|| SvREADONLY(AvARRAY(cframe)[real_ix])) { 0 238 238
304 1933 av_store(cframe, real_ix, newSV(0));
305 }
306 2171 st->pad = AvARRAY(cframe) + TXframe_START_LVAR;
307  
308 2171 return TX_lvarx_get(st, lvar_ix);
309 }
310  
311 static AV*
312 4998 tx_push_frame(pTHX_ tx_state_t* const st) {
313 AV* newframe;
314  
315 4998 ▶ if(st->current_frame > TX_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if(st->current_frame > TX_MAX_DEPTH) { 2 4996 4998
316 2 croak("Macro call is too deep (> %d)", TX_MAX_DEPTH);
317 }
318 4996 st->current_frame++;
319  
320 4996 newframe = (AV*)*av_fetch(st->frames, st->current_frame, TRUE);
321  
322 4996 ▶ (void)SvUPGRADE((SV*)newframe, SVt_PVAV);
Branch: 1 branch
BranchTrueFalseTotal
(void)SvUPGRADE((SV*)newframe, SVt_PVAV); 3878 1118 4996
323 // In perl 5.8, SvUPGRADE(newframe, SVt_PVAV) does not make newframe AvREAL_only.
324 // Then av_fill() in tx_pop_frame() does not frees deleted elements, and it implies memory leaks.
325 // So we need to set AvREAL_only (= AvREIFY_off + AvREAL_on) manually.
326 // Note that if we drop support for perl 5.8, then we can safely delete these lines.
327 4996 AvREIFY_off(newframe);
328 4996 AvREAL_on(newframe);
329  
330 4996 ▶ if(AvFILLp(newframe) < TXframe_START_LVAR) {
Branch: 1 branch
BranchTrueFalseTotal
if(AvFILLp(newframe) < TXframe_START_LVAR) { 4996 0 4996
331 4996 av_extend(newframe, TXframe_START_LVAR);
332 }
333 /* switch the pad */
334 4996 st->pad = AvARRAY(newframe) + TXframe_START_LVAR;
335 4996 return newframe;
336 }
337  
338 static void
339 4992 tx_pop_frame(pTHX_ tx_state_t* const st, bool const replace_output) {
340 4992 AV* const top = TX_frame_at(st, st->current_frame);
341  
342 4992 av_fill(top, TXframe_START_LVAR - 1);
343  
344 assert( st->current_frame >= 0 );
345 4992 ▶ if (--st->current_frame >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--st->current_frame >= 0) { 1437 3555 4992
346 /* switch the pad */
347 1437 st->pad = AvARRAY(TX_frame_at(st, st->current_frame))
348 1437 + TXframe_START_LVAR;
349 }
350  
351 4992 ▶ if(replace_output) {
Branch: 1 branch
BranchTrueFalseTotal
if(replace_output) { 1432 3560 4992
352 1432 SV** const ary = AvARRAY(top);
353 1432 SV* const tmp = ary[TXframe_OUTPUT];
354 1432 ary[TXframe_OUTPUT] = st->output;
355 1432 st->output = tmp;
356 }
357 4992 }
358  
359 SV* /* thin wrapper of Perl_call_sv() */
360 1021 tx_call_sv(pTHX_ tx_state_t* const st, SV* const sv, I32 const flags, const char* const name) {
361 SV* retval;
362 1021 call_sv(sv, G_SCALAR | G_EVAL | flags);
363 1021 retval = TX_pop();
364 1021 ▶ if(TX_CATCH_ERROR()) {
Branch: 2 branches
BranchTrueFalseTotal
if(TX_CATCH_ERROR()) { 1021 0 1021
if(TX_CATCH_ERROR()) { 16 1005 1021
365 16 tx_error(aTHX_ st, "%"SVf "\n"
366 16 ▶ "\t... exception caught on %s", ERRSV, name);
Branch: 1 branch
BranchTrueFalseTotal
"\t... exception caught on %s", ERRSV, name); 16 0 16
367 }
368 1018 return retval;
369 }
370  
371 static SV*
372 283 tx_funcall(pTHX_ tx_state_t* const st, SV* const func, const char* const name) {
373 HV* dummy_stash;
374 GV* dummy_gv;
375 CV* cv;
376 SV* retval;
377 283 ▶ SvGETMAGIC(func);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(func); 0 283 283
SvGETMAGIC(func); 0 0 0
378  
379 283 ▶ if(UNLIKELY(!SvOK(func))) {
Branch: 1 branch
BranchTrueFalseTotal
if(UNLIKELY(!SvOK(func))) { 2 281 283
380 2 dTX_optable;
381 2 tx_code_t* const c = st->pc - 1;
382 2 (void)POPMARK;
383 2 tx_error(aTHX_ st, "Undefined function%s is called on %s",
384 2 ▶ c->exec_code == tx_optable[TXOP_fetch_s]
Branch: 1 branch
BranchTrueFalseTotal
c->exec_code == tx_optable[TXOP_fetch_s] 2 0 2
385 2 ? form(" %"SVf"()", c->u_arg.sv)
386 : "", name);
387 1 retval = NULL;
388 1 goto finish;
389 }
390  
391 281 cv = sv_2cv(func, &dummy_stash, &dummy_gv, FALSE);
392  
393 281 ▶ if(UNLIKELY(!cv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(UNLIKELY(!cv)) { 0 281 281
394 0 (void)POPMARK;
395 0 tx_error(aTHX_ st, "Functions must be a CODE reference, not %s",
396 tx_neat(aTHX_ func));
397 0 retval = NULL;
398 0 goto finish;
399 }
400  
401 281 retval = tx_call_sv(aTHX_ st, (SV*)cv, 0, "function call");
402  
403 280 finish:
404 280 sv_setsv_nomg(st->targ, retval);
405  
406 280 return st->targ;
407 }
408  
409 static SV*
410 1165 tx_fetch(pTHX_ tx_state_t* const st, SV* const var, SV* const key) {
411 SV* retval;
412  
413 1165 ▶ SvGETMAGIC(var);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(var); 0 1165 1165
SvGETMAGIC(var); 0 0 0
414 1165 ▶ if(SvROK(var) && SvOBJECT(SvRV(var))) {
Branch: 2 branches
BranchTrueFalseTotal
if(SvROK(var) && SvOBJECT(SvRV(var))) { 1162 3 1165
if(SvROK(var) && SvOBJECT(SvRV(var))) { 699 463 1162
415 699 dSP;
416 699 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 699 699
417 699 ▶ XPUSHs(var);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(var); 0 699 699
418 699 PUTBACK;
419  
420 699 return tx_call_sv(aTHX_ st, key, G_METHOD, "accessor");
421 }
422  
423 466 retval = NULL;
424 466 ▶ if(SvROK(var)){
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(var)){ 463 3 466
425 463 SV* const rv = SvRV(var);
426 463 ▶ SvGETMAGIC(key);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(key); 0 463 463
SvGETMAGIC(key); 0 0 0
427 463 ▶ if(SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE(rv) == SVt_PVHV) { 392 71 463
428 392 ▶ if(SvOK(key)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(key)) { 390 2 392
429 390 HE* const he = hv_fetch_ent((HV*)rv, key, FALSE, 0U);
430 390 ▶ if(he) {
Branch: 1 branch
BranchTrueFalseTotal
if(he) { 379 11 390
431 379 retval = hv_iterval((HV*)rv, he);
432 }
433 }
434 else {
435 2 tx_warn(aTHX_ st, "Use of nil as a field key");
436 }
437 }
438 71 ▶ else if(SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
else if(SvTYPE(rv) == SVt_PVAV) { 71 0 71
439 71 ▶ if(LooksLikeNumber(key)) {
Branch: 1 branch
BranchTrueFalseTotal
if(LooksLikeNumber(key)) { 69 2 71
440 69 SV** const svp = av_fetch((AV*)rv, SvIV(key), FALSE);
441 69 ▶ if(svp) {
Branch: 1 branch
BranchTrueFalseTotal
if(svp) { 65 4 69
442 65 retval = *svp;
443 }
444 }
445 else {
446 2 tx_warn(aTHX_ st, "Use of %s as an array index",
447 tx_neat(aTHX_ key));
448 }
449 }
450 else {
451 0 goto invalid_container;
452 }
453 }
454 3 ▶ else if(SvOK(var)){ /* string, number, etc. */
Branch: 1 branch
BranchTrueFalseTotal
else if(SvOK(var)){ /* string, number, etc. */ 1 2 3
455 1 invalid_container:
456 1 tx_error(aTHX_ st, "Cannot access %s (%s is not a container)",
457 tx_neat(aTHX_ key), tx_neat(aTHX_ var));
458 }
459 else { /* undef */
460 2 tx_warn(aTHX_ st, "Use of nil to access %s", tx_neat(aTHX_ key));
461 }
462 466 TAINT_NOT;
463  
464 466 ▶ return retval ? retval : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return retval ? retval : &PL_sv_undef; 444 22 466
465 }
466  
467 #ifndef amagic_deref_call
468 #define amagic_deref_call(ref, method) my_amagic_deref_call(aTHX_ ref, method)
469 /* portability */
470 static SV*
471 my_amagic_deref_call(pTHX_ SV* ref, const int method) {
472 SV* tmpsv = NULL;
473  
474 while (SvAMAGIC(ref) &&
475 (tmpsv = amagic_call(ref, &PL_sv_undef, method,
476 AMGf_noright | AMGf_unary))) {
477 if (!SvROK(tmpsv))
478 Perl_croak(aTHX_ "Overloaded dereference did not return a reference");
479 if (tmpsv == ref || SvRV(tmpsv) == SvRV(ref)) {
480 /* Bail out if it returns us the same reference. */
481 return tmpsv;
482 }
483 ref = tmpsv;
484 }
485 return tmpsv ? tmpsv : ref;
486 }
487 #endif
488  
489 static SV*
490 202 tx_sv_to_ref(pTHX_ SV* const sv, svtype const svt, const int amg_id) {
491 202 ▶ if(SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(sv)) { 193 9 202
492 193 SV* const r = SvRV(sv);
493 193 ▶ if(SvOBJECT(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOBJECT(r)) { 4 189 193
494 4 ▶ if(SvAMAGIC(sv)) {
Branch: 3 branches
BranchTrueFalseTotal
if(SvAMAGIC(sv)) { 4 0 4
if(SvAMAGIC(sv)) { 4 0 4
if(SvAMAGIC(sv)) { 4 0 4
495 4 SV* const tmpsv = amagic_deref_call(sv, amg_id);
496 3 ▶ if(SvROK(tmpsv)
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(tmpsv) 3 0 3
497 3 ▶ && SvTYPE(SvRV(tmpsv)) == svt
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(tmpsv)) == svt 2 1 3
498 2 ▶ && !SvOBJECT(SvRV(tmpsv))) {
Branch: 1 branch
BranchTrueFalseTotal
&& !SvOBJECT(SvRV(tmpsv))) { 2 0 2
499 2 return tmpsv;
500 }
501 }
502 }
503 189 ▶ else if(SvTYPE(r) == svt) {
Branch: 1 branch
BranchTrueFalseTotal
else if(SvTYPE(r) == svt) { 189 0 189
504 189 return sv;
505 }
506 }
507 10 return NULL;
508 }
509  
510 STATIC_INLINE bool
511 6708 tx_str_is_raw(pTHX_ pMY_CXT_ SV* const sv) {
512 6708 ▶ if(SvROK(sv) && SvOBJECT(SvRV(sv))) {
Branch: 2 branches
BranchTrueFalseTotal
if(SvROK(sv) && SvOBJECT(SvRV(sv))) { 1370 5338 6708
if(SvROK(sv) && SvOBJECT(SvRV(sv))) { 1366 4 1370
513 1366 return SvTYPE(SvRV(sv)) <= SVt_PVMG
514 1366 ▶ && SvSTASH(SvRV(sv)) == MY_CXT.raw_stash;
Branch: 2 branches
BranchTrueFalseTotal
&& SvSTASH(SvRV(sv)) == MY_CXT.raw_stash; 1363 3 1366
&& SvSTASH(SvRV(sv)) == MY_CXT.raw_stash; 1363 0 1363
515 }
516 5342 return FALSE;
517 }
518  
519 SV*
520 1342 tx_mark_raw(pTHX_ SV* const str) {
521 dMY_CXT;
522 1342 ▶ SvGETMAGIC(str);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(str); 0 1342 1342
SvGETMAGIC(str); 0 0 0
523 1342 ▶ if(!SvOK(str)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(str)) { 2 1340 1342
524 2 return str;
525 }
526 1340 ▶ else if(tx_str_is_raw(aTHX_ aMY_CXT_ str)) {
Branch: 1 branch
BranchTrueFalseTotal
else if(tx_str_is_raw(aTHX_ aMY_CXT_ str)) { 2 1338 1340
527 2 return str;
528 }
529 else {
530 1338 SV* const sv = newSV_type(SVt_PVMG);
531 1338 sv_setsv(sv, str);
532 1338 return sv_2mortal(sv_bless(newRV_noinc(sv), MY_CXT.raw_stash));
533 }
534 }
535  
536 SV*
537 1038 tx_unmark_raw(pTHX_ SV* const str) {
538 dMY_CXT;
539 1038 ▶ SvGETMAGIC(str);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(str); 0 1038 1038
SvGETMAGIC(str); 0 0 0
540 1038 ▶ if(tx_str_is_raw(aTHX_ aMY_CXT_ str)) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_str_is_raw(aTHX_ aMY_CXT_ str)) { 844 194 1038
541 844 return TX_UNMARK_RAW(str);
542 }
543 else {
544 194 return str;
545 }
546 }
547  
548 /* does sv_catsv_nomg(dest, src), but significantly faster */
549 STATIC_INLINE void
550 8183 tx_sv_cat(pTHX_ SV* const dest, SV* const src) {
551 STRLEN len;
552 8183 const char* pv = SvPV_const(src, len);
553  
554 8183 ▶ if(!SvUTF8(dest) && SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!SvUTF8(dest) && SvUTF8(src)) { 5724 2459 8183
if(!SvUTF8(dest) && SvUTF8(src)) { 889 4835 5724
555 889 sv_utf8_upgrade(dest);
556 }
557  
558 8183 ▶ if(SvUTF8(dest) == SvUTF8(src)
Branch: 1 branch
BranchTrueFalseTotal
if(SvUTF8(dest) == SvUTF8(src) 199 7984 8183
559 199 ▶ || is_utf8_string((const U8 *)pv, len)) {
Branch: 1 branch
BranchTrueFalseTotal
|| is_utf8_string((const U8 *)pv, len)) { 198 1 199
560 8182 STRLEN const dest_cur = SvCUR(dest);
561 8182 ▶ char* const d = SvGROW(dest, dest_cur + len + 1 /* count '\0' */);
Branch: 2 branches
BranchTrueFalseTotal
char* const d = SvGROW(dest, dest_cur + len + 1 /* count '\0' */); 8182 0 8182
char* const d = SvGROW(dest, dest_cur + len + 1 /* count '\0' */); 26 8156 8182
562  
563 8182 SvCUR_set(dest, dest_cur + len);
564 8182 Copy(pv, d + dest_cur, len + 1 /* copy '\0' */, char);
565 }
566 else {
567 1 STRLEN const dest_cur = SvCUR(dest);
568 /* Longest UTF-8 representation of each char is 2 octets. */
569 1 ▶ char* const d_start = SvGROW(dest, dest_cur + 2 * len + 1 /* count '\0' */);
Branch: 2 branches
BranchTrueFalseTotal
char* const d_start = SvGROW(dest, dest_cur + 2 * len + 1 /* count '\0' */); 1 0 1
char* const d_start = SvGROW(dest, dest_cur + 2 * len + 1 /* count '\0' */); 0 1 1
570 1 char* d = d_start + dest_cur;
571  
572 2 ▶ while(len--) {
Branch: 1 branch
BranchTrueFalseTotal
while(len--) { 1 1 2
573 1 const U8 c = *pv++;
574 1 ▶ if (UTF8_IS_INVARIANT(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (UTF8_IS_INVARIANT(c)) { 0 1 1
575 0 *(d++) = c;
576 } else {
577 1 *(d++) = UTF8_EIGHT_BIT_HI(c);
578 1 *(d++) = UTF8_EIGHT_BIT_LO(c);
579 }
580 }
581 1 *d = '\0';
582 1 SvCUR_set(dest, d - d_start);
583 }
584 8183 }
585  
586 static void /* doesn't care about raw-ness */
587 3608 tx_sv_cat_with_html_escape_force(pTHX_ SV* const dest, SV* const src) {
588 STRLEN len;
589 3608 const char* cur = SvPV_const(src, len);
590 3608 const char* const end = cur + len;
591 3608 STRLEN const dest_cur = SvCUR(dest);
592 char* d;
593 910 ▶ const U32 upgrade_on_copy = SvUTF8(dest) && !SvUTF8(src)
Branch: 1 branch
BranchTrueFalseTotal
const U32 upgrade_on_copy = SvUTF8(dest) && !SvUTF8(src) 857 53 910
594 4518 ▶ && !is_utf8_string((const U8 *)cur, len);
Branch: 2 branches
BranchTrueFalseTotal
&& !is_utf8_string((const U8 *)cur, len); 910 2698 3608
&& !is_utf8_string((const U8 *)cur, len); 1 856 857
595  
596 3608 ▶ (void)SvGROW(dest, dest_cur + ( len * ( sizeof("&quot;") - 1) ) + 1);
Branch: 2 branches
BranchTrueFalseTotal
(void)SvGROW(dest, dest_cur + ( len * ( sizeof("&quot;") - 1) ) + 1); 3608 0 3608
(void)SvGROW(dest, dest_cur + ( len * ( sizeof("&quot;") - 1) ) + 1); 39 3569 3608
597 3608 ▶ if(!SvUTF8(dest) && SvUTF8(src)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!SvUTF8(dest) && SvUTF8(src)) { 2698 910 3608
if(!SvUTF8(dest) && SvUTF8(src)) { 20 2678 2698
598 20 sv_utf8_upgrade(dest);
599 }
600  
601 3608 d = SvPVX(dest) + dest_cur;
602  
603 #define CopyToken(token, to) STMT_START { \
604 Copy(token "", to, sizeof(token)-1, char); \
605 to += sizeof(token)-1; \
606 } STMT_END
607  
608 17982 ▶ while(cur != end) {
Branch: 1 branch
BranchTrueFalseTotal
while(cur != end) { 14374 3608 17982
609 14374 const char c = *(cur++);
610 14374 ▶ if(c == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if(c == '&') { 5 14369 14374
611 5 CopyToken("&amp;", d);
612 }
613 14369 ▶ else if(c == '<') {
Branch: 1 branch
BranchTrueFalseTotal
else if(c == '<') { 93 14276 14369
614 93 CopyToken("&lt;", d);
615 }
616 14276 ▶ else if(c == '>') {
Branch: 1 branch
BranchTrueFalseTotal
else if(c == '>') { 100 14176 14276
617 100 CopyToken("&gt;", d);
618 }
619 14176 ▶ else if(c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
else if(c == '"') { 9 14167 14176
620 9 CopyToken("&quot;", d);
621 }
622 14167 ▶ else if(c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
else if(c == '\'') { 25 14142 14167
623 // XXX: Internet Explorer (at least version 8) doesn't support &apos; in title
624 // CopyToken("&apos;", d);
625 25 CopyToken("&#39;", d);
626 }
627 14142 ▶ else if (upgrade_on_copy && !UTF8_IS_INVARIANT(c)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (upgrade_on_copy && !UTF8_IS_INVARIANT(c)) { 1 14141 14142
else if (upgrade_on_copy && !UTF8_IS_INVARIANT(c)) { 1 0 1
628 1 *(d++) = UTF8_EIGHT_BIT_HI((U8) c);
629 1 *(d++) = UTF8_EIGHT_BIT_LO((U8) c);
630 }
631 else {
632 14141 *(d++) = c;
633 }
634 }
635  
636 #undef CopyToken
637  
638 3608 SvCUR_set(dest, d - SvPVX(dest));
639 3608 *SvEND(dest) = '\0';
640 3608 }
641  
642 STATIC_INLINE void
643 4130 tx_print(pTHX_ tx_state_t* const st, SV* const sv) {
644 dMY_CXT;
645 4130 SV* const out = st->output;
646  
647 4130 ▶ SvGETMAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(sv); 19 4111 4130
SvGETMAGIC(sv); 0 19 19
648 4130 ▶ if(tx_str_is_raw(aTHX_ aMY_CXT_ sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_str_is_raw(aTHX_ aMY_CXT_ sv)) { 490 3640 4130
649 490 SV* const arg = TX_UNMARK_RAW(sv);
650 490 ▶ if(SvOK(arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(arg)) { 490 0 490
651 490 tx_sv_cat(aTHX_ out, arg);
652 }
653 else {
654 0 tx_warn(aTHX_ st, "Use of nil to print");
655 }
656 }
657 3640 ▶ else if(SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
else if(SvOK(sv)) { 3577 63 3640
658 3577 tx_sv_cat_with_html_escape_force(aTHX_ out, sv);
659 }
660 else {
661 63 tx_warn(aTHX_ st, "Use of nil to print");
662 /* does nothing */
663 }
664 4129 }
665  
666 static SV*
667 24 tx_html_escape(pTHX_ SV* const str) {
668 dMY_CXT;
669 24 ▶ SvGETMAGIC(str);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(str); 1 23 24
SvGETMAGIC(str); 0 1 1
670 24 ▶ if(!( !SvOK(str) || tx_str_is_raw(aTHX_ aMY_CXT_ str) )) {
Branch: 2 branches
BranchTrueFalseTotal
if(!( !SvOK(str) || tx_str_is_raw(aTHX_ aMY_CXT_ str) )) { 22 2 24
if(!( !SvOK(str) || tx_str_is_raw(aTHX_ aMY_CXT_ str) )) { 19 3 22
671 19 SV* const dest = newSVpvs_flags("", SVs_TEMP);
672 19 tx_sv_cat_with_html_escape_force(aTHX_ dest, str);
673 19 return tx_mark_raw(aTHX_ dest);
674 }
675 else {
676 5 return str;
677 }
678 }
679  
680  
681 static SV*
682 2150 tx_uri_escape(pTHX_ SV* const src) {
683 /* TODO:
684 Currently it is encoded to UTF-8, but
685 the output encoding can be specified in a future (?).
686 */
687  
688 2150 ▶ SvGETMAGIC(src);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(src); 1 2149 2150
SvGETMAGIC(src); 0 1 1
689 2150 ▶ if(SvOK(src)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(src)) { 2148 2 2150
690 STRLEN len;
691 2148 const char* pv = SvPV_const(src, len);
692 2148 const char* const end = pv + len;
693 2148 SV* const dest = sv_newmortal();
694 2148 sv_grow(dest, len * 2); /* just a hint; upgrading Svt_PV */
695 2148 SvPOK_on(dest);
696  
697 36324 ▶ while(pv != end) {
Branch: 1 branch
BranchTrueFalseTotal
while(pv != end) { 34176 2148 36324
698 34176 ▶ if(is_uri_unsafe(*pv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(is_uri_unsafe(*pv)) { 4758 29418 34176
699 /* identical to PL_hexdigit + 16 */
700 static const char hexdigit[] = "0123456789ABCDEF";
701 char p[3];
702 4758 p[0] = '%';
703 4758 p[1] = hexdigit[((U8)*pv & 0xF0) >> 4]; /* high 4 bits */
704 4758 p[2] = hexdigit[((U8)*pv & 0x0F)]; /* low 4 bits */
705 4758 sv_catpvn(dest, p, 3);
706 }
707 else {
708 29418 sv_catpvn(dest, pv, 1);
709 }
710 34176 pv++;
711 }
712 2148 return dest;
713 }
714 else {
715 2 return &PL_sv_undef;
716 }
717 }
718  
719 /* for tx_ckuuv_lhs() / tx_ckuuv_rhs() macros */
720 static SV*
721 1090 tx_sv_check_uuv(pTHX_ SV* const sv, const char* const name) {
722 /* check "Use of uninitialized value" (uuv) */
723 1090 ▶ SvGETMAGIC(sv);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(sv); 0 1090 1090
SvGETMAGIC(sv); 0 0 0
724 1090 ▶ if(!SvOK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(sv)) { 1 1089 1090
725 dMY_CXT;
726 1 tx_warn(aTHX_ MY_CXT.current_st,
727 "Use of nil for %s of binary operator", name);
728 1 return &PL_sv_no;
729 }
730 1089 return sv;
731 }
732  
733 static I32
734 619 tx_sv_eq_nomg(pTHX_ SV* const a, SV* const b) {
735 619 ▶ if(SvOK(a)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(a)) { 606 13 619
736 606 ▶ if(SvOK(b)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(b)) { 488 118 606
737 488 U32 const af = (SvFLAGS(a) & (SVf_POK|SVf_IOK|SVf_NOK));
738 488 U32 const bf = SvFLAGS(b) & af;
739 return bf == SVf_IOK
740 413 ? SvIVX(a) == SvIVX(b)
741 488 ▶ : sv_eq(a, b);
Branch: 1 branch
BranchTrueFalseTotal
: sv_eq(a, b); 413 75 488
742 }
743 else {
744 118 return FALSE;
745 }
746 }
747 else { /* !SvOK(a) */
748 13 return !SvOK(b);
749 }
750 }
751  
752 STATIC_INLINE I32
753 585 tx_sv_eq(pTHX_ SV* const a, SV* const b) {
754 585 ▶ SvGETMAGIC(a);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(a); 0 585 585
SvGETMAGIC(a); 0 0 0
755 585 ▶ SvSETMAGIC(b);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(b); 0 585 585
756 585 return tx_sv_eq_nomg(aTHX_ a, b);
757 }
758  
759 static I32
760 32 tx_sv_match(pTHX_ SV* const a, SV* const b) {
761 32 ▶ SvGETMAGIC(a);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(a); 0 32 32
SvGETMAGIC(a); 0 0 0
762 32 ▶ SvGETMAGIC(b);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(b); 0 32 32
SvGETMAGIC(b); 0 0 0
763  
764 32 ▶ if(SvROK(b)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(b)) { 7 25 32
765 7 SV* const r = SvRV(b);
766 7 ▶ if(SvOBJECT(r)) { /* a ~~ $object */
Branch: 1 branch
BranchTrueFalseTotal
if(SvOBJECT(r)) { /* a ~~ $object */ 0 7 7
767 /* XXX: what I should do? */
768 0 return tx_sv_eq_nomg(aTHX_ a, b);
769 }
770 7 ▶ else if(SvTYPE(r) == SVt_PVAV) { /* a ~~ [ ... ] */
Branch: 1 branch
BranchTrueFalseTotal
else if(SvTYPE(r) == SVt_PVAV) { /* a ~~ [ ... ] */ 4 3 7
771 4 AV* const av = (AV*)r;
772 4 I32 const len = av_len(av) + 1;
773 I32 i;
774 10 ▶ for(i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) { 9 1 10
775 9 SV** const svp = av_fetch(av, i, FALSE);
776 SV* item;
777 9 ▶ if(svp) {
Branch: 1 branch
BranchTrueFalseTotal
if(svp) { 9 0 9
778 9 item = *svp;
779 9 ▶ SvGETMAGIC(item);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(item); 0 9 9
SvGETMAGIC(item); 0 0 0
780 }
781 else {
782 0 item = &PL_sv_undef;
783 }
784 9 ▶ if(tx_sv_eq_nomg(aTHX_ a, item)) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_sv_eq_nomg(aTHX_ a, item)) { 3 6 9
785 3 return TRUE;
786 }
787 }
788 1 return FALSE;
789 }
790 3 ▶ else if(SvTYPE(r) == SVt_PVHV) { /* a ~~ { ... } */
Branch: 1 branch
BranchTrueFalseTotal
else if(SvTYPE(r) == SVt_PVHV) { /* a ~~ { ... } */ 3 0 3
791 3 ▶ if(SvOK(a)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOK(a)) { 3 0 3
792 3 HV* const hv = (HV*)r;
793 3 return hv_exists_ent(hv, a, 0U);
794 }
795 else {
796 0 return FALSE;
797 }
798 }
799 /* fallthrough */
800 }
801  
802 25 return tx_sv_eq_nomg(aTHX_ a, b);
803 }
804  
805 static bool
806 2323 tx_sv_is_macro(pTHX_ SV* const sv) {
807  
808 2323 ▶ if(sv_isobject(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if(sv_isobject(sv)) { 2040 283 2323
809 2040 AV* const macro = (AV*)SvRV(sv);
810 dMY_CXT;
811 2040 ▶ if(SvSTASH(macro) == MY_CXT.macro_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvSTASH(macro) == MY_CXT.macro_stash) { 2040 0 2040
812 2040 ▶ if(!(SvTYPE(macro) == SVt_PVAV && AvFILLp(macro) == (TXm_size - 1))) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvTYPE(macro) == SVt_PVAV && AvFILLp(macro) == (TXm_size - 1))) { 2040 0 2040
if(!(SvTYPE(macro) == SVt_PVAV && AvFILLp(macro) == (TXm_size - 1))) { 0 2040 2040
813 0 croak("Oops: Invalid macro object");
814 }
815 2040 return TRUE;
816 }
817 }
818 283 return FALSE;
819 }
820  
821 XS(XS_Text__Xslate__macrocall); /* -Wmissing-prototype */
822 604 XS(XS_Text__Xslate__macrocall){
823 604 dVAR; dSP; /* macrocall routine do dMARK, so we don't it here */
824 dMY_CXT;
825 604 SV* const macro = (SV*)CvXSUBANY(cv).any_ptr;
826 604 ▶ if(!(MY_CXT.current_st && macro)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(MY_CXT.current_st && macro)) { 604 0 604
if(!(MY_CXT.current_st && macro)) { 0 604 604
827 0 croak("Macro is not callable outside of templates");
828 }
829 604 ▶ XPUSHs( tx_proccall(aTHX_ MY_CXT.current_st, macro, "macro") );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( tx_proccall(aTHX_ MY_CXT.current_st, macro, "macro") ); 0 604 604
830 604 PUTBACK;
831 604 return;
832 }
833  
834 /* called by tx_methodcall() */
835 /* proc may be a Xslate macro or a Perl subroutine (code ref) */
836 SV*
837 859 tx_proccall(pTHX_ tx_state_t* const txst, SV* const proc, const char* const name) {
838 859 ▶ if(tx_sv_is_macro(aTHX_ proc)) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_sv_is_macro(aTHX_ proc)) { 780 79 859
839 780 dTX_optable;
840 780 tx_pc_t const save_pc = TX_st->pc;
841 tx_code_t proc_end;
842  
843 780 proc_end.exec_code = tx_optable[ TXOP_end ];
844 780 tx_macro_enter(aTHX_ TX_st, (AV*)SvRV(proc), &proc_end);
845 780 TX_RUNOPS(TX_st);
846 /* after tx_macro_end */
847  
848 780 TX_st->pc = save_pc;
849 //warn("# return from %s\n", name);
850  
851 780 return TX_st_sa;
852 }
853 79 ▶ else if (tx_sv_is_code_ref(aTHX_ proc) && CvXSUB((CV*)SvRV(proc)) == XS_Text__Xslate__macrocall) {
Branch: 2 branches
BranchTrueFalseTotal
else if (tx_sv_is_code_ref(aTHX_ proc) && CvXSUB((CV*)SvRV(proc)) == XS_Text__Xslate__macrocall) { 79 0 79
else if (tx_sv_is_code_ref(aTHX_ proc) && CvXSUB((CV*)SvRV(proc)) == XS_Text__Xslate__macrocall) { 0 79 79
854 /* macro wrapper XSUB created by Text::Xslate::Type::as_code_ref() */
855 0 SV* const m = CvXSUBANY((CV*)SvRV(proc)).any_ptr;
856 0 sv_dump(proc);
857 0 sv_dump(m);
858 0 Perl_croak(aTHX_ "xxx");
859 return tx_proccall(aTHX_ TX_st, m, name);
860 }
861 else {
862 79 return tx_funcall(aTHX_ TX_st, proc, name);
863 }
864 }
865  
866  
867 static void
868 1436 tx_macro_enter(pTHX_ tx_state_t* const txst, AV* const macro, tx_pc_t const retaddr) {
869 1436 dSP;
870 1436 dMARK;
871 1436 I32 const items = SP - MARK;
872 1436 SV* const name = AvARRAY(macro)[TXm_NAME];
873 1436 tx_pc_t const addr = INT2PTR(tx_pc_t, SvUVX(AvARRAY(macro)[TXm_ADDR]));
874 1436 IV const nargs = SvIVX(AvARRAY(macro)[TXm_NARGS]);
875 1436 UV const outer = SvUVX(AvARRAY(macro)[TXm_OUTER]);
876 AV* cframe; /* new frame */
877 UV i;
878 SV* tmp;
879  
880 1436 ▶ if(items != nargs) {
Branch: 1 branch
BranchTrueFalseTotal
if(items != nargs) { 2 1434 1436
881 2 tx_error(aTHX_ TX_st, "Wrong number of arguments for %"SVf" (%d %c %d)",
882 2 ▶ name, (int)items, items > nargs ? '>' : '<', (int)nargs);
Branch: 1 branch
BranchTrueFalseTotal
name, (int)items, items > nargs ? '>' : '<', (int)nargs); 1 1 2
883 2 TX_st->sa = &PL_sv_undef;
884 2 TX_RETURN_NEXT();
885 }
886  
887 /* create a new frame */
888 1434 cframe = tx_push_frame(aTHX_ TX_st);
889  
890 /* setup frame info: name, retaddr and output buffer */
891 1432 sv_setsv(*av_fetch(cframe, TXframe_NAME, TRUE), name);
892 1432 sv_setuv(*av_fetch(cframe, TXframe_RETADDR, TRUE), PTR2UV(retaddr));
893  
894 /* swap TXframe_OUTPUT and TX_st->output.
895 I know it's ugly. Any ideas?
896 */
897 1432 tmp = *av_fetch(cframe, TXframe_OUTPUT, TRUE);
898 1432 AvARRAY(cframe)[TXframe_OUTPUT] = TX_st->output;
899 1432 TX_st->output = tmp;
900 1432 sv_setpvs(tmp, "");
901 1432 ▶ SvGROW(tmp, TX_HINT_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(tmp, TX_HINT_SIZE); 1432 0 1432
SvGROW(tmp, TX_HINT_SIZE); 400 1032 1432
902  
903 1432 i = 0;
904 1432 ▶ if(outer > 0) { /* refers outer lexical variales */
Branch: 1 branch
BranchTrueFalseTotal
if(outer > 0) { /* refers outer lexical variales */ 11 1421 1432
905 /* copies lexical variables from the old frame to the new one */
906 11 AV* const oframe = TX_frame_at(TX_st, TX_st->current_frame-1);
907 32 ▶ for(NOOP; i < outer; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(NOOP; i < outer; i++) { 21 11 32
908 21 IV const real_ix = i + TXframe_START_LVAR;
909 /* XXX: macros can refer to unallocated lvars */
910 SV* sv;
911 21 SV* v = AvARRAY(oframe)[real_ix];
912 21 ▶ if (v != NULL && AvFILLp(oframe) >= real_ix) {
Branch: 2 branches
BranchTrueFalseTotal
if (v != NULL && AvFILLp(oframe) >= real_ix) { 19 2 21
if (v != NULL && AvFILLp(oframe) >= real_ix) { 19 0 19
913 19 sv = sv_mortalcopy(v);
914 } else {
915 2 sv = &PL_sv_undef;
916 }
917 21 av_store(cframe, real_ix , sv);
918 21 SvREFCNT_inc_simple_void_NN(sv);
919 }
920 }
921 1432 ▶ if(items > 0) { /* has arguments */
Branch: 1 branch
BranchTrueFalseTotal
if(items > 0) { /* has arguments */ 871 561 1432
922 871 dORIGMARK;
923 871 MARK++;
924 1882 ▶ for(NOOP; MARK <= SP; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(NOOP; MARK <= SP; i++) { 1011 871 1882
925 1011 sv_setsv(TX_lvar(i), *MARK);
926 1011 MARK++;
927 }
928 871 SP = ORIGMARK;
929 871 PUTBACK;
930 }
931 1432 TX_st->pad = AvARRAY(cframe) + TXframe_START_LVAR;
932 1432 TX_RETURN_PC(addr);
933 }
934  
935 /* The virtual machine code interpreter */
936 /* NOTE: tx_execute() must be surrounded in ENTER and LEAVE */
937 static void
938 3564 tx_execute(pTHX_ pMY_CXT_ tx_state_t* const base, SV* const output, HV* const hv) {
939 3564 dXCPT;
940 tx_state_t st;
941  
942 3564 StructCopy(base, &st, tx_state_t);
943  
944 //PerlIO_stdoutf("#>> 0x%p %d %d\n", base, (int)MY_CXT.depth, (int)st.current_frame);
945 3564 st.output = output;
946 3564 st.vars = hv;
947  
948 assert(st.tmpl != NULL);
949  
950 /* local $current_st */
951 3564 SAVEVPTR(MY_CXT.current_st);
952 3564 MY_CXT.current_st = &st;
953  
954 assert(MY_CXT.depth >= 0);
955 3564 ▶ if(MY_CXT.depth > TX_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if(MY_CXT.depth > TX_MAX_DEPTH) { 4 3560 3564
956 4 croak("Execution is too deep (> %d)", TX_MAX_DEPTH);
957 }
958  
959 /* local $depth = $depth + 1 */
960 3560 MY_CXT.depth++;
961  
962 3560 ▶ XCPT_TRY_START {
Branch: 1 branch
BranchTrueFalseTotal
XCPT_TRY_START { 3560 426 3986
963 3560 TX_RUNOPS(&st);
964 }
965 3560 XCPT_TRY_END;
966  
967 /* finally */
968 3560 MY_CXT.depth--;
969  
970 3560 ▶ XCPT_CATCH {
Branch: 1 branch
BranchTrueFalseTotal
XCPT_CATCH { 426 3134 3560
971 426 I32 const start = base->current_frame;
972 /* unwind the stack frames */
973 629 ▶ while(st.current_frame > start) {
Branch: 1 branch
BranchTrueFalseTotal
while(st.current_frame > start) { 203 426 629
974 203 tx_pop_frame(aTHX_ &st, TRUE);
975 }
976 426 tx_pop_frame(aTHX_ base, FALSE); // pushed before tx_execute()
977 426 ▶ XCPT_RETHROW;
Branch: 2 branches
BranchTrueFalseTotal
XCPT_RETHROW; 426 0 426
XCPT_RETHROW; 0 0 0
978 }
979 3134 tx_pop_frame(aTHX_ base, FALSE); // pushed before tx_execute()
980  
981 /* clear temporary buffers */
982 3134 sv_setsv(st.targ, NULL);
983  
984 /* store the current buffer size as a hint size */
985 3134 base->hint_size = SvCUR(st.output);
986 3134 }
987  
988  
989 static MAGIC*
990 5994 mgx_find(pTHX_ SV* const sv, const MGVTBL* const vtbl){
991 MAGIC* mg;
992  
993 assert(sv != NULL);
994 assert(vtbl != NULL);
995  
996 5994 ▶ for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
Branch: 1 branch
BranchTrueFalseTotal
for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){ 5987 7 5994
997 5987 ▶ if(mg->mg_virtual == vtbl){
Branch: 1 branch
BranchTrueFalseTotal
if(mg->mg_virtual == vtbl){ 5987 0 5987
998 assert(mg->mg_type == PERL_MAGIC_ext);
999 5987 return mg;
1000 }
1001 }
1002  
1003 7 return NULL;
1004 }
1005  
1006 static int
1007 3571 tx_mg_free(pTHX_ SV* const sv, MAGIC* const mg){
1008 3571 tx_state_t* const st = (tx_state_t*)mg->mg_ptr;
1009 3571 tx_info_t* const info = st->info;
1010 3571 tx_code_t* const code = st->code;
1011 3571 I32 const len = st->code_len;
1012 I32 i;
1013  
1014 // PerlIO_stdoutf("# tx_mg_free()\n");
1015 45072 ▶ for(i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) { 41501 3571 45072
1016 /* opcode */
1017 41501 ▶ if( tx_oparg[ info[i].optype ] & TXARGf_SV ) {
Branch: 1 branch
BranchTrueFalseTotal
if( tx_oparg[ info[i].optype ] & TXARGf_SV ) { 17057 24444 41501
1018 17057 SvREFCNT_dec(code[i].u_arg.sv);
1019 }
1020  
1021 /* opinfo */
1022 41501 SvREFCNT_dec(info[i].file);
1023 }
1024  
1025 3571 Safefree(code);
1026 3571 Safefree(info);
1027  
1028 3571 SvREFCNT_dec(st->symbol);
1029 3571 SvREFCNT_dec(st->frames);
1030 3571 SvREFCNT_dec(st->targ);
1031 3571 SvREFCNT_dec(st->engine);
1032  
1033 PERL_UNUSED_ARG(sv);
1034  
1035 3571 return 0;
1036 }
1037  
1038 #ifdef USE_ITHREADS
1039 static SV*
1040 tx_sv_dup_inc(pTHX_ SV* const sv, CLONE_PARAMS* const param) {
1041 return SvREFCNT_inc( sv_dup(sv, param) );
1042 }
1043 #endif
1044  
1045 static int
1046 0 tx_mg_dup(pTHX_ MAGIC* const mg, CLONE_PARAMS* const param){
1047 #ifdef USE_ITHREADS /* single threaded perl has no "xxx_dup()" APIs */
1048 tx_state_t* st = (tx_state_t*)mg->mg_ptr;
1049 tx_info_t* const proto_info = st->info;
1050 tx_code_t* const proto_code = st->code;
1051 U32 const len = st->code_len;
1052 U32 i;
1053  
1054 Newx(st->code, len, tx_code_t);
1055 Newx(st->info, len, tx_info_t);
1056  
1057 for(i = 0; i < len; i++) {
1058 U8 const oparg = tx_oparg[ proto_info[i].optype ];
1059 /* opcode */
1060 st->code[i].exec_code = proto_code[i].exec_code;
1061 if( oparg & TXARGf_SV ) {
1062 st->code[i].u_arg.sv = tx_sv_dup_inc(aTHX_ proto_code[i].u_arg.sv, param);
1063 }
1064 else if ( oparg & TXARGf_INT ) {
1065 st->code[i].u_arg.iv = proto_code[i].u_arg.iv;
1066 }
1067 else if( oparg & TXARGf_PC ) {
1068 st->code[i].u_arg.pc = proto_code[i].u_arg.pc;
1069 }
1070  
1071 /* opinfo */
1072 st->info[i].optype = proto_info[i].optype;
1073 st->info[i].line = proto_info[i].line;
1074 st->info[i].file = tx_sv_dup_inc(aTHX_ proto_info[i].file, param);
1075 }
1076  
1077 st->symbol = (HV*)tx_sv_dup_inc(aTHX_ (SV*)st->symbol, param);
1078 st->frames = (AV*)tx_sv_dup_inc(aTHX_ (SV*)st->frames, param);
1079 st->targ = tx_sv_dup_inc(aTHX_ st->targ, param);
1080 st->engine = tx_sv_dup_inc(aTHX_ st->engine, param);
1081 #else
1082 PERL_UNUSED_ARG(mg);
1083 PERL_UNUSED_ARG(param);
1084 #endif
1085 0 return 0;
1086 }
1087  
1088  
1089 static MGVTBL xslate_vtbl = { /* for identity */
1090 NULL, /* get */
1091 NULL, /* set */
1092 NULL, /* len */
1093 NULL, /* clear */
1094 tx_mg_free, /* free */
1095 NULL, /* copy */
1096 tx_mg_dup, /* dup */
1097 #ifdef MGf_LOCAL
1098 NULL, /* local */
1099 #endif
1100 };
1101  
1102  
1103 static void
1104 2018 tx_invoke_load_file(pTHX_ SV* const self, SV* const name, SV* const mtime, bool const from_include) {
1105 2018 dSP;
1106 2018 ENTER;
1107 2018 SAVETMPS;
1108  
1109 2018 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2018 2018
1110 2018 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 2018 2018
1111 2018 PUSHs(self);
1112 2018 PUSHs(name);
1113 2018 ▶ PUSHs(mtime ? mtime : &PL_sv_undef);
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(mtime ? mtime : &PL_sv_undef); 1824 194 2018
1114 2018 ▶ PUSHs(boolSV(from_include));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(boolSV(from_include)); 1231 787 2018
1115 2018 PUTBACK;
1116  
1117 2018 call_method("load_file", G_EVAL | G_VOID | G_DISCARD);
1118 2018 ▶ if(TX_CATCH_ERROR()){
Branch: 2 branches
BranchTrueFalseTotal
if(TX_CATCH_ERROR()){ 2018 0 2018
if(TX_CATCH_ERROR()){ 9 2009 2018
1119 dMY_CXT;
1120 18 SV* const msg = PL_diehook == MY_CXT.die_handler
1121 0 ▶ ? sv_2mortal(newRV_inc(sv_mortalcopy(ERRSV)))
Branch: 1 branch
BranchTrueFalseTotal
? sv_2mortal(newRV_inc(sv_mortalcopy(ERRSV))) 0 0 0
1122 9 ▶ : ERRSV;
Branch: 2 branches
BranchTrueFalseTotal
: ERRSV; 0 9 9
: ERRSV; 9 0 9
1123 9 tx_call_error_handler(aTHX_ MY_CXT.die_handler, msg);
1124 /* not reached */
1125 }
1126  
1127 2009 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2009 0 2009
1128 2009 LEAVE;
1129 2009 }
1130  
1131 static bool
1132 1883 tx_all_deps_are_fresh(pTHX_ AV* const tmpl, Time_t const cache_mtime) {
1133 1883 I32 const len = AvFILLp(tmpl) + 1;
1134 I32 i;
1135 Stat_t st;
1136  
1137 1954 ▶ for(i = TXo_FULLPATH; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = TXo_FULLPATH; i < len; i++) { 1895 59 1954
1138 1895 SV* const deppath = AvARRAY(tmpl)[i];
1139  
1140 1895 ▶ if(SvROK(deppath)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(deppath)) { 39 1856 1895
1141 39 continue;
1142 }
1143  
1144 //PerlIO_stdoutf("check deps: %"SVf" ...\n", deppath); // */
1145 1856 ▶ if(PerlLIO_stat(SvPV_nolen_const(deppath), &st) < 0
Branch: 1 branch
BranchTrueFalseTotal
if(PerlLIO_stat(SvPV_nolen_const(deppath), &st) < 0 1856 0 1856
1146 1856 ▶ || st.st_mtime > cache_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
|| st.st_mtime > cache_mtime) { 1824 32 1856
1147 1824 SV* const main_cache = AvARRAY(tmpl)[TXo_CACHEPATH];
1148 /* compiled caches are no longer fresh, so it must be discarded */
1149  
1150 1824 ▶ if(i != TXo_FULLPATH && SvOK(main_cache)) {
Branch: 2 branches
BranchTrueFalseTotal
if(i != TXo_FULLPATH && SvOK(main_cache)) { 2 1822 1824
if(i != TXo_FULLPATH && SvOK(main_cache)) { 2 0 2
1151 2 PerlLIO_unlink(SvPV_nolen_const(main_cache));
1152 }
1153 //PerlLIO_unlink(SvPV_nolen_const(AvARRAY(tmpl);
1154  
1155 1824 ▶ if (dump_load) {
Branch: 1 branch
BranchTrueFalseTotal
if (dump_load) { 0 1824 1824
1156 0 PerlIO_printf(PerlIO_stderr(),
1157 "#[XS] %"SVf": too old (%d < %d)\n",
1158 0 deppath, (int)cache_mtime, (int)st.st_mtime);
1159 }
1160 1824 return FALSE;
1161 }
1162 else {
1163 32 ▶ if (dump_load) {
Branch: 1 branch
BranchTrueFalseTotal
if (dump_load) { 0 32 32
1164 0 PerlIO_printf(PerlIO_stderr(),
1165 "#[XS] %"SVf": fresh enough (%d >= %d)\n",
1166 0 deppath, (int)cache_mtime, (int)st.st_mtime);
1167 }
1168 }
1169 }
1170 59 return TRUE;
1171 }
1172  
1173 static tx_state_t*
1174 3578 tx_load_template(pTHX_ SV* const self, SV* const name, bool const from_include) {
1175 HV* hv;
1176 3578 const char* why = NULL;
1177 HE* he;
1178 SV** svp;
1179 SV* sv;
1180 HV* ttable;
1181 AV* tmpl;
1182 MAGIC* mg;
1183 SV* cache_mtime;
1184 3578 int retried = 0;
1185  
1186 3578 ▶ if (dump_load) {
Branch: 1 branch
BranchTrueFalseTotal
if (dump_load) { 0 3578 3578
1187 0 PerlIO_printf(PerlIO_stderr(),
1188 "#[XS] load_template(%"SVf")\n", name);
1189 }
1190  
1191 3578 ▶ if(!SvOK(name)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(name)) { 0 3578 3578
1192 0 why = "template name is invalid";
1193 0 goto err;
1194 }
1195  
1196 assert( SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV );
1197  
1198 3578 hv = (HV*)SvRV(self);
1199  
1200 2009 retry:
1201 5587 ▶ if(retried > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if(retried > 1) { 0 5587 5587
1202 0 why = "retried reloading, but failed";
1203 0 goto err;
1204 }
1205  
1206 /* validation by modified time (mtime) */
1207  
1208 /* my $ttable = $self->{template} */
1209 5587 svp = hv_fetchs(hv, "template", FALSE);
1210 5587 ▶ if(!svp) {
Branch: 1 branch
BranchTrueFalseTotal
if(!svp) { 0 5587 5587
1211 0 why = "template table is not found";
1212 0 goto err;
1213 }
1214  
1215 5587 sv = *svp;
1216 5587 ▶ if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)) { 5587 0 5587
if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV)) { 1 5586 5587
1217 1 why = "template table is not a HASH reference";
1218 1 goto err;
1219 }
1220  
1221 5586 ttable = (HV*)SvRV(sv);
1222  
1223 /* $tmpl = $ttable->{$name} */
1224 5586 he = hv_fetch_ent(ttable, name, FALSE, 0U);
1225 5586 ▶ if(!he) {
Branch: 1 branch
BranchTrueFalseTotal
if(!he) { 194 5392 5586
1226 194 tx_invoke_load_file(aTHX_ self, name, NULL, from_include);
1227 185 retried++;
1228 185 goto retry;
1229 }
1230  
1231 5392 sv = hv_iterval(ttable, he);
1232 5392 ▶ if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)) { 5391 1 5392
if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)) { 0 5391 5391
1233 1 why = "template entry is invalid";
1234 1 goto err;
1235 }
1236  
1237 5391 tmpl = (AV*)SvRV(sv);
1238 5391 ▶ if(AvFILLp(tmpl) < (TXo_least_size-1)) {
Branch: 1 branch
BranchTrueFalseTotal
if(AvFILLp(tmpl) < (TXo_least_size-1)) { 1 5390 5391
1239 1 why = form("template entry is broken (size: %d < %d)",
1240 (int)AvFILLp(tmpl)+1, (int)TXo_least_size);
1241 1 goto err;
1242 }
1243  
1244 5390 mg = mgx_find(aTHX_ (SV*)tmpl, &xslate_vtbl);
1245 5390 ▶ if(!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!mg) { 0 5390 5390
1246 0 croak("Xslate: Invalid template holder was passed");
1247 }
1248 /* check mtime */
1249  
1250 5390 cache_mtime = AvARRAY(tmpl)[TXo_MTIME];
1251  
1252 /* NOTE: Ensure the life of the template object */
1253 5390 sv_2mortal( (SV*)SvREFCNT_inc_simple_NN(tmpl) );
1254  
1255 5390 ▶ if(!SvOK(cache_mtime)) { /* non-checking mode (i.e. release mode) */
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(cache_mtime)) { /* non-checking mode (i.e. release mode) */ 1507 3883 5390
1256  
1257 1507 return (tx_state_t*)mg->mg_ptr;
1258 }
1259  
1260 3883 ▶ if (dump_load) {
Branch: 1 branch
BranchTrueFalseTotal
if (dump_load) { 0 3883 3883
1261 0 PerlIO_printf(PerlIO_stderr(),
1262 "#[XS] %"SVf" (mtime=%"SVf")\n", name, cache_mtime);
1263 }
1264  
1265 3883 ▶ if(retried > 0 /* if already retried, it should be valid */
Branch: 1 branch
BranchTrueFalseTotal
if(retried > 0 /* if already retried, it should be valid */ 1883 2000 3883
1266 1883 ▶ || tx_all_deps_are_fresh(aTHX_ tmpl, SvIVX(cache_mtime))) {
Branch: 1 branch
BranchTrueFalseTotal
|| tx_all_deps_are_fresh(aTHX_ tmpl, SvIVX(cache_mtime))) { 59 1824 1883
1267 2059 return (tx_state_t*)mg->mg_ptr;
1268 }
1269 else {
1270 1824 tx_invoke_load_file(aTHX_ self, name, cache_mtime, from_include);
1271 1824 retried++;
1272 1824 goto retry;
1273 }
1274  
1275 3 err:
1276 3 croak("Xslate: Cannot load template %s: %s", tx_neat(aTHX_ name), why);
1277 }
1278  
1279 static int
1280 7 tx_macro_free(pTHX_ SV* const sv PERL_UNUSED_DECL, MAGIC* const mg){
1281 7 CV* const xsub = (CV*)mg->mg_obj;
1282  
1283 assert(SvTYPE(xsub) == SVt_PVCV);
1284 assert(CvXSUB(xsub) != NULL);
1285  
1286 7 CvXSUBANY(xsub).any_ptr = NULL;
1287 7 return 0;
1288 }
1289  
1290 static MGVTBL macro_vtbl = { /* identity */
1291 NULL, /* get */
1292 NULL, /* set */
1293 NULL, /* len */
1294 NULL, /* clear */
1295 tx_macro_free, /* free */
1296 NULL, /* copy */
1297 NULL, /* dup */
1298 #ifdef MGf_LOCAL
1299 NULL, /* local */
1300 #endif
1301 };
1302  
1303  
1304 static void
1305 188 tx_my_cxt_init(pTHX_ pMY_CXT_ bool const cloning PERL_UNUSED_DECL) {
1306 188 MY_CXT.depth = 0;
1307 188 MY_CXT.raw_stash = gv_stashpvs(TX_RAW_CLASS, GV_ADDMULTI);
1308 188 MY_CXT.macro_stash = gv_stashpvs(TX_MACRO_CLASS, GV_ADDMULTI);
1309 188 MY_CXT.warn_handler = SvREFCNT_inc_NN(
1310 (SV*)get_cv("Text::Xslate::Engine::_warn", GV_ADD));
1311 188 MY_CXT.die_handler = SvREFCNT_inc_NN(
1312 (SV*)get_cv("Text::Xslate::Engine::_die", GV_ADD));
1313 188 MY_CXT.make_error = SvREFCNT_inc_NN(
1314 (SV*)get_cv("Text::Xslate::Engine::make_error", GV_ADD));
1315 188 }
1316  
1317 /* Because overloading stuff of old xsubpp didn't work,
1318 we need to copy them. */
1319 XS(XS_Text__Xslate__fallback); /* prototype to pass -Wmissing-prototypes */
1320 0 XS(XS_Text__Xslate__fallback)
1321 {
1322 0 dXSARGS;
1323 PERL_UNUSED_VAR(cv);
1324 PERL_UNUSED_VAR(items);
1325 0 XSRETURN_EMPTY;
1326 }
1327  
1328 EXTERN_C XS(boot_Text__Xslate__Methods);
1329  
1330 MODULE = Text::Xslate PACKAGE = Text::Xslate::Engine
1331  
1332 PROTOTYPES: DISABLE
1333  
1334 BOOT:
1335 {
1336 188 HV* const ops = get_hv("Text::Xslate::OPS", GV_ADDMULTI);
1337 MY_CXT_INIT;
1338 188 tx_my_cxt_init(aTHX_ aMY_CXT_ FALSE);
1339 188 tx_init_ops(aTHX_ ops);
1340  
1341 {
1342 188 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 188 188
1343 188 boot_Text__Xslate__Methods(aTHX_ cv);
1344 }
1345 }
1346  
1347 #ifdef USE_ITHREADS
1348  
1349 void
1350 CLONE(...)
1351 CODE:
1352 {
1353 MY_CXT_CLONE;
1354 tx_my_cxt_init(aTHX_ aMY_CXT_ FALSE);
1355 PERL_UNUSED_VAR(items);
1356 }
1357  
1358 #endif
1359  
1360 void
1361 _register_builtin_methods(self, HV* hv)
1362 CODE:
1363 {
1364 297 tx_register_builtin_methods(aTHX_ hv);
1365 }
1366  
1367 void
1368 _assemble(HV* self, AV* proto, SV* name, SV* fullpath, SV* cachepath, SV* mtime)
1369 CODE:
1370 {
1371 dMY_CXT;
1372 3571 dTX_optable;
1373 MAGIC* mg;
1374 HV* hv;
1375 3571 HV* const ops = get_hv("Text::Xslate::OPS", GV_ADD);
1376 3571 U32 const len = av_len(proto) + 1;
1377 U32 i;
1378 U16 oi_line; /* opinfo.line */
1379 SV* oi_file;
1380 tx_state_t st;
1381 AV* tmpl;
1382 SV* tobj;
1383 SV** svp;
1384 3571 AV* macro = NULL;
1385  
1386 3571 TAINT_NOT; /* All the SVs we'll create here are safe */
1387  
1388 3571 Zero(&st, 1, tx_state_t);
1389  
1390 3571 svp = hv_fetchs(self, "template", FALSE);
1391 3571 ▶ if(!(svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV)) {
Branch: 3 branches
BranchTrueFalseTotal
if(!(svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV)) { 3571 0 3571
if(!(svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV)) { 3571 0 3571
if(!(svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV)) { 0 3571 3571
1392 0 croak("The xslate instance has no template table");
1393 }
1394 3571 hv = (HV*)SvRV(*svp);
1395  
1396 3571 ▶ if(!SvOK(name)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(name)) { 0 3571 3571
1397 0 croak("Undefined template name is invalid");
1398 }
1399  
1400 /* fetch the template object from $self->{template}{$name} */
1401 3571 tobj = hv_iterval(hv, hv_fetch_ent(hv, name, TRUE, 0U));
1402  
1403 3571 tmpl = newAV();
1404 /* store the template object to $self->{template}{$name} */
1405 3571 sv_setsv(tobj, sv_2mortal(newRV_noinc((SV*)tmpl)));
1406 3571 av_extend(tmpl, TXo_least_size - 1);
1407  
1408 3571 sv_setsv(*av_fetch(tmpl, TXo_MTIME, TRUE), mtime);
1409 3571 sv_setsv(*av_fetch(tmpl, TXo_CACHEPATH, TRUE), cachepath);
1410 3571 sv_setsv(*av_fetch(tmpl, TXo_FULLPATH, TRUE), fullpath);
1411  
1412 /* prepare function table */
1413 3571 svp = hv_fetchs(self, "function", FALSE);
1414 3571 TAINT_NOT;
1415  
1416 3571 ▶ if(!( SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV )) {
Branch: 2 branches
BranchTrueFalseTotal
if(!( SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV )) { 3571 0 3571
if(!( SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV )) { 0 3571 3571
1417 0 croak("Function table must be a HASH reference");
1418 }
1419 /* $self->{function} must be copied
1420 because it might be changed per templates */
1421 3571 st.symbol = newHVhv( (HV*)SvRV(*svp) );
1422  
1423 3571 st.tmpl = tmpl;
1424 3571 st.engine = newRV_inc((SV*)self);
1425 3571 sv_rvweaken(st.engine);
1426  
1427 3571 st.hint_size = TX_HINT_SIZE;
1428  
1429 3571 st.sa = &PL_sv_undef;
1430 3571 st.sb = &PL_sv_undef;
1431 3571 st.targ = newSV(0);
1432  
1433 /* stack frame */
1434 3571 st.frames = newAV();
1435 3571 st.current_frame = -1;
1436  
1437 3571 Newxz(st.info, len + 1, tx_info_t);
1438 3571 st.info[len].line = (U16)-1; /* invalid value */
1439 3571 st.info[len].file = SvREFCNT_inc_simple_NN(name);
1440  
1441 3571 Newxz(st.code, len, tx_code_t);
1442  
1443 3571 st.code_len = len;
1444 3571 st.pc = &st.code[0];
1445  
1446 3571 mg = sv_magicext((SV*)tmpl, NULL, PERL_MAGIC_ext,
1447 &xslate_vtbl, (char*)&st, sizeof(st));
1448 3571 mg->mg_flags |= MGf_DUP;
1449  
1450 3571 oi_line = 0;
1451 3571 oi_file = name;
1452  
1453 45072 ▶ for(i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 0; i < len; i++) { 41501 3571 45072
1454 41501 SV* const code = *av_fetch(proto, i, TRUE);
1455 83002 ▶ if(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVAV) { 41501 0 41501
if(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVAV) { 41501 0 41501
1456 41501 AV* const av = (AV*)SvRV(code);
1457 41501 SV* const opname = *av_fetch(av, 0, TRUE);
1458 41501 SV** const arg = av_fetch(av, 1, FALSE);
1459 41501 SV** const line = av_fetch(av, 2, FALSE);
1460 41501 SV** const file = av_fetch(av, 3, FALSE);
1461 41501 HE* const he = hv_fetch_ent(ops, opname, FALSE, 0U);
1462 IV opnum;
1463  
1464 41501 ▶ if(!he){
Branch: 1 branch
BranchTrueFalseTotal
if(!he){ 0 41501 41501
1465 0 croak("Oops: Unknown opcode '%"SVf"' on [%d]", opname, (int)i);
1466 }
1467  
1468 41501 opnum = SvIVx(hv_iterval(ops, he));
1469 41501 st.code[i].exec_code = tx_optable[opnum];
1470 41501 ▶ if(tx_oparg[opnum] & TXARGf_SV) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_oparg[opnum] & TXARGf_SV) { 17057 24444 41501
1471 17057 ▶ if(!arg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!arg) { 0 17057 17057
1472 0 croak("Oops: Opcode %"SVf" must have an argument on [%d]", opname, (int)i);
1473 }
1474  
1475 17057 ▶ if(tx_oparg[opnum] & TXARGf_KEY) { /* shared sv */
Branch: 1 branch
BranchTrueFalseTotal
if(tx_oparg[opnum] & TXARGf_KEY) { /* shared sv */ 4222 12835 17057
1476 STRLEN len;
1477 4222 const char* const pv = SvPV_const(*arg, len);
1478 4222 ▶ st.code[i].u_arg.sv = newSVpvn_share(pv, SvUTF8(*arg) ? -len : len, 0U);
Branch: 1 branch
BranchTrueFalseTotal
st.code[i].u_arg.sv = newSVpvn_share(pv, SvUTF8(*arg) ? -len : len, 0U); 1959 2263 4222
1479 }
1480 12835 ▶ else if(tx_oparg[opnum] & TXARGf_INT) { /* sviv */
Branch: 1 branch
BranchTrueFalseTotal
else if(tx_oparg[opnum] & TXARGf_INT) { /* sviv */ 1209 11626 12835
1481 1209 ▶ SvIV_please(*arg);
Branch: 2 branches
BranchTrueFalseTotal
SvIV_please(*arg); 0 1209 1209
SvIV_please(*arg); 0 0 0
1482 1209 st.code[i].u_arg.sv = SvIsUV(*arg)
1483 0 ? newSVuv(SvUV(*arg))
1484 1209 ▶ : newSViv(SvIV(*arg));
Branch: 1 branch
BranchTrueFalseTotal
: newSViv(SvIV(*arg)); 0 1209 1209
1485 }
1486 else { /* normal sv */
1487 11626 st.code[i].u_arg.sv = newSVsv(*arg);
1488 }
1489 }
1490 24444 ▶ else if(tx_oparg[opnum] & TXARGf_INT) {
Branch: 1 branch
BranchTrueFalseTotal
else if(tx_oparg[opnum] & TXARGf_INT) { 1286 23158 24444
1491 1286 st.code[i].u_arg.iv = SvIV(*arg);
1492 }
1493 23158 ▶ else if(tx_oparg[opnum] & TXARGf_PC) {
Branch: 1 branch
BranchTrueFalseTotal
else if(tx_oparg[opnum] & TXARGf_PC) { 1191 21967 23158
1494 /* calculate relational addresses to absolute addresses */
1495 1191 UV const abs_pos = (UV)(i + SvIV(*arg));
1496  
1497 1191 ▶ if(abs_pos >= (UV)len) {
Branch: 1 branch
BranchTrueFalseTotal
if(abs_pos >= (UV)len) { 0 1191 1191
1498 0 croak("Oops: goto address %"IVdf" is out of range (must be 0 <= addr <= %"IVdf")",
1499 SvIV(*arg), (IV)len);
1500 }
1501 1191 st.code[i].u_arg.pc = TX_POS2PC(&st, abs_pos);
1502 }
1503 else {
1504 21967 ▶ if(arg && SvOK(*arg)) {
Branch: 2 branches
BranchTrueFalseTotal
if(arg && SvOK(*arg)) { 21967 0 21967
if(arg && SvOK(*arg)) { 0 21967 21967
1505 0 croak("Oops: Opcode %"SVf" has an extra argument %s on [%d]",
1506 opname, tx_neat(aTHX_ *arg), (int)i);
1507 }
1508 }
1509  
1510 /* setup opinfo */
1511 41501 ▶ if(line && SvOK(*line)) {
Branch: 2 branches
BranchTrueFalseTotal
if(line && SvOK(*line)) { 41398 103 41501
if(line && SvOK(*line)) { 17901 23497 41398
1512 17901 oi_line = (U16)SvUV(*line);
1513 }
1514 41501 ▶ if(file && SvOK(*file) && !sv_eq(*file, oi_file)) {
Branch: 3 branches
BranchTrueFalseTotal
if(file && SvOK(*file) && !sv_eq(*file, oi_file)) { 41355 146 41501
if(file && SvOK(*file) && !sv_eq(*file, oi_file)) { 6306 35049 41355
if(file && SvOK(*file) && !sv_eq(*file, oi_file)) { 4360 1946 6306
1515 4360 oi_file = sv_mortalcopy(*file);
1516 }
1517 41501 st.info[i].optype = (U16)opnum;
1518 41501 st.info[i].line = oi_line;
1519 41501 st.info[i].file = SvREFCNT_inc_simple_NN(oi_file);
1520  
1521 /* special cases */
1522 41501 ▶ if(opnum == TXOP_macro_begin) {
Branch: 1 branch
BranchTrueFalseTotal
if(opnum == TXOP_macro_begin) { 330 41171 41501
1523 330 SV* const name = st.code[i].u_arg.sv;
1524 330 SV* const ent = hv_iterval(st.symbol,
1525 hv_fetch_ent(st.symbol, name, TRUE, 0U));
1526  
1527 330 ▶ if(!sv_true(ent)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!sv_true(ent)) { 330 0 330
1528 SV* mref;
1529 330 macro = newAV();
1530 330 mref = sv_2mortal(newRV_noinc((SV*)macro));
1531 330 sv_bless(mref, MY_CXT.macro_stash);
1532 330 sv_setsv(ent, mref);
1533  
1534 330 (void)av_store(macro, TXm_OUTER, newSViv(0));
1535 330 (void)av_store(macro, TXm_NARGS, newSViv(0));
1536 330 (void)av_store(macro, TXm_ADDR, newSVuv(PTR2UV(TX_POS2PC(&st, i))));
1537 330 (void)av_store(macro, TXm_NAME, name);
1538 330 st.code[i].u_arg.sv = NULL;
1539 }
1540 else { /* already defined */
1541 0 macro = NULL;
1542 }
1543 }
1544 41171 ▶ else if(opnum == TXOP_macro_nargs) {
Branch: 1 branch
BranchTrueFalseTotal
else if(opnum == TXOP_macro_nargs) { 91 41080 41171
1545 91 ▶ if(macro) {
Branch: 1 branch
BranchTrueFalseTotal
if(macro) { 91 0 91
1546 /* the number of outer lexical variables */
1547 91 (void)av_store(macro, TXm_NARGS, st.code[i].u_arg.sv);
1548 91 st.code[i].u_arg.sv = NULL;
1549 }
1550 }
1551 41080 ▶ else if(opnum == TXOP_macro_outer) {
Branch: 1 branch
BranchTrueFalseTotal
else if(opnum == TXOP_macro_outer) { 13 41067 41080
1552 13 ▶ if(macro) {
Branch: 1 branch
BranchTrueFalseTotal
if(macro) { 13 0 13
1553 /* the number of outer lexical variables */
1554 13 (void)av_store(macro, TXm_OUTER, st.code[i].u_arg.sv);
1555 13 st.code[i].u_arg.sv = NULL;
1556 }
1557 }
1558 41067 ▶ else if(opnum == TXOP_depend) {
Branch: 1 branch
BranchTrueFalseTotal
else if(opnum == TXOP_depend) { 103 40964 41067
1559 /* add a dependent file to the tmpl object */
1560 103 av_push(tmpl, st.code[i].u_arg.sv);
1561 103 st.code[i].u_arg.sv = NULL;
1562 }
1563 }
1564 else {
1565 0 croak("Oops: Broken code found on [%d]", (int)i);
1566 }
1567 } /* end for each code */
1568 }
1569  
1570 void
1571 render(SV* self, SV* source, SV* vars = &PL_sv_undef)
1572 ALIAS:
1573 render = 0
1574 render_string = 1
1575 CODE:
1576 {
1577 dMY_CXT;
1578 tx_state_t* st;
1579  
1580 2394 TAINT_NOT; /* All the SVs we'll create here are safe */
1581  
1582 /* $_[0]: engine */
1583 2394 ▶ if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) { 2393 1 2394
if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) { 0 2393 2393
1584 1 croak("Xslate: Invalid xslate instance: %s",
1585 tx_neat(aTHX_ self));
1586 }
1587  
1588 /* $_[1]: template source */
1589 2393 ▶ if(ix == 1) { /* render_string() */
Branch: 1 branch
BranchTrueFalseTotal
if(ix == 1) { /* render_string() */ 1366 1027 2393
1590 1366 ▶ dXSTARG;
Branch: 1 branch
BranchTrueFalseTotal
dXSTARG; 1366 0 1366
1591 1366 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1366 1366
1592 1366 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1366 1366
1593 1366 PUSHs(self);
1594 1366 PUSHs(source);
1595 1366 PUTBACK;
1596 1366 call_method("load_string", G_VOID | G_DISCARD);
1597 1302 SPAGAIN;
1598 1302 source = TARG;
1599 1302 sv_setpvs(source, "<string>");
1600 }
1601  
1602 2329 ▶ SvGETMAGIC(source);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(source); 0 2329 2329
SvGETMAGIC(source); 0 0 0
1603 2329 ▶ if(!SvOK(source)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(source)) { 0 2329 2329
1604 0 croak("Xslate: Template name is not given");
1605 }
1606  
1607 /* $_[2]: template variable */
1608 2329 ▶ if(!SvOK(vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(vars)) { 378 1951 2329
1609 378 vars = sv_2mortal(newRV_noinc((SV*)newHV()));
1610 }
1611 1951 ▶ else if(!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV)) {
Branch: 2 branches
BranchTrueFalseTotal
else if(!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV)) { 1951 0 1951
else if(!(SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV)) { 1 1950 1951
1612 1 croak("Xslate: Template variables must be a HASH reference, not %s",
1613 tx_neat(aTHX_ vars));
1614 }
1615 2328 ▶ if(SvOBJECT(SvRV(vars))) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvOBJECT(SvRV(vars))) { 0 2328 2328
1616 0 Perl_warner(aTHX_ packWARN(WARN_MISC),
1617 "Xslate: Template variables must be a HASH reference, not %s",
1618 tx_neat(aTHX_ vars));
1619 }
1620  
1621 2328 st = tx_load_template(aTHX_ self, source, FALSE);
1622  
1623 /* local $SIG{__WARN__} = \&warn_handler */
1624 2322 ▶ if (PL_warnhook != MY_CXT.warn_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_warnhook != MY_CXT.warn_handler) { 2320 2 2322
1625 2320 SAVEGENERICSV(PL_warnhook);
1626 2320 MY_CXT.orig_warn_handler = PL_warnhook;
1627 2320 PL_warnhook = SvREFCNT_inc_NN(MY_CXT.warn_handler);
1628 }
1629  
1630 /* local $SIG{__DIE__} = \&die_handler */
1631 2322 ▶ if (PL_diehook != MY_CXT.die_handler) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_diehook != MY_CXT.die_handler) { 2320 2 2322
1632 2320 SAVEGENERICSV(PL_diehook);
1633 2320 MY_CXT.orig_die_handler = PL_diehook;
1634 2320 PL_diehook = SvREFCNT_inc_NN(MY_CXT.die_handler);
1635 }
1636  
1637 {
1638 2322 AV* mainframe = tx_push_frame(aTHX_ st); // frame[0]
1639 2322 SV* result = sv_newmortal();
1640 2322 sv_grow(result, st->hint_size + TX_HINT_SIZE);
1641 2322 SvPOK_on(result);
1642  
1643 2322 av_store(mainframe, TXframe_NAME, SvREFCNT_inc_simple_NN(source));
1644 2322 av_store(mainframe, TXframe_RETADDR, newSVuv(st->code_len));
1645 2322 tx_execute(aTHX_ aMY_CXT_ st, result, (HV*)SvRV(vars));
1646 2297 ST(0) = result;
1647 }
1648 }
1649  
1650 void
1651 validate(SV* self, SV* source)
1652 CODE:
1653 {
1654 4 TAINT_NOT; /* All the SVs we'll create here are safe */
1655  
1656 /* $_[0]: engine */
1657 4 ▶ if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) {
Branch: 2 branches
BranchTrueFalseTotal
if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) { 4 0 4
if(!(SvROK(self) && SvTYPE(SvRV(self)) == SVt_PVHV)) { 0 4 4
1658 0 croak("Xslate: Invalid xslate instance: %s",
1659 tx_neat(aTHX_ self));
1660 }
1661  
1662 4 ▶ SvGETMAGIC(source);
Branch: 2 branches
BranchTrueFalseTotal
SvGETMAGIC(source); 0 4 4
SvGETMAGIC(source); 0 0 0
1663 4 ▶ if(!SvOK(source)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(source)) { 0 4 4
1664 0 croak("Xslate: Template name is not given");
1665 }
1666  
1667 4 tx_load_template(aTHX_ self, source, FALSE);
1668 }
1669  
1670 void
1671 current_engine(klass)
1672 CODE:
1673 {
1674 dMY_CXT;
1675 11 tx_state_t* const st = MY_CXT.current_st;
1676 SV* retval;
1677 11 ▶ if(st) {
Branch: 1 branch
BranchTrueFalseTotal
if(st) { 7 4 11
1678 7 ▶ if(ix == 0) { /* current_engine */
Branch: 1 branch
BranchTrueFalseTotal
if(ix == 0) { /* current_engine */ 1 6 7
1679 1 retval = st->engine;
1680 }
1681 6 ▶ else if(ix == 1) { /* current_vars */
Branch: 1 branch
BranchTrueFalseTotal
else if(ix == 1) { /* current_vars */ 2 4 6
1682 2 retval = sv_2mortal(newRV_inc((SV*)st->vars));
1683 }
1684 else { /* current_file / current_line */
1685 4 const tx_info_t* const info
1686 4 = &(st->info[ TX_PC2POS(st, st->pc) ]);
1687  
1688 4 retval = (ix == 2)
1689 ? info->file
1690 4 ▶ : sv_2mortal(newSViv(info->line));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSViv(info->line)); 1 3 4
1691 }
1692 }
1693 else {
1694 4 retval = &PL_sv_undef;
1695 }
1696 11 ST(0) = retval;
1697 }
1698 ALIAS:
1699 current_engine = 0
1700 current_vars = 1
1701 current_file = 2
1702 current_line = 3
1703  
1704 void
1705 print(klass, ...)
1706 CODE:
1707 {
1708 dMY_CXT;
1709 int i;
1710 8 tx_state_t* const st = MY_CXT.current_st;
1711 8 ▶ if(!st) {
Branch: 1 branch
BranchTrueFalseTotal
if(!st) { 0 8 8
1712 0 croak("You cannot call print() method outside render()");
1713 }
1714  
1715 19 ▶ for(i = 1; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for(i = 1; i < items; i++) { 11 8 19
1716 11 tx_print(aTHX_ st, ST(i));
1717 }
1718 8 XSRETURN_NO; /* return false as an empty string */
1719 }
1720  
1721 void
1722 _warn(SV* msg)
1723 ALIAS:
1724 _warn = 0
1725 _die = 1
1726 CODE:
1727 {
1728 dMY_CXT;
1729 105 tx_state_t* const st = MY_CXT.current_st;
1730 SV* engine;
1731 AV* cframe;
1732 SV* name;
1733 SV* full_message;
1734 SV** svp;
1735 CV* handler;
1736 UV pc_pos;
1737 SV* file;
1738  
1739  
1740 /* restore error handlers to avoid recursion */
1741 105 SAVESPTR(PL_warnhook);
1742 105 SAVESPTR(PL_diehook);
1743 105 PL_warnhook = MY_CXT.orig_warn_handler;
1744 105 PL_diehook = MY_CXT.orig_die_handler;
1745 105 msg = sv_mortalcopy(msg);
1746  
1747 105 ▶ if(!st) {
Branch: 1 branch
BranchTrueFalseTotal
if(!st) { 5 100 105
1748 5 croak("%"SVf, msg);
1749 }
1750  
1751  
1752 100 engine = st->engine;
1753 100 cframe = TX_current_framex(st);
1754 100 name = AvARRAY(cframe)[TXframe_NAME];
1755  
1756 100 svp = (ix == 0)
1757 65 ? hv_fetchs((HV*)SvRV(engine), "warn_handler", FALSE)
1758 100 ▶ : hv_fetchs((HV*)SvRV(engine), "die_handler", FALSE);
Branch: 1 branch
BranchTrueFalseTotal
: hv_fetchs((HV*)SvRV(engine), "die_handler", FALSE); 65 35 100
1759  
1760 159 ▶ if(svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if(svp && SvOK(*svp)) { 59 41 100
if(svp && SvOK(*svp)) { 59 0 59
1761 HV* stash;
1762 GV* gv;
1763 59 handler = sv_2cv(*svp, &stash, &gv, 0);
1764 }
1765 else {
1766 41 handler = NULL;
1767 }
1768  
1769 100 pc_pos = TX_PC2POS(st, st->pc);
1770 100 file = st->info[ pc_pos ].file;
1771 100 ▶ if(strEQ(SvPV_nolen_const(file), "<string>")) {
Branch: 1 branch
BranchTrueFalseTotal
if(strEQ(SvPV_nolen_const(file), "<string>")) { 90 10 100
1772 90 svp = hv_fetchs((HV*)SvRV(engine), "string_buffer", FALSE);
1773 90 ▶ if(svp) {
Branch: 1 branch
BranchTrueFalseTotal
if(svp) { 80 10 90
1774 80 file = sv_2mortal(newRV_inc(*svp));
1775 }
1776 }
1777 /* TODO: append the stack info to msg */
1778 /* $full_message = make_error(engine, msg, file, line, vm_pos) */
1779 100 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 100 100
1780 100 ▶ EXTEND(SP, 6);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 6); 0 100 100
1781 100 PUSHs(sv_mortalcopy(engine)); /* XXX: avoid premature free */
1782 100 PUSHs(msg);
1783 100 PUSHs(file);
1784 100 mPUSHi(st->info[ pc_pos ].line);
1785 100 ▶ if(tx_verbose(aTHX_ st) >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if(tx_verbose(aTHX_ st) >= 3) { 0 100 100
1786 0 ▶ if(!SvOK(name)) { // FIXME: something's wrong
Branch: 1 branch
BranchTrueFalseTotal
if(!SvOK(name)) { // FIXME: something's wrong 0 0 0
1787 0 name = newSVpvs_flags("(oops)", SVs_TEMP);
1788 }
1789 0 mPUSHs(newSVpvf("&%"SVf"[%"UVuf"]", name, pc_pos));
1790 }
1791 100 PUTBACK;
1792 100 call_sv(MY_CXT.make_error, G_SCALAR);
1793 100 SPAGAIN;
1794 100 full_message = POPs;
1795 100 PUTBACK;
1796  
1797 100 ▶ if(ix == 0) { /* warn */
Branch: 1 branch
BranchTrueFalseTotal
if(ix == 0) { /* warn */ 65 35 100
1798 /* handler can ignore warnings */
1799 65 ▶ if(handler) {
Branch: 1 branch
BranchTrueFalseTotal
if(handler) { 58 7 65
1800 58 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 58 58
1801 58 ▶ XPUSHs(full_message);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(full_message); 0 58 58
1802 58 PUTBACK;
1803 58 call_sv((SV*)handler, G_VOID | G_DISCARD);
1804 /* handler can ignore errors */
1805 }
1806 else {
1807 7 warn("%"SVf, full_message);
1808 }
1809 }
1810 else {
1811 35 ▶ if(handler) {
Branch: 1 branch
BranchTrueFalseTotal
if(handler) { 1 34 35
1812 1 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1 1
1813 1 ▶ XPUSHs(full_message);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(full_message); 0 1 1
1814 1 PUTBACK;
1815 1 call_sv((SV*)handler, G_VOID | G_DISCARD);
1816 /* handler cannot ignore errors */
1817 }
1818 35 croak("%"SVf, full_message); /* must die */
1819 /* not reached */
1820 }
1821 }
1822  
1823 MODULE = Text::Xslate PACKAGE = Text::Xslate::Util
1824  
1825 void
1826 mark_raw(SV* str)
1827 CODE:
1828 {
1829 58 ST(0) = tx_mark_raw(aTHX_ str);
1830 }
1831  
1832 void
1833 unmark_raw(SV* str)
1834 CODE:
1835 {
1836 5 ST(0) = tx_unmark_raw(aTHX_ str);
1837 }
1838  
1839  
1840 void
1841 html_escape(SV* str)
1842 CODE:
1843 {
1844 14 ST(0) = tx_html_escape(aTHX_ str);
1845 }
1846  
1847 void
1848 uri_escape(SV* str)
1849 CODE:
1850 {
1851 2144 ST(0) = tx_uri_escape(aTHX_ str);
1852 }
1853  
1854 void
1855 is_array_ref(SV* sv)
1856 CODE:
1857 {
1858 1 ▶ ST(0) = boolSV( tx_sv_is_array_ref(aTHX_ sv));
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV( tx_sv_is_array_ref(aTHX_ sv)); 1 0 1
1859 }
1860  
1861 void
1862 is_hash_ref(SV* sv)
1863 CODE:
1864 {
1865 1 ▶ ST(0) = boolSV( tx_sv_is_hash_ref(aTHX_ sv));
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV( tx_sv_is_hash_ref(aTHX_ sv)); 1 0 1
1866 }
1867  
1868 void
1869 is_code_ref(SV* sv)
1870 CODE:
1871 {
1872 0 ▶ ST(0) = boolSV( tx_sv_is_code_ref(aTHX_ sv));
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = boolSV( tx_sv_is_code_ref(aTHX_ sv)); 0 0 0
1873 }
1874  
1875 void
1876 merge_hash(SV* base, SV* value)
1877 CODE:
1878 {
1879 0 ST(0) = tx_merge_hash(aTHX_ NULL, base, value);
1880 }
1881  
1882 MODULE = Text::Xslate PACKAGE = Text::Xslate::Type::Raw
1883  
1884 BOOT:
1885 {
1886 SV* as_string;
1887 /* overload stuff */
1888 188 PL_amagic_generation++;
1889 188 sv_setsv(
1890 get_sv( TX_RAW_CLASS "::()", TRUE ),
1891 &PL_sv_yes
1892 );
1893 188 (void)newXS( TX_RAW_CLASS "::()",
1894 XS_Text__Xslate__fallback, file);
1895  
1896 /* *{'(""'} = \&as_string */
1897 188 as_string = sv_2mortal(newRV_inc(
1898 (SV*)get_cv( TX_RAW_CLASS "::as_string", GV_ADD)));
1899 188 sv_setsv_mg(
1900 (SV*)gv_fetchpvs( TX_RAW_CLASS "::(\"\"", GV_ADDMULTI, SVt_PVCV),
1901 as_string);
1902 }
1903  
1904 void
1905 new(SV* klass, SV* str)
1906 CODE:
1907 {
1908 10 ▶ if(SvROK(klass)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvROK(klass)) { 1 9 10
1909 1 croak("You cannot call %s->new() as an instance method", TX_RAW_CLASS);
1910 }
1911 9 ▶ if(strNE(SvPV_nolen_const(klass), TX_RAW_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if(strNE(SvPV_nolen_const(klass), TX_RAW_CLASS)) { 1 8 9
1912 1 croak("You cannot extend %s", TX_RAW_CLASS);
1913 }
1914 8 ST(0) = tx_mark_raw(aTHX_ tx_unmark_raw(aTHX_ str));
1915 }
1916  
1917 void
1918 as_string(SV* self, ...)
1919 CODE:
1920 {
1921 752 ▶ if(!SvROK(self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(self)) { 1 751 752
1922 1 croak("You cannot call %s->as_string() as a class method", TX_RAW_CLASS);
1923 }
1924 751 ST(0) = tx_unmark_raw(aTHX_ self);
1925 }
1926  
1927 MODULE = Text::Xslate PACKAGE = Text::Xslate::Type::Macro
1928  
1929  
1930 BOOT:
1931 {
1932 SV* code_ref;
1933 /* overload stuff */
1934 188 PL_amagic_generation++;
1935 188 sv_setsv(
1936 get_sv( TX_MACRO_CLASS "::()", TRUE ),
1937 &PL_sv_yes
1938 );
1939 188 (void)newXS( TX_MACRO_CLASS "::()",
1940 XS_Text__Xslate__fallback, file);
1941  
1942 /* *{'(&{}'} = \&as_code_ref */
1943 188 code_ref = sv_2mortal(newRV_inc((SV*)get_cv( TX_MACRO_CLASS "::as_code_ref", GV_ADD)));
1944 188 sv_setsv_mg(
1945 (SV*)gv_fetchpvs( TX_MACRO_CLASS "::(&{}", GV_ADDMULTI, SVt_PVCV),
1946 code_ref);
1947  
1948 // debug flag
1949 188 code_ref = sv_2mortal(newRV_inc((SV*)get_cv( "Text::Xslate::Engine::_DUMP_LOAD", GV_ADD)));
1950 {
1951 188 dSP;
1952 188 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 188 188
1953 188 call_sv(code_ref, G_SCALAR);
1954 188 SPAGAIN;
1955 188 dump_load = sv_true(POPs);
1956 188 PUTBACK;
1957 }
1958 }
1959  
1960 CV*
1961 as_code_ref(SV* self, ...)
1962 CODE:
1963 {
1964 /* the macro object is responsible to its xsub's refcount */
1965 MAGIC* mg;
1966 CV* xsub;
1967  
1968 604 ▶ if(!tx_sv_is_macro(aTHX_ self)) {
Branch: 1 branch
BranchTrueFalseTotal
if(!tx_sv_is_macro(aTHX_ self)) { 0 604 604
1969 0 croak("Not a macro object: %s", tx_neat(aTHX_ self));
1970 }
1971  
1972 604 mg = mgx_find(aTHX_ SvRV(self), &macro_vtbl);
1973 604 ▶ if(!mg) {
Branch: 1 branch
BranchTrueFalseTotal
if(!mg) { 7 597 604
1974 7 xsub = newXS(NULL, XS_Text__Xslate__macrocall, __FILE__);
1975 7 sv_magicext(SvRV(self), (SV*)xsub, PERL_MAGIC_ext, &macro_vtbl,
1976 NULL, 0);
1977 7 SvREFCNT_dec(xsub); /* refcnt++ in sv_magicext */
1978 7 CvXSUBANY(xsub).any_ptr = (void*)self;
1979 }
1980 else {
1981 597 xsub = (CV*)mg->mg_obj;
1982 assert(xsub);
1983 assert(SvTYPE(xsub) == SVt_PVCV);
1984 }
1985 604 RETVAL = xsub;
1986 }
1987 OUTPUT:
1988 RETVAL
« blib/lib/Text/Xslate/Symbol.pm ↑ Summary src/xslate_opcode.inc »