Skip to content

Commit

Permalink
Refactor variable compilation to allow evaluation without write
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanahsmith committed Oct 20, 2020
1 parent d613a98 commit ff04785
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
3 changes: 1 addition & 2 deletions ext/liquid_c/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ static tag_markup_t internal_block_body_parse(block_body_t *body, parse_context_
.markup_end = token.str_trimmed + token.len_trimmed,
.body = body,
.parse_context = parse_context->ruby_obj,
.line_number = token_start_line_number,
};
internal_variable_parse(&parse_args);
internal_variable_compile(&parse_args, token_start_line_number);
render_score_increment += 1;
body->blank = false;
break;
Expand Down
3 changes: 1 addition & 2 deletions ext/liquid_c/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ static VALUE echo_class_compile(VALUE klass, VALUE tag_name, VALUE markup,
.markup_end = RSTRING_PTR(markup) + RSTRING_LEN(markup),
.body = body,
.parse_context = parse_context_obj,
.line_number = tokenizer->line_number,
};
internal_variable_parse(&parse_args);
internal_variable_compile(&parse_args, tokenizer->line_number);
return Qnil;
}

Expand Down
20 changes: 12 additions & 8 deletions ext/liquid_c/variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ static VALUE try_variable_strict_parse(VALUE uncast_args)
init_parser(&p, parse_args->markup, parse_args->markup_end);
vm_assembler_t *code = &parse_args->body->code;

if (p.cur.type == TOKEN_EOS)
if (p.cur.type == TOKEN_EOS) {
vm_assembler_add_push_nil(code);
return Qnil;

vm_assembler_add_render_variable_rescue(code, parse_args->line_number);
}

parse_and_compile_expression(&p, code);

Expand Down Expand Up @@ -70,8 +70,6 @@ static VALUE try_variable_strict_parse(VALUE uncast_args)
vm_assembler_add_filter(code, filter_name, arg_count);
}

vm_assembler_add_pop_write(code);

parser_must_consume(&p, TOKEN_EOS);

return Qnil;
Expand Down Expand Up @@ -106,17 +104,15 @@ static VALUE variable_strict_parse_rescue(VALUE uncast_args, VALUE exception)

// lax parse
code->protected_stack_size = code->stack_size;
vm_assembler_add_render_variable_rescue(code, parse_args->line_number);
rb_funcall(variable_obj, id_compile_evaluate, 1, parse_args->body->obj);
if (code->stack_size != code->protected_stack_size + 1) {
rb_raise(rb_eRuntimeError, "Liquid::Variable#compile_evaluate didn't leave exactly 1 new element on the stack");
}
vm_assembler_add_pop_write(code);

return Qnil;
}

void internal_variable_parse(variable_parse_args_t *parse_args)
void internal_variable_compile_evaluate(variable_parse_args_t *parse_args)
{
vm_assembler_t *code = &parse_args->body->code;
variable_strict_parse_rescue_t rescue_args = {
Expand All @@ -128,6 +124,14 @@ void internal_variable_parse(variable_parse_args_t *parse_args)
rb_rescue(try_variable_strict_parse, (VALUE)parse_args, variable_strict_parse_rescue, (VALUE)&rescue_args);
}

void internal_variable_compile(variable_parse_args_t *parse_args, unsigned int line_number)
{
vm_assembler_t *code = &parse_args->body->code;
vm_assembler_add_render_variable_rescue(code, line_number);
internal_variable_compile_evaluate(parse_args);
vm_assembler_add_pop_write(code);
}

void init_liquid_variable(void)
{
id_rescue_strict_parse_syntax_error = rb_intern("rescue_strict_parse_syntax_error");
Expand Down
4 changes: 2 additions & 2 deletions ext/liquid_c/variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ typedef struct variable_parse_args {
const char *markup_end;
block_body_t *body;
VALUE parse_context;
unsigned int line_number;
} variable_parse_args_t;

void init_liquid_variable(void);
void internal_variable_parse(variable_parse_args_t *parse_args);
void internal_variable_compile(variable_parse_args_t *parse_args, unsigned int line_number);
void internal_variable_compile_evaluate(variable_parse_args_t *parse_args);

#endif

0 comments on commit ff04785

Please sign in to comment.