Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions core/src/main/java/org/jruby/RubyBasicObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import org.jcodings.Encoding;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.Convert;
import org.jruby.api.Create;
import org.jruby.api.JRubyAPI;
import org.jruby.ast.util.ArgsUtil;
Expand Down Expand Up @@ -680,7 +681,7 @@ public String asJavaString() {
*
* First converts this object into a String using the "to_s"
* method and returns it. If
* to_s doesn't return a Ruby String, {@link #anyToString} is used
* to_s doesn't return a Ruby String, {@link Convert#anyToString} is used
* instead.
*/
@Override
Expand All @@ -690,7 +691,7 @@ public RubyString asString() {
BasicObjectSites sites = sites(context);
IRubyObject str = sites.to_s.call(context, this, this);

if (!(str instanceof RubyString)) return (RubyString) anyToString();
if (!(str instanceof RubyString)) return Convert.anyToString(context, this);
return (RubyString) str;
}

Expand Down Expand Up @@ -789,20 +790,7 @@ public RubyString convertToString() {
@Override
public IRubyObject anyToString() {
Ruby runtime = metaClass.runtime;

/* 6:tags 16:addr 1:eos */
String hex = Integer.toHexString(System.identityHashCode(this));
ByteList className = metaClass.getRealClass().toRubyString(runtime.getCurrentContext()).getByteList();
ByteList bytes = new ByteList(2 + className.realSize() + 3 + hex.length() + 1);
bytes.setEncoding(className.getEncoding());
bytes.append('#').append('<');
bytes.append(className);
bytes.append(':').append('0').append('x');
bytes.append(hex.getBytes());
bytes.append('>');

RubyString str = RubyString.newString(runtime, bytes);
return str;
return Convert.anyToString(runtime.getCurrentContext(), this);
}

/**
Expand Down Expand Up @@ -2646,7 +2634,7 @@ public IRubyObject to_s() {
* initial execution context of Ruby programs returns ``main.''
*/
public IRubyObject to_s(ThreadContext context) {
return anyToString();
return Convert.anyToString(context, this);
}

/* rb_any_to_a
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyMatchData.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.joni.exception.ValueException;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.Convert;
import org.jruby.api.Create;
import org.jruby.ast.util.ArgsUtil;
import org.jruby.runtime.Arity;
Expand Down Expand Up @@ -419,7 +420,7 @@ public RubyString inspect() {
@JRubyMethod
@Override
public RubyString inspect(ThreadContext context) {
if (str == null) return (RubyString) anyToString();
if (str == null) return (RubyString) Convert.anyToString(context, this);

RubyString result = newString(context, "#<");
result.append(getMetaClass().getRealClass().to_s(context));
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.jruby.anno.JRubyMethod;
import org.jruby.anno.JavaMethodDescriptor;
import org.jruby.anno.TypePopulator;
import org.jruby.api.Convert;
import org.jruby.api.JRubyAPI;
import org.jruby.api.Warn;
import org.jruby.common.IRubyWarnings.ID;
Expand Down Expand Up @@ -3241,7 +3242,7 @@ public IRubyObject to_s(ThreadContext context) {
if (attached instanceof RubyModule) {
buffer.catWithCodeRange(attached.inspect(context).convertToString());
} else if (attached != null) {
buffer.catWithCodeRange((RubyString) attached.anyToString());
buffer.catWithCodeRange((RubyString) Convert.anyToString(context, attached));
}
buffer.cat('>', buffer.getEncoding());

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/jruby/RubyNameError.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public IRubyObject to_str(ThreadContext context) {
if (object == runtime.getTopSelf()) {
className = RubyString.newString(runtime, "main");
} else {
className = (RubyString) object.anyToString();
className = (RubyString) Convert.anyToString(context, object);
}
// we have our string, break out to final composition
break;
Expand All @@ -189,7 +189,7 @@ private static RubyString getNameOrInspect(ThreadContext context, IRubyObject ob
if (tmp == UNDEF || tmp.isNil()) tmp = tryInspect(context, object);
if (tmp == UNDEF) context.clearErrorInfo();
tmp = TypeConverter.checkStringType(context.runtime, tmp);
if (tmp.isNil()) tmp = object.anyToString();
if (tmp.isNil()) tmp = Convert.anyToString(context, object);
return (RubyString) tmp;
}

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/org/jruby/RubyString.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.joni.Region;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.api.Convert;
import org.jruby.api.Create;
import org.jruby.api.JRubyAPI;
import org.jruby.ast.util.ArgsUtil;
Expand Down Expand Up @@ -1609,7 +1610,7 @@ final boolean equals(RubyString other) {
public static RubyString objAsString(ThreadContext context, IRubyObject obj) {
if (obj instanceof RubyString str) return str;
IRubyObject str = sites(context).to_s.call(context, obj, obj);
if (!(str instanceof RubyString)) return (RubyString) obj.anyToString();
if (!(str instanceof RubyString)) return (RubyString) Convert.anyToString(context, obj);
// TODO: MRI sets an fstring flag on fstrings and uses that flag here
return (RubyString) str;
}
Expand Down
23 changes: 23 additions & 0 deletions core/src/main/java/org/jruby/api/Convert.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jruby.api;

import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyBignum;
import org.jruby.RubyBoolean;
Expand Down Expand Up @@ -636,4 +637,26 @@ public static RubySymbol asSymbol(ThreadContext context, RubyString string) {
public static RubySymbol toSymbol(ThreadContext context, IRubyObject arg) {
return RubySymbol.toSymbol(context, arg);
}

/**
* Produce a string from a given object using its type identity.
*
* Equivalent to RubyBasicObject#anyToString but without re-acquiring context.
*
* @return The object represented as a hashy type string.
*/
public static RubyString anyToString(ThreadContext context, IRubyObject obj) {
/* 6:tags 16:addr 1:eos */
String hex = Integer.toHexString(System.identityHashCode(obj));
ByteList className = obj.getType().toRubyString(context).getByteList();
ByteList bytes = new ByteList(2 + className.realSize() + 3 + hex.length() + 1);
bytes.setEncoding(className.getEncoding());
bytes.append('#').append('<');
bytes.append(className);
bytes.append(':').append('0').append('x');
bytes.append(hex.getBytes());
bytes.append('>');

return Create.newString(context, bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.api.Convert;
import org.jruby.api.Create;
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.Unrescuable;
Expand Down Expand Up @@ -2446,7 +2447,7 @@ public static IRubyObject callOptimizedAref(ThreadContext context, IRubyObject c
@JIT
public static RubyString asString(ThreadContext context, IRubyObject caller, IRubyObject target, CallSite site) {
IRubyObject str = site.call(context, caller, target);
return str instanceof RubyString string ? string : (RubyString) target.anyToString();
return str instanceof RubyString string ? string : (RubyString) Convert.anyToString(context, target);
}

@JIT
Expand Down
Loading
Loading