@@ -47,6 +47,21 @@ pm_serialize_string(pm_parser_t *parser, pm_string_t *string, pm_buffer_t *buffe
4747 }
4848}
4949
50+ /**
51+ * Serialize a 32-bit integer to the given address always in little-endian.
52+ */
53+ static void
54+ pm_serialize_32(char *address, uint32_t value) {
55+ #ifdef PRISM_WORDS_BIGENDIAN
56+ address[0] = (char) ((value > > 24) & 0xFF);
57+ address[1] = (char) ((value > > 16) & 0xFF);
58+ address[2] = (char) ((value > > 8) & 0xFF);
59+ address[3] = (char) (value & 0xFF);
60+ #else
61+ memcpy(address, &value, sizeof(uint32_t));
62+ #endif
63+ }
64+
5065static void
5166pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
5267 pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
@@ -118,7 +133,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
118133 <%- if node . needs_serialized_length? -%>
119134 // serialize length
120135 uint32_t length = pm_sizet_to_u32(buffer-> length - offset - sizeof(uint32_t));
121- memcpy (buffer-> value + length_offset, & length, sizeof(uint32_t) );
136+ pm_serialize_32 (buffer-> value + length_offset, length);
122137 <%- end -%>
123138 break;
124139 }
@@ -231,7 +246,7 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
231246 // Now we're going to serialize the offset of the constant pool back where
232247 // we left space for it.
233248 uint32_t length = pm_sizet_to_u32(buffer-> length);
234- memcpy (buffer-> value + offset, & length, sizeof(uint32_t) );
249+ pm_serialize_32 (buffer-> value + offset, length);
235250
236251 // Now we're going to serialize the constant pool.
237252 offset = buffer-> length;
@@ -258,18 +273,18 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
258273 assert(content_offset < owned _mask);
259274 content_offset | = owned_mask;
260275
261- memcpy (buffer-> value + buffer_offset, & content_offset, 4 );
276+ pm_serialize_32 (buffer-> value + buffer_offset, content_offset);
262277 pm_buffer_append_bytes(buffer, constant-> start, constant-> length);
263278 } else {
264279 // Since this is a shared constant, we are going to write its
265280 // source offset directly into the buffer.
266281 uint32_t source_offset = pm_ptrdifft_to_u32(constant-> start - parser-> start);
267- memcpy (buffer-> value + buffer_offset, & source_offset, 4 );
282+ pm_serialize_32 (buffer-> value + buffer_offset, source_offset);
268283 }
269284
270285 // Now we can write the length of the constant into the buffer.
271286 uint32_t constant_length = pm_sizet_to_u32(constant-> length);
272- memcpy (buffer-> value + buffer_offset + 4, & constant_length, 4 );
287+ pm_serialize_32 (buffer-> value + buffer_offset + 4, constant_length);
273288 }
274289 }
275290}
0 commit comments