Skip to content

Commit bafe96f

Browse files
committed
Implement serialize
1 parent 8968499 commit bafe96f

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

ext/liquid_c/block.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,16 @@ static VALUE block_body_disassemble(VALUE self)
513513
document_body_get_constants_ptr(entry), (const VALUE *)body->tags.data);
514514
}
515515

516+
static VALUE block_body_dump(VALUE self)
517+
{
518+
block_body_t *body;
519+
BlockBody_Get_Struct(self, body);
520+
ensure_body_compiled(body);
521+
522+
return document_body_dump(body->as.compiled.document_body_entry.body,
523+
(uint32_t)body->as.compiled.document_body_entry.buffer_offset);
524+
}
525+
516526

517527
static VALUE block_body_add_evaluate_expression(VALUE self, VALUE expression)
518528
{
@@ -602,6 +612,7 @@ void liquid_define_block_body()
602612
rb_define_method(cLiquidCBlockBody, "blank?", block_body_blank_p, 0);
603613
rb_define_method(cLiquidCBlockBody, "nodelist", block_body_nodelist, 0);
604614
rb_define_method(cLiquidCBlockBody, "disassemble", block_body_disassemble, 0);
615+
rb_define_method(cLiquidCBlockBody, "dump", block_body_dump, 0);
605616

606617
rb_define_method(cLiquidCBlockBody, "add_evaluate_expression", block_body_add_evaluate_expression, 1);
607618
rb_define_method(cLiquidCBlockBody, "add_find_variable", block_body_add_find_variable, 1);

ext/liquid_c/document_body.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ void document_body_write_block_body(VALUE self, bool blank, uint32_t render_scor
129129
memcpy(body->buffer.data + buf_block_body_offset, &buf_block_body, sizeof(block_body_header_t));
130130
}
131131

132+
133+
VALUE document_body_dump(document_body_t *body, uint32_t entrypoint_block_index)
134+
{
135+
assert(BUILTIN_TYPE(body->constants) == T_ARRAY);
136+
137+
uint32_t buffer_len = (uint32_t)c_buffer_size(&body->buffer);
138+
139+
VALUE constants = rb_marshal_dump(body->constants, Qnil);
140+
uint32_t constants_len = (uint32_t)RSTRING_LEN(constants);
141+
142+
VALUE str = rb_str_buf_new(sizeof(document_body_header_t) + buffer_len + constants_len);
143+
144+
document_body_header_t header = {
145+
.entrypoint_block_index = entrypoint_block_index,
146+
.buffer_offset = sizeof(document_body_header_t),
147+
.buffer_len = buffer_len,
148+
.constants_offset = sizeof(document_body_header_t) + buffer_len,
149+
.constants_len = constants_len
150+
};
151+
152+
rb_str_cat(str, (const char *)&header, sizeof(document_body_header_t));
153+
rb_str_cat(str, (const char *)body->buffer.data, buffer_len);
154+
rb_str_append(str, constants);
155+
156+
return str;
157+
}
158+
159+
132160
void liquid_define_document_body()
133161
{
134162
cLiquidCDocumentBody = rb_define_class_under(mLiquidC, "DocumentBody", rb_cObject);

ext/liquid_c/document_body.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ typedef struct document_body {
2828
c_buffer_t buffer;
2929
} document_body_t;
3030

31+
typedef struct document_body_header {
32+
uint32_t entrypoint_block_index;
33+
uint32_t buffer_offset;
34+
uint32_t buffer_len;
35+
uint32_t constants_offset;
36+
uint32_t constants_len;
37+
} document_body_header_t;
38+
3139
typedef struct document_body_entry {
3240
document_body_t *body;
3341
size_t buffer_offset;
@@ -36,6 +44,7 @@ typedef struct document_body_entry {
3644
void liquid_define_document_body();
3745
VALUE document_body_new_instance();
3846
void document_body_write_block_body(VALUE self, bool blank, uint32_t render_score, vm_assembler_t *code, document_body_entry_t *entry);
47+
VALUE document_body_dump(document_body_t *body, uint32_t entrypoint_block_index);
3948

4049
static inline void document_body_entry_mark(document_body_entry_t *entry)
4150
{

lib/liquid/c.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ def parse(tokenizer, parse_context)
109109
end
110110
end
111111

112+
Liquid::Template.class_eval do
113+
def dump
114+
@root.dump
115+
end
116+
end
117+
118+
Liquid::Document.class_eval do
119+
def dump
120+
@body.dump
121+
end
122+
end
123+
112124
Liquid::Variable.class_eval do
113125
class << self
114126
# @api private

0 commit comments

Comments
 (0)