Skip to content

Commit

Permalink
Showing 7 changed files with 30 additions and 45 deletions.
2 changes: 1 addition & 1 deletion bin/jruby.bash
Original file line number Diff line number Diff line change
@@ -318,7 +318,7 @@ done

if [[ "$USING_TRUFFLE" != "" ]]; then
JRUBY_CP="$JRUBY_CP$CP_DELIMITER$JRUBY_HOME/lib/jruby-truffle.jar"
ruby_args=("${ruby_args[@]}" "-X+T")
ruby_args=("-X+T" "${ruby_args[@]}")
fi

# Force file.encoding to UTF-8 when on Mac, since Apple JDK defaults to MacRoman (JRUBY-3576)
1 change: 1 addition & 0 deletions spec/truffle/tags/core/string/modulo_tags.txt
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ fails:String#% raises a TypeError if #to_ary does not return an Array
fails:String#% tries to convert the argument to Array by calling #to_ary
fails:String#% doesn't return subclass instances when called on a subclass
fails:String#% always taints the result when the format string is tainted
fails:String#% supports binary formats using %b for positive numbers
fails:String#% supports binary formats using %b for negative numbers
fails:String#% supports character formats using %c
fails:String#% supports single character strings as argument for %c
Original file line number Diff line number Diff line change
@@ -6,6 +6,9 @@
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*
* Some of the code in this class is modified from org.jruby.runtime.encoding.EncodingService,
* licensed under the same EPL1.0/GPL 2.0/LGPL 2.1 used throughout.
*/
package org.jruby.truffle.core.encoding;

Original file line number Diff line number Diff line change
@@ -32,17 +32,12 @@ public abstract class FormatIntegerBinaryNode extends FormatNode {
private final char format;
private final boolean hasPlusFlag;
private final boolean useAlternativeFormat;
private final boolean isLeftJustified;
private final boolean hasSpaceFlag;

public FormatIntegerBinaryNode(RubyContext context, char format, boolean hasPlusFlag, boolean useAlternativeFormat,
boolean isLeftJustified, boolean hasSpaceFlag) {
public FormatIntegerBinaryNode(RubyContext context, char format, boolean hasPlusFlag, boolean useAlternativeFormat) {
super(context);
this.format = format;
this.hasPlusFlag = hasPlusFlag;
this.useAlternativeFormat = useAlternativeFormat;
this.isLeftJustified = isLeftJustified;
this.hasSpaceFlag = hasSpaceFlag;
}

@Specialization
@@ -54,7 +49,7 @@ public byte[] format(int spacePadding,
final boolean negativeAndPadded = isNegative && (isSpacePadded || this.hasPlusFlag);
final String formatted = negativeAndPadded ? Integer.toBinaryString(-value) : Integer.toBinaryString(value);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag,
this.useAlternativeFormat, this.format, this.isLeftJustified, this.hasSpaceFlag);
this.useAlternativeFormat, this.format);
}

@TruffleBoundary
@@ -66,17 +61,13 @@ public byte[] format(int spacePadding, int zeroPadding, DynamicObject value) {
final boolean negativeAndPadded = isNegative && (isSpacePadded || this.hasPlusFlag);
final String formatted = negativeAndPadded ? bigInteger.abs().toString(2) : bigInteger.toString(2);
return getFormattedString(formatted, spacePadding, zeroPadding, isNegative, isSpacePadded, this.hasPlusFlag,
this.useAlternativeFormat, this.format, this.isLeftJustified, this.hasSpaceFlag);
this.useAlternativeFormat, this.format);
}

@TruffleBoundary
private static byte[] getFormattedString(String formatted, int spacePadding, int zeroPadding, boolean isNegative,
boolean isSpacePadded, boolean hasPlusFlag, boolean useAlternativeFormat,
char format, boolean leftJustified, boolean hasSpaceFlag) {
final boolean isLeftJustified = leftJustified || spacePadding < 0;
if(spacePadding < 0){
spacePadding = -spacePadding;
}
char format) {
if (isNegative && !(isSpacePadded || hasPlusFlag)) {
if (formatted.contains("0")) {
formatted = formatted.substring(formatted.indexOf('0'), formatted.length());
@@ -104,7 +95,7 @@ private static byte[] getFormattedString(String formatted, int spacePadding, int
}
}

if (hasSpaceFlag || hasPlusFlag) {
if (isSpacePadded || hasPlusFlag) {
if (isNegative) {
formatted = "-" + formatted;
} else {
@@ -113,14 +104,7 @@ private static byte[] getFormattedString(String formatted, int spacePadding, int
} else {
formatted = " " + formatted;
}
}
}

while (formatted.length() < spacePadding) {
if(isLeftJustified){
formatted = formatted + " ";
} else {
formatted = " " + formatted;
}
}

Original file line number Diff line number Diff line change
@@ -87,7 +87,6 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
int spacePadding = DEFAULT;
int zeroPadding = DEFAULT;
boolean hasPlusFlag = false;
boolean hasSpaceFlag = false;
boolean useAlternativeFormat = false;
int absoluteArgumentIndex = DEFAULT;

@@ -97,11 +96,12 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
if (flag.MINUS() != null) {
leftJustified = true;
} else if (flag.SPACE() != null) {
hasSpaceFlag = true;
if (n + 1 < ctx.flag().size() && ctx.flag(n + 1).STAR() != null) {
spacePadding = PADDING_FROM_ARGUMENT;
} else if(width != DEFAULT) {
spacePadding = width;
} else {
spacePadding = 1;
}
} else if (flag.ZERO() != null) {
if (n + 1 < ctx.flag().size() && ctx.flag(n + 1).STAR() != null) {
@@ -110,7 +110,7 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
zeroPadding = width;
}
} else if (flag.STAR() != null) {
spacePadding = PADDING_FROM_ARGUMENT;
// Handled in space and zero, above
} else if (flag.PLUS() != null) {
hasPlusFlag = true;
} else if (flag.HASH() != null) {
@@ -226,8 +226,6 @@ public void exitFormat(PrintfParser.FormatContext ctx) {
if(type == 'b' || type == 'B'){
node = WriteBytesNodeGen.create(context,
FormatIntegerBinaryNodeGen.create(context, format, hasPlusFlag, useAlternativeFormat,
leftJustified,
hasSpaceFlag,
spacePaddingNode,
zeroPaddingNode,
ToIntegerNodeGen.create(context, valueNode)));
Original file line number Diff line number Diff line change
@@ -9,8 +9,12 @@
*/
package org.jruby.truffle.core.string;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.object.DynamicObject;
import org.jcodings.Encoding;
import org.jruby.truffle.Layouts;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.encoding.EncodingNodes;
import org.jruby.truffle.language.RubyGuards;
import org.jruby.util.ByteList;
import org.jruby.util.CodeRangeable;
@@ -70,8 +74,20 @@ public final void modifyAndKeepCodeRange() {
}

@Override
@TruffleBoundary(throwsControlFlowException = true)
public Encoding checkEncoding(CodeRangeable other) {
return StringOperations.checkEncoding(string, other);
final Encoding encoding = EncodingNodes.CompatibleQueryNode.compatibleEncodingForStrings(string, ((StringCodeRangeableWrapper) other).getString());

// TODO (nirvdrum 23-Mar-15) We need to raise a proper Truffle+JRuby exception here, rather than a non-Truffle JRuby exception.
if (encoding == null) {
final RubyContext context = Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(string)).getContext();
throw context.getJRubyRuntime().newEncodingCompatibilityError(
String.format("incompatible character encodings: %s and %s",
Layouts.STRING.getRope(string).getEncoding().toString(),
other.getByteList().getEncoding().toString()));
}

return encoding;
}

@Override
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@
import org.jruby.truffle.language.RubyGuards;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.util.ByteList;
import org.jruby.util.CodeRangeable;

import java.nio.charset.Charset;

@@ -134,22 +133,6 @@ public static void keepCodeRange(DynamicObject string) {
}
}

@TruffleBoundary(throwsControlFlowException = true)
public static Encoding checkEncoding(DynamicObject string, CodeRangeable other) {
final Encoding encoding = EncodingNodes.CompatibleQueryNode.compatibleEncodingForStrings(string, ((StringCodeRangeableWrapper) other).getString());

// TODO (nirvdrum 23-Mar-15) We need to raise a proper Truffle+JRuby exception here, rather than a non-Truffle JRuby exception.
if (encoding == null) {
final RubyContext context = Layouts.MODULE.getFields(Layouts.BASIC_OBJECT.getLogicalClass(string)).getContext();
throw context.getJRubyRuntime().newEncodingCompatibilityError(
String.format("incompatible character encodings: %s and %s",
Layouts.STRING.getRope(string).getEncoding().toString(),
other.getByteList().getEncoding().toString()));
}

return encoding;
}

public static int normalizeIndex(int length, int index) {
return ArrayOperations.normalizeIndex(length, index);
}

0 comments on commit b438638

Please sign in to comment.