Skip to content

Commit f03c19b

Browse files
authored
Use new name for cat19 (#84)
This is safe on both old and new versions of JRuby, using MethodHandle to indirect access to cat19 or its replacement method. See #83
1 parent 42983f8 commit f03c19b

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ext/java/org/jruby/ext/stringio/StringIO.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
import org.jruby.util.io.ModeFlags;
5656
import org.jruby.util.io.OpenFile;
5757

58+
import java.lang.invoke.MethodHandle;
59+
import java.lang.invoke.MethodHandles;
60+
import java.lang.invoke.MethodType;
5861
import java.util.Arrays;
5962

6063
import static org.jruby.RubyEnumerator.enumeratorize;
@@ -1270,6 +1273,23 @@ public IRubyObject write(ThreadContext context, IRubyObject[] args) {
12701273
return RubyFixnum.newFixnum(runtime, len);
12711274
}
12721275

1276+
private static final MethodHandle CAT_WITH_CODE_RANGE;
1277+
1278+
static {
1279+
MethodHandle cat;
1280+
try {
1281+
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "catWithCodeRange", MethodType.methodType(RubyString.class, RubyString.class));
1282+
} catch (NoSuchMethodException | IllegalAccessException ex) {
1283+
try {
1284+
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "cat19", MethodType.methodType(RubyString.class, RubyString.class));
1285+
} catch (NoSuchMethodException | IllegalAccessException ex2) {
1286+
throw new ExceptionInInitializerError(ex2);
1287+
}
1288+
}
1289+
1290+
CAT_WITH_CODE_RANGE = cat;
1291+
}
1292+
12731293
// MRI: strio_write
12741294
private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg) {
12751295
checkWritable();
@@ -1299,7 +1319,11 @@ private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg)
12991319
if (enc == EncodingUtils.ascii8bitEncoding(runtime) || encStr == EncodingUtils.ascii8bitEncoding(runtime)) {
13001320
EncodingUtils.encStrBufCat(runtime, ptr.string, strByteList, enc);
13011321
} else {
1302-
ptr.string.cat19(str);
1322+
try {
1323+
RubyString unused = (RubyString) CAT_WITH_CODE_RANGE.invokeExact(ptr.string, str);
1324+
} catch (Throwable t) {
1325+
throw new RuntimeException(t);
1326+
}
13031327
}
13041328
} else {
13051329
strioExtend(ptr.pos, len);

0 commit comments

Comments
 (0)