Skip to content

Commit 54468de

Browse files
committed
Copy and embed for serialization format
1 parent 7414a8c commit 54468de

1 file changed

Lines changed: 15 additions & 42 deletions

File tree

templates/src/serialize.c.erb

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,11 @@ pm_serialize_location(const pm_location_t *location, pm_buffer_t *buffer) {
2626
}
2727

2828
static void
29-
pm_serialize_string(const pm_parser_t *parser, const pm_string_t *string, pm_buffer_t *buffer) {
30-
switch (string->type) {
31-
case PM_STRING_SHARED: {
32-
pm_buffer_append_byte(buffer, 1);
33-
pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(pm_string_source(string) - parser->start));
34-
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_string_length(string)));
35-
break;
36-
}
37-
case PM_STRING_OWNED:
38-
case PM_STRING_CONSTANT: {
39-
uint32_t length = pm_sizet_to_u32(pm_string_length(string));
40-
pm_buffer_append_byte(buffer, 2);
41-
pm_buffer_append_varuint(buffer, length);
42-
pm_buffer_append_bytes(buffer, pm_string_source(string), length);
43-
break;
44-
}
45-
#ifdef PRISM_HAS_MMAP
46-
case PM_STRING_MAPPED:
47-
assert(false && "Cannot serialize mapped strings.");
48-
break;
49-
#endif
50-
}
29+
pm_serialize_string(const pm_string_t *string, pm_buffer_t *buffer) {
30+
uint32_t length = pm_sizet_to_u32(pm_string_length(string));
31+
pm_buffer_append_byte(buffer, 2);
32+
pm_buffer_append_varuint(buffer, length);
33+
pm_buffer_append_bytes(buffer, pm_string_source(string), length);
5134
}
5235

5336
static void
@@ -102,7 +85,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
10285
pm_serialize_node(parser, (pm_node_t *)((pm_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
10386
}
10487
<%- when Prism::Template::StringField -%>
105-
pm_serialize_string(parser, &((pm_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
88+
pm_serialize_string(&((pm_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
10689
<%- when Prism::Template::NodeListField -%>
10790
uint32_t <%= field.name %>_size = pm_sizet_to_u32(((pm_<%= node.human %>_t *)node)-><%= field.name %>.size);
10891
pm_buffer_append_varuint(buffer, <%= field.name %>_size);
@@ -304,28 +287,18 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
304287
pm_constant_t *constant = &parser->constant_pool.constants[bucket->id - 1];
305288
size_t buffer_offset = offset + ((((size_t)bucket->id) - 1) * 8);
306289

307-
if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED || bucket->type == PM_CONSTANT_POOL_BUCKET_CONSTANT) {
308-
// Since this is an owned or constant constant, we are going to
309-
// write its contents into the buffer after the constant pool.
310-
// So effectively in place of the source offset, we have a
311-
// buffer offset. We will add a leading 1 to indicate that this
312-
// is a buffer offset.
313-
uint32_t content_offset = pm_sizet_to_u32(buffer->length);
314-
uint32_t owned_mask = 1U << 31;
290+
// Write the constant contents into the buffer after the constant
291+
// pool. In place of the source offset, we store a buffer offset
292+
// with the high bit set to indicate embedded content.
293+
uint32_t content_offset = pm_sizet_to_u32(buffer->length);
294+
uint32_t owned_mask = 1U << 31;
315295

316-
assert(content_offset < owned_mask);
317-
content_offset |= owned_mask;
296+
assert(content_offset < owned_mask);
297+
content_offset |= owned_mask;
318298

319-
memcpy(buffer->value + buffer_offset, &content_offset, 4);
320-
pm_buffer_append_bytes(buffer, constant->start, constant->length);
321-
} else {
322-
// Since this is a shared constant, we are going to write its
323-
// source offset directly into the buffer.
324-
uint32_t source_offset = pm_ptrdifft_to_u32(constant->start - parser->start);
325-
memcpy(buffer->value + buffer_offset, &source_offset, 4);
326-
}
299+
memcpy(buffer->value + buffer_offset, &content_offset, 4);
300+
pm_buffer_append_bytes(buffer, constant->start, constant->length);
327301

328-
// Now we can write the length of the constant into the buffer.
329302
uint32_t constant_length = pm_sizet_to_u32(constant->length);
330303
memcpy(buffer->value + buffer_offset + 4, &constant_length, 4);
331304
}

0 commit comments

Comments
 (0)