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.
- checksums.yaml +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +164 -0
- data/ext/liquid_c/block.c +970 -0
- data/ext/liquid_c/block.h +37 -0
- data/ext/liquid_c/c_buffer.c +48 -0
- data/ext/liquid_c/c_buffer.h +82 -0
- data/ext/liquid_c/context.c +262 -0
- data/ext/liquid_c/context.h +72 -0
- data/ext/liquid_c/document_body.c +181 -0
- data/ext/liquid_c/document_body.h +84 -0
- data/ext/liquid_c/expression.c +130 -0
- data/ext/liquid_c/expression.h +24 -0
- data/ext/liquid_c/extconf.rb +60 -0
- data/ext/liquid_c/intutil.h +36 -0
- data/ext/liquid_c/lexer.c +172 -0
- data/ext/liquid_c/lexer.h +61 -0
- data/ext/liquid_c/liquid.c +182 -0
- data/ext/liquid_c/liquid.h +112 -0
- data/ext/liquid_c/liquid_vm.c +907 -0
- data/ext/liquid_c/liquid_vm.h +35 -0
- data/ext/liquid_c/mri_compat.h +35 -0
- data/ext/liquid_c/native_parser.c +2360 -0
- data/ext/liquid_c/native_value.c +49 -0
- data/ext/liquid_c/parse_context.c +84 -0
- data/ext/liquid_c/parse_context.h +13 -0
- data/ext/liquid_c/parser.c +285 -0
- data/ext/liquid_c/parser.h +25 -0
- data/ext/liquid_c/raw.c +136 -0
- data/ext/liquid_c/raw.h +6 -0
- data/ext/liquid_c/resource_limits.c +459 -0
- data/ext/liquid_c/resource_limits.h +28 -0
- data/ext/liquid_c/stringutil.h +37 -0
- data/ext/liquid_c/tokenizer.c +326 -0
- data/ext/liquid_c/tokenizer.h +46 -0
- data/ext/liquid_c/usage.c +18 -0
- data/ext/liquid_c/usage.h +9 -0
- data/ext/liquid_c/variable.c +198 -0
- data/ext/liquid_c/variable.h +61 -0
- data/ext/liquid_c/variable_lookup.c +73 -0
- data/ext/liquid_c/variable_lookup.h +8 -0
- data/ext/liquid_c/vm_assembler.c +631 -0
- data/ext/liquid_c/vm_assembler.h +294 -0
- data/ext/liquid_c/vm_assembler_pool.c +117 -0
- data/ext/liquid_c/vm_assembler_pool.h +27 -0
- data/lib/liquid/c/compile_ext.rb +47 -0
- data/lib/liquid/c/native_compiler.rb +262 -0
- data/lib/liquid/c/native_parser.rb +261 -0
- data/lib/liquid/c/native_tags.rb +103 -0
- data/lib/liquid/c/native_values.rb +9 -0
- data/lib/liquid/c/version.rb +7 -0
- data/lib/liquid/c.rb +379 -0
- metadata +140 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
if Liquid::C.respond_to?(:native_register_compiler)
|
|
4
|
+
Liquid::If.class_eval do
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
def liquid_c_finish_parse
|
|
8
|
+
@blocks.reverse_each do |block|
|
|
9
|
+
block.attachment.remove_blank_strings if blank?
|
|
10
|
+
block.attachment.freeze
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
Liquid::For.class_eval do
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def liquid_c_finish_parse
|
|
18
|
+
if blank?
|
|
19
|
+
@else_block&.remove_blank_strings
|
|
20
|
+
@for_block.remove_blank_strings
|
|
21
|
+
end
|
|
22
|
+
@else_block&.freeze
|
|
23
|
+
@for_block.freeze
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
source_root = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib", "liquid")
|
|
27
|
+
library_root = File.expand_path("..", __dir__)
|
|
28
|
+
context_method = Liquid::ParserSwitching.instance_method(:strict2_parse_with_error_context)
|
|
29
|
+
expected_context_owner = context_method.owner == Liquid::ParserSwitching
|
|
30
|
+
if expected_context_owner && context_method.original_name == :strict2_parse_with_error_context &&
|
|
31
|
+
context_method.source_location&.first == File.join(source_root, "parser_switching.rb")
|
|
32
|
+
Liquid::C.native_install_compiler_context
|
|
33
|
+
end
|
|
34
|
+
specifications = {
|
|
35
|
+
Liquid::Variable.singleton_class => [:c_parse],
|
|
36
|
+
Liquid::C::BlockBody.singleton_class => [:new, :allocate],
|
|
37
|
+
Liquid::Condition.singleton_class => [:new, :allocate, :parse_expression],
|
|
38
|
+
Liquid::ElseCondition.singleton_class => [:new, :allocate],
|
|
39
|
+
Liquid::Variable => [
|
|
40
|
+
:initialize,
|
|
41
|
+
:strict_parse,
|
|
42
|
+
:lax_parse,
|
|
43
|
+
:c_compilable?,
|
|
44
|
+
:compile_evaluate,
|
|
45
|
+
:filters,
|
|
46
|
+
:strict_parse_with_error_mode_fallback,
|
|
47
|
+
:strict_parse_with_error_context,
|
|
48
|
+
:strict2_mode?,
|
|
49
|
+
],
|
|
50
|
+
Liquid::ParseContext => [:line_number, :depth, :depth=, :new_block_body, :environment],
|
|
51
|
+
Liquid::Tag => [:initialize, :parse, :parse_context, :line_number, :options],
|
|
52
|
+
Liquid::Tag.singleton_class => [:parse],
|
|
53
|
+
Liquid::Block => [:initialize, :new_body, :parse_body, :block_name, :block_delimiter, :blank?],
|
|
54
|
+
Liquid::Assign => [:initialize, :parse, :strict_parse, :strict2_parse, :lax_parse, :blank?],
|
|
55
|
+
Liquid::Assign.singleton_class => [:parse],
|
|
56
|
+
Liquid::Capture => [
|
|
57
|
+
:initialize, :parse, :strict_parse, :strict2_parse, :lax_parse, :blank?, :unknown_tag, :raise_tag_never_closed,
|
|
58
|
+
],
|
|
59
|
+
Liquid::Capture.singleton_class => [:parse],
|
|
60
|
+
Liquid::Include => [
|
|
61
|
+
:initialize, :parse, :strict_parse, :strict2_parse, :lax_parse, :blank?, :safe_parse_expression, :parse_context,
|
|
62
|
+
],
|
|
63
|
+
Liquid::Include.singleton_class => [:parse],
|
|
64
|
+
Liquid::If => [
|
|
65
|
+
:initialize,
|
|
66
|
+
:parse,
|
|
67
|
+
:push_block,
|
|
68
|
+
:unknown_tag,
|
|
69
|
+
:strict_parse,
|
|
70
|
+
:strict2_parse,
|
|
71
|
+
:parse_binary_comparisons,
|
|
72
|
+
:parse_comparison,
|
|
73
|
+
:parse_expression,
|
|
74
|
+
],
|
|
75
|
+
Liquid::Unless => [
|
|
76
|
+
:initialize,
|
|
77
|
+
:parse,
|
|
78
|
+
:push_block,
|
|
79
|
+
:unknown_tag,
|
|
80
|
+
:strict_parse,
|
|
81
|
+
:strict2_parse,
|
|
82
|
+
:parse_binary_comparisons,
|
|
83
|
+
:parse_comparison,
|
|
84
|
+
:parse_expression,
|
|
85
|
+
],
|
|
86
|
+
Liquid::For => [
|
|
87
|
+
:initialize,
|
|
88
|
+
:parse,
|
|
89
|
+
:unknown_tag,
|
|
90
|
+
:strict_parse,
|
|
91
|
+
:strict2_parse,
|
|
92
|
+
:set_attribute,
|
|
93
|
+
:parse_expression,
|
|
94
|
+
],
|
|
95
|
+
Liquid::If.singleton_class => [:parse],
|
|
96
|
+
Liquid::Unless.singleton_class => [:parse],
|
|
97
|
+
Liquid::For.singleton_class => [:parse],
|
|
98
|
+
Liquid::Condition => [:initialize, :and, :or, :attach, :attachment],
|
|
99
|
+
Liquid::ElseCondition => [:initialize, :attach, :attachment],
|
|
100
|
+
Liquid::Environment => [:tag_for_name],
|
|
101
|
+
Liquid::ParserSwitching => [:strict2_parse_with_error_context],
|
|
102
|
+
Liquid::VariableLookup => [:compile_evaluate, :name, :lookups],
|
|
103
|
+
Liquid::RangeLookup => [:compile_evaluate],
|
|
104
|
+
Liquid::C::BlockBody => [
|
|
105
|
+
:add_evaluate_expression,
|
|
106
|
+
:add_find_variable,
|
|
107
|
+
:add_lookup_guard,
|
|
108
|
+
:patch_lookup_guard,
|
|
109
|
+
:add_lookup_command,
|
|
110
|
+
:add_lookup_key,
|
|
111
|
+
:add_new_int_range,
|
|
112
|
+
:add_hash_new,
|
|
113
|
+
:add_filter,
|
|
114
|
+
:initialize,
|
|
115
|
+
:parse,
|
|
116
|
+
:freeze,
|
|
117
|
+
:remove_blank_strings,
|
|
118
|
+
:blank?,
|
|
119
|
+
],
|
|
120
|
+
}
|
|
121
|
+
switching = [:parse_with_selected_parser, :strict_parse_with_error_context, :strict2_parse_with_error_context]
|
|
122
|
+
inherited = [:new_body, :parse_body, :block_name, :block_delimiter, :blank?, :parse_context, :line_number, :options]
|
|
123
|
+
[Liquid::If, Liquid::Unless, Liquid::For].each { |klass| specifications[klass].concat(inherited + switching) }
|
|
124
|
+
specifications[Liquid::Include].concat(switching + [:line_number, :options])
|
|
125
|
+
specifications[Liquid::Capture].concat((inherited + switching).reject { |name| name == :blank? })
|
|
126
|
+
specifications[Liquid::Assign].concat(switching)
|
|
127
|
+
standard = Liquid::C::NativeParser::NATIVE_GUARDS
|
|
128
|
+
standard &&= Liquid::Tag.method(:new).unbind == Class.instance_method(:new)
|
|
129
|
+
control_owners = [
|
|
130
|
+
Liquid::Assign,
|
|
131
|
+
Liquid::Assign.singleton_class,
|
|
132
|
+
Liquid::Capture,
|
|
133
|
+
Liquid::Capture.singleton_class,
|
|
134
|
+
Liquid::Include,
|
|
135
|
+
Liquid::Include.singleton_class,
|
|
136
|
+
Liquid::Tag,
|
|
137
|
+
Liquid::Tag.singleton_class,
|
|
138
|
+
Liquid::Block,
|
|
139
|
+
Liquid::If,
|
|
140
|
+
Liquid::Unless,
|
|
141
|
+
Liquid::For,
|
|
142
|
+
Liquid::If.singleton_class,
|
|
143
|
+
Liquid::Unless.singleton_class,
|
|
144
|
+
Liquid::For.singleton_class,
|
|
145
|
+
Liquid::Condition,
|
|
146
|
+
Liquid::Condition.singleton_class,
|
|
147
|
+
Liquid::ElseCondition,
|
|
148
|
+
Liquid::ElseCondition.singleton_class,
|
|
149
|
+
Liquid::C::BlockBody.singleton_class,
|
|
150
|
+
]
|
|
151
|
+
guards = specifications.flat_map do |owner, names|
|
|
152
|
+
names.map do |name|
|
|
153
|
+
method = owner.instance_method(name)
|
|
154
|
+
location = method.source_location&.first
|
|
155
|
+
builtin_constructor = [:new, :allocate].include?(name) &&
|
|
156
|
+
[
|
|
157
|
+
Liquid::C::BlockBody.singleton_class,
|
|
158
|
+
Liquid::Condition.singleton_class,
|
|
159
|
+
Liquid::ElseCondition.singleton_class,
|
|
160
|
+
].include?(owner)
|
|
161
|
+
expected_owner = if builtin_constructor
|
|
162
|
+
Class
|
|
163
|
+
elsif (switching + [:strict_parse_with_error_mode_fallback, :strict2_mode?]).include?(name)
|
|
164
|
+
Liquid::ParserSwitching
|
|
165
|
+
elsif inherited.include?(name) && [Liquid::If, Liquid::Unless, Liquid::For].include?(owner)
|
|
166
|
+
[:parse_context, :line_number, :options].include?(name) ? Liquid::Tag : Liquid::Block
|
|
167
|
+
elsif owner == Liquid::Include && [:blank?, :safe_parse_expression, :line_number, :options].include?(name)
|
|
168
|
+
Liquid::Tag
|
|
169
|
+
elsif owner == Liquid::Capture && [:parse, :unknown_tag, :raise_tag_never_closed].include?(name)
|
|
170
|
+
Liquid::Block
|
|
171
|
+
elsif owner == Liquid::Capture && inherited.include?(name) && name != :blank?
|
|
172
|
+
[:parse_context, :line_number, :options].include?(name) ? Liquid::Tag : Liquid::Block
|
|
173
|
+
elsif owner == Liquid::Assign && name == :parse
|
|
174
|
+
Liquid::Tag
|
|
175
|
+
elsif owner == Liquid::Unless
|
|
176
|
+
Liquid::If
|
|
177
|
+
elsif owner == Liquid::For && name == :parse_expression
|
|
178
|
+
Liquid::Tag
|
|
179
|
+
elsif owner == Liquid::ElseCondition
|
|
180
|
+
Liquid::Condition
|
|
181
|
+
elsif [
|
|
182
|
+
Liquid::If.singleton_class,
|
|
183
|
+
Liquid::Unless.singleton_class,
|
|
184
|
+
Liquid::For.singleton_class,
|
|
185
|
+
Liquid::Assign.singleton_class,
|
|
186
|
+
Liquid::Capture.singleton_class,
|
|
187
|
+
Liquid::Include.singleton_class,
|
|
188
|
+
].include?(owner)
|
|
189
|
+
Liquid::Tag.singleton_class
|
|
190
|
+
else
|
|
191
|
+
owner
|
|
192
|
+
end
|
|
193
|
+
expected_name = name == :options ? :parse_context : name
|
|
194
|
+
standard &&= method.owner == expected_owner && method.original_name == expected_name &&
|
|
195
|
+
(if owner == Liquid::C::BlockBody || builtin_constructor || [
|
|
196
|
+
:strict_parse_with_error_context,
|
|
197
|
+
:strict2_parse_with_error_context,
|
|
198
|
+
].include?(name)
|
|
199
|
+
location.nil?
|
|
200
|
+
else
|
|
201
|
+
location && (location.start_with?(source_root + "/") || location.start_with?(library_root + "/"))
|
|
202
|
+
end)
|
|
203
|
+
control = control_owners.include?(owner) ||
|
|
204
|
+
owner == Liquid::ParseContext && [:depth, :depth=, :new_block_body].include?(name) ||
|
|
205
|
+
owner == Liquid::C::BlockBody && [:initialize, :parse, :freeze, :remove_blank_strings, :blank?].include?(name)
|
|
206
|
+
[owner, name, control].freeze
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
constants = [
|
|
210
|
+
*[
|
|
211
|
+
:Block, :Tag, :If, :For, :Unless, :Condition, :ElseCondition, :Environment, :Assign, :Capture, :Include,
|
|
212
|
+
].map do |name|
|
|
213
|
+
[Liquid, name]
|
|
214
|
+
end,
|
|
215
|
+
[Liquid::Assign, :Syntax],
|
|
216
|
+
[Liquid::Include, :FOR],
|
|
217
|
+
[Liquid::Block, :MAX_DEPTH],
|
|
218
|
+
[Liquid::If, :ELSE_TAG_NAMES],
|
|
219
|
+
[Liquid::C, :BlockBody],
|
|
220
|
+
].map do |owner, name|
|
|
221
|
+
location = owner.const_source_location(name)&.first
|
|
222
|
+
native_class = owner == Liquid::C && name == :BlockBody &&
|
|
223
|
+
$LOADED_FEATURES.any? { |path| path == location && path.end_with?("/liquid_c.so", "/liquid_c.bundle") }
|
|
224
|
+
standard &&= native_class || location && location.start_with?(source_root + "/", library_root + "/")
|
|
225
|
+
[owner, name, owner.const_get(name, false)].freeze
|
|
226
|
+
end.freeze
|
|
227
|
+
ancestor_prefixes = [
|
|
228
|
+
[Liquid::Assign, [Liquid::Assign, Liquid::Tag]],
|
|
229
|
+
[Liquid::Capture, [Liquid::Capture, Liquid::Block, Liquid::Tag]],
|
|
230
|
+
[Liquid::Include, [Liquid::Tag::Disableable, Liquid::Include, Liquid::Tag]],
|
|
231
|
+
[Liquid::If, [Liquid::If, Liquid::Block, Liquid::Tag]],
|
|
232
|
+
[Liquid::Unless, [Liquid::Unless, Liquid::If, Liquid::Block, Liquid::Tag]],
|
|
233
|
+
[Liquid::For, [Liquid::For, Liquid::Block, Liquid::Tag]],
|
|
234
|
+
[Liquid::C::BlockBody, [Liquid::C::BlockBody, Object]],
|
|
235
|
+
].map do |klass, prefix|
|
|
236
|
+
standard &&= klass.ancestors.first(prefix.size) == prefix
|
|
237
|
+
prefix = klass.ancestors
|
|
238
|
+
[klass, prefix.freeze].freeze
|
|
239
|
+
end.freeze
|
|
240
|
+
constant_watchers = if Liquid::C::NativeParser::CONSTANT_WATCHERS
|
|
241
|
+
evaluator = Module.instance_method(:module_eval)
|
|
242
|
+
constants.map do |owner, name, expected|
|
|
243
|
+
reader = evaluator.bind_call(owner, "-> { #{name} }", __FILE__, __LINE__)
|
|
244
|
+
standard &&= reader.call.equal?(expected)
|
|
245
|
+
reader.freeze
|
|
246
|
+
end.freeze
|
|
247
|
+
end
|
|
248
|
+
Liquid::C.native_register_control(
|
|
249
|
+
[Liquid::If.instance_method(:liquid_c_finish_parse), Liquid::For.instance_method(:liquid_c_finish_parse)].freeze,
|
|
250
|
+
constants,
|
|
251
|
+
ancestor_prefixes,
|
|
252
|
+
constant_watchers,
|
|
253
|
+
)
|
|
254
|
+
[:render, :render_to_output_buffer, :render_obj_to_output].each do |name|
|
|
255
|
+
method = Liquid::Variable.instance_method(name)
|
|
256
|
+
standard &&= method.owner == Liquid::Variable && method.source_location.nil? && method.original_name == :"c_#{name}"
|
|
257
|
+
end
|
|
258
|
+
Liquid::C.native_register_compiler(guards.freeze, standard)
|
|
259
|
+
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
Liquid::C.private_constant(:VariablePlaceholder)
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rbconfig"
|
|
4
|
+
|
|
5
|
+
module Liquid
|
|
6
|
+
module C
|
|
7
|
+
# Method identities are checked on each operation. Startup-only checks miss
|
|
8
|
+
# application reloads, prepend, singleton methods and direct method changes.
|
|
9
|
+
module NativeParser
|
|
10
|
+
source_root = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib", "liquid")
|
|
11
|
+
native_source = if RUBY_ENGINE == "truffleruby"
|
|
12
|
+
File.join(RbConfig::CONFIG.fetch("prefix"), "lib/truffle/truffle/cext_ruby.rb")
|
|
13
|
+
end
|
|
14
|
+
scanner_sources = if RUBY_ENGINE == "truffleruby"
|
|
15
|
+
[
|
|
16
|
+
File.join(Gem.loaded_specs.fetch("strscan").full_gem_path, "lib/strscan/truffleruby.rb"),
|
|
17
|
+
File.join(RbConfig::CONFIG.fetch("rubylibdir"), "strscan/truffleruby.rb"),
|
|
18
|
+
]
|
|
19
|
+
else
|
|
20
|
+
[]
|
|
21
|
+
end
|
|
22
|
+
native_location = ->(location) { location.nil? || location == native_source }
|
|
23
|
+
specifications = {
|
|
24
|
+
Liquid::Variable => [:ruby_strict_parse, :parse_filterargs, :lax_parse_filter_expressions, :parse_context],
|
|
25
|
+
Liquid::ParseContext => [
|
|
26
|
+
:new_parser,
|
|
27
|
+
:safe_parse_expression,
|
|
28
|
+
:parse_expression,
|
|
29
|
+
:error_mode,
|
|
30
|
+
:liquid_c_nodes_disabled?,
|
|
31
|
+
],
|
|
32
|
+
Liquid::Parser => [:initialize, :expression, :argument, :variable_lookups, :consume, :consume?, :look, :id?],
|
|
33
|
+
Liquid::VariableLookup => [:initialize],
|
|
34
|
+
Liquid::RangeLookup => [:initialize],
|
|
35
|
+
Liquid::Expression.singleton_class => [:parse, :safe_parse, :inner_parse, :parse_number],
|
|
36
|
+
Liquid::Lexer.singleton_class => [:tokenize],
|
|
37
|
+
Liquid::VariableLookup.singleton_class => [:parse],
|
|
38
|
+
Liquid::RangeLookup.singleton_class => [:parse],
|
|
39
|
+
StringScanner => [:scan, :skip, :pos, :pos=, :peek_byte, :scan_byte, :eos?, :string, :string=],
|
|
40
|
+
Integer => [:to_i],
|
|
41
|
+
Array => [:first, :last],
|
|
42
|
+
Liquid::C.singleton_class => [:enabled],
|
|
43
|
+
}
|
|
44
|
+
GUARDS = specifications.flat_map do |owner, names|
|
|
45
|
+
names.map do |name|
|
|
46
|
+
method = owner.instance_method(name)
|
|
47
|
+
location = method.source_location&.first
|
|
48
|
+
standard = if owner == StringScanner
|
|
49
|
+
native_location.call(location) || scanner_sources.include?(location)
|
|
50
|
+
else
|
|
51
|
+
location&.start_with?(source_root + "/")
|
|
52
|
+
end
|
|
53
|
+
if owner == Integer || owner == Array
|
|
54
|
+
internal_source = owner == Integer ? "<internal:numeric>" : "<internal:array>"
|
|
55
|
+
standard = native_location.call(location) || location == internal_source ||
|
|
56
|
+
(RUBY_ENGINE == "truffleruby" && location&.start_with?("<internal:core> "))
|
|
57
|
+
end
|
|
58
|
+
if (owner == Liquid::ParseContext && name == :liquid_c_nodes_disabled?) || owner == Liquid::C.singleton_class
|
|
59
|
+
standard = location == File.expand_path("../c.rb", __dir__)
|
|
60
|
+
end
|
|
61
|
+
expected_name = name == :ruby_strict_parse ? :strict_parse : name
|
|
62
|
+
standard &&= method.original_name == expected_name
|
|
63
|
+
standard &&= method.owner == owner if owner == StringScanner
|
|
64
|
+
[owner, name, method, standard].freeze
|
|
65
|
+
end
|
|
66
|
+
end.freeze
|
|
67
|
+
CONTEXT_GUARDS = GUARDS.select { |owner, *| owner == Liquid::ParseContext }.freeze
|
|
68
|
+
CONSTRUCTORS = [Liquid::VariableLookup, Liquid::RangeLookup, Liquid::Parser].flat_map do |klass|
|
|
69
|
+
[:new, :allocate].map { |name| [klass, name, klass.method(name)].freeze }
|
|
70
|
+
end.freeze
|
|
71
|
+
special_tokens = []
|
|
72
|
+
{
|
|
73
|
+
"|" => :PIPE,
|
|
74
|
+
"." => :DOT,
|
|
75
|
+
":" => :COLON,
|
|
76
|
+
"," => :COMMA,
|
|
77
|
+
"[" => :OPEN_SQUARE,
|
|
78
|
+
"]" => :CLOSE_SQUARE,
|
|
79
|
+
"(" => :OPEN_ROUND,
|
|
80
|
+
")" => :CLOSE_ROUND,
|
|
81
|
+
"?" => :QUESTION,
|
|
82
|
+
"-" => :DASH,
|
|
83
|
+
}.each do |byte, name|
|
|
84
|
+
special_tokens[byte.ord] = Liquid::Lexer.const_get(name, false)
|
|
85
|
+
end
|
|
86
|
+
CONSTANT_GUARDS = [
|
|
87
|
+
*Liquid::Expression.constants(false).map { |name| [Liquid::Expression, name] },
|
|
88
|
+
*[:Expression, :Lexer, :VariableLookup, :RangeLookup, :Parser, :ParseContext, :Variable].map do |name|
|
|
89
|
+
[Liquid, name]
|
|
90
|
+
end,
|
|
91
|
+
[Liquid, :Const],
|
|
92
|
+
[Liquid::Const, :EMPTY_ARRAY],
|
|
93
|
+
[Liquid, :VariableParser],
|
|
94
|
+
[Liquid::Variable, :JustTagAttributes],
|
|
95
|
+
[Liquid::VariableLookup, :COMMAND_METHODS],
|
|
96
|
+
*Liquid::Lexer.constants(false).map { |name| [Liquid::Lexer, name] },
|
|
97
|
+
].map do |owner, name|
|
|
98
|
+
pending_autoload = owner.autoload?(name)
|
|
99
|
+
value = pending_autoload ? nil : owner.const_get(name, false)
|
|
100
|
+
source = owner.const_source_location(name)&.first
|
|
101
|
+
mutable_table = owner == Liquid::Lexer && name == :SPECIAL_TABLE
|
|
102
|
+
standard = !pending_autoload && (source&.start_with?(source_root + "/") || source == File.join(
|
|
103
|
+
File.dirname(source_root),
|
|
104
|
+
"liquid.rb",
|
|
105
|
+
)) &&
|
|
106
|
+
(mutable_table ? value == special_tokens : value.is_a?(Module) || value.frozen?)
|
|
107
|
+
[owner, name, value, standard, mutable_table ? value.dup.freeze : nil].freeze
|
|
108
|
+
end.freeze
|
|
109
|
+
GUARD_SPECS = GUARDS.map do |owner, name, *_|
|
|
110
|
+
receiver = if owner == Liquid::ParseContext
|
|
111
|
+
1
|
|
112
|
+
elsif owner == StringScanner
|
|
113
|
+
2
|
|
114
|
+
elsif owner == Liquid::Variable
|
|
115
|
+
3
|
|
116
|
+
else
|
|
117
|
+
0
|
|
118
|
+
end
|
|
119
|
+
[owner, name, receiver, false, nil, nil, ![:initialize, :lax_parse_filter_expressions].include?(name)].freeze
|
|
120
|
+
end.concat(CONSTRUCTORS.map { |klass, name, _| [klass.singleton_class, name, 0, false, nil, nil, true].freeze })
|
|
121
|
+
.concat(CONSTANT_GUARDS.map do |owner, name, value, _standard, snapshot|
|
|
122
|
+
[owner, name, 0, true, snapshot, value, false].freeze
|
|
123
|
+
end).freeze
|
|
124
|
+
STANDARD_GUARDS = GUARDS.all?(&:last) && CONSTANT_GUARDS.all? { |guard| guard[3] } &&
|
|
125
|
+
CONSTRUCTORS.all? do |_klass, name, method|
|
|
126
|
+
method.source_location.nil? && method.owner == Class && method.original_name == name
|
|
127
|
+
end
|
|
128
|
+
# MRI 3.2's public counter counts registered IC sites. Warm and retain
|
|
129
|
+
# one actual Ruby constant reader per guarded name: changes then invalidate
|
|
130
|
+
# the epoch even when the native parser never executes the Ruby lexer.
|
|
131
|
+
# Method identities and mutable table slots are NOT cached.
|
|
132
|
+
CONSTANT_WATCHERS = if STANDARD_GUARDS && RUBY_ENGINE == "ruby" && RUBY_VERSION.start_with?("3.2.") &&
|
|
133
|
+
Liquid::C.respond_to?(:native_constant_watchers_supported?) &&
|
|
134
|
+
Liquid::C.native_constant_watchers_supported?(RubyVM)
|
|
135
|
+
unchanged = true
|
|
136
|
+
evaluator = Module.instance_method(:module_eval)
|
|
137
|
+
readers = CONSTANT_GUARDS.map do |owner, name, expected, *_|
|
|
138
|
+
reader = evaluator.bind_call(owner, "-> { #{name} }", __FILE__, __LINE__)
|
|
139
|
+
unchanged &&= reader.call.equal?(expected)
|
|
140
|
+
reader.freeze
|
|
141
|
+
end
|
|
142
|
+
unchanged ? readers.freeze : nil
|
|
143
|
+
end
|
|
144
|
+
NATIVE_GUARDS = if Liquid::C.respond_to?(:native_try_parse_variable) && STANDARD_GUARDS && CONSTANT_WATCHERS
|
|
145
|
+
watchers = ENV["LIQUID_C_NATIVE_CONST_CACHE"] == "false" ? nil : CONSTANT_WATCHERS
|
|
146
|
+
Liquid::C.native_parser_register_guards(GUARD_SPECS, watchers, RubyVM)
|
|
147
|
+
else
|
|
148
|
+
false
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
class << self
|
|
152
|
+
def parse(markup, context, variable = nil)
|
|
153
|
+
return record(context, :tracing) if Liquid::C.native_parser_tracing?
|
|
154
|
+
return record(context, :disabled) if ENV["LIQUID_C_NATIVE_PARSER"] == "false"
|
|
155
|
+
return record(context, :disabled) unless Liquid::C.instance_variable_get(:@enabled)
|
|
156
|
+
|
|
157
|
+
if NATIVE_GUARDS && variable
|
|
158
|
+
result = Liquid::C.native_try_parse_variable(markup, context, variable)
|
|
159
|
+
if result.equal?(false) && compatible?(context, variable)
|
|
160
|
+
result = Liquid::C.native_try_parse_variable(markup, context, variable)
|
|
161
|
+
end
|
|
162
|
+
return result || nil
|
|
163
|
+
end
|
|
164
|
+
return record(context, :disabled) if context.instance_variable_get(:@liquid_c_nodes_disabled)
|
|
165
|
+
return record(context, :hooks) if variable && !variable.instance_of?(Liquid::Variable)
|
|
166
|
+
return record(context, :guard_cost) unless NATIVE_GUARDS || Liquid::C.native_frontend_candidate?(markup)
|
|
167
|
+
return record(context, :hooks) unless compatible?(context, variable)
|
|
168
|
+
return record(context, :disabled) if context.liquid_c_nodes_disabled?
|
|
169
|
+
return record(context, :mode) unless [:lax, :warn, :strict].include?(context.error_mode)
|
|
170
|
+
|
|
171
|
+
cache = context.instance_variable_get(:@expression_cache)
|
|
172
|
+
# A caller-supplied cache can execute arbitrary Ruby hooks. Preserve
|
|
173
|
+
# its complete key?/[]/[]= protocol through the original parser.
|
|
174
|
+
return record(context, :cache) unless cache.nil? || cache.equal?(false) || cache.instance_of?(Hash)
|
|
175
|
+
|
|
176
|
+
result = Liquid::C.native_parse_variable(markup, cache || nil)
|
|
177
|
+
return record(context, :syntax) unless result
|
|
178
|
+
return record(context, :tracing) if Liquid::C.native_parser_tracing?
|
|
179
|
+
return record(context, :hooks) unless compatible?(context, variable)
|
|
180
|
+
|
|
181
|
+
name, filters, pending = result
|
|
182
|
+
Liquid::C.native_publish_cache(cache, pending)
|
|
183
|
+
record(context, :accepted)
|
|
184
|
+
[name, filters]
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def tag_payload(markup, context, kind)
|
|
188
|
+
return if ENV["LIQUID_C_NATIVE_TAGS"] == "false"
|
|
189
|
+
return unless NATIVE_GUARDS || Liquid::C.native_frontend_candidate?(markup)
|
|
190
|
+
return unless compatible?(context)
|
|
191
|
+
return if context.liquid_c_nodes_disabled?
|
|
192
|
+
return unless context.error_mode == :strict && compatible?(context)
|
|
193
|
+
|
|
194
|
+
cache = context.instance_variable_get(:@expression_cache)
|
|
195
|
+
return unless cache.nil? || cache.equal?(false) || cache.instance_of?(Hash)
|
|
196
|
+
|
|
197
|
+
result = Liquid::C.public_send(:"native_parse_#{kind}", markup, cache || nil)
|
|
198
|
+
return unless result && !Liquid::C.native_parser_tracing? && compatible?(context)
|
|
199
|
+
|
|
200
|
+
result
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def commit_tag(context, result, kind)
|
|
204
|
+
payload, pending = result
|
|
205
|
+
cache = context.instance_variable_get(:@expression_cache)
|
|
206
|
+
Liquid::C.native_publish_cache(cache, pending)
|
|
207
|
+
record(context, :"accepted_#{kind}")
|
|
208
|
+
payload
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
private
|
|
212
|
+
|
|
213
|
+
def compatible?(context, variable = nil)
|
|
214
|
+
return false unless context.instance_of?(Liquid::ParseContext)
|
|
215
|
+
return false unless STANDARD_GUARDS
|
|
216
|
+
return true if NATIVE_GUARDS && Liquid::C.native_parser_methods_compatible?(context, variable)
|
|
217
|
+
return false unless CONSTANT_GUARDS.all? do |owner, name, value, standard, snapshot|
|
|
218
|
+
standard && Liquid::C.native_constant_matches?(owner, name, value, snapshot)
|
|
219
|
+
end
|
|
220
|
+
return false unless GUARDS.all? do |owner, name, method, standard|
|
|
221
|
+
standard && owner.instance_method(name) == method &&
|
|
222
|
+
([
|
|
223
|
+
:initialize,
|
|
224
|
+
:lax_parse_filter_expressions,
|
|
225
|
+
].include?(name) || owner.public_method_defined?(name))
|
|
226
|
+
end
|
|
227
|
+
return false unless CONSTRUCTORS.all? do |klass, name, method|
|
|
228
|
+
klass.singleton_class.public_method_defined?(name) &&
|
|
229
|
+
method.source_location.nil? && klass.method(name) == method
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
return false unless CONTEXT_GUARDS.all? do |_owner, name, method, _standard|
|
|
233
|
+
Liquid::C.native_public_method?(context, name) && context.method(name).unbind == method
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
scanner = context.instance_variable_get(:@string_scanner)
|
|
237
|
+
return false if scanner.frozen?
|
|
238
|
+
return false unless GUARDS.all? do |owner, name, method, _standard|
|
|
239
|
+
target = if owner == StringScanner
|
|
240
|
+
scanner
|
|
241
|
+
elsif owner == Liquid::Variable
|
|
242
|
+
variable
|
|
243
|
+
end
|
|
244
|
+
!target || ([:initialize, :lax_parse_filter_expressions].include?(name) ||
|
|
245
|
+
Liquid::C.native_public_method?(target, name)) && target.method(name).unbind == method
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
return true unless NATIVE_GUARDS
|
|
249
|
+
|
|
250
|
+
watchers = ENV["LIQUID_C_NATIVE_CONST_CACHE"] == "false" ? nil : CONSTANT_WATCHERS
|
|
251
|
+
Liquid::C.native_parser_register_guards(GUARD_SPECS, watchers, RubyVM)
|
|
252
|
+
Liquid::C.native_parser_methods_compatible?(context, variable)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def record(context, reason)
|
|
256
|
+
Liquid::C.native_record_stats(context, reason)
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Liquid
|
|
4
|
+
module C
|
|
5
|
+
module NativeTags
|
|
6
|
+
root = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib", "liquid")
|
|
7
|
+
specifications = {
|
|
8
|
+
condition: {
|
|
9
|
+
Liquid::If => [:parse_binary_comparisons, :parse_comparison, :parse_expression],
|
|
10
|
+
Liquid::Condition => [:initialize, :and, :or],
|
|
11
|
+
Liquid::Condition.singleton_class => [:parse_expression],
|
|
12
|
+
},
|
|
13
|
+
for: { Liquid::For => [:set_attribute, :parse_expression] },
|
|
14
|
+
expression: { Liquid::Tag => [:parse_expression] },
|
|
15
|
+
}
|
|
16
|
+
GUARDS = specifications.transform_values do |owners|
|
|
17
|
+
owners.flat_map do |owner, names|
|
|
18
|
+
names.map do |name|
|
|
19
|
+
method = owner.instance_method(name)
|
|
20
|
+
standard = method.original_name == name && method.source_location&.first&.start_with?(root + "/")
|
|
21
|
+
[owner, name, method, standard].freeze
|
|
22
|
+
end
|
|
23
|
+
end.freeze
|
|
24
|
+
end.freeze
|
|
25
|
+
CONDITION_CLASS = Liquid::Condition
|
|
26
|
+
CONSTRUCTORS = [:new, :allocate].map { |name| [name, CONDITION_CLASS.method(name)].freeze }.freeze
|
|
27
|
+
|
|
28
|
+
class << self
|
|
29
|
+
def compatible?(object, kind)
|
|
30
|
+
GUARDS.fetch(kind).all? do |owner, name, expected, standard|
|
|
31
|
+
standard && owner.instance_method(name) == expected &&
|
|
32
|
+
(kind == :expression || !object.is_a?(owner) || object.method(name).unbind == expected)
|
|
33
|
+
end && (kind != :condition || Liquid::Condition.equal?(CONDITION_CLASS) &&
|
|
34
|
+
CONSTRUCTORS.all? do |name, expected|
|
|
35
|
+
expected.source_location.nil? && expected.owner == Class && expected.original_name == name &&
|
|
36
|
+
CONDITION_CLASS.method(name) == expected
|
|
37
|
+
end)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def try_tag_header(object, markup, kind)
|
|
41
|
+
result = parse(object, markup, kind)
|
|
42
|
+
return unless result
|
|
43
|
+
|
|
44
|
+
context = object.instance_variable_get(:@parse_context)
|
|
45
|
+
payload = NativeParser.commit_tag(context, result, kind)
|
|
46
|
+
[payload]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def try_case_expression(object, markup)
|
|
50
|
+
return if ENV["LIQUID_C_NATIVE_CASE"] == "false"
|
|
51
|
+
|
|
52
|
+
result = parse(object, markup, :expression)
|
|
53
|
+
[NativeParser.commit_tag(object.instance_variable_get(:@parse_context), result, :case)] if result
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def parse(object, markup, kind)
|
|
57
|
+
return if Liquid::C.native_parser_tracing?
|
|
58
|
+
|
|
59
|
+
context = object.instance_variable_get(:@parse_context)
|
|
60
|
+
return if ENV["LIQUID_C_NATIVE_TAGS"] == "false" || context.instance_variable_get(:@error_mode) != :strict
|
|
61
|
+
return unless Liquid::C.instance_variable_get(:@enabled)
|
|
62
|
+
return if context.instance_variable_get(:@liquid_c_nodes_disabled)
|
|
63
|
+
return unless NativeParser::NATIVE_GUARDS || Liquid::C.native_frontend_candidate?(markup)
|
|
64
|
+
return unless compatible?(object, kind)
|
|
65
|
+
|
|
66
|
+
result = NativeParser.tag_payload(markup, context, kind)
|
|
67
|
+
return unless result && compatible?(object, kind)
|
|
68
|
+
|
|
69
|
+
result
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
original_if = Liquid::If.instance_method(:strict_parse)
|
|
74
|
+
if NativeParser::NATIVE_GUARDS && original_if.owner == Liquid::If && original_if.original_name == :strict_parse &&
|
|
75
|
+
original_if.source_location&.first == File.join(root, "tags", "if.rb")
|
|
76
|
+
Liquid::C.native_install_header_parser(original_if, false)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
original_for = Liquid::For.instance_method(:strict_parse)
|
|
80
|
+
if NativeParser::NATIVE_GUARDS && original_for.owner == Liquid::For &&
|
|
81
|
+
original_for.original_name == :strict_parse &&
|
|
82
|
+
original_for.source_location&.first == File.join(root, "tags", "for.rb")
|
|
83
|
+
Liquid::C.native_install_header_parser(original_for, true)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
original_context = Liquid::ParserSwitching.instance_method(:strict_parse_with_error_context)
|
|
87
|
+
if NativeParser::NATIVE_GUARDS && original_context.owner == Liquid::ParserSwitching &&
|
|
88
|
+
original_context.original_name == :strict_parse_with_error_context &&
|
|
89
|
+
original_context.source_location&.first == File.join(root, "parser_switching.rb")
|
|
90
|
+
Liquid::C.native_install_strict_context
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Case's legacy fragment matcher deliberately remains responsible for
|
|
94
|
+
# choosing the fragment. Its permissive semantics differ from Parser.
|
|
95
|
+
original_case = Liquid::Case.instance_method(:parse_expression)
|
|
96
|
+
if NativeParser::NATIVE_GUARDS && original_case.owner == Liquid::Tag &&
|
|
97
|
+
original_case.original_name == :parse_expression &&
|
|
98
|
+
original_case.source_location&.first == File.join(root, "tag.rb")
|
|
99
|
+
Liquid::C.native_install_case_parser
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
if Liquid::C.respond_to?(:native_register_identity_methods)
|
|
4
|
+
source = File.join(Gem.loaded_specs.fetch("liquid").full_gem_path, "lib", "liquid", "extensions.rb")
|
|
5
|
+
flags = [String, Array, Hash, Numeric, Range, TrueClass, FalseClass, NilClass].map do |klass|
|
|
6
|
+
ENV["LIQUID_C_IDENTITY_VALUES"] != "false" && klass.instance_method(:to_liquid).source_location&.first == source
|
|
7
|
+
end
|
|
8
|
+
Liquid::C.native_register_identity_methods(flags)
|
|
9
|
+
end
|