Skip to content

Commit

Permalink
Merge pull request #256 from headius/coderange_cat_1.2_for_9.3
Browse files Browse the repository at this point in the history
Use handles to indirect catWithCodeRange or cat19
  • Loading branch information
headius authored Jun 6, 2024
2 parents faaabb6 + 5330d2a commit 58c83f3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/main/java/org/jruby/rack/ext/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand All @@ -21,6 +24,7 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.javasupport.JavaEmbedUtils;
import org.jruby.runtime.Block;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
Expand All @@ -39,6 +43,24 @@
*/
@SuppressWarnings("serial")
public class Input extends RubyObject {
private static final MethodHandle CONCAT_WITH_CODERANGE;

static {
// set up coderange-aware concat that works with the new catWithCodeRange as well as earlier JRuby without it.
// TODO: remove and replace with direct call once 9.3 is fully unsupported
MethodHandle catWithCR = null;
MethodHandles.Lookup lookup = MethodHandles.publicLookup();
try {
catWithCR = lookup.findVirtual(RubyString.class, "catWithCodeRange", MethodType.methodType(int.class, ByteList.class, int.class));
} catch (NoSuchMethodException | IllegalAccessException e) {
try {
catWithCR = lookup.findVirtual(RubyString.class, "cat19", MethodType.methodType(int.class, ByteList.class, int.class));
} catch (Exception t) {
Helpers.throwException(t);
}
}
CONCAT_WITH_CODERANGE = catWithCR;
}

static final ObjectAllocator ALLOCATOR = new ObjectAllocator() {
public IRubyObject allocate(Ruby runtime, RubyClass klass) {
Expand Down Expand Up @@ -144,7 +166,12 @@ public IRubyObject read(final ThreadContext context, final IRubyObject[] args) {
if ( bytes != null ) {
if ( buffer != null ) {
buffer.clear();
buffer.catWithCodeRange(new ByteList(bytes, false), StringSupport.CR_UNKNOWN);
try {
int _ = (int) CONCAT_WITH_CODERANGE.invokeExact(new ByteList(bytes, false), StringSupport.CR_UNKNOWN);
} catch (Throwable t) {
Helpers.throwException(t);
}

return buffer;
}
return context.runtime.newString(new ByteList(bytes, false));
Expand Down

0 comments on commit 58c83f3

Please sign in to comment.