Skip to content

Commit

Permalink
[Truffle] Implemented most of Enconding::Converter#putback by way of …
Browse files Browse the repository at this point in the history
…the 'encoding_converter_putback' primitive.
  • Loading branch information
nirvdrum committed Mar 31, 2015
1 parent 850bfab commit 6a67caa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
5 changes: 0 additions & 5 deletions spec/truffle/tags/core/encoding/converter/putback_tags.txt
@@ -1,6 +1 @@
fails:Encoding::Converter#putback returns a String
fails:Encoding::Converter#putback returns a String in the source encoding
fails:Encoding::Converter#putback returns the bytes buffered due to an :invalid_byte_sequence error
fails:Encoding::Converter#putback allows conversion to be resumed after an :invalid_byte_sequence
fails:Encoding::Converter#putback returns an empty String when there are no more bytes to put back
fails:Encoding::Converter#putback accepts an integer argument corresponding to the number of bytes to be put back
Expand Up @@ -20,6 +20,7 @@
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.UndefinedPlaceholder;
import org.jruby.truffle.runtime.control.RaiseException;
import org.jruby.truffle.runtime.core.RubyArray;
import org.jruby.truffle.runtime.core.RubyBasicObject;
Expand Down Expand Up @@ -167,10 +168,39 @@ public EncodingConverterPutbackNode(EncodingConverterPutbackNode prev) {
}

@Specialization
public Object encodingConverterPutback(RubyBasicObject encodingConverter, int maxBytes) {
throw new UnsupportedOperationException("not implemented");
public RubyString encodingConverterPutback(RubyEncodingConverter encodingConverter, int maxBytes) {
// Taken from org.jruby.RubyConverter#putback.

final EConv ec = encodingConverter.getEConv();
final int putbackable = ec.putbackable();

return putback(encodingConverter, putbackable < maxBytes ? putbackable : maxBytes);
}

@Specialization
public RubyString encodingConverterPutback(RubyEncodingConverter encodingConverter, UndefinedPlaceholder maxBytes) {
// Taken from org.jruby.RubyConverter#putback.

final EConv ec = encodingConverter.getEConv();

return putback(encodingConverter, ec.putbackable());
}

private RubyString putback(RubyEncodingConverter encodingConverter, int n) {
// Taken from org.jruby.RubyConverter#putback.

final EConv ec = encodingConverter.getEConv();

final ByteList bytes = new ByteList(n);
ec.putback(bytes.getUnsafeBytes(), bytes.getBegin(), n);
bytes.setRealSize(n);

if (ec.sourceEncoding != null) {
bytes.setEncoding(ec.sourceEncoding);
}

return getContext().makeString(bytes);
}
}

@RubiniusPrimitive(name = "encoding_converter_last_error")
Expand Down

0 comments on commit 6a67caa

Please sign in to comment.