@@ -41,22 +41,18 @@ class Tokenizer
41
41
end
42
42
end
43
43
44
- Liquid ::Tokenizer . class_eval do
45
- def self . new ( source , line_numbers = false , line_number : nil , for_liquid_tag : false )
46
- source = source . to_s
47
- if Liquid :: C . enabled && source . bytesize <= Liquid :: C :: Tokenizer :: MAX_SOURCE_BYTE_SIZE
48
- source = source . encode ( Encoding :: UTF_8 )
49
- Liquid :: C :: Tokenizer . new ( source , line_number || ( line_numbers ? 1 : 0 ) , for_liquid_tag )
44
+ Liquid ::Raw . class_eval do
45
+ alias_method :ruby_parse , :parse
46
+
47
+ def parse ( tokenizer )
48
+ if parse_context . liquid_c_nodes_disabled?
49
+ ruby_parse ( tokenizer )
50
50
else
51
- super
51
+ c_parse ( tokenizer )
52
52
end
53
53
end
54
54
end
55
55
56
- Liquid ::Raw . class_eval do
57
- alias_method :ruby_parse , :parse
58
- end
59
-
60
56
Liquid ::ParseContext . class_eval do
61
57
class << self
62
58
attr_accessor :liquid_c_nodes_disabled
@@ -73,34 +69,42 @@ def new_block_body
73
69
end
74
70
end
75
71
72
+ alias_method :ruby_new_tokenizer , :new_tokenizer
73
+
74
+ def new_tokenizer ( source , start_line_number : nil , for_liquid_tag : false )
75
+ unless liquid_c_nodes_disabled?
76
+ source = source . to_s
77
+ if source . bytesize <= Liquid ::C ::Tokenizer ::MAX_SOURCE_BYTE_SIZE
78
+ source = source . encode ( Encoding ::UTF_8 )
79
+ return Liquid ::C ::Tokenizer . new ( source , start_line_number || 0 , for_liquid_tag )
80
+ else
81
+ @liquid_c_nodes_disabled = true
82
+ end
83
+ end
84
+
85
+ ruby_new_tokenizer ( source , start_line_number : start_line_number , for_liquid_tag : for_liquid_tag )
86
+ end
87
+
76
88
# @api private
77
89
def liquid_c_nodes_disabled?
78
90
# Liquid::Profiler exposes the internal parse tree that we don't want to build when
79
91
# parsing with liquid-c, so consider liquid-c to be disabled when using it.
80
92
# Also, some templates are parsed before the profiler is running, on which case we
81
93
# provide the `disable_liquid_c_nodes` option to enable the Ruby AST to be produced
82
94
# so the profiler can use it on future runs.
83
- @liquid_c_nodes_disabled ||= !Liquid ::C . enabled || @template_options [ :profile ] ||
95
+ return @liquid_c_nodes_disabled if defined? ( @liquid_c_nodes_disabled )
96
+ @liquid_c_nodes_disabled = !Liquid ::C . enabled || @template_options [ :profile ] ||
84
97
@template_options [ :disable_liquid_c_nodes ] || self . class . liquid_c_nodes_disabled
85
98
end
86
-
87
- # @api private
88
- attr_writer :liquid_c_nodes_disabled
89
99
end
90
100
91
101
module Liquid
92
102
module C
93
103
module DocumentClassPatch
94
104
def parse ( tokenizer , parse_context )
95
- if tokenizer . is_a? ( Liquid ::C ::Tokenizer )
105
+ if tokenizer . is_a? ( Liquid ::C ::Tokenizer ) && parse_context [ :bug_compatible_whitespace_trimming ]
96
106
# Temporary to test rollout of the fix for this bug
97
- if parse_context [ :bug_compatible_whitespace_trimming ]
98
- tokenizer . bug_compatible_whitespace_trimming!
99
- end
100
- else
101
- # Liquid::Tokenizer.new may return a Liquid::Tokenizer if the source is too large
102
- # to be supported, so indicate in the parse context that the liquid VM won't be used
103
- parse_context . liquid_c_nodes_disabled = true
107
+ tokenizer . bug_compatible_whitespace_trimming!
104
108
end
105
109
super
106
110
end
@@ -237,12 +241,10 @@ def enabled=(value)
237
241
Liquid ::Context . send ( :alias_method , :evaluate , :c_evaluate )
238
242
Liquid ::Context . send ( :alias_method , :find_variable , :c_find_variable_kwarg )
239
243
Liquid ::Context . send ( :alias_method , :strict_variables= , :c_strict_variables= )
240
- Liquid ::Raw . send ( :alias_method , :parse , :c_parse )
241
244
else
242
245
Liquid ::Context . send ( :alias_method , :evaluate , :ruby_evaluate )
243
246
Liquid ::Context . send ( :alias_method , :find_variable , :ruby_find_variable )
244
247
Liquid ::Context . send ( :alias_method , :strict_variables= , :ruby_strict_variables= )
245
- Liquid ::Raw . send ( :alias_method , :parse , :ruby_parse )
246
248
end
247
249
end
248
250
end
0 commit comments