Skip to content

Commit

Permalink
[ruby/prism] Revert "Ensure serialized file is little endian"
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored and matzbot committed Nov 18, 2023
1 parent 24fe22a commit f479e62
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 34 deletions.
10 changes: 0 additions & 10 deletions prism/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,4 @@
# define snprintf _snprintf
#endif

/**
* Defined PRISM_WORDS_BIGENDIAN so we can ensure our serialization happens in
* little endian format regardless of platform.
*/
#if defined(WORDS_BIGENDIAN)
# define PRISM_WORDS_BIGENDIAN
#elif defined(AC_APPLE_UNIVERSAL_BUILD) && defined(__BIG_ENDIAN__)
# define PRISM_WORDS_BIGENDIAN
#endif

#endif
8 changes: 4 additions & 4 deletions prism/templates/lib/prism/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module Prism

comments, magic_comments, errors, warnings = load_metadata

@constant_pool_offset = io.read(4).unpack1("L<")
@constant_pool_offset = io.read(4).unpack1("L")
@constant_pool = Array.new(load_varint, nil)

[load_node, comments, magic_comments, errors, warnings]
Expand Down Expand Up @@ -167,7 +167,7 @@ module Prism
end

def load_serialized_length
io.read(4).unpack1("L<")
io.read(4).unpack1("L")
end

def load_optional_node
Expand Down Expand Up @@ -206,8 +206,8 @@ module Prism

unless constant
offset = constant_pool_offset + index * 8
start = serialized.unpack1("L<", offset: offset)
length = serialized.unpack1("L<", offset: offset + 4)
start = serialized.unpack1("L", offset: offset)
length = serialized.unpack1("L", offset: offset + 4)

constant =
if start.nobits?(1 << 31)
Expand Down
25 changes: 5 additions & 20 deletions prism/templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,6 @@ pm_serialize_string(pm_parser_t *parser, pm_string_t *string, pm_buffer_t *buffe
}
}

/**
* Serialize a 32-bit integer to the given address always in little-endian.
*/
static void
pm_serialize_32(char *address, uint32_t value) {
#ifdef PRISM_WORDS_BIGENDIAN
address[0] = (char) ((value >> 24) & 0xFF);
address[1] = (char) ((value >> 16) & 0xFF);
address[2] = (char) ((value >> 8) & 0xFF);
address[3] = (char) (value & 0xFF);
#else
memcpy(address, &value, sizeof(uint32_t));
#endif
}

static void
pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
Expand Down Expand Up @@ -133,7 +118,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
<%- if node.needs_serialized_length? -%>
// serialize length
uint32_t length = pm_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
pm_serialize_32(buffer->value + length_offset, length);
memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
<%- end -%>
break;
}
Expand Down Expand Up @@ -246,7 +231,7 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
// Now we're going to serialize the offset of the constant pool back where
// we left space for it.
uint32_t length = pm_sizet_to_u32(buffer->length);
pm_serialize_32(buffer->value + offset, length);
memcpy(buffer->value + offset, &length, sizeof(uint32_t));

// Now we're going to serialize the constant pool.
offset = buffer->length;
Expand All @@ -273,18 +258,18 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
assert(content_offset < owned_mask);
content_offset |= owned_mask;

pm_serialize_32(buffer->value + buffer_offset, content_offset);
memcpy(buffer->value + buffer_offset, &content_offset, 4);
pm_buffer_append_bytes(buffer, constant->start, constant->length);
} else {
// Since this is a shared constant, we are going to write its
// source offset directly into the buffer.
uint32_t source_offset = pm_ptrdifft_to_u32(constant->start - parser->start);
pm_serialize_32(buffer->value + buffer_offset, source_offset);
memcpy(buffer->value + buffer_offset, &source_offset, 4);
}

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

0 comments on commit f479e62

Please sign in to comment.