Revert "Merge pull request #466 from Shopify/remove-expression-cache"

This reverts commit d9ae36ec40, reversing
changes made to 2da9d49478.
This commit is contained in:
Florian Weingarten
2014-11-07 01:48:51 +00:00
parent 9c33e9601b
commit 475ea51f1f
12 changed files with 123 additions and 127 deletions
+4 -4
View File
@@ -3,7 +3,7 @@ module Liquid
# #
# Example: # Example:
# #
# c = Condition.new(1, '==', 1) # c = Condition.new('1', '==', '1')
# c.evaluate #=> true # c.evaluate #=> true
# #
class Condition #:nodoc: class Condition #:nodoc:
@@ -96,10 +96,10 @@ module Liquid
# If the operator is empty this means that the decision statement is just # If the operator is empty this means that the decision statement is just
# a single variable. We can just poll this variable from the context and # a single variable. We can just poll this variable from the context and
# return this as the result. # return this as the result.
return context.evaluate(left) if op == nil return context[left] if op == nil
left = context.evaluate(left) left = context[left]
right = context.evaluate(right) right = context[right]
operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}")) operation = self.class.operators[op] || raise(Liquid::ArgumentError.new("Unknown operator #{op}"))
+2 -1
View File
@@ -24,6 +24,7 @@ module Liquid
@resource_limits = resource_limits || Template.default_resource_limits.dup @resource_limits = resource_limits || Template.default_resource_limits.dup
@resource_limits[:render_score_current] = 0 @resource_limits[:render_score_current] = 0
@resource_limits[:assign_score_current] = 0 @resource_limits[:assign_score_current] = 0
@parsed_expression = Hash.new{ |cache, markup| cache[markup] = Expression.parse(markup) }
squash_instance_assigns_with_environments squash_instance_assigns_with_environments
@this_stack_used = false @this_stack_used = false
@@ -169,7 +170,7 @@ module Liquid
# Example: # Example:
# products == empty #=> products.empty? # products == empty #=> products.empty?
def [](expression) def [](expression)
evaluate(Expression.parse(expression)) evaluate(@parsed_expression[expression])
end end
def has_key?(key) def has_key?(key)
+1 -1
View File
@@ -12,7 +12,7 @@ module Liquid
class Include < Tag class Include < Tag
def render_with_profiling(context) def render_with_profiling(context)
Profiler.profile_children(context.evaluate(@template_name).to_s) do Profiler.profile_children(@template_name) do
render_without_profiling(context) render_without_profiling(context)
end end
end end
+2 -2
View File
@@ -8,7 +8,7 @@ module Liquid
@blocks = [] @blocks = []
if markup =~ Syntax if markup =~ Syntax
@left = Expression.parse($1) @left = $1
else else
raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze)) raise SyntaxError.new(options[:locale].t("errors.syntax.case".freeze))
end end
@@ -58,7 +58,7 @@ module Liquid
markup = $2 markup = $2
block = Condition.new(@left, '=='.freeze, Expression.parse($1)) block = Condition.new(@left, '=='.freeze, $1)
block.attach(@nodelist) block.attach(@nodelist)
@blocks.push(block) @blocks.push(block)
end end
+5 -5
View File
@@ -20,10 +20,10 @@ module Liquid
case markup case markup
when NamedSyntax when NamedSyntax
@variables = variables_from_string($2) @variables = variables_from_string($2)
@name = Expression.parse($1) @name = $1
when SimpleSyntax when SimpleSyntax
@variables = variables_from_string(markup) @variables = variables_from_string(markup)
@name = @variables.to_s @name = "'#{@variables.to_s}'"
else else
raise SyntaxError.new(options[:locale].t("errors.syntax.cycle".freeze)) raise SyntaxError.new(options[:locale].t("errors.syntax.cycle".freeze))
end end
@@ -33,9 +33,9 @@ module Liquid
context.registers[:cycle] ||= Hash.new(0) context.registers[:cycle] ||= Hash.new(0)
context.stack do context.stack do
key = context.evaluate(@name) key = context[@name]
iteration = context.registers[:cycle][key] iteration = context.registers[:cycle][key]
result = context.evaluate(@variables[iteration]) result = context[@variables[iteration]]
iteration += 1 iteration += 1
iteration = 0 if iteration >= @variables.size iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration context.registers[:cycle][key] = iteration
@@ -48,7 +48,7 @@ module Liquid
def variables_from_string(markup) def variables_from_string(markup)
markup.split(',').collect do |var| markup.split(',').collect do |var|
var =~ /\s*(#{QuotedFragment})\s*/o var =~ /\s*(#{QuotedFragment})\s*/o
$1 ? Expression.parse($1) : nil $1 ? $1 : nil
end.compact end.compact
end end
end end
+13 -25
View File
@@ -68,19 +68,19 @@ module Liquid
def render(context) def render(context)
context.registers[:for] ||= Hash.new(0) context.registers[:for] ||= Hash.new(0)
collection = context.evaluate(@collection_name) collection = context[@collection_name]
collection = collection.to_a if collection.is_a?(Range) collection = collection.to_a if collection.is_a?(Range)
# Maintains Ruby 1.8.7 String#each behaviour on 1.9 # Maintains Ruby 1.8.7 String#each behaviour on 1.9
return render_else(context) unless iterable?(collection) return render_else(context) unless iterable?(collection)
from = if @from == :continue from = if @attributes['offset'.freeze] == 'continue'.freeze
context.registers[:for][@name].to_i context.registers[:for][@name].to_i
else else
context.evaluate(@from).to_i context[@attributes['offset'.freeze]].to_i
end end
limit = context.evaluate(@limit) limit = context[@attributes['limit'.freeze]]
to = limit ? limit.to_i + from : nil to = limit ? limit.to_i + from : nil
segment = Utils.slice_collection(collection, from, to) segment = Utils.slice_collection(collection, from, to)
@@ -128,12 +128,12 @@ module Liquid
def lax_parse(markup) def lax_parse(markup)
if markup =~ Syntax if markup =~ Syntax
@variable_name = $1 @variable_name = $1
collection_name = $2 @collection_name = $2
@name = "#{$1}-#{$2}"
@reversed = $3 @reversed = $3
@name = "#{@variable_name}-#{collection_name}" @attributes = {}
@collection_name = Expression.parse(collection_name)
markup.scan(TagAttributes) do |key, value| markup.scan(TagAttributes) do |key, value|
set_attribute(key, value) @attributes[key] = value
end end
else else
raise SyntaxError.new(options[:locale].t("errors.syntax.for".freeze)) raise SyntaxError.new(options[:locale].t("errors.syntax.for".freeze))
@@ -144,36 +144,24 @@ module Liquid
p = Parser.new(markup) p = Parser.new(markup)
@variable_name = p.consume(:id) @variable_name = p.consume(:id)
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze) raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_in".freeze)) unless p.id?('in'.freeze)
collection_name = p.expression @collection_name = p.expression
@name = "#{@variable_name}-#{collection_name}" @name = "#{@variable_name}-#{@collection_name}"
@collection_name = Expression.parse(collection_name)
@reversed = p.id?('reversed'.freeze) @reversed = p.id?('reversed'.freeze)
@attributes = {}
while p.look(:id) && p.look(:colon, 1) while p.look(:id) && p.look(:colon, 1)
unless attribute = p.id?('limit'.freeze) || p.id?('offset'.freeze) unless attribute = p.id?('limit'.freeze) || p.id?('offset'.freeze)
raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_attribute".freeze)) raise SyntaxError.new(options[:locale].t("errors.syntax.for_invalid_attribute".freeze))
end end
p.consume p.consume
set_attribute(attribute, p.expression) val = p.expression
@attributes[attribute] = val
end end
p.consume(:end_of_string) p.consume(:end_of_string)
end end
private private
def set_attribute(key, expr)
case key
when 'offset'.freeze
@from = if expr == 'continue'.freeze
:continue
else
Expression.parse(expr)
end
when 'limit'.freeze
@limit = Expression.parse(expr)
end
end
def render_else(context) def render_else(context)
return @else_block ? [render_all(@else_block, context)] : ''.freeze return @else_block ? [render_all(@else_block, context)] : ''.freeze
end end
+4 -4
View File
@@ -60,14 +60,14 @@ module Liquid
expressions = markup.scan(ExpressionsAndOperators) expressions = markup.scan(ExpressionsAndOperators)
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop =~ Syntax raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop =~ Syntax
condition = Condition.new(Expression.parse($1), $2, Expression.parse($3)) condition = Condition.new($1, $2, $3)
while not expressions.empty? while not expressions.empty?
operator = expressions.pop.to_s.strip operator = expressions.pop.to_s.strip
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop.to_s =~ Syntax raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless expressions.pop.to_s =~ Syntax
new_condition = Condition.new(Expression.parse($1), $2, Expression.parse($3)) new_condition = Condition.new($1, $2, $3)
raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator) raise(SyntaxError.new(options[:locale].t("errors.syntax.if".freeze))) unless BOOLEAN_OPERATORS.include?(operator)
new_condition.send(operator, condition) new_condition.send(operator, condition)
condition = new_condition condition = new_condition
@@ -92,9 +92,9 @@ module Liquid
end end
def parse_comparison(p) def parse_comparison(p)
a = Expression.parse(p.expression) a = p.expression
if op = p.consume?(:comparison) if op = p.consume?(:comparison)
b = Expression.parse(p.expression) b = p.expression
Condition.new(a, op, b) Condition.new(a, op, b)
else else
Condition.new(a) Condition.new(a)
+11 -14
View File
@@ -22,16 +22,12 @@ module Liquid
if markup =~ Syntax if markup =~ Syntax
template_name = $1 @template_name = $1
variable_name = $3 @variable_name = $3
@variable_name = Expression.parse(variable_name || template_name[1..-2])
@context_variable_name = template_name[1..-2].split('/'.freeze).last
@template_name = Expression.parse(template_name)
@attributes = {} @attributes = {}
markup.scan(TagAttributes) do |key, value| markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value) @attributes[key] = value
end end
else else
@@ -44,20 +40,21 @@ module Liquid
def render(context) def render(context)
partial = load_cached_partial(context) partial = load_cached_partial(context)
variable = context.evaluate(@variable_name) variable = context[@variable_name || @template_name[1..-2]]
context.stack do context.stack do
@attributes.each do |key, value| @attributes.each do |key, value|
context[key] = context.evaluate(value) context[key] = context[value]
end end
context_variable_name = @template_name[1..-2].split('/'.freeze).last
if variable.is_a?(Array) if variable.is_a?(Array)
variable.collect do |var| variable.collect do |var|
context[@context_variable_name] = var context[context_variable_name] = var
partial.render(context) partial.render(context)
end end
else else
context[@context_variable_name] = variable context[context_variable_name] = variable
partial.render(context) partial.render(context)
end end
end end
@@ -66,7 +63,7 @@ module Liquid
private private
def load_cached_partial(context) def load_cached_partial(context)
cached_partials = context.registers[:cached_partials] || {} cached_partials = context.registers[:cached_partials] || {}
template_name = context.evaluate(@template_name) template_name = context[@template_name]
if cached = cached_partials[template_name] if cached = cached_partials[template_name]
return cached return cached
@@ -84,9 +81,9 @@ module Liquid
# make read_template_file call backwards-compatible. # make read_template_file call backwards-compatible.
case file_system.method(:read_template_file).arity case file_system.method(:read_template_file).arity
when 1 when 1
file_system.read_template_file(context.evaluate(@template_name)) file_system.read_template_file(context[@template_name])
when 2 when 2
file_system.read_template_file(context.evaluate(@template_name), context) file_system.read_template_file(context[@template_name], context)
else else
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)" raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
end end
+6 -6
View File
@@ -6,10 +6,10 @@ module Liquid
super super
if markup =~ Syntax if markup =~ Syntax
@variable_name = $1 @variable_name = $1
@collection_name = Expression.parse($2) @collection_name = $2
@attributes = {} @attributes = {}
markup.scan(TagAttributes) do |key, value| markup.scan(TagAttributes) do |key, value|
@attributes[key] = Expression.parse(value) @attributes[key] = value
end end
else else
raise SyntaxError.new(options[:locale].t("errors.syntax.table_row".freeze)) raise SyntaxError.new(options[:locale].t("errors.syntax.table_row".freeze))
@@ -17,16 +17,16 @@ module Liquid
end end
def render(context) def render(context)
collection = context.evaluate(@collection_name) or return ''.freeze collection = context[@collection_name] or return ''.freeze
from = @attributes.key?('offset'.freeze) ? context.evaluate(@attributes['offset'.freeze]).to_i : 0 from = @attributes['offset'.freeze] ? context[@attributes['offset'.freeze]].to_i : 0
to = @attributes.key?('limit'.freeze) ? from + context.evaluate(@attributes['limit'.freeze]).to_i : nil to = @attributes['limit'.freeze] ? from + context[@attributes['limit'.freeze]].to_i : nil
collection = Utils.slice_collection(collection, from, to) collection = Utils.slice_collection(collection, from, to)
length = collection.length length = collection.length
cols = context.evaluate(@attributes['cols'.freeze]).to_i cols = context[@attributes['cols'.freeze]].to_i
row = 1 row = 1
col = 0 col = 0
+5 -5
View File
@@ -89,7 +89,7 @@ class RenderProfilingTest < Minitest::Test
include_node = t.profiler[1] include_node = t.profiler[1]
include_node.children.each do |child| include_node.children.each do |child|
assert_equal "a_template", child.partial assert_equal "'a_template'", child.partial
end end
end end
@@ -99,12 +99,12 @@ class RenderProfilingTest < Minitest::Test
a_template = t.profiler[1] a_template = t.profiler[1]
a_template.children.each do |child| a_template.children.each do |child|
assert_equal "a_template", child.partial assert_equal "'a_template'", child.partial
end end
b_template = t.profiler[2] b_template = t.profiler[2]
b_template.children.each do |child| b_template.children.each do |child|
assert_equal "b_template", child.partial assert_equal "'b_template'", child.partial
end end
end end
@@ -114,12 +114,12 @@ class RenderProfilingTest < Minitest::Test
a_template1 = t.profiler[1] a_template1 = t.profiler[1]
a_template1.children.each do |child| a_template1.children.each do |child|
assert_equal "a_template", child.partial assert_equal "'a_template'", child.partial
end end
a_template2 = t.profiler[2] a_template2 = t.profiler[2]
a_template2.children.each do |child| a_template2.children.each do |child|
assert_equal "a_template", child.partial assert_equal "'a_template'", child.partial
end end
end end
+58 -58
View File
@@ -4,111 +4,110 @@ class ConditionUnitTest < Minitest::Test
include Liquid include Liquid
def test_basic_condition def test_basic_condition
assert_equal false, Condition.new(1, '==', 2).evaluate assert_equal false, Condition.new('1', '==', '2').evaluate
assert_equal true, Condition.new(1, '==', 1).evaluate assert_equal true, Condition.new('1', '==', '1').evaluate
end end
def test_default_operators_evalute_true def test_default_operators_evalute_true
assert_evalutes_true 1, '==', 1 assert_evalutes_true '1', '==', '1'
assert_evalutes_true 1, '!=', 2 assert_evalutes_true '1', '!=', '2'
assert_evalutes_true 1, '<>', 2 assert_evalutes_true '1', '<>', '2'
assert_evalutes_true 1, '<', 2 assert_evalutes_true '1', '<', '2'
assert_evalutes_true 2, '>', 1 assert_evalutes_true '2', '>', '1'
assert_evalutes_true 1, '>=', 1 assert_evalutes_true '1', '>=', '1'
assert_evalutes_true 2, '>=', 1 assert_evalutes_true '2', '>=', '1'
assert_evalutes_true 1, '<=', 2 assert_evalutes_true '1', '<=', '2'
assert_evalutes_true 1, '<=', 1 assert_evalutes_true '1', '<=', '1'
# negative numbers # negative numbers
assert_evalutes_true 1, '>', -1 assert_evalutes_true '1', '>', '-1'
assert_evalutes_true -1, '<', 1 assert_evalutes_true '-1', '<', '1'
assert_evalutes_true 1.0, '>', -1.0 assert_evalutes_true '1.0', '>', '-1.0'
assert_evalutes_true -1.0, '<', 1.0 assert_evalutes_true '-1.0', '<', '1.0'
end end
def test_default_operators_evalute_false def test_default_operators_evalute_false
assert_evalutes_false 1, '==', 2 assert_evalutes_false '1', '==', '2'
assert_evalutes_false 1, '!=', 1 assert_evalutes_false '1', '!=', '1'
assert_evalutes_false 1, '<>', 1 assert_evalutes_false '1', '<>', '1'
assert_evalutes_false 1, '<', 0 assert_evalutes_false '1', '<', '0'
assert_evalutes_false 2, '>', 4 assert_evalutes_false '2', '>', '4'
assert_evalutes_false 1, '>=', 3 assert_evalutes_false '1', '>=', '3'
assert_evalutes_false 2, '>=', 4 assert_evalutes_false '2', '>=', '4'
assert_evalutes_false 1, '<=', 0 assert_evalutes_false '1', '<=', '0'
assert_evalutes_false 1, '<=', 0 assert_evalutes_false '1', '<=', '0'
end end
def test_contains_works_on_strings def test_contains_works_on_strings
assert_evalutes_true 'bob', 'contains', 'o' assert_evalutes_true "'bob'", 'contains', "'o'"
assert_evalutes_true 'bob', 'contains', 'b' assert_evalutes_true "'bob'", 'contains', "'b'"
assert_evalutes_true 'bob', 'contains', 'bo' assert_evalutes_true "'bob'", 'contains', "'bo'"
assert_evalutes_true 'bob', 'contains', 'ob' assert_evalutes_true "'bob'", 'contains', "'ob'"
assert_evalutes_true 'bob', 'contains', 'bob' assert_evalutes_true "'bob'", 'contains', "'bob'"
assert_evalutes_false 'bob', 'contains', 'bob2' assert_evalutes_false "'bob'", 'contains', "'bob2'"
assert_evalutes_false 'bob', 'contains', 'a' assert_evalutes_false "'bob'", 'contains', "'a'"
assert_evalutes_false 'bob', 'contains', '---' assert_evalutes_false "'bob'", 'contains', "'---'"
end end
def test_invalid_comparation_operator def test_invalid_comparation_operator
assert_evaluates_argument_error 1, '~~', 0 assert_evaluates_argument_error "1", '~~', '0'
end end
def test_comparation_of_int_and_str def test_comparation_of_int_and_str
assert_evaluates_argument_error '1', '>', 0 assert_evaluates_argument_error "'1'", '>', '0'
assert_evaluates_argument_error '1', '<', 0 assert_evaluates_argument_error "'1'", '<', '0'
assert_evaluates_argument_error '1', '>=', 0 assert_evaluates_argument_error "'1'", '>=', '0'
assert_evaluates_argument_error '1', '<=', 0 assert_evaluates_argument_error "'1'", '<=', '0'
end end
def test_contains_works_on_arrays def test_contains_works_on_arrays
@context = Liquid::Context.new @context = Liquid::Context.new
@context['array'] = [1,2,3,4,5] @context['array'] = [1,2,3,4,5]
array_expr = VariableLookup.new("array")
assert_evalutes_false array_expr, 'contains', 0 assert_evalutes_false "array", 'contains', '0'
assert_evalutes_true array_expr, 'contains', 1 assert_evalutes_true "array", 'contains', '1'
assert_evalutes_true array_expr, 'contains', 2 assert_evalutes_true "array", 'contains', '2'
assert_evalutes_true array_expr, 'contains', 3 assert_evalutes_true "array", 'contains', '3'
assert_evalutes_true array_expr, 'contains', 4 assert_evalutes_true "array", 'contains', '4'
assert_evalutes_true array_expr, 'contains', 5 assert_evalutes_true "array", 'contains', '5'
assert_evalutes_false array_expr, 'contains', 6 assert_evalutes_false "array", 'contains', '6'
assert_evalutes_false array_expr, 'contains', "1" assert_evalutes_false "array", 'contains', '"1"'
end end
def test_contains_returns_false_for_nil_operands def test_contains_returns_false_for_nil_operands
@context = Liquid::Context.new @context = Liquid::Context.new
assert_evalutes_false VariableLookup.new('not_assigned'), 'contains', '0' assert_evalutes_false "not_assigned", 'contains', '0'
assert_evalutes_false 0, 'contains', VariableLookup.new('not_assigned') assert_evalutes_false "0", 'contains', 'not_assigned'
end end
def test_contains_return_false_on_wrong_data_type def test_contains_return_false_on_wrong_data_type
assert_evalutes_false 1, 'contains', 0 assert_evalutes_false "1", 'contains', '0'
end end
def test_or_condition def test_or_condition
condition = Condition.new(1, '==', 2) condition = Condition.new('1', '==', '2')
assert_equal false, condition.evaluate assert_equal false, condition.evaluate
condition.or Condition.new(2, '==', 1) condition.or Condition.new('2', '==', '1')
assert_equal false, condition.evaluate assert_equal false, condition.evaluate
condition.or Condition.new(1, '==', 1) condition.or Condition.new('1', '==', '1')
assert_equal true, condition.evaluate assert_equal true, condition.evaluate
end end
def test_and_condition def test_and_condition
condition = Condition.new(1, '==', 1) condition = Condition.new('1', '==', '1')
assert_equal true, condition.evaluate assert_equal true, condition.evaluate
condition.and Condition.new(2, '==', 2) condition.and Condition.new('2', '==', '2')
assert_equal true, condition.evaluate assert_equal true, condition.evaluate
condition.and Condition.new(2, '==', 1) condition.and Condition.new('2', '==', '1')
assert_equal false, condition.evaluate assert_equal false, condition.evaluate
end end
@@ -116,8 +115,9 @@ class ConditionUnitTest < Minitest::Test
def test_should_allow_custom_proc_operator def test_should_allow_custom_proc_operator
Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}} } Condition.operators['starts_with'] = Proc.new { |cond, left, right| left =~ %r{^#{right}} }
assert_evalutes_true 'bob', 'starts_with', 'b' assert_evalutes_true "'bob'", 'starts_with', "'b'"
assert_evalutes_false 'bob', 'starts_with', 'o' assert_evalutes_false "'bob'", 'starts_with', "'o'"
ensure ensure
Condition.operators.delete 'starts_with' Condition.operators.delete 'starts_with'
end end
@@ -126,7 +126,7 @@ class ConditionUnitTest < Minitest::Test
@context = Liquid::Context.new @context = Liquid::Context.new
@context['one'] = @context['another'] = "gnomeslab-and-or-liquid" @context['one'] = @context['another'] = "gnomeslab-and-or-liquid"
assert_evalutes_true VariableLookup.new("one"), '==', VariableLookup.new("another") assert_evalutes_true "one", '==', "another"
end end
private private
+10
View File
@@ -471,6 +471,16 @@ class ContextUnitTest < Minitest::Test
assert mock_empty.has_been_called? assert mock_empty.has_been_called?
end end
def test_variable_lookup_caches_markup
mock_scan = Spy.on_instance_method(String, :scan).and_return(["string"])
@context['string'] = 'string'
@context['string']
@context['string']
assert_equal 1, mock_scan.calls.size
end
def test_context_initialization_with_a_proc_in_environment def test_context_initialization_with_a_proc_in_environment
contx = Context.new([:test => lambda { |c| c['poutine']}], {:test => :foo}) contx = Context.new([:test => lambda { |c| c['poutine']}], {:test => :foo})