Skip to content

Commit

Permalink
Merge pull request Shopify#1137 from Shopify/remove-lazy-stacks
Browse files Browse the repository at this point in the history
Remove lazy load stacks
  • Loading branch information
shopmike authored Sep 16, 2019
2 parents 806b262 + dafbb4a commit d7514b1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 64 deletions.
20 changes: 3 additions & 17 deletions lib/liquid/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_erro
@base_scope_depth = 0
squash_instance_assigns_with_environments

@this_stack_used = false

self.exception_renderer = Template.default_exception_renderer
if rethrow_errors
self.exception_renderer = ->(e) { raise }
Expand Down Expand Up @@ -122,19 +120,11 @@ def pop
# 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(new_scope = {})
push(new_scope)
yield
ensure
pop if @this_stack_used
@this_stack_used = old_stack_used
pop
end

# Creates a new context inheriting resource limits, filters, environment etc.,
Expand Down Expand Up @@ -162,10 +152,6 @@ def clear_instance_assigns

# 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

Expand Down
18 changes: 8 additions & 10 deletions lib/liquid/tags/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,14 @@ def unknown_tag(tag, markup, tokens)
end

def render_to_output_buffer(context, output)
context.stack do
execute_else_block = true

@blocks.each do |block|
if block.else?
block.attachment.render_to_output_buffer(context, output) if execute_else_block
elsif block.evaluate(context)
execute_else_block = false
block.attachment.render_to_output_buffer(context, output)
end
execute_else_block = true

@blocks.each do |block|
if block.else?
block.attachment.render_to_output_buffer(context, output) if execute_else_block
elsif block.evaluate(context)
execute_else_block = false
block.attachment.render_to_output_buffer(context, output)
end
end

Expand Down
26 changes: 12 additions & 14 deletions lib/liquid/tags/cycle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,22 @@ def initialize(tag_name, markup, options)
def render_to_output_buffer(context, output)
context.registers[:cycle] ||= {}

context.stack do
key = context.evaluate(@name)
iteration = context.registers[:cycle][key].to_i
key = context.evaluate(@name)
iteration = context.registers[:cycle][key].to_i

val = context.evaluate(@variables[iteration])
val = context.evaluate(@variables[iteration])

if val.is_a?(Array)
val = val.join
elsif !val.is_a?(String)
val = val.to_s
end
if val.is_a?(Array)
val = val.join
elsif !val.is_a?(String)
val = val.to_s
end

output << val
output << val

iteration += 1
iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration
end
iteration += 1
iteration = 0 if iteration >= @variables.size
context.registers[:cycle][key] = iteration

output
end
Expand Down
8 changes: 3 additions & 5 deletions lib/liquid/tags/if.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ def unknown_tag(tag, markup, tokens)
end

def render_to_output_buffer(context, output)
context.stack do
@blocks.each do |block|
if block.evaluate(context)
return block.attachment.render_to_output_buffer(context, output)
end
@blocks.each do |block|
if block.evaluate(context)
return block.attachment.render_to_output_buffer(context, output)
end
end

Expand Down
12 changes: 5 additions & 7 deletions lib/liquid/tags/ifchanged.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Liquid
class Ifchanged < Block
def render_to_output_buffer(context, output)
context.stack do
block_output = ''
super(context, block_output)
block_output = ''
super(context, block_output)

if block_output != context.registers[:ifchanged]
context.registers[:ifchanged] = block_output
output << block_output
end
if block_output != context.registers[:ifchanged]
context.registers[:ifchanged] = block_output
output << block_output
end

output
Expand Down
20 changes: 9 additions & 11 deletions lib/liquid/tags/unless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ module Liquid
#
class Unless < If
def render_to_output_buffer(context, output)
context.stack do
# First condition is interpreted backwards ( if not )
first_block = @blocks.first
unless first_block.evaluate(context)
return first_block.attachment.render_to_output_buffer(context, output)
end
# First condition is interpreted backwards ( if not )
first_block = @blocks.first
unless first_block.evaluate(context)
return first_block.attachment.render_to_output_buffer(context, output)
end

# After the first condition unless works just like if
@blocks[1..-1].each do |block|
if block.evaluate(context)
return block.attachment.render_to_output_buffer(context, output)
end
# After the first condition unless works just like if
@blocks[1..-1].each do |block|
if block.evaluate(context)
return block.attachment.render_to_output_buffer(context, output)
end
end

Expand Down

0 comments on commit d7514b1

Please sign in to comment.