Skip to content

Commit

Permalink
Serialize the encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jun 20, 2023
1 parent cfe73c8 commit 8f85dd5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ The header is structured like the following table:
| `1` | major version number |
| `1` | minor version number |
| `1` | patch version number |
| varint | the length of the encoding name |
| | the encoding name |
| `4` | content pool offset |
| varint | content pool size |

Expand Down
6 changes: 6 additions & 0 deletions templates/java/org/yarp/Loader.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class Loader {
expect((byte) 4);
expect((byte) 0);

// This loads the name of the encoding. We don't actually do anything
// with it just yet.
int encodingLength = loadVarInt();
byte[] encodingName = new byte[encodingLength];
buffer.get(encodingName);

int constantPoolBufferOffset = buffer.getInt();
int constantPoolLength = loadVarInt();
this.constantPool = new ConstantPool(source, constantPoolBufferOffset, constantPoolLength);
Expand Down
11 changes: 8 additions & 3 deletions templates/lib/yarp/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@ module YARP
attr_reader :constant_pool_offset, :constant_pool

def initialize(source, serialized, io)
# TODO: This is wrong, we should be dumping and loading the encoding.
# For now, we assume that the source is UTF-8.
@encoding = Encoding::UTF_8
@source = source.dup.force_encoding(@encoding).freeze

@source = source.dup
@serialized = serialized
@io = io

@constant_pool_offset = nil
@constant_pool = nil
end

def load
io.read(4) => "YARP"
io.read(3).unpack("C3") => [0, 4, 0]

@encoding = Encoding.find(io.read(load_varint))
@source = source.force_encoding(@encoding).freeze

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

Expand Down
5 changes: 5 additions & 0 deletions templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {

void
yp_serialize_content(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
// First, serialize the encoding of the parser.
size_t encoding_length = strlen(parser->encoding.name);
yp_buffer_append_u32(buffer, yp_ulong_to_u32(encoding_length));
yp_buffer_append_str(buffer, parser->encoding.name, encoding_length);

// Here we're going to leave space for the offset of the constant pool in
// the buffer.
size_t offset = buffer->length;
Expand Down

0 comments on commit 8f85dd5

Please sign in to comment.