Skip to content

Commit aa22a50

Browse files
authored
Merge pull request #4022 from ruby/revert-4016-byte_ary_identifiers
Revert "Switch identifiers to byte[]"
2 parents 62d51ad + efe36fb commit aa22a50

3 files changed

Lines changed: 40 additions & 34 deletions

File tree

templates/java/org/ruby_lang/prism/Loader.java.erb

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- id_type = Prism::Template::JAVA_IDENTIFIER_TYPE -%>
1+
<%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22
package org.ruby_lang.prism;
33

44
import java.lang.Short;
@@ -19,29 +19,37 @@ public class Loader {
1919

2020
// Overridable methods
2121

22-
<%- if id_type == "String" -%>
23-
public abstract <%= id_type %> bytesToName(byte[] bytes);
24-
<%- end -%>
22+
public Charset getEncodingCharset(String encodingName) {
23+
encodingName = encodingName.toLowerCase(Locale.ROOT);
24+
if (encodingName.equals("ascii-8bit")) {
25+
return StandardCharsets.US_ASCII;
26+
}
27+
return Charset.forName(encodingName);
28+
}
29+
30+
public <%= string_type %> bytesToName(byte[] bytes) {
31+
<%- if string_type == "String" -%>
32+
return new String(bytes, encodingCharset).intern();
33+
<%- else -%>
34+
return null; // Must be implemented by subclassing Loader
35+
<%- end -%>
36+
}
2537

2638
private static final class ConstantPool {
2739

2840
private final Loader loader;
2941
private final int bufferOffset;
30-
private final <%= id_type %>[] cache;
42+
private final <%= string_type %>[] cache;
3143

3244
ConstantPool(Loader loader, int bufferOffset, int length) {
3345
this.loader = loader;
3446
this.bufferOffset = bufferOffset;
35-
<%- if id_type == "String" -%>
36-
cache = new <%= id_type %>[length];
37-
<%- else -%>
38-
cache = new byte[length][];
39-
<%- end -%>
47+
cache = new <%= string_type %>[length];
4048
}
4149

42-
<%= id_type %> get(ByteBuffer buffer, int oneBasedIndex) {
50+
<%= string_type %> get(ByteBuffer buffer, int oneBasedIndex) {
4351
int index = oneBasedIndex - 1;
44-
<%= id_type %> constant = cache[index];
52+
<%= string_type %> constant = cache[index];
4553

4654
if (constant == null) {
4755
int offset = bufferOffset + index * 8;
@@ -51,11 +59,7 @@ public class Loader {
5159
byte[] bytes = new byte[length];
5260
buffer.get(start, bytes);
5361

54-
<%- if id_type == "byte[]" -%>
55-
constant = bytes;
56-
<%- else %>
5762
constant = loader.bytesToName(bytes);
58-
<%- end %>
5963
cache[index] = constant;
6064
}
6165

@@ -66,6 +70,9 @@ public class Loader {
6670

6771
private final ByteBuffer buffer;
6872
protected String encodingName;
73+
<%- if string_type == "String" -%>
74+
private Charset encodingCharset;
75+
<%- end -%>
6976
private ConstantPool constantPool;
7077
private Nodes.Source source = null;
7178

@@ -93,6 +100,9 @@ public class Loader {
93100
byte[] encodingNameBytes = new byte[encodingLength];
94101
buffer.get(encodingNameBytes);
95102
this.encodingName = new String(encodingNameBytes, StandardCharsets.US_ASCII);
103+
<%- if string_type == "String" -%>
104+
this.encodingCharset = getEncodingCharset(this.encodingName);
105+
<%- end -%>
96106

97107
source.setStartLine(loadVarSInt());
98108
source.setLineOffsets(loadLineOffsets());
@@ -203,11 +213,11 @@ public class Loader {
203213
}
204214
}
205215

206-
private <%= id_type %> loadConstant() {
216+
private <%= string_type %> loadConstant() {
207217
return constantPool.get(buffer, loadVarUInt());
208218
}
209219

210-
private <%= id_type %> loadOptionalConstant() {
220+
private <%= string_type %> loadOptionalConstant() {
211221
if (buffer.get(buffer.position()) != 0) {
212222
return loadConstant();
213223
} else {
@@ -216,16 +226,12 @@ public class Loader {
216226
}
217227
}
218228

219-
private <%= id_type %>[] loadConstants() {
229+
private <%= string_type %>[] loadConstants() {
220230
int length = loadVarUInt();
221231
if (length == 0) {
222-
return Nodes.EMPTY_IDENTIFIER_ARRAY;
232+
return Nodes.EMPTY_STRING_ARRAY;
223233
}
224-
<%- if id_type == "String" -%>
225-
<%= id_type %>[] constants = new <%= id_type %>[length];
226-
<%- else -%>
227-
<%= id_type %>[] constants = new byte[length][];
228-
<%- end -%>
234+
<%= string_type %>[] constants = new <%= string_type %>[length];
229235
for (int i = 0; i < length; i++) {
230236
constants[i] = constantPool.get(buffer, loadVarUInt());
231237
}
@@ -389,7 +395,7 @@ public class Loader {
389395
int bufferPosition = buffer.position();
390396
int serializedLength = buffer.getInt();
391397
// Load everything except the body and locals, because the name, receiver, parameters are still needed for lazily defining the method
392-
Nodes.DefNode lazyDefNode = new Nodes.DefNode(<%= base_params.join(", ") -%>, -bufferPosition, this, loadConstant(), loadOptionalNode(), (Nodes.ParametersNode) loadOptionalNode(), null, Nodes.EMPTY_IDENTIFIER_ARRAY);
398+
Nodes.DefNode lazyDefNode = new Nodes.DefNode(<%= base_params.join(", ") -%>, -bufferPosition, this, loadConstant(), loadOptionalNode(), (Nodes.ParametersNode) loadOptionalNode(), null, Nodes.EMPTY_STRING_ARRAY);
393399
buffer.position(bufferPosition + serializedLength); // skip past the serialized DefNode
394400
return lazyDefNode;
395401
}

templates/java/org/ruby_lang/prism/Nodes.java.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%- id_type = Prism::Template::JAVA_IDENTIFIER_TYPE -%>
1+
<%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22
package org.ruby_lang.prism;
33

44
import java.lang.Override;
@@ -16,7 +16,7 @@ import java.util.Arrays;
1616
// @formatter:off
1717
public abstract class Nodes {
1818

19-
public static final <%= id_type %>[] EMPTY_IDENTIFIER_ARRAY = {};
19+
public static final <%= string_type %>[] EMPTY_STRING_ARRAY = {};
2020

2121
@Target(ElementType.FIELD)
2222
@Retention(RetentionPolicy.SOURCE)
@@ -383,7 +383,7 @@ public abstract class Nodes {
383383
builder.append('\n');
384384
<%- when Prism::Template::ConstantListField -%>
385385
builder.append('\n');
386-
for (<%= id_type %> constant : this.<%= field.name %>) {
386+
for (<%= string_type %> constant : this.<%= field.name %>) {
387387
builder.append(nextNextIndent).append('"').append(constant).append('"').append('\n');
388388
}
389389
<%- when Prism::Template::Flags -%>

templates/template.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ module Template # :nodoc: all
1111
REMOVE_ON_ERROR_TYPES = SERIALIZE_ONLY_SEMANTICS_FIELDS
1212
CHECK_FIELD_KIND = ENV.fetch("CHECK_FIELD_KIND", false)
1313

14-
JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "default"
15-
JAVA_IDENTIFIER_TYPE = JAVA_BACKEND == "truffleruby" ? "String" : "byte[]"
14+
JAVA_BACKEND = ENV["PRISM_JAVA_BACKEND"] || "truffleruby"
15+
JAVA_STRING_TYPE = JAVA_BACKEND == "jruby" ? "org.jruby.RubySymbol" : "String"
1616
INCLUDE_NODE_ID = !SERIALIZE_ONLY_SEMANTICS_FIELDS || JAVA_BACKEND == "jruby"
1717

1818
COMMON_FLAGS_COUNT = 2
@@ -272,7 +272,7 @@ def call_seq_type
272272
end
273273

274274
def java_type
275-
JAVA_IDENTIFIER_TYPE
275+
JAVA_STRING_TYPE
276276
end
277277
end
278278

@@ -292,7 +292,7 @@ def call_seq_type
292292
end
293293

294294
def java_type
295-
JAVA_IDENTIFIER_TYPE
295+
JAVA_STRING_TYPE
296296
end
297297
end
298298

@@ -312,7 +312,7 @@ def call_seq_type
312312
end
313313

314314
def java_type
315-
"#{JAVA_IDENTIFIER_TYPE}[]"
315+
"#{JAVA_STRING_TYPE}[]"
316316
end
317317
end
318318

0 commit comments

Comments
 (0)