Skip to content

Commit

Permalink
Use handles to work on both old and new name
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 12, 2024
1 parent 84ee4cd commit 345a252
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.catWithCodeRange(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 345a252

Please sign in to comment.