1- <%- id_type = Prism::Template::JAVA_IDENTIFIER_TYPE -%>
1+ <%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22package org.ruby_lang.prism;
33
44import 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 }
0 commit comments