From ba6e3e3da62d0d5d654f163792554cf9da8c7e8e Mon Sep 17 00:00:00 2001 From: Tom Burns Date: Mon, 28 Jul 2014 14:12:11 +0000 Subject: [PATCH] lazily create stacks --- lib/liquid.rb | 1 + lib/liquid/context.rb | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/liquid.rb b/lib/liquid.rb index bb81745a..f196d3bf 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -41,6 +41,7 @@ module Liquid singleton_class.send(:attr_accessor, :cache_classes) self.cache_classes = true + end require "liquid/version" diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index faa3da23..b3bd776e 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -27,6 +27,12 @@ module Liquid @parsed_variables = Hash.new{ |cache, markup| cache[markup] = variable_parse(markup) } squash_instance_assigns_with_environments + @used_stacks = 0 + @unused_stacks = 0 + @max_stack_depth = 0 + @this_stack_depth = 0 + @this_stack_used = false + if rethrow_errors self.exception_handler = ->(e) { true } end @@ -134,10 +140,13 @@ module Liquid # # context['var] #=> nil def stack(new_scope={}) - push(new_scope) + old_stack_used = @this_stack_used + @this_stack_used = (new_scope != {}) + push(new_scope) if @this_stack_used yield ensure - pop + pop if @this_stack_used + @this_stack_used = old_stack_used end def clear_instance_assigns @@ -146,6 +155,10 @@ module Liquid # Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop def []=(key, value) + if !@this_stack_used + @this_stack_used = true + push({}) + end @scopes[0][key] = value end