mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 03:10:39 -07:00
Freeze block body after parsing completes
This commit is contained in:
@@ -22,6 +22,6 @@ group :test do
|
|||||||
gem 'rubocop-performance', require: false
|
gem 'rubocop-performance', require: false
|
||||||
|
|
||||||
platform :mri, :truffleruby do
|
platform :mri, :truffleruby do
|
||||||
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'master'
|
gem 'liquid-c', github: 'Shopify/liquid-c', ref: 'pz-block-body-buffer'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ module Liquid
|
|||||||
@body = new_body
|
@body = new_body
|
||||||
while parse_body(@body, tokens)
|
while parse_body(@body, tokens)
|
||||||
end
|
end
|
||||||
|
@body.freeze(parse_context)
|
||||||
end
|
end
|
||||||
|
|
||||||
# For backwards compatibility
|
# For backwards compatibility
|
||||||
|
|||||||
@@ -16,9 +16,12 @@ module Liquid
|
|||||||
def initialize
|
def initialize
|
||||||
@nodelist = []
|
@nodelist = []
|
||||||
@blank = true
|
@blank = true
|
||||||
|
@frozen = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(tokenizer, parse_context, &block)
|
def parse(tokenizer, parse_context, &block)
|
||||||
|
raise FrozenError, "can't modify frozen Liquid::BlockBody" if @frozen
|
||||||
|
|
||||||
parse_context.line_number = tokenizer.line_number
|
parse_context.line_number = tokenizer.line_number
|
||||||
|
|
||||||
if tokenizer.for_liquid_tag
|
if tokenizer.for_liquid_tag
|
||||||
@@ -28,6 +31,10 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def freeze(_context)
|
||||||
|
@frozen = true
|
||||||
|
end
|
||||||
|
|
||||||
private def parse_for_liquid_tag(tokenizer, parse_context)
|
private def parse_for_liquid_tag(tokenizer, parse_context)
|
||||||
while (token = tokenizer.shift)
|
while (token = tokenizer.shift)
|
||||||
unless token.empty? || token =~ WhitespaceOrNothing
|
unless token.empty? || token =~ WhitespaceOrNothing
|
||||||
@@ -192,6 +199,8 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_to_output_buffer(context, output)
|
def render_to_output_buffer(context, output)
|
||||||
|
raise "Can only render when frozen" unless @frozen
|
||||||
|
|
||||||
context.resource_limits.increment_render_score(@nodelist.length)
|
context.resource_limits.increment_render_score(@nodelist.length)
|
||||||
|
|
||||||
idx = 0
|
idx = 0
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ module Liquid
|
|||||||
def parse(tokenizer, parse_context)
|
def parse(tokenizer, parse_context)
|
||||||
while parse_body(tokenizer)
|
while parse_body(tokenizer)
|
||||||
end
|
end
|
||||||
|
@body.freeze(parse_context)
|
||||||
rescue SyntaxError => e
|
rescue SyntaxError => e
|
||||||
e.line_number ||= parse_context.line_number
|
e.line_number ||= parse_context.line_number
|
||||||
raise
|
raise
|
||||||
|
|||||||
@@ -20,7 +20,11 @@ module Liquid
|
|||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
body = new_body
|
body = new_body
|
||||||
body = @blocks.last.attachment while parse_body(body, tokens)
|
while parse_body(body, tokens)
|
||||||
|
body.freeze(parse_context)
|
||||||
|
body = @blocks.last.attachment
|
||||||
|
end
|
||||||
|
body.freeze(parse_context)
|
||||||
if blank?
|
if blank?
|
||||||
@blocks.each { |condition| condition.attachment.remove_blank_strings }
|
@blocks.each { |condition| condition.attachment.remove_blank_strings }
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -61,7 +61,9 @@ module Liquid
|
|||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
if parse_body(@for_block, tokens)
|
if parse_body(@for_block, tokens)
|
||||||
parse_body(@else_block, tokens)
|
parse_body(@else_block, tokens)
|
||||||
|
@else_block.freeze(parse_context)
|
||||||
end
|
end
|
||||||
|
@for_block.freeze(parse_context)
|
||||||
if blank?
|
if blank?
|
||||||
@for_block.remove_blank_strings
|
@for_block.remove_blank_strings
|
||||||
@else_block&.remove_blank_strings
|
@else_block&.remove_blank_strings
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ module Liquid
|
|||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
while parse_body(@blocks.last.attachment, tokens)
|
while parse_body(@blocks.last.attachment, tokens)
|
||||||
end
|
end
|
||||||
|
@blocks.each { |block| block.attachment.freeze(parse_context) }
|
||||||
if blank?
|
if blank?
|
||||||
@blocks.each { |condition| condition.attachment.remove_blank_strings }
|
@blocks.each { |condition| condition.attachment.remove_blank_strings }
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user