liquid5-c 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +22 -0
  3. data/README.md +164 -0
  4. data/ext/liquid_c/block.c +970 -0
  5. data/ext/liquid_c/block.h +37 -0
  6. data/ext/liquid_c/c_buffer.c +48 -0
  7. data/ext/liquid_c/c_buffer.h +82 -0
  8. data/ext/liquid_c/context.c +262 -0
  9. data/ext/liquid_c/context.h +72 -0
  10. data/ext/liquid_c/document_body.c +181 -0
  11. data/ext/liquid_c/document_body.h +84 -0
  12. data/ext/liquid_c/expression.c +130 -0
  13. data/ext/liquid_c/expression.h +24 -0
  14. data/ext/liquid_c/extconf.rb +60 -0
  15. data/ext/liquid_c/intutil.h +36 -0
  16. data/ext/liquid_c/lexer.c +172 -0
  17. data/ext/liquid_c/lexer.h +61 -0
  18. data/ext/liquid_c/liquid.c +182 -0
  19. data/ext/liquid_c/liquid.h +112 -0
  20. data/ext/liquid_c/liquid_vm.c +907 -0
  21. data/ext/liquid_c/liquid_vm.h +35 -0
  22. data/ext/liquid_c/mri_compat.h +35 -0
  23. data/ext/liquid_c/native_parser.c +2360 -0
  24. data/ext/liquid_c/native_value.c +49 -0
  25. data/ext/liquid_c/parse_context.c +84 -0
  26. data/ext/liquid_c/parse_context.h +13 -0
  27. data/ext/liquid_c/parser.c +285 -0
  28. data/ext/liquid_c/parser.h +25 -0
  29. data/ext/liquid_c/raw.c +136 -0
  30. data/ext/liquid_c/raw.h +6 -0
  31. data/ext/liquid_c/resource_limits.c +459 -0
  32. data/ext/liquid_c/resource_limits.h +28 -0
  33. data/ext/liquid_c/stringutil.h +37 -0
  34. data/ext/liquid_c/tokenizer.c +326 -0
  35. data/ext/liquid_c/tokenizer.h +46 -0
  36. data/ext/liquid_c/usage.c +18 -0
  37. data/ext/liquid_c/usage.h +9 -0
  38. data/ext/liquid_c/variable.c +198 -0
  39. data/ext/liquid_c/variable.h +61 -0
  40. data/ext/liquid_c/variable_lookup.c +73 -0
  41. data/ext/liquid_c/variable_lookup.h +8 -0
  42. data/ext/liquid_c/vm_assembler.c +631 -0
  43. data/ext/liquid_c/vm_assembler.h +294 -0
  44. data/ext/liquid_c/vm_assembler_pool.c +117 -0
  45. data/ext/liquid_c/vm_assembler_pool.h +27 -0
  46. data/lib/liquid/c/compile_ext.rb +47 -0
  47. data/lib/liquid/c/native_compiler.rb +262 -0
  48. data/lib/liquid/c/native_parser.rb +261 -0
  49. data/lib/liquid/c/native_tags.rb +103 -0
  50. data/lib/liquid/c/native_values.rb +9 -0
  51. data/lib/liquid/c/version.rb +7 -0
  52. data/lib/liquid/c.rb +379 -0
  53. metadata +140 -0
data/lib/liquid/c.rb ADDED
@@ -0,0 +1,379 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "liquid/c/version"
4
+ require "liquid"
5
+ require "liquid_c"
6
+ require "liquid/c/compile_ext"
7
+
8
+ Liquid::C::BlockBody.class_eval do
9
+ def render(context)
10
+ render_to_output_buffer(context, +"")
11
+ end
12
+ end
13
+
14
+ module Liquid
15
+ BlockBody.class_eval do
16
+ class << self
17
+ def c_rescue_render_node(context, output, line_number, exc, blank_tag)
18
+ # There seems to be a MRI ruby bug with how the rb_rescue C function,
19
+ # where $! gets set for its rescue callback, but it doesn't stay set
20
+ # after a nested exception is raised and handled as is the case in
21
+ # Liquid::Context#internal_error. This isn't a problem for plain ruby code,
22
+ # so use a ruby rescue block to have setup $! properly.
23
+ raise(exc)
24
+ rescue => exc
25
+ rescue_render_node(context, output, line_number, exc, blank_tag)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ module Liquid
32
+ module C
33
+ class Tokenizer
34
+ MAX_SOURCE_BYTE_SIZE = (1 << 24) - 1
35
+ end
36
+ end
37
+ end
38
+
39
+ Liquid::Raw.class_eval do
40
+ alias_method :ruby_parse, :parse
41
+
42
+ def parse(tokenizer)
43
+ if parse_context.liquid_c_nodes_disabled?
44
+ ruby_parse(tokenizer)
45
+ else
46
+ c_parse(tokenizer)
47
+ end
48
+ end
49
+ end
50
+
51
+ module Liquid
52
+ module C
53
+ module CaseDiscardedBody
54
+ def parse(tokens)
55
+ @c_case_track_first_body = true
56
+ super
57
+ ensure
58
+ @c_case_track_first_body = false
59
+ @c_case_first_body = nil
60
+ end
61
+
62
+ def unknown_tag(tag, markup, tokens)
63
+ if (tag == "when" || tag == "else") && @c_case_first_body
64
+ @c_case_first_body.send(:discard_unreachable)
65
+ @c_case_first_body = nil
66
+ end
67
+ super
68
+ end
69
+
70
+ private
71
+
72
+ def new_body
73
+ body = super
74
+ if @c_case_track_first_body
75
+ @c_case_track_first_body = false
76
+ if body.is_a?(Liquid::C::BlockBody)
77
+ body.send(:mark_discardable)
78
+ @c_case_first_body = body
79
+ end
80
+ end
81
+ body
82
+ end
83
+ end
84
+ end
85
+ end
86
+ Liquid::Case.prepend(Liquid::C::CaseDiscardedBody)
87
+
88
+ Liquid::ParseContext.class_eval do
89
+ class << self
90
+ attr_accessor :liquid_c_nodes_disabled
91
+ end
92
+ self.liquid_c_nodes_disabled = false
93
+
94
+ alias_method :ruby_new_block_body, :new_block_body
95
+
96
+ def new_block_body
97
+ if liquid_c_nodes_disabled?
98
+ ruby_new_block_body
99
+ else
100
+ Liquid::C::BlockBody.new(self)
101
+ end
102
+ end
103
+
104
+ alias_method :ruby_new_tokenizer, :new_tokenizer
105
+ def new_tokenizer(source, start_line_number: nil, for_liquid_tag: false)
106
+ unless liquid_c_nodes_disabled?
107
+ source = source.to_s.to_str
108
+ encoded_source = source.encode(Encoding::UTF_8)
109
+ if encoded_source.bytesize <= Liquid::C::Tokenizer::MAX_SOURCE_BYTE_SIZE
110
+ return Liquid::C::Tokenizer.new(encoded_source, start_line_number || 0, for_liquid_tag)
111
+ end
112
+
113
+ tokenizer = ruby_new_tokenizer(source, start_line_number: start_line_number, for_liquid_tag: for_liquid_tag)
114
+ tokenizer.instance_variable_set(:@liquid_c_fallback_context, self)
115
+ @liquid_c_nodes_disabled = true
116
+ return tokenizer
117
+ end
118
+
119
+ ruby_new_tokenizer(source, start_line_number: start_line_number, for_liquid_tag: for_liquid_tag)
120
+ end
121
+
122
+ # @api private
123
+ def liquid_c_nodes_disabled?
124
+ # Liquid::Profiler exposes the internal parse tree that we don't want to build when
125
+ # parsing with liquid-c, so consider liquid-c to be disabled when using it.
126
+ # Also, some templates are parsed before the profiler is running, on which case we
127
+ # provide the `disable_liquid_c_nodes` option to enable the Ruby AST to be produced
128
+ # so the profiler can use it on future runs.
129
+ return @liquid_c_nodes_disabled if defined?(@liquid_c_nodes_disabled)
130
+
131
+ @liquid_c_nodes_disabled = !Liquid::C.enabled || @template_options[:profile] ||
132
+ @template_options[:disable_liquid_c_nodes] || self.class.liquid_c_nodes_disabled
133
+ end
134
+ end
135
+
136
+ module Liquid
137
+ module C
138
+ module DocumentClassPatch
139
+ def parse(tokenizer, parse_context)
140
+ if tokenizer.is_a?(Liquid::C::Tokenizer)
141
+ if parse_context[:bug_compatible_whitespace_trimming]
142
+ # Temporary to test rollout of the fix for this bug
143
+ tokenizer.bug_compatible_whitespace_trimming!
144
+ end
145
+
146
+ begin
147
+ parse_context.start_liquid_c_parsing
148
+ super
149
+ ensure
150
+ parse_context.cleanup_liquid_c_parsing
151
+ end
152
+ elsif tokenizer.instance_variable_get(:@liquid_c_fallback_context).equal?(parse_context)
153
+ begin
154
+ parse_context.instance_variable_set(:@liquid_c_nodes_disabled, true)
155
+ super
156
+ ensure
157
+ parse_context.remove_instance_variable(:@liquid_c_nodes_disabled)
158
+ end
159
+ else
160
+ super
161
+ end
162
+ end
163
+ end
164
+ Liquid::Document.singleton_class.prepend(DocumentClassPatch)
165
+ end
166
+ end
167
+
168
+ Liquid::Variable.class_eval do
169
+ output_methods = [:render, :render_to_output_buffer, :render_obj_to_output]
170
+ source = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib/liquid/variable.rb")
171
+ Liquid::C::VARIABLE_RENDERERS = output_methods.filter_map do |name|
172
+ original = instance_method(name)
173
+ original = original.super_method while original && original.owner != self
174
+ next unless original&.source_location&.first == source
175
+
176
+ define_method(:"ruby_#{name}", original)
177
+ [name, original, instance_method(:"c_#{name}")]
178
+ end.freeze
179
+
180
+ class << self
181
+ def c_parse(markup, parse_context)
182
+ if /\A\s*-\.\d/.match?(markup)
183
+ Liquid::Usage.increment("liquid_c_negative_float_without_integer")
184
+ end
185
+ new(markup, parse_context)
186
+ end
187
+ end
188
+
189
+ alias_method :ruby_strict_parse, :strict_parse
190
+
191
+ def strict_parse(markup)
192
+ result = liquid_c_native_parse(markup)
193
+ if result
194
+ @name, @filters = result
195
+ nil
196
+ else
197
+ ruby_strict_parse(markup)
198
+ end
199
+ rescue Liquid::SyntaxError
200
+ if !parse_context.liquid_c_nodes_disabled? && [:lax, :warn].include?(parse_context.error_mode)
201
+ self.class.call_variable_fallback_stats_callback(parse_context)
202
+ end
203
+ raise
204
+ end
205
+
206
+ def c_compilable?
207
+ filters.all? do |_name, args, kwargs|
208
+ args.size + (kwargs ? 1 : 0) <= 254 && (!kwargs || kwargs.size <= 255)
209
+ end
210
+ end
211
+
212
+ class << self
213
+ # @api private
214
+ def call_variable_fallback_stats_callback(parse_context)
215
+ callbacks = parse_context[:stats_callbacks]
216
+ callbacks[:variable_fallback]&.call if callbacks
217
+ end
218
+ end
219
+ end
220
+
221
+ Liquid::StrainerTemplate.class_eval do
222
+ class << self
223
+ original = instance_method(:add_filter)
224
+ original = original.super_method while original.owner != self
225
+ define_method(:ruby_add_filter, original)
226
+
227
+ def add_filter(filter)
228
+ result = ruby_add_filter(filter)
229
+ @filter_methods_hash&.replace(filter_methods.to_h { |name| [name.to_sym, true] })
230
+ result
231
+ end
232
+
233
+ private
234
+
235
+ def filter_methods_hash
236
+ @filter_methods_hash ||= {}.tap do |hash|
237
+ filter_methods.each do |method_name|
238
+ hash[method_name.to_sym] = true
239
+ end
240
+ end
241
+ end
242
+
243
+ # Convert wrong number of argument error into a liquid exception to
244
+ # treat it as an error in the template, not an internal error.
245
+ def arg_exc_to_liquid_exc(argument_error)
246
+ raise Liquid::ArgumentError, argument_error.message, argument_error.backtrace
247
+ rescue Liquid::ArgumentError => liquid_exc
248
+ liquid_exc
249
+ end
250
+ end
251
+ end
252
+
253
+ Liquid::C::Expression.class_eval do
254
+ class << self
255
+ def lax_parse(markup)
256
+ strict_parse(markup)
257
+ rescue Liquid::SyntaxError
258
+ Liquid::Expression.parse(markup)
259
+ end
260
+
261
+ # Default to strict parsing, since Liquid::C::Expression.parse should only really
262
+ # be used with constant expressions. Otherwise, prefer parse_context.parse_expression.
263
+ alias_method :parse, :strict_parse
264
+ end
265
+ end
266
+
267
+ Liquid::Context.class_eval do
268
+ native_methods = {
269
+ :[] => :c_parse_evaluate,
270
+ evaluate: :c_evaluate,
271
+ find_variable: :c_find_variable_kwarg,
272
+ :strict_variables= => :c_strict_variables=,
273
+ :strict_filters= => :c_strict_filters=,
274
+ :global_filter= => :c_global_filter=,
275
+ add_filters: :c_add_filters,
276
+ }
277
+ source = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib/liquid/context.rb")
278
+ Liquid::C::CONTEXT_METHODS = native_methods.filter_map do |name, native_name|
279
+ original = instance_method(name)
280
+ original = original.super_method while original && original.owner != self
281
+ ruby_name = name == :[] ? :ruby_parse_evaluate : :"ruby_#{name}"
282
+ define_method(ruby_name, original)
283
+ next unless original.source_location&.first == source
284
+
285
+ [name, original, native_name]
286
+ end.freeze
287
+
288
+ # This isn't entered often by Ruby (most calls stay in C via VariableLookup#evaluate)
289
+ # so the wrapper method isn't costly.
290
+ def c_find_variable_kwarg(key, raise_on_not_found: true)
291
+ c_find_variable(key, raise_on_not_found)
292
+ end
293
+
294
+ def c_parse_evaluate(expression)
295
+ ruby_parse_evaluate(expression)
296
+ end
297
+ end
298
+
299
+ module Liquid
300
+ module C
301
+ module ContextCopyPatch
302
+ private
303
+
304
+ def initialize_copy(other)
305
+ result = super
306
+ Liquid::C::VM.reset_context(self)
307
+ result
308
+ end
309
+ end
310
+ end
311
+ end
312
+ Liquid::Context.prepend(Liquid::C::ContextCopyPatch)
313
+
314
+ Liquid::ResourceLimits.class_eval do
315
+ class << self
316
+ def new(limits)
317
+ if equal?(Liquid::ResourceLimits) && Liquid::C.enabled
318
+ Liquid::C::ResourceLimits.new(
319
+ limits[:render_length_limit],
320
+ limits[:render_score_limit],
321
+ limits[:assign_score_limit],
322
+ limits[:cumulative_render_score_limit],
323
+ limits[:cumulative_assign_score_limit],
324
+ )
325
+ else
326
+ super
327
+ end
328
+ end
329
+ end
330
+ end
331
+
332
+ module Liquid
333
+ module C
334
+ class << self
335
+ attr_reader :enabled
336
+
337
+ def enabled=(value)
338
+ @enabled = value
339
+ Liquid::C::VM.invalidate_contexts
340
+ Liquid::C::VARIABLE_RENDERERS.each do |name, ruby_method, native_method|
341
+ next if Liquid::Variable.frozen?
342
+
343
+ current = Liquid::Variable.instance_method(name)
344
+ current = current.super_method while current && current.owner != Liquid::Variable
345
+ expected = value ? ruby_method : native_method
346
+ if current == expected
347
+ replacement = value ? :"c_#{name}" : :"ruby_#{name}"
348
+ Liquid::Variable.alias_method(name, replacement)
349
+ end
350
+ end
351
+ Liquid::C::CONTEXT_METHODS.each do |name, ruby_method, native_name|
352
+ next if Liquid::Context.frozen?
353
+ # These setters must keep the hidden VM in sync even while rendering
354
+ # falls back to Ruby, so it can release references immediately.
355
+ next if !value && (name == :global_filter= || name == :add_filters)
356
+
357
+ current = Liquid::Context.instance_method(name)
358
+ current = current.super_method while current && current.owner != Liquid::Context
359
+ native_method = Liquid::Context.instance_method(native_name)
360
+ expected = value ? ruby_method : native_method
361
+ next unless current == expected
362
+
363
+ replacement = value ? native_method : ruby_method
364
+ Liquid::Context.define_method(name, replacement)
365
+ end
366
+ end
367
+ end
368
+
369
+ self.enabled = true
370
+ end
371
+ end
372
+
373
+ require "liquid/c/native_parser"
374
+ require "liquid/c/native_tags"
375
+ require "liquid/c/native_values"
376
+
377
+ # Parsing during require retains the original path until guards are ready.
378
+ Liquid::C.native_enable_variable_frontend
379
+ require "liquid/c/native_compiler"
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid5-c
3
+ version: !ruby/object:Gem::Version
4
+ version: 5.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Li
8
+ - Dylan Thacker-Smith
9
+ - Vladimir Ivanin
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2026-11-24 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: liquid
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 5.14.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - '='
27
+ - !ruby/object:Gem::Version
28
+ version: 5.14.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: base64
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.2'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.2'
43
+ - !ruby/object:Gem::Dependency
44
+ name: strscan
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "!="
48
+ - !ruby/object:Gem::Version
49
+ version: 3.1.8
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "!="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.1.8
57
+ description:
58
+ email:
59
+ executables: []
60
+ extensions:
61
+ - ext/liquid_c/extconf.rb
62
+ extra_rdoc_files: []
63
+ files:
64
+ - LICENSE.txt
65
+ - README.md
66
+ - ext/liquid_c/block.c
67
+ - ext/liquid_c/block.h
68
+ - ext/liquid_c/c_buffer.c
69
+ - ext/liquid_c/c_buffer.h
70
+ - ext/liquid_c/context.c
71
+ - ext/liquid_c/context.h
72
+ - ext/liquid_c/document_body.c
73
+ - ext/liquid_c/document_body.h
74
+ - ext/liquid_c/expression.c
75
+ - ext/liquid_c/expression.h
76
+ - ext/liquid_c/extconf.rb
77
+ - ext/liquid_c/intutil.h
78
+ - ext/liquid_c/lexer.c
79
+ - ext/liquid_c/lexer.h
80
+ - ext/liquid_c/liquid.c
81
+ - ext/liquid_c/liquid.h
82
+ - ext/liquid_c/liquid_vm.c
83
+ - ext/liquid_c/liquid_vm.h
84
+ - ext/liquid_c/mri_compat.h
85
+ - ext/liquid_c/native_parser.c
86
+ - ext/liquid_c/native_value.c
87
+ - ext/liquid_c/parse_context.c
88
+ - ext/liquid_c/parse_context.h
89
+ - ext/liquid_c/parser.c
90
+ - ext/liquid_c/parser.h
91
+ - ext/liquid_c/raw.c
92
+ - ext/liquid_c/raw.h
93
+ - ext/liquid_c/resource_limits.c
94
+ - ext/liquid_c/resource_limits.h
95
+ - ext/liquid_c/stringutil.h
96
+ - ext/liquid_c/tokenizer.c
97
+ - ext/liquid_c/tokenizer.h
98
+ - ext/liquid_c/usage.c
99
+ - ext/liquid_c/usage.h
100
+ - ext/liquid_c/variable.c
101
+ - ext/liquid_c/variable.h
102
+ - ext/liquid_c/variable_lookup.c
103
+ - ext/liquid_c/variable_lookup.h
104
+ - ext/liquid_c/vm_assembler.c
105
+ - ext/liquid_c/vm_assembler.h
106
+ - ext/liquid_c/vm_assembler_pool.c
107
+ - ext/liquid_c/vm_assembler_pool.h
108
+ - lib/liquid/c.rb
109
+ - lib/liquid/c/compile_ext.rb
110
+ - lib/liquid/c/native_compiler.rb
111
+ - lib/liquid/c/native_parser.rb
112
+ - lib/liquid/c/native_tags.rb
113
+ - lib/liquid/c/native_values.rb
114
+ - lib/liquid/c/version.rb
115
+ homepage: https://github.com/VladimirIvanin/liquid-c
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ allowed_push_host: https://rubygems.org
120
+ source_code_uri: https://github.com/VladimirIvanin/liquid-c/tree/v5.0.0
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: 3.0.0
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubygems_version: 3.5.22
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Native parser and rendering VM for Liquid 5.14.0
140
+ test_files: []