Skip to content

Commit

Permalink
Showing 7 changed files with 47 additions and 145 deletions.
1 change: 0 additions & 1 deletion spec/truffle/tags/core/array/inspect_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/truffle/tags/core/array/to_s_tags.txt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.RubyString;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.builtins.CoreClass;
@@ -1128,6 +1129,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 {

@@ -3103,6 +3123,19 @@ protected static boolean hasRawBytes(DynamicObject string) {

}

@Primitive(name = "string_escape", needsSelf = false)
public abstract static class StringEscapePrimitiveNode extends PrimitiveArrayArgumentsNode {

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

}

@Primitive(name = "string_find_character")
@ImportStatic(StringGuards.class)
public static abstract class StringFindCharacterNode extends PrimitiveArrayArgumentsNode {
Original file line number Diff line number Diff line change
@@ -9,17 +9,17 @@
*/
package org.jruby.truffle.interop.cext;

import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.CreateCast;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.NodeChildren;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.Frame;
import com.oracle.truffle.api.frame.FrameInstance;
import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
import com.oracle.truffle.api.frame.FrameInstanceVisitor;
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.truffle.builtins.CoreClass;
import org.jruby.truffle.builtins.CoreMethod;
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.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
11 changes: 11 additions & 0 deletions truffle/src/main/ruby/core/type.rb
Original file line number Diff line number Diff line change
@@ -419,6 +419,17 @@ def self.compatible_encoding(a, b)
enc
end

# similar to rb_inspect
def self.inspect(val)
str = Rubinius::Type.coerce_to(val.inspect, String, :to_s)
result_encoding = Encoding.default_internal || Encoding.default_external
if str.ascii_only? || (result_encoding.ascii_compatible? && str.encoding == result_encoding)
str
else
Truffle.invoke_primitive :string_escape, str
end
end

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

0 comments on commit d577cff

Please sign in to comment.