Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ext/liquid_c/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ static VALUE internal_expression_parse(parser_t *p)

static VALUE expression_strict_parse(VALUE klass, VALUE markup)
{
if (NIL_P(markup))
return Qnil;

StringValue(markup);
char *start = RSTRING_PTR(markup);

Expand Down
34 changes: 22 additions & 12 deletions lib/liquid/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ def new_block_body
end

alias_method :ruby_new_tokenizer, :new_tokenizer

def new_tokenizer(source, start_line_number: nil, for_liquid_tag: false)
unless liquid_c_nodes_disabled?
source = source.to_s.to_str
Expand All @@ -85,6 +84,14 @@ def new_tokenizer(source, start_line_number: nil, for_liquid_tag: false)
ruby_new_tokenizer(source, start_line_number: start_line_number, for_liquid_tag: for_liquid_tag)
end

def parse_expression(markup)
if liquid_c_nodes_disabled?
Liquid::Expression.ruby_parse(markup)
else
Liquid::C::Expression.lax_parse(markup)
end
end

# @api private
def liquid_c_nodes_disabled?
# Liquid::Profiler exposes the internal parse tree that we don't want to build when
Expand Down Expand Up @@ -195,21 +202,22 @@ def arg_exc_to_liquid_exc(argument_error)
end
end

Liquid::C::Expression.class_eval do
class << self
def lax_parse(markup)
strict_parse(markup)
rescue Liquid::SyntaxError
Liquid::Expression.ruby_parse(markup)
end
end
end

Liquid::Expression.class_eval do
class << self
alias_method :ruby_parse, :parse

def parse(markup)
return nil unless markup

if Liquid::C.enabled
begin
return Liquid::C::Expression.strict_parse(markup)
rescue Liquid::SyntaxError
# no-op
end
end
ruby_parse(markup)
def c_parse(markup)
Liquid::C::Expression.lax_parse(markup)
end
end
end
Expand Down Expand Up @@ -251,10 +259,12 @@ def enabled=(value)
Liquid::Context.send(:alias_method, :evaluate, :c_evaluate)
Liquid::Context.send(:alias_method, :find_variable, :c_find_variable_kwarg)
Liquid::Context.send(:alias_method, :strict_variables=, :c_strict_variables=)
Liquid::Expression.singleton_class.send(:alias_method, :parse, :c_parse)
else
Liquid::Context.send(:alias_method, :evaluate, :ruby_evaluate)
Liquid::Context.send(:alias_method, :find_variable, :ruby_find_variable)
Liquid::Context.send(:alias_method, :strict_variables=, :ruby_strict_variables=)
Liquid::Expression.singleton_class.send(:alias_method, :parse, :ruby_parse)
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/unit/expression_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,18 @@ def test_disassemble_int16
ASM
end

def test_disable_c_nodes
context = Liquid::Context.new({ "x" => 123 })

expr = Liquid::ParseContext.new.parse_expression('x')
assert_instance_of(Liquid::C::Expression, expr)
assert_equal(123, context.evaluate(expr))

expr = Liquid::ParseContext.new(disable_liquid_c_nodes: true).parse_expression('x')
assert_instance_of(Liquid::VariableLookup, expr)
assert_equal(123, context.evaluate(expr))
end

private

class ReturnKeyDrop < Liquid::Drop
Expand Down