Skip to content

Commit

Permalink
Restructuring of Array#inspect and missing enc checks rb_inspect.
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Mar 23, 2015
1 parent 0c9ebf6 commit 802927c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
15 changes: 8 additions & 7 deletions core/src/main/java/org/jruby/RubyArray.java
Expand Up @@ -39,6 +39,7 @@

import org.jcodings.Encoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF16BEEncoding;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.common.IRubyWarnings.ID;
Expand Down Expand Up @@ -1444,15 +1445,15 @@ private IRubyObject inspectAry(ThreadContext context) {
boolean tainted = isTaint();

for (int i = 0; i < realLength; i++) {
if (i > 0) str.cat((byte)',').cat((byte)' ');

RubyString str2 = inspect(context, safeArrayRef(values, begin + i));
if (i == 0 && str2.getEncoding() != encoding) str.setEncoding(str2.getEncoding());
if (str2.isTaint()) tainted = true;

str.cat19(str2);
RubyString s = inspect(context, safeArrayRef(values, begin + i));
if (s.isTaint()) tainted = true;
if (i > 0) str.cat(',', UTF16BEEncoding.INSTANCE).cat(' ', UTF16BEEncoding.INSTANCE);
else str.setEncoding(s.getEncoding());

str.cat19(s);
}
str.cat((byte)']');
str.cat(']', UTF16BEEncoding.INSTANCE);

if (tainted) str.setTaint(true);

Expand Down
15 changes: 14 additions & 1 deletion core/src/main/java/org/jruby/RubyObject.java
Expand Up @@ -45,6 +45,7 @@
import java.util.List;
import java.util.Set;

import org.jcodings.Encoding;
import org.jruby.anno.JRubyClass;
import org.jruby.runtime.Helpers;
import org.jruby.runtime.Block;
Expand Down Expand Up @@ -532,7 +533,19 @@ private int nonFixnumHashCode(IRubyObject hashValue) {
* Prefered over callMethod(context, "inspect")
*/
public static RubyString inspect(ThreadContext context, IRubyObject object) {
return RubyString.objAsString(context, object.callMethod(context, "inspect"));
Ruby runtime = context.runtime;
RubyString str = RubyString.objAsString(context, object.callMethod(context, "inspect"));
Encoding ext = runtime.getDefaultExternalEncoding();
if (!ext.isAsciiCompatible()) {
if (!str.isAsciiOnly()) {
throw runtime.newEncodingCompatibilityError("inspected result must be ASCII only if default external encoding is ASCII incompatible");
}
return str;
}
if (str.getEncoding() != ext && !str.isAsciiOnly()) {
throw runtime.newEncodingCompatibilityError("inspected result must be ASCII only or use the default external encoding");
}
return str;
}

/**
Expand Down

0 comments on commit 802927c

Please sign in to comment.