Skip to content

Commit

Permalink
Showing 6 changed files with 52 additions and 142 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/array/inspect_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1128,6 +1128,25 @@ private Object substr(Rope rope, DynamicObject string, int beg, int len) {
}
}

@CoreMethod(names = "inspect")
public abstract static class InspectNode extends CoreMethodArrayArgumentsNode {

@Child private TaintResultNode taintResultNode;

public InspectNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
taintResultNode = new TaintResultNode(context, sourceSection);
}

@TruffleBoundary
@Specialization
public Object inspect(DynamicObject string) {
DynamicObject result = createString(org.jruby.RubyString.inspect(getContext().getJRubyRuntime(),
StringOperations.getByteListReadOnly(string)).getByteList());
return taintResultNode.maybeTaint(string, result);
}
}

@CoreMethod(names = "empty?")
public abstract static class IsEmptyNode extends CoreMethodArrayArgumentsNode {

Original file line number Diff line number Diff line change
@@ -21,11 +21,13 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.RubyString;
import org.jruby.truffle.builtins.CoreClass;
import org.jruby.truffle.builtins.CoreMethod;
import org.jruby.truffle.builtins.CoreMethodArrayArgumentsNode;
import org.jruby.truffle.builtins.CoreMethodNode;
import org.jruby.truffle.core.cast.NameToJavaStringNodeGen;
import org.jruby.truffle.core.string.StringOperations;
import org.jruby.truffle.language.RubyConstant;
import org.jruby.truffle.language.RubyNode;
import org.jruby.truffle.language.arguments.RubyArguments;
@@ -173,6 +175,20 @@ public DynamicObject toRubyString(DynamicObject string) {

}

@CoreMethod(names = "rb_str_escape", isModuleFunction = true, required = 1)
public abstract static class RbStrEscapeNode extends CoreMethodArrayArgumentsNode {

@TruffleBoundary
@Specialization
public CExtString rb_str_escape(CExtString cExtString) {
final org.jruby.RubyString rubyString = new RubyString(getContext().getJRubyRuntime(), getContext().getJRubyRuntime().getString(),
StringOperations.getByteListReadOnly(cExtString.getString()));
DynamicObject result = createString(((RubyString) org.jruby.RubyString.rbStrEscape(getContext().getJRubyRuntime().getCurrentContext(), rubyString)).getByteList());
return new CExtString(result);
}

}

@CoreMethod(names = "get_block", isModuleFunction = true)
public abstract static class GetBlockNode extends CoreMethodArrayArgumentsNode {

2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core/array.rb
Original file line number Diff line number Diff line change
@@ -515,7 +515,7 @@ def inspect

return "[...]" if Thread.detect_recursion self do
each_with_index do |element, index|
temp = element.inspect
temp = Rubinius::Type.object_inspect(element)
result.force_encoding(temp.encoding) if index == 0
result << temp << comma
end
140 changes: 0 additions & 140 deletions truffle/src/main/ruby/core/string.rb
Original file line number Diff line number Diff line change
@@ -565,146 +565,6 @@ def end_with?(*suffixes)
false
end

def inspect
result_encoding = Encoding.default_internal || Encoding.default_external
unless result_encoding.ascii_compatible?
result_encoding = Encoding::US_ASCII
end

enc = encoding
ascii = enc.ascii_compatible?
enc_name = enc.name
unicode = enc_name.start_with?("UTF-") && enc_name[4] != ?7

if unicode
if enc.equal? Encoding::UTF_16
a = getbyte 0
b = getbyte 1

if a == 0xfe and b == 0xff
enc = Encoding::UTF_16BE
elsif a == 0xff and b == 0xfe
enc = Encoding::UTF_16LE
else
unicode = false
end
elsif enc.equal? Encoding::UTF_32
a = getbyte 0
b = getbyte 1
c = getbyte 2
d = getbyte 3

if a == 0 and b == 0 and c == 0xfe and d == 0xfe
enc = Encoding::UTF_32BE
elsif a == 0xff and b == 0xfe and c == 0 and d == 0
enc = Encoding::UTF_32LE
else
unicode = false
end
end
end

array = []

index = 0
total = bytesize
while index < total
char = chr_at index

if char
bs = char.bytesize

if (ascii or unicode) and bs == 1
escaped = nil

byte = getbyte(index)
if byte >= 7 and byte <= 92
case byte
when 7 # \a
escaped = '\a'
when 8 # \b
escaped = '\b'
when 9 # \t
escaped = '\t'
when 10 # \n
escaped = '\n'
when 11 # \v
escaped = '\v'
when 12 # \f
escaped = '\f'
when 13 # \r
escaped = '\r'
when 27 # \e
escaped = '\e'
when 34 # \"
escaped = '\"'
when 35 # #
case getbyte(index += 1)
when 36 # $
escaped = '\#$'
when 64 # @
escaped = '\#@'
when 123 # {
escaped = '\#{'
else
index -= 1
end
when 92 # \\
escaped = '\\\\'
end

if escaped
array << escaped
index += 1
next
end
end
end

if char.printable?
array << char
else
code = char.ord
escaped = code.to_s(16).upcase

if unicode
if code < 0x10000
pad = "0" * (4 - escaped.bytesize)
array << "\\u#{pad}#{escaped}"
else
array << "\\u{#{escaped}}"
end
else
if code < 0x100
pad = "0" * (2 - escaped.bytesize)
array << "\\x#{pad}#{escaped}"
else
array << "\\x{#{escaped}}"
end
end
end

index += bs
else
array << "\\x#{getbyte(index).to_s(16)}"
index += 1
end
end

size = array.inject(0) { |s, chr| s += chr.bytesize }
result = String.pattern size + 2, ?".ord
m = Rubinius::Mirror.reflect result

index = 1
array.each do |chr|
m.copy_from chr, 0, chr.bytesize, index
index += chr.bytesize
end

Rubinius::Type.infect result, self
result.force_encoding result_encoding
end

def prepend(other)
self[0, 0] = other
self
16 changes: 16 additions & 0 deletions truffle/src/main/ruby/core/type.rb
Original file line number Diff line number Diff line change
@@ -419,6 +419,22 @@ def self.compatible_encoding(a, b)
enc
end

# similar to rb_inspect
def self.object_inspect(val)
str = Rubinius::Type.coerce_to(val.inspect, String, :to_s)
result_encoding = Encoding.default_internal || Encoding.default_external
unless result_encoding.ascii_compatible?
unless str.ascii_only?
return Truffle::CExt.to_ruby_string(Truffle::CExt.rb_str_escape(Truffle::CExt.CExtString(str)))
end
return str
end
if (str.encoding != result_encoding && !str.ascii_only?)
return Truffle::CExt.to_ruby_string(Truffle::CExt.rb_str_escape(Truffle::CExt.CExtString(str)))
end
return str
end

def self.object_respond_to__dump?(obj)
object_respond_to? obj, :_dump, true
end

0 comments on commit b3499cd

Please sign in to comment.