Skip to content

Commit fb80fe2

Browse files
committed
Do not pass source bytes to Loader.java at all
* That way we are sure it's not retained by it. * Source#bytes seems to have no usages. * Arrays.binarySearch() works just fine if the value is > the last array element.
1 parent eceb44d commit fb80fe2

4 files changed

Lines changed: 7 additions & 13 deletions

File tree

java-wasm/src/main/java/org/jruby/parser/prism/wasm/Prism.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public byte[] serialize(byte[] packedOptions, byte[] sourceBytes, int sourceLeng
8383
public ParseResult serializeParse(byte[] packedOptions, String source) {
8484
var sourceBytes = source.getBytes(StandardCharsets.ISO_8859_1);
8585
byte[] result = serialize(packedOptions, sourceBytes, sourceBytes.length);
86-
return Loader.load(result, sourceBytes);
86+
return Loader.load(result);
8787
}
8888

8989
@Override

rakelib/serialization.rake

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ task "test:java_loader:internal" => :compile do
2525
puts
2626
puts path
2727
serialized = Prism.dump_file(path)
28-
source_bytes = File.binread(path).unpack('c*')
29-
parse_result = org.ruby_lang.prism.Loader.load(serialized.unpack('c*'), source_bytes)
28+
parse_result = org.ruby_lang.prism.Loader.load(serialized.unpack('c*'))
3029
puts parse_result.value
3130
end
3231
end

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import java.util.Locale;
1515
// @formatter:off
1616
public class Loader {
1717

18-
public static ParseResult load(byte[] serialized, byte[] sourceBytes) {
19-
return new Loader(serialized).load(sourceBytes);
18+
public static ParseResult load(byte[] serialized) {
19+
return new Loader(serialized).load();
2020
}
2121

2222
// Overridable methods
@@ -81,10 +81,8 @@ public class Loader {
8181
this.buffer = ByteBuffer.wrap(serialized).order(ByteOrder.nativeOrder());
8282
}
8383

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);
84+
protected ParseResult load() {
85+
Nodes.Source source = new Nodes.Source();
8886

8987
expect((byte) 'P', "incorrect prism header");
9088
expect((byte) 'R', "incorrect prism header");

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ public abstract class Nodes {
4747
}
4848

4949
public static final class Source {
50-
public final byte[] bytes;
5150
private int startLine = 1;
5251
private int[] lineOffsets = null;
5352

54-
Source(byte[] bytes) {
55-
this.bytes = bytes;
53+
Source() {
5654
}
5755

5856
void setStartLine(int startLine) {
@@ -70,7 +68,6 @@ public abstract class Nodes {
7068

7169
// 0-based
7270
public int findLine(int byteOffset) {
73-
if (byteOffset >= bytes.length) byteOffset = bytes.length - 1;
7471
assert byteOffset >= 0 : byteOffset;
7572
int index = Arrays.binarySearch(lineOffsets, byteOffset);
7673
int line;

0 commit comments

Comments
 (0)