11<%- string_type = Prism::Template::JAVA_STRING_TYPE -%>
22package org.ruby_lang.prism;
33
4- import org.ruby_lang.prism.Nodes;
5-
64import java.lang.Short;
75import java.math.BigInteger;
86import java.nio.ByteBuffer;
@@ -15,8 +13,8 @@ import java.util.Locale;
1513// @formatter:off
1614public class Loader {
1715
18- public static ParseResult load(byte[] serialized, byte[] sourceBytes ) {
19- return new Loader(serialized).load(sourceBytes );
16+ public static ParseResult load(byte[] serialized) {
17+ return new Loader(serialized).load();
2018 }
2119
2220 // Overridable methods
@@ -76,15 +74,14 @@ public class Loader {
7674 private Charset encodingCharset;
7775 <%- end -%>
7876 private ConstantPool constantPool;
77+ private Nodes.Source source = null;
7978
8079 protected Loader(byte[] serialized) {
8180 this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
8281 }
8382
84- // We pass sourceBytes here and not in the constructor to avoid keeping
85- // the sourceBytes in memory unnecessarily with lazy DefNode's which hold on the Loader.
86- protected ParseResult load(byte[] sourceBytes) {
87- Nodes.Source source = new Nodes.Source(sourceBytes);
83+ protected ParseResult load() {
84+ this.source = new Nodes.Source();
8885
8986 expect((byte) 'P', "incorrect prism header");
9087 expect((byte) 'R', "incorrect prism header");
@@ -129,8 +126,7 @@ public class Loader {
129126 throw new Error("Expected to consume all bytes while deserializing but there were " + left + " bytes left");
130127 }
131128
132- boolean[] newlineMarked = new boolean[1 + source.getLineCount()];
133- MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source, newlineMarked);
129+ MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
134130 node.accept(visitor);
135131 } else {
136132 node = null;
@@ -409,11 +405,17 @@ public class Loader {
409405 }
410406
411407 Nodes.DefNode createDefNodeFromSavedPosition(<%= base_params_sig -%> , int bufferPosition) {
408+ Nodes.DefNode node;
412409 // This method mutates the buffer position and may be called from different threads so we must synchronize
413410 synchronized (this) {
414411 buffer.position(bufferPosition);
415- return createDefNode(<%= base_params . join ( ", " ) -%> );
412+ node = createDefNode(<%= base_params . join ( ", " ) -%> );
416413 }
414+
415+ MarkNewlinesVisitor visitor = new MarkNewlinesVisitor(source);
416+ node.accept(visitor);
417+
418+ return node;
417419 }
418420 <%- array_types . uniq . each do |type | -%>
419421
0 commit comments