Lazy init context errors and interrupts to prevent excess allocation

This commit is contained in:
Lourens Naudé
2015-05-05 23:40:52 -04:00
parent 174839fbef
commit 27248f1eb1
2 changed files with 10 additions and 6 deletions
+9 -6
View File
@@ -13,14 +13,13 @@ module Liquid
# #
# context['bob'] #=> nil class Context # context['bob'] #=> nil class Context
class Context class Context
attr_reader :scopes, :errors, :registers, :environments, :resource_limits attr_reader :scopes, :registers, :environments, :resource_limits
attr_accessor :exception_handler attr_accessor :exception_handler
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil) def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
@environments = [environments].flatten @environments = [environments].flatten
@scopes = [(outer_scope || {})] @scopes = [(outer_scope || {})]
@registers = registers @registers = registers
@errors = []
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits) @resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
squash_instance_assigns_with_environments squash_instance_assigns_with_environments
@@ -30,10 +29,14 @@ module Liquid
self.exception_handler = ->(e) { true } self.exception_handler = ->(e) { true }
end end
@interrupts = [] @interrupts = nil
@filters = [] @filters = []
end end
def errors
@errors ||= []
end
def strainer def strainer
@strainer ||= Strainer.create(self, @filters) @strainer ||= Strainer.create(self, @filters)
end end
@@ -50,17 +53,17 @@ module Liquid
# are there any not handled interrupts? # are there any not handled interrupts?
def has_interrupt? def has_interrupt?
!@interrupts.empty? @interrupts && !@interrupts.empty?
end end
# push an interrupt to the stack. this interrupt is considered not handled. # push an interrupt to the stack. this interrupt is considered not handled.
def push_interrupt(e) def push_interrupt(e)
@interrupts.push(e) (@interrupts ||= []).push(e)
end end
# pop an interrupt from the stack # pop an interrupt from the stack
def pop_interrupt def pop_interrupt
@interrupts.pop @interrupts.pop if @interrupts
end end
+1
View File
@@ -465,6 +465,7 @@ class ContextUnitTest < Minitest::Test
mock_empty = Spy.on_instance_method(Array, :empty?) mock_empty = Spy.on_instance_method(Array, :empty?)
mock_has_interrupt = Spy.on(@context, :has_interrupt?).and_call_through mock_has_interrupt = Spy.on(@context, :has_interrupt?).and_call_through
@context.push_interrupt(StandardError.new)
@context.has_interrupt? @context.has_interrupt?
refute mock_any.has_been_called? refute mock_any.has_been_called?