Skip to content

Commit f479e62

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Revert "Ensure serialized file is little endian"
ruby/prism@4cec275fff
1 parent 24fe22a commit f479e62

File tree

3 files changed

+9
-34
lines changed

3 files changed

+9
-34
lines changed

prism/defines.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,4 @@
7474
# define snprintf _snprintf
7575
#endif
7676

77-
/**
78-
* Defined PRISM_WORDS_BIGENDIAN so we can ensure our serialization happens in
79-
* little endian format regardless of platform.
80-
*/
81-
#if defined(WORDS_BIGENDIAN)
82-
# define PRISM_WORDS_BIGENDIAN
83-
#elif defined(AC_APPLE_UNIVERSAL_BUILD) && defined(__BIG_ENDIAN__)
84-
# define PRISM_WORDS_BIGENDIAN
85-
#endif
86-
8777
#endif

prism/templates/lib/prism/serialize.rb.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ module Prism
137137

138138
comments, magic_comments, errors, warnings = load_metadata
139139

140-
@constant_pool_offset = io.read(4).unpack1("L<")
140+
@constant_pool_offset = io.read(4).unpack1("L")
141141
@constant_pool = Array.new(load_varint, nil)
142142

143143
[load_node, comments, magic_comments, errors, warnings]
@@ -167,7 +167,7 @@ module Prism
167167
end
168168

169169
def load_serialized_length
170-
io.read(4).unpack1("L<")
170+
io.read(4).unpack1("L")
171171
end
172172

173173
def load_optional_node
@@ -206,8 +206,8 @@ module Prism
206206

207207
unless constant
208208
offset = constant_pool_offset + index * 8
209-
start = serialized.unpack1("L<", offset: offset)
210-
length = serialized.unpack1("L<", offset: offset + 4)
209+
start = serialized.unpack1("L", offset: offset)
210+
length = serialized.unpack1("L", offset: offset + 4)
211211

212212
constant =
213213
if start.nobits?(1 << 31)

prism/templates/src/serialize.c.erb

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,6 @@ 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-
6550
static void
6651
pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
6752
pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
@@ -133,7 +118,7 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
133118
<%- if node.needs_serialized_length? -%>
134119
// serialize length
135120
uint32_t length = pm_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
136-
pm_serialize_32(buffer->value + length_offset, length);
121+
memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
137122
<%- end -%>
138123
break;
139124
}
@@ -246,7 +231,7 @@ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer)
246231
// Now we're going to serialize the offset of the constant pool back where
247232
// we left space for it.
248233
uint32_t length = pm_sizet_to_u32(buffer->length);
249-
pm_serialize_32(buffer->value + offset, length);
234+
memcpy(buffer->value + offset, &length, sizeof(uint32_t));
250235

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

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

285270
// Now we can write the length of the constant into the buffer.
286271
uint32_t constant_length = pm_sizet_to_u32(constant->length);
287-
pm_serialize_32(buffer->value + buffer_offset + 4, constant_length);
272+
memcpy(buffer->value + buffer_offset + 4, &constant_length, 4);
288273
}
289274
}
290275
}

0 commit comments

Comments
 (0)