Skip to content

Commit 1b02ef4

Browse files
committed
Move semantic field flag into C sources
Rather than templating two versions of sources with and without non-semantic fields, we can make that determination at build time. Passing -DPRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 to `make` will force that variable to be literally true, and the compiler will eliminate `if` blocks that use it to conditionally serialize non- semantic data. This simplifies the templates by removing that variation and allows building both forms of the library from a single generated set of sources.
1 parent b81eba8 commit 1b02ef4

File tree

9 files changed

+41
-35
lines changed

9 files changed

+41
-35
lines changed

.github/workflows/java-wasm-bindings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
bundler-cache: true
2828

2929
- name: rake templates
30-
run: PRISM_EXCLUDE_PRETTYPRINT=1 PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 bundle exec rake templates
30+
run: PRISM_EXCLUDE_PRETTYPRINT=1 PRISM_OMIT_NODE_ID=1 bundle exec rake templates
3131

3232
- name: Set up WASI-SDK
3333
run: |

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ java/wasm/src/main/wasm/prism.wasm: Makefile $(SOURCES) $(HEADERS)
5656
$(Q) $(MAKEDIRS) $(@D)
5757
$(Q) $(WASI_SDK_PATH)/bin/clang \
5858
$(DEBUG_FLAGS) \
59-
-DPRISM_EXCLUDE_PRETTYPRINT -DPRISM_EXPORT_SYMBOLS -D_WASI_EMULATED_MMAN \
59+
-DPRISM_EXCLUDE_PRETTYPRINT -DPRISM_EXPORT_SYMBOLS -D_WASI_EMULATED_MMAN -DPRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1 \
6060
-lwasi-emulated-mman $(CPPFLAGS) $(JAVA_WASM_CFLAGS) \
6161
-Wl,--export-all -Wl,--no-entry -mexec-model=reactor -lc++ -lc++abi \
6262
-o $@ $(SOURCES)

rakelib/serialization.rake

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# frozen_string_literal: true
22

33
task "test:java_loader" do
4-
# Recompile with PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1
5-
# Due to some JRuby bug this does not get propagated to the compile task, so require the caller to set the env var
6-
# ENV["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"] = "1"
7-
raise "this task requires $SERIALIZE_ONLY_SEMANTICS_FIELDS to be set" unless ENV["PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS"]
8-
94
Rake::Task["clobber"].invoke
5+
# All Java API consumers want semantic-only build
6+
ENV["CFLAGS"] = "-DPRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS=1"
107
Rake::Task["test:java_loader:internal"].invoke
118
end
129

src/prism.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22843,7 +22843,7 @@ pm_serialize_header(pm_buffer_t *buffer) {
2284322843
pm_buffer_append_byte(buffer, PRISM_VERSION_MAJOR);
2284422844
pm_buffer_append_byte(buffer, PRISM_VERSION_MINOR);
2284522845
pm_buffer_append_byte(buffer, PRISM_VERSION_PATCH);
22846-
pm_buffer_append_byte(buffer, PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS ? 1 : 0);
22846+
pm_buffer_append_byte(buffer, PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS);
2284722847
}
2284822848

2284922849
/**

templates/include/prism/ast.h.erb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,13 @@ PRISM_EXPORTED_FUNCTION pm_<%= node.human %>_t * pm_<%= node.human %>_new(pm_are
268268
<%- end -%>
269269

270270
/**
271-
* When we're serializing to Java, we want to skip serializing the location
272-
* fields as they won't be used by JRuby or TruffleRuby. This boolean allows us
273-
* to specify that through the environment. It will never be true except for in
274-
* those build systems.
275-
*/
276-
#define PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS <%= Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS ? 1 : 0 %>
271+
* When we're serializing to Java, we want to skip serializing the location
272+
* fields as they won't be used by JRuby or TruffleRuby. This boolean allows us
273+
* to specify that through the environment. It will never be true except for in
274+
* those build systems.
275+
*/
276+
#ifndef PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS
277+
#define PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS 0
278+
#endif
277279

278280
#endif

templates/java/api/src/main/java-templates/org/ruby_lang/prism/Loader.java.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ public class Loader {
326326
}
327327

328328
<%-
329-
base_params = [*("nodeId" if Prism::Template::INCLUDE_NODE_ID), "startOffset", "length"]
329+
base_params = [*("nodeId" unless Prism::Template::OMIT_NODE_ID), "startOffset", "length"]
330330
base_params_sig = base_params.map { "int #{_1}" }.join(", ")
331331
-%>
332332
private Nodes.Node loadNode() {
333333
int type = buffer.get() & 0xFF;
334-
<%- if Prism::Template::INCLUDE_NODE_ID -%>
334+
<%- unless Prism::Template::OMIT_NODE_ID -%>
335335
int nodeId = loadVarUInt();
336336
<%- end -%>
337337
int startOffset = loadVarUInt();
@@ -343,7 +343,7 @@ public class Loader {
343343
case <%= index + 1 %>:
344344
<%-
345345
params = []
346-
params << "nodeId" if Prism::Template::INCLUDE_NODE_ID
346+
params << "nodeId" unless Prism::Template::OMIT_NODE_ID
347347
params << "startOffset" << "length"
348348
params << "buffer.getInt()" << "null" if node.needs_serialized_length?
349349
params << "loadFlags()" if node.flags

templates/java/api/src/main/java-templates/org/ruby_lang/prism/Nodes.java.erb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ public abstract class Nodes {
8989

9090
public static final Node[] EMPTY_ARRAY = {};
9191

92-
<%- if Prism::Template::INCLUDE_NODE_ID -%>
92+
<%- unless Prism::Template::OMIT_NODE_ID -%>
9393
public final int nodeId;
9494
<%- end -%>
9595
public final int startOffset;
9696
public final int length;
9797
private boolean newLineFlag = false;
9898

99-
<%- if Prism::Template::INCLUDE_NODE_ID -%>
99+
<%- unless Prism::Template::OMIT_NODE_ID -%>
100100
public Node(int nodeId, int startOffset, int length) {
101101
this.nodeId = nodeId;
102102
<%- else -%>
@@ -246,7 +246,7 @@ public abstract class Nodes {
246246

247247
<%-
248248
params = []
249-
params << "int nodeId" if Prism::Template::INCLUDE_NODE_ID
249+
params << "int nodeId" unless Prism::Template::OMIT_NODE_ID
250250
params << "int startOffset" << "int length"
251251
if node.needs_serialized_length?
252252
params << "int serializedLength"
@@ -256,7 +256,7 @@ public abstract class Nodes {
256256
params.concat(node.semantic_fields.map { |field| "#{field.java_type} #{field.name}" })
257257
-%>
258258
public <%= node.name -%>(<%= params.join(", ") %>) {
259-
<%- if Prism::Template::INCLUDE_NODE_ID -%>
259+
<%- unless Prism::Template::OMIT_NODE_ID -%>
260260
super(nodeId, startOffset, length);
261261
<%- else -%>
262262
super(startOffset, length);
@@ -281,7 +281,7 @@ public abstract class Nodes {
281281
282282
public <%= node.name -%> getNonLazy() {
283283
if (isLazy()) {
284-
return loader.createDefNodeFromSavedPosition(<%= "nodeId, " if Prism::Template::INCLUDE_NODE_ID %>startOffset, length, -serializedLength);
284+
return loader.createDefNodeFromSavedPosition(<%= "nodeId, " unless Prism::Template::OMIT_NODE_ID %>startOffset, length, -serializedLength);
285285
} else {
286286
return this;
287287
}

templates/src/serialize.c.erb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void
7373
pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
7474
pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
7575

76-
<%- if Prism::Template::INCLUDE_NODE_ID -%>
76+
<%- unless Prism::Template::OMIT_NODE_ID -%>
7777
pm_buffer_append_varuint(buffer, node->node_id);
7878
<%- end -%>
7979
pm_serialize_location(&node->location, buffer);
@@ -91,8 +91,12 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
9191
size_t length_offset = buffer->length;
9292
pm_buffer_append_string(buffer, "\0\0\0\0", 4); /* consume 4 bytes, updated below */
9393
<%- end -%>
94-
<%- unless Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS && !node.flags -%>
94+
<%- if node.flags -%>
9595
pm_buffer_append_varuint(buffer, (uint32_t) node->flags);
96+
<%- else -%>
97+
if (!PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS) {
98+
pm_buffer_append_varuint(buffer, (uint32_t) node->flags);
99+
}
96100
<%- end -%>
97101
<%- node.fields.each do |field| -%>
98102
<%- case field -%>
@@ -121,17 +125,25 @@ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
121125
pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_<%= node.human %>_t *)node)-><%= field.name %>.ids[index]));
122126
}
123127
<%- when Prism::Template::LocationField -%>
124-
<%- if field.should_be_serialized? -%>
128+
<%- unless field.semantic_field? -%>
129+
if (!PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS) {
130+
<%- end -%>
125131
pm_serialize_location(&((pm_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
132+
<%- unless field.semantic_field? -%>
133+
}
126134
<%- end -%>
127135
<%- when Prism::Template::OptionalLocationField -%>
128-
<%- if field.should_be_serialized? -%>
136+
<%- unless field.semantic_field? -%>
137+
if (!PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS) {
138+
<%- end -%>
129139
if (((pm_<%= node.human %>_t *)node)-><%= field.name %>.length == 0) {
130140
pm_buffer_append_byte(buffer, 0);
131141
} else {
132142
pm_buffer_append_byte(buffer, 1);
133143
pm_serialize_location(&((pm_<%= node.human %>_t *)node)-><%= field.name %>, buffer);
134144
}
145+
<%- unless field.semantic_field? -%>
146+
}
135147
<%- end -%>
136148
<%- when Prism::Template::UInt8Field -%>
137149
pm_buffer_append_byte(buffer, ((pm_<%= node.human %>_t *)node)-><%= field.name %>);
@@ -261,9 +273,9 @@ pm_serialize_metadata(pm_parser_t *parser, pm_buffer_t *buffer) {
261273
pm_serialize_encoding(parser->encoding, buffer);
262274
pm_buffer_append_varsint(buffer, parser->start_line);
263275
pm_serialize_line_offset_list(&parser->line_offsets, buffer);
264-
<%- unless Prism::Template::SERIALIZE_ONLY_SEMANTICS_FIELDS -%>
265-
pm_serialize_comment_list(&parser->comment_list, buffer);
266-
<%- end -%>
276+
if (!PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS) {
277+
pm_serialize_comment_list(&parser->comment_list, buffer);
278+
}
267279
pm_serialize_magic_comment_list(&parser->magic_comment_list, buffer);
268280
pm_serialize_data_loc(parser, buffer);
269281
pm_serialize_diagnostic_list(&parser->error_list, buffer);

templates/template.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77

88
module Prism
99
module Template # :nodoc: all
10-
SERIALIZE_ONLY_SEMANTICS_FIELDS = ENV.fetch("PRISM_SERIALIZE_ONLY_SEMANTICS_FIELDS", false)
1110
CHECK_FIELD_KIND = ENV.fetch("CHECK_FIELD_KIND", false)
1211

1312
JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "default"
1413
JAVA_IDENTIFIER_TYPE = JAVA_BACKEND == "truffleruby" ? "String" : "byte[]"
15-
INCLUDE_NODE_ID = !SERIALIZE_ONLY_SEMANTICS_FIELDS || JAVA_BACKEND == "jruby"
14+
OMIT_NODE_ID = ENV.fetch("PRISM_OMIT_NODE_ID", false)
1615

1716
COMMON_FLAGS_COUNT = 2
1817

@@ -95,10 +94,6 @@ def each_comment_java_line(&block)
9594
def semantic_field?
9695
true
9796
end
98-
99-
def should_be_serialized?
100-
SERIALIZE_ONLY_SEMANTICS_FIELDS ? semantic_field? : true
101-
end
10297
end
10398

10499
# Some node fields can be specialized if they point to a specific kind of

0 commit comments

Comments
 (0)