Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

fix problem: can't access global vars at <:include 'x.tx' $has :>. #40

Closed
wants to merge 3 commits into from

1 participant

@hanabokuro
Collaborator

<:inlucde 'x.tx' { a => 1 }> can access global vars.
but <: include 'x.tx' { $hash }> can't access gloval vars.

@hanabokuro
Collaborator

there is typo and memory error in my patch.
i fixed that. hanabokuro@d7693d2

@hanabokuro hanabokuro closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 lib/Text/Xslate/PP/Opcode.pm
@@ -80,12 +80,9 @@ sub op_localize_vars {
$st->warn(undef, "Variable map must be a HASH reference");
}
- push @{ $st->{local_stack} }, bless sub {
- $st->vars($old_vars);
- return;
- }, 'Text::Xslate::PP::Guard';
-
- $st->vars($new_vars);
+ foreach my $key (keys %{ $new_vars }){
+ $st->localize($key, $new_vars->{$key});
+ }
goto $st->{ code }->[ ++$st->{ pc } ]->{ exec_code };
}
View
4 t/030_kolon/035_include_w_vars.t 100644 → 100755
@@ -27,6 +27,10 @@ is $tx->render_string(':include "bar" { $baz }; $lang',
{ baz => { lang => 'Xslate' }, lang => '!!' }),
'Hello, Xslate world!!!';
+is $tx->render_string(':include "bar" { $baz }; $lang',
+ { baz => { }, lang => '!!' }),
+ 'Hello, !! world!!!';
+
eval { $tx->render_string(': include "foo" { a => 42, "b" }' ) };
like $@, qr/pairs/;
View
13 xs/xslate_opcode.inc
@@ -64,6 +64,8 @@ TXC_w_key(localize_s) {
/* local $engine->{vars} = expr; */
TXC(localize_vars) {
SV* vars = sv_mortalcopy(TX_st_sa);
+ HV* original_vars = TX_st->vars;
+ HE *entry;
if(!( SvROK(vars) && SvTYPE(SvRV(vars)) == SVt_PVHV
&& !SvOBJECT(SvRV(vars)) )) {
@@ -75,6 +77,17 @@ TXC(localize_vars) {
save_hptr(&(TX_st->vars));
TX_st->vars = (HV*)SvRV(vars);
+ hv_iterinit(original_vars);
+ while((entry = hv_iternext(original_vars)) != NULL){
+ SV *key = hv_iterkeysv(entry);
+ if(! hv_exists_ent(TX_st->vars, key, 0U)){
+ HE *he = hv_fetch_ent(original_vars, key, FALSE, 0U);
+ SV *sv = hv_iterval(original_vars, he);
+ SvREFCNT_inc(sv);
+ hv_store_ent(TX_st->vars, key, sv, 0U);
+ }
+ }
+
TX_RETURN_NEXT();
}
Something went wrong with that request. Please try again.