1- <%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
1+ <%- id_type = Prism::Template::JAVA_IDENTIFIER_TYPE -%>
22package org.ruby_lang.prism;
33
44import java.lang.Short;
@@ -19,37 +19,33 @@ public class Loader {
1919
2020 // Overridable methods
2121
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();
22+ public <%= id_type %> bytesToName(byte[] bytes) {
23+ <%- if id_type == "byte[]" -%>
24+ return bytes;
3325 <%- else -%>
34- return null; // Must be implemented by subclassing Loader
26+ throw new AbstractMethodError("Loader.bytesToName( <%= id_type %> ) is not implemented");
3527 <%- end -%>
3628 }
3729
3830 private static final class ConstantPool {
3931
4032 private final Loader loader;
4133 private final int bufferOffset;
42- private final <%= string_type %> [] cache;
34+ private final <%= id_type %> [] cache;
4335
4436 ConstantPool(Loader loader, int bufferOffset, int length) {
4537 this.loader = loader;
4638 this.bufferOffset = bufferOffset;
47- cache = new <%= string_type %> [length];
39+ <%- if id_type == "String" -%>
40+ cache = new <%= id_type %> [length];
41+ <%- else -%>
42+ cache = new byte[length][];
43+ <%- end -%>
4844 }
4945
50- <%= string_type %> get(ByteBuffer buffer, int oneBasedIndex) {
46+ <%= id_type %> get(ByteBuffer buffer, int oneBasedIndex) {
5147 int index = oneBasedIndex - 1;
52- <%= string_type %> constant = cache[index];
48+ <%= id_type %> constant = cache[index];
5349
5450 if (constant == null) {
5551 int offset = bufferOffset + index * 8;
@@ -70,9 +66,6 @@ public class Loader {
7066
7167 private final ByteBuffer buffer;
7268 protected String encodingName;
73- <%- if string_type == "String" -%>
74- private Charset encodingCharset;
75- <%- end -%>
7669 private ConstantPool constantPool;
7770 private Nodes.Source source = null;
7871
@@ -100,9 +93,6 @@ public class Loader {
10093 byte[] encodingNameBytes = new byte[encodingLength];
10194 buffer.get(encodingNameBytes);
10295 this.encodingName = new String(encodingNameBytes, StandardCharsets.US_ASCII);
103- <%- if string_type == "String" -%>
104- this.encodingCharset = getEncodingCharset(this.encodingName);
105- <%- end -%>
10696
10797 source.setStartLine(loadVarSInt());
10898 source.setLineOffsets(loadLineOffsets());
@@ -213,11 +203,11 @@ public class Loader {
213203 }
214204 }
215205
216- private <%= string_type %> loadConstant() {
206+ private <%= id_type %> loadConstant() {
217207 return constantPool.get(buffer, loadVarUInt());
218208 }
219209
220- private <%= string_type %> loadOptionalConstant() {
210+ private <%= id_type %> loadOptionalConstant() {
221211 if (buffer.get(buffer.position()) != 0) {
222212 return loadConstant();
223213 } else {
@@ -226,12 +216,16 @@ public class Loader {
226216 }
227217 }
228218
229- private <%= string_type %> [] loadConstants() {
219+ private <%= id_type %> [] loadConstants() {
230220 int length = loadVarUInt();
231221 if (length == 0) {
232- return Nodes.EMPTY_STRING_ARRAY ;
222+ return Nodes.EMPTY_IDENTIFIER_ARRAY ;
233223 }
234- <%= string_type %> [] constants = new <%= string_type %> [length];
224+ <%- if id_type == "String" -%>
225+ <%= id_type %> [] constants = new <%= id_type %> [length];
226+ <%- else -%>
227+ <%= id_type %> [] constants = new byte[length][];
228+ <%- end -%>
235229 for (int i = 0; i < length ; i++) {
236230 constants[i] = constantPool.get(buffer, loadVarUInt());
237231 }
@@ -395,7 +389,7 @@ public class Loader {
395389 int bufferPosition = buffer.position();
396390 int serializedLength = buffer.getInt();
397391 // Load everything except the body and locals, because the name, receiver, parameters are still needed for lazily defining the method
398- Nodes.DefNode lazyDefNode = new Nodes.DefNode(<%= base_params . join ( ", " ) -%> , -bufferPosition, this, loadConstant(), loadOptionalNode(), (Nodes.ParametersNode) loadOptionalNode(), null, Nodes.EMPTY_STRING_ARRAY );
392+ Nodes.DefNode lazyDefNode = new Nodes.DefNode(<%= base_params . join ( ", " ) -%> , -bufferPosition, this, loadConstant(), loadOptionalNode(), (Nodes.ParametersNode) loadOptionalNode(), null, Nodes.EMPTY_IDENTIFIER_ARRAY );
399393 buffer.position(bufferPosition + serializedLength); // skip past the serialized DefNode
400394 return lazyDefNode;
401395 }
0 commit comments