Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add length() and endOffset() to migrate to (startOffset, length) in a compatible way #932

Merged
merged 2 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def initialize(config)

# Should emit serialized length of node so implementations can skip
# the node to enable lazy parsing.
def needs_length?
def needs_serialized_length?
@name == "DefNode"
end
end
Expand Down
2 changes: 1 addition & 1 deletion bin/templates/java/org/yarp/Loader.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public class Loader {
<%- nodes.each_with_index do |node, index| -%>
case <%= index + 1 %>:
<%-
params = node.needs_length? ? ["buffer.getInt()"] : []
params = node.needs_serialized_length? ? ["buffer.getInt()"] : []
params.concat node.params.map { |param|
case param
when NodeParam then "#{param.java_cast}loadNode()"
Expand Down
34 changes: 29 additions & 5 deletions bin/templates/java/org/yarp/Nodes.java.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public abstract class Nodes {
this.startOffset = startOffset;
this.endOffset = endOffset;
}

public int length() {
return endOffset - startOffset;
}

public int endOffset() {
return endOffset;
}
}

public static final class Location {
Expand All @@ -39,6 +47,14 @@ public abstract class Nodes {
this.startOffset = startOffset;
this.endOffset = endOffset;
}

public int length() {
return endOffset - startOffset;
}

public int endOffset() {
return endOffset;
}
}

public static abstract class Node {
Expand All @@ -53,6 +69,14 @@ public abstract class Nodes {
this.endOffset = endOffset;
}

public int length() {
return endOffset - startOffset;
}

public int endOffset() {
return endOffset;
}

public abstract <T> T accept(AbstractNodeVisitor<T> visitor);

public abstract Node[] childNodes();
Expand All @@ -78,22 +102,22 @@ public abstract class Nodes {

<%= "#{node.comment.split("\n").map { |line| "// #{line}" }.join("\n ")}\n" if node.comment -%>
public static final class <%= node.name -%> extends Node {
<%- if node.needs_length? -%>
public final int length;
<%- if node.needs_serialized_length? -%>
public final int serializedLength;
<%- end -%>
<%- node.params.each do |param| -%>
public final <%= param.java_type %> <%= param.name %>;<%= ' // optional' if param.class.name.start_with?('Optional') %>
<%- end -%>

<%-
params = node.needs_length? ? ["int length"] : []
params = node.needs_serialized_length? ? ["int serializedLength"] : []
params.concat node.params.map { "#{_1.java_type} #{_1.name}" }
params.concat ["int startOffset", "int endOffset"]
-%>
public <%=node.name -%>(<%= params.join(", ") %>) {
super(startOffset, endOffset);
<%- if node.needs_length? -%>
this.length = length;
<%- if node.needs_serialized_length? -%>
this.serializedLength = serializedLength;
<%- end -%>
<%- node.params.each do |param| -%>
this.<%= param.name %> = <%= param.name %>;
Expand Down
2 changes: 1 addition & 1 deletion bin/templates/lib/yarp/serialize.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module YARP
case type
<%- nodes.each_with_index do |node, index| -%>
when <%= index + 1 %> then
<%- if node.needs_length? -%>
<%- if node.needs_serialized_length? -%>
load_serialized_length
<%- end -%>
<%= node.name %>.new(<%= (node.params.map { |param|
Expand Down
4 changes: 2 additions & 2 deletions bin/templates/src/serialize.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
switch (node->type) {
<%- nodes.each do |node| -%>
case <%= node.type %>: {
<%- if node.needs_length? -%>
<%- if node.needs_serialized_length? -%>
// serialize length
// encoding of location u32s make us need to save this offset.
size_t length_offset = buffer->length;
Expand Down Expand Up @@ -91,7 +91,7 @@ yp_serialize_node(yp_parser_t *parser, yp_node_t *node, yp_buffer_t *buffer) {
<%- raise -%>
<%- end -%>
<%- end -%>
<%- if node.needs_length? -%>
<%- if node.needs_serialized_length? -%>
// serialize length
uint32_t length = yp_ulong_to_u32(buffer->length - offset - sizeof(uint32_t));
memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
Expand Down