Skip to content

Commit

Permalink
Use new name for cat19 (#84)
Browse files Browse the repository at this point in the history
This is safe on both old and new versions of JRuby, using MethodHandle to indirect access to cat19 or its replacement method.

See #83
  • Loading branch information
headius authored Mar 12, 2024
1 parent 42983f8 commit f03c19b
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion ext/java/org/jruby/ext/stringio/StringIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
import org.jruby.util.io.ModeFlags;
import org.jruby.util.io.OpenFile;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Arrays;

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

private static final MethodHandle CAT_WITH_CODE_RANGE;

static {
MethodHandle cat;
try {
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "catWithCodeRange", MethodType.methodType(RubyString.class, RubyString.class));
} catch (NoSuchMethodException | IllegalAccessException ex) {
try {
cat = MethodHandles.publicLookup().findVirtual(RubyString.class, "cat19", MethodType.methodType(RubyString.class, RubyString.class));
} catch (NoSuchMethodException | IllegalAccessException ex2) {
throw new ExceptionInInitializerError(ex2);
}
}

CAT_WITH_CODE_RANGE = cat;
}

// MRI: strio_write
private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg) {
checkWritable();
Expand Down Expand Up @@ -1299,7 +1319,11 @@ private long stringIOWrite(ThreadContext context, Ruby runtime, IRubyObject arg)
if (enc == EncodingUtils.ascii8bitEncoding(runtime) || encStr == EncodingUtils.ascii8bitEncoding(runtime)) {
EncodingUtils.encStrBufCat(runtime, ptr.string, strByteList, enc);
} else {
ptr.string.cat19(str);
try {
RubyString unused = (RubyString) CAT_WITH_CODE_RANGE.invokeExact(ptr.string, str);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
} else {
strioExtend(ptr.pos, len);
Expand Down

0 comments on commit f03c19b

Please sign in to comment.