Skip to content

Commit

Permalink
Implement serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhu2118 committed Dec 16, 2020
1 parent 0d28189 commit 986bff5
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
11 changes: 11 additions & 0 deletions ext/liquid_c/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,16 @@ static VALUE block_body_disassemble(VALUE self)
document_body_get_constants_ptr(entry), (const VALUE *)body->tags.data);
}

static VALUE block_body_dump(VALUE self)
{
block_body_t *body;
BlockBody_Get_Struct(self, body);
ensure_body_compiled(body);

return document_body_dump(body->as.compiled.document_body_entry.body,
(uint32_t)body->as.compiled.document_body_entry.buffer_offset);
}


static VALUE block_body_add_evaluate_expression(VALUE self, VALUE expression)
{
Expand Down Expand Up @@ -602,6 +612,7 @@ void liquid_define_block_body()
rb_define_method(cLiquidCBlockBody, "blank?", block_body_blank_p, 0);
rb_define_method(cLiquidCBlockBody, "nodelist", block_body_nodelist, 0);
rb_define_method(cLiquidCBlockBody, "disassemble", block_body_disassemble, 0);
rb_define_method(cLiquidCBlockBody, "dump", block_body_dump, 0);

rb_define_method(cLiquidCBlockBody, "add_evaluate_expression", block_body_add_evaluate_expression, 1);
rb_define_method(cLiquidCBlockBody, "add_find_variable", block_body_add_find_variable, 1);
Expand Down
28 changes: 28 additions & 0 deletions ext/liquid_c/document_body.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,34 @@ void document_body_write_block_body(VALUE self, bool blank, uint32_t render_scor
memcpy(body->buffer.data + buf_block_body_offset, &buf_block_body, sizeof(block_body_header_t));
}


VALUE document_body_dump(document_body_t *body, uint32_t entrypoint_block_index)
{
assert(BUILTIN_TYPE(body->constants) == T_ARRAY);

uint32_t buffer_len = (uint32_t)c_buffer_size(&body->buffer);

VALUE constants = rb_marshal_dump(body->constants, Qnil);
uint32_t constants_len = (uint32_t)RSTRING_LEN(constants);

VALUE str = rb_str_buf_new(sizeof(document_body_header_t) + buffer_len + constants_len);

document_body_header_t header = {
.entrypoint_block_index = entrypoint_block_index,
.buffer_offset = sizeof(document_body_header_t),
.buffer_len = buffer_len,
.constants_offset = sizeof(document_body_header_t) + buffer_len,
.constants_len = constants_len
};

rb_str_cat(str, (const char *)&header, sizeof(document_body_header_t));
rb_str_cat(str, (const char *)body->buffer.data, buffer_len);
rb_str_append(str, constants);

return str;
}


void liquid_define_document_body()
{
cLiquidCDocumentBody = rb_define_class_under(mLiquidC, "DocumentBody", rb_cObject);
Expand Down
9 changes: 9 additions & 0 deletions ext/liquid_c/document_body.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ typedef struct document_body {
c_buffer_t buffer;
} document_body_t;

typedef struct document_body_header {
uint32_t entrypoint_block_index;
uint32_t buffer_offset;
uint32_t buffer_len;
uint32_t constants_offset;
uint32_t constants_len;
} document_body_header_t;

typedef struct document_body_entry {
document_body_t *body;
size_t buffer_offset;
Expand All @@ -36,6 +44,7 @@ typedef struct document_body_entry {
void liquid_define_document_body();
VALUE document_body_new_instance();
void document_body_write_block_body(VALUE self, bool blank, uint32_t render_score, vm_assembler_t *code, document_body_entry_t *entry);
VALUE document_body_dump(document_body_t *body, uint32_t entrypoint_block_index);

static inline void document_body_entry_mark(document_body_entry_t *entry)
{
Expand Down
12 changes: 12 additions & 0 deletions lib/liquid/c.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ def parse(tokenizer, parse_context)
end
end

Liquid::Template.class_eval do
def dump
@root.dump
end
end

Liquid::Document.class_eval do
def dump
@body.dump
end
end

Liquid::Variable.class_eval do
class << self
# @api private
Expand Down

0 comments on commit 986bff5

Please sign in to comment.