Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Angell
6fd5e4e6ce Use same approach in scope and environment 2019-08-28 23:35:14 +10:00
Mike Angell
56c667d8f9 Simplify context and scopes 2019-08-28 18:00:37 +10:00
2 changed files with 16 additions and 54 deletions

View File

@@ -20,21 +20,17 @@ module Liquid
@scopes = [(outer_scope || {})]
@registers = registers
@errors = []
@interrupts = []
@filters = []
@global_filter = nil
@partial = false
@strict_variables = false
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
squash_instance_assigns_with_environments
@this_stack_used = false
self.exception_renderer = Template.default_exception_renderer
if rethrow_errors
self.exception_renderer = ->(e) { raise }
end
self.exception_renderer = ->(e) { raise } if rethrow_errors
@interrupts = []
@filters = []
@global_filter = nil
squash_instance_assigns_with_environments
end
def warnings
@@ -87,9 +83,9 @@ module Liquid
end
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
def push(new_scope = {})
@scopes.unshift(new_scope)
raise StackLevelError, "Nesting too deep".freeze if @scopes.length > Block::MAX_DEPTH
def push
@scopes.unshift({})
raise StackLevelError, "Nesting too deep".freeze if @scopes.length > (Block::MAX_DEPTH + 1)
end
# Merge a hash of variables in the current local scope
@@ -111,31 +107,15 @@ module Liquid
# end
#
# context['var] #=> nil
def stack(new_scope = nil)
old_stack_used = @this_stack_used
if new_scope
push(new_scope)
@this_stack_used = true
else
@this_stack_used = false
end
def stack
push
yield
ensure
pop if @this_stack_used
@this_stack_used = old_stack_used
end
def clear_instance_assigns
@scopes[0] = {}
pop
end
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
def []=(key, value)
unless @this_stack_used
@this_stack_used = true
push({})
end
@scopes[0][key] = value
end
@@ -163,27 +143,12 @@ module Liquid
def find_variable(key, raise_on_not_found: true)
# This was changed from find() to find_index() because this is a very hot
# path and find_index() is optimized in MRI to reduce object allocation
index = @scopes.find_index { |s| s.key?(key) }
scope = @scopes[index] if index
variable = nil
scope = (index = @scopes.find_index { |s| s.key?(key) }) && @scopes[index]
scope ||= (index = @environments.find_index { |s| s.key?(key) }) && @environments[index]
scope ||= {}
if scope.nil?
@environments.each do |e|
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
# When lookup returned a value OR there is no value but the lookup also did not raise
# then it is the value we are looking for.
if !variable.nil? || @strict_variables && raise_on_not_found
scope = e
break
end
end
end
scope ||= @environments.last || @scopes.last
variable ||= lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found)
variable = variable.to_liquid
variable = lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found).to_liquid
variable.context = self if variable.respond_to?(:context=)
variable

View File

@@ -80,10 +80,7 @@ class VariableTest < Minitest::Test
assigns['test'] = 'Tobi'
assert_equal 'Hello Tobi', template.render!(assigns)
assigns.delete('test')
e = assert_raises(RuntimeError) do
template.render!(assigns)
end
assert_equal "Unknown variable 'test'", e.message
assert_equal "Hello ", template.render!(assigns)
end
def test_multiline_variable