Skip to content

Commit

Permalink
Fix variable scope bug #12
Browse files Browse the repository at this point in the history
This bug was being caused by a faulty optimization. This fix disables the
optimization.
  • Loading branch information
henrycatalinismith committed Dec 30, 2013
1 parent 4c41625 commit 0e601db
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 69 deletions.
58 changes: 58 additions & 0 deletions src-js/templates/js/scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @fileoverview Compiled template for file
*
* scope.twig
*
* @suppress {checkTypes|fileoverviewTags}
*/

goog.provide('scope');

goog.require('twig');
goog.require('twig.filter');

/**
* @constructor
* @param {twig.Environment} env
* @extends {twig.Template}
*/
scope = function(env) {
twig.Template.call(this, env);
};
twig.inherits(scope, twig.Template);

/**
* @inheritDoc
*/
scope.prototype.getParent_ = function(context) {
return false;
};

/**
* @inheritDoc
*/
scope.prototype.render_ = function(sb, context, blocks) {
// line 1
context["example"] = 12345;
if (false) {
sb.append(twig.filter.escape(this.env_, ("example" in context ? context["example"] : null), "html", null, true));
}
sb.append(3);
sb.append("\n");
};

/**
* @inheritDoc
*/
scope.prototype.getTemplateName = function() {
return "scope";
};

/**
* Returns whether this template can be used as trait.
*
* @return {boolean}
*/
scope.prototype.isTraitable = function() {
return false;
};
4 changes: 2 additions & 2 deletions src-js/templates/js/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ welcome.prototype.getParent_ = function(context) {
*/
welcome.prototype.render_ = function(sb, context, blocks) {
// line 3
context["__internal_956a77761d743f7d7b806e9cb74f4177058ae947140188bc65d2d53343b99c2a"] = this.env_.createTemplate(macros);
context["__internal_d792bee1591572ed53d391dc81bc8e853002aa32"] = this.env_.createTemplate(macros);
this.getParent(context).render_(sb, context, twig.extend({}, this.getBlocks(), blocks));
};

Expand Down Expand Up @@ -85,7 +85,7 @@ welcome.prototype.block_body = function(sb, context, blocks) {
sb.append("Some Status.");
} else {
// line 19
sb.append(this.callMacro(context["__internal_956a77761d743f7d7b806e9cb74f4177058ae947140188bc65d2d53343b99c2a"], "link", ["\/login", "Please login.", "Login"]));
sb.append(context["__internal_d792bee1591572ed53d391dc81bc8e853002aa32"].getlink("\/login", "Please login.", "Login"));
}
// line 21
sb.append("<\/p>\n\n");
Expand Down
7 changes: 2 additions & 5 deletions src-js/templates/php/ajax_layout.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* ajax_layout.twig */
class __TwigTemplate_3505873dec53fd303f4d4f58d431be039042116fbc0cd08f14bbdfe9db8e92d7 extends Twig_Template
class __TwigTemplate_17a83cebc5fd369a7dfafd83b675de7f extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -12,9 +12,6 @@ public function __construct(Twig_Environment $env)
$this->blocks = array(
'body' => array($this, 'block_body'),
);

$this->macros = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
Expand All @@ -34,6 +31,6 @@ public function getTemplateName()

public function getDebugInfo()
{
return array ( 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 20 => 1,);
}
}
13 changes: 5 additions & 8 deletions src-js/templates/php/counter.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* counter.twig */
class __TwigTemplate_93979ac3b3749ca27d0a5899f5a762b994df1827cd11a59c71d5f537341bac40 extends Twig_Template
class __TwigTemplate_743499044622a97adcf22fa3019d9633 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -11,9 +11,6 @@ public function __construct(Twig_Environment $env)

$this->blocks = array(
);

$this->macros = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
Expand Down Expand Up @@ -46,9 +43,9 @@ protected function doDisplay(array $context, array $blocks = array())
}
$_parent = $context['_parent'];
unset($context['_seq'], $context['_iterated'], $context['_key'], $context['j'], $context['_parent'], $context['loop']);
$context = array_intersect_key($context, $_parent) + $_parent;
$context = array_merge($_parent, array_intersect_key($context, $_parent));
// line 8
if ((!twig_template_get_attributes($this, (isset($context["loop"]) ? $context["loop"] : null), "last"))) {
if ((!$this->getAttribute((isset($context["loop"]) ? $context["loop"] : null), "last"))) {
echo ", ";
}
++$context['loop']['index0'];
Expand All @@ -62,7 +59,7 @@ protected function doDisplay(array $context, array $blocks = array())
}
$_parent = $context['_parent'];
unset($context['_seq'], $context['_iterated'], $context['_key'], $context['i'], $context['_parent'], $context['loop']);
$context = array_intersect_key($context, $_parent) + $_parent;
$context = array_merge($_parent, array_intersect_key($context, $_parent));
}

public function getTemplateName()
Expand All @@ -77,6 +74,6 @@ public function isTraitable()

public function getDebugInfo()
{
return array ( 55 => 4, 49 => 3, 45 => 5, 32 => 1, 75 => 21, 72 => 19, 69 => 17, 67 => 16, 62 => 13, 59 => 12, 56 => 11, 47 => 6, 44 => 5, 39 => 2, 12 => 2, 22 => 1, 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 8, 46 => 14, 43 => 11, 41 => 4, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 48 => 8, 42 => 5, 38 => 4, 36 => 2, 19 => 1, 20 => 1,);
}
}
7 changes: 2 additions & 5 deletions src-js/templates/php/hello_world.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* hello_world.twig */
class __TwigTemplate_2e2dc4863a0f81d6834be9b5418092566e877cdcf1d9713034648c7ad19e02cb extends Twig_Template
class __TwigTemplate_97a90f689cd5466a596b47262f283de8 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -11,9 +11,6 @@ public function __construct(Twig_Environment $env)

$this->blocks = array(
);

$this->macros = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
Expand All @@ -36,6 +33,6 @@ public function isTraitable()

public function getDebugInfo()
{
return array ( 22 => 1, 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 48 => 8, 42 => 5, 38 => 4, 36 => 2, 19 => 1, 20 => 1,);
}
}
7 changes: 2 additions & 5 deletions src-js/templates/php/layout.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* layout.twig */
class __TwigTemplate_9aaa986cffef21138e1fce33237ac67366b91ca80b38d7de01ca7f0ccbbf1ebc extends Twig_Template
class __TwigTemplate_1e4995ca60952324c5f709f7bf7ad63b extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -15,9 +15,6 @@ public function __construct(Twig_Environment $env)
'body' => array($this, 'block_body'),
'javascripts' => array($this, 'block_javascripts'),
);

$this->macros = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
Expand Down Expand Up @@ -81,6 +78,6 @@ public function getTemplateName()

public function getDebugInfo()
{
return array ( 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 70 => 12, 67 => 11, 62 => 10, 57 => 6, 54 => 5, 43 => 14, 40 => 11, 34 => 8, 32 => 5, 28 => 4, 23 => 1, 48 => 4, 42 => 5, 38 => 10, 36 => 2, 19 => 1, 20 => 1,);
}
}
17 changes: 3 additions & 14 deletions src-js/templates/php/macros.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* macros.twig */
class __TwigTemplate_46b00b74f2e8d8d93b97e0c7d1c90047dd5621efe0a2a005eebccd51dc7fe233 extends Twig_Template
class __TwigTemplate_58836c3cfbf133af2f75655f7e89638c extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -11,25 +11,14 @@ public function __construct(Twig_Environment $env)

$this->blocks = array(
);

$this->macros = array(
"link" => array(
'method' => "getLink",
'arguments' => array(
"link" => null,
"content" => null,
"title" => null,
),
),
);
}

protected function doDisplay(array $context, array $blocks = array())
{
}

// line 1
public function getLink($_link = null, $_content = null, $_title = null)
public function getlink($_link = null, $_content = null, $_title = null)
{
$context = $this->env->mergeGlobals(array(
"link" => $_link,
Expand Down Expand Up @@ -77,6 +66,6 @@ public function isTraitable()

public function getDebugInfo()
{
return array ( 55 => 4, 49 => 3, 45 => 2, 32 => 1, 75 => 21, 72 => 19, 69 => 17, 67 => 16, 62 => 13, 59 => 12, 56 => 11, 47 => 6, 44 => 5, 39 => 3, 12 => 2, 22 => 1, 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 44 => 4, 21 => 1, 70 => 12, 67 => 11, 62 => 10, 57 => 6, 54 => 5, 43 => 14, 40 => 11, 34 => 2, 32 => 5, 28 => 4, 23 => 1, 48 => 4, 42 => 5, 38 => 3, 36 => 2, 19 => 1, 20 => 1,);
}
}
42 changes: 42 additions & 0 deletions src-js/templates/php/scope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/* scope.twig */
class __TwigTemplate_6c16a53e9ecd87c1c4276ac1be26ad50 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
parent::__construct($env);

$this->parent = false;

$this->blocks = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
{
// line 1
$context["example"] = 12345;
if (false) {
echo twig_escape_filter($this->env, (isset($context["example"]) ? $context["example"] : null), "html", null, true);
}
echo 3;
echo "
";
}

public function getTemplateName()
{
return "scope.twig";
}

public function isTraitable()
{
return false;
}

public function getDebugInfo()
{
return array ( 44 => 4, 21 => 1, 70 => 12, 67 => 11, 62 => 10, 57 => 6, 54 => 5, 43 => 14, 40 => 11, 34 => 2, 32 => 5, 28 => 4, 23 => 1, 48 => 4, 42 => 5, 38 => 3, 36 => 2, 19 => 1, 20 => 1,);
}
}
7 changes: 2 additions & 5 deletions src-js/templates/php/traitable.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* traitable.twig */
class __TwigTemplate_8eccb8b18c8f1c60507303b1226eb981e38ebc1bc4aab0c165f8478d6586965a extends Twig_Template
class __TwigTemplate_121df410c96ee16ccddd9c869f4f71e1 extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -12,9 +12,6 @@ public function __construct(Twig_Environment $env)
$this->blocks = array(
'title' => array($this, 'block_title'),
);

$this->macros = array(
);
}

protected function doDisplay(array $context, array $blocks = array())
Expand All @@ -35,6 +32,6 @@ public function getTemplateName()

public function getDebugInfo()
{
return array ( 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 44 => 4, 21 => 1, 70 => 12, 67 => 11, 62 => 10, 57 => 6, 54 => 5, 43 => 14, 40 => 11, 34 => 2, 32 => 5, 28 => 4, 23 => 1, 48 => 4, 42 => 5, 38 => 3, 36 => 2, 19 => 1, 20 => 1,);
}
}
11 changes: 4 additions & 7 deletions src-js/templates/php/welcome.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/* welcome.twig */
class __TwigTemplate_a8c7a5e9a8e2922fac4a1c87b11dcb2b5b6d4824e43c915c29add216509528d0 extends Twig_Template
class __TwigTemplate_0d01cbbae4db7bbbe92b7eea47d5b5ff extends Twig_Template
{
public function __construct(Twig_Environment $env)
{
Expand All @@ -23,9 +23,6 @@ public function __construct(Twig_Environment $env)
'body' => array($this, 'block_body'),
)
);

$this->macros = array(
);
}

protected function doGetParent(array $context)
Expand All @@ -36,7 +33,7 @@ protected function doGetParent(array $context)
protected function doDisplay(array $context, array $blocks = array())
{
// line 3
$context["__internal_4959edb76f68f86a4f4dad86d32f84c5a2e579fdd62090890e3ccbc17254c81e"] = $this->env->loadTemplate("macros.twig");
$context["__internal_5eb248d32c980bfa7dcb3a198af790d6606f7848"] = $this->env->loadTemplate("macros.twig");
$this->getParent($context)->display($context, array_merge($this->blocks, $blocks));
}

Expand Down Expand Up @@ -69,7 +66,7 @@ public function block_body($context, array $blocks = array())
echo "Some Status.";
} else {
// line 19
echo $this->callMacro($context["__internal_4959edb76f68f86a4f4dad86d32f84c5a2e579fdd62090890e3ccbc17254c81e"], "link", array(0 => "/login", 1 => "Please login.", 2 => "Login"));
echo $context["__internal_5eb248d32c980bfa7dcb3a198af790d6606f7848"]->getlink("/login", "Please login.", "Login");
}
// line 21
echo "</p>
Expand All @@ -89,6 +86,6 @@ public function isTraitable()

public function getDebugInfo()
{
return array ( 75 => 21, 72 => 19, 69 => 17, 67 => 16, 62 => 13, 59 => 12, 56 => 11, 47 => 6, 44 => 5, 39 => 3, 12 => 2, 22 => 1, 23 => 1, 73 => 12, 70 => 11, 65 => 10, 60 => 6, 57 => 5, 51 => 4, 46 => 14, 43 => 11, 41 => 10, 37 => 8, 35 => 5, 31 => 4, 26 => 1,);
return array ( 72 => 21, 69 => 19, 66 => 17, 64 => 16, 59 => 13, 56 => 12, 53 => 11, 41 => 5, 12 => 2, 44 => 6, 21 => 1, 70 => 12, 67 => 11, 62 => 10, 57 => 6, 54 => 5, 43 => 14, 40 => 11, 34 => 2, 32 => 5, 28 => 4, 23 => 1, 48 => 4, 42 => 5, 38 => 3, 36 => 3, 19 => 1, 20 => 1,);
}
}
1 change: 1 addition & 0 deletions src-js/templates/twig/scope.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% set example = 12345 %}{% if false %}{{ example }}{% endif %}{{ example }}
14 changes: 13 additions & 1 deletion src/TwigJs/Compiler/Expression/TempNameCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
return;
}

$compiler->raw('tmp_')->raw($name);
/*
Contrary to the name of this class, this code no longer compiles the
name of the node to a temporary variable name. Because of the troubles
resulting from this optimization reported in schmittjoh/twig.js#12, this
has been replaced with the slightly less performant but more reliable
NameCompiler output which reads the variable directly from the context.
*/
$compiler
->string($name)
->raw(' in context ? context[')
->string($name)
->raw('] : null')
;
}
}

0 comments on commit 0e601db

Please sign in to comment.