Skip to content

Commit

Permalink
Showing 9 changed files with 30 additions and 14 deletions.
10 changes: 10 additions & 0 deletions truffle/src/main/java/org/jruby/truffle/pack/nodes/PackNode.java
Original file line number Diff line number Diff line change
@@ -134,6 +134,16 @@ protected void setTainted(VirtualFrame frame) {
frame.setBoolean(PackFrameDescriptor.TAINT_SLOT, true);
}

/**
* Write a single byte.
*/
protected void writeByte(VirtualFrame frame, byte value) {
byte[] output = ensureCapacity(frame, 1);
final int outputPosition = getOutputPosition(frame);
output[outputPosition] = value;
setOutputPosition(frame, outputPosition + 1);
}

/**
* Write an array of bytes to the output.
*/
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public Write8Node(RubyContext context) {

@Specialization
public Object doWrite(VirtualFrame frame, long value) {
writeBytes(frame, (byte) value);
writeByte(frame, (byte) value);
return null;
}

Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ public WriteBinaryStringNode(RubyContext context, boolean pad, boolean padOnNil,
public Object write(VirtualFrame frame, Object nil) {
if (padOnNil) {
for (int n = 0; n < width; n++) {
writeBytes(frame, padding);
writeByte(frame, padding);
}
}

@@ -73,14 +73,14 @@ public Object write(VirtualFrame frame, ByteList bytes) {
writeBytes(frame, bytes.getUnsafeBytes(), bytes.begin(), lengthFromBytes);

for (int n = 0; n < lengthFromPadding; n++) {
writeBytes(frame, padding);
writeByte(frame, padding);
}
} else {
writeBytes(frame, bytes.getUnsafeBytes(), bytes.begin(), lengthFromBytes);
}

if (appendNull) {
writeBytes(frame, (byte) 0);
writeByte(frame, (byte) 0);
}

return null;
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ public Object write(VirtualFrame frame, ByteList bytes) {
}

if ((i & 7) == 0) {
writeBytes(frame, (byte) (currentByte & 0xff));
writeByte(frame, (byte) (currentByte & 0xff));
currentByte = 0;
continue;
}
@@ -81,7 +81,7 @@ public Object write(VirtualFrame frame, ByteList bytes) {

if ((occurrences & 7) != 0) { //if the length is not a multiple of 8
currentByte >>= 7 - (occurrences & 7); //we need to pad the last byte
writeBytes(frame, (byte) (currentByte & 0xff));
writeByte(frame, (byte) (currentByte & 0xff));
}
} break;

@@ -91,7 +91,7 @@ public Object write(VirtualFrame frame, ByteList bytes) {

// we filled up current byte; append it and create next one
if ((i & 7) == 0) {
writeBytes(frame, (byte) (currentByte & 0xff));
writeByte(frame, (byte) (currentByte & 0xff));
currentByte = 0;
continue;
}
@@ -102,7 +102,7 @@ public Object write(VirtualFrame frame, ByteList bytes) {

if ((occurrences & 7) != 0) { //if the length is not a multiple of 8
currentByte <<= 7 - (occurrences & 7); //we need to pad the last byte
writeBytes(frame, (byte) (currentByte & 0xff));
writeByte(frame, (byte) (currentByte & 0xff));
}
} break;
}
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public WriteByteNode(RubyContext context, byte value) {

@Override
public Object execute(VirtualFrame frame) {
writeBytes(frame, value);
writeByte(frame, value);
return null;
}

Original file line number Diff line number Diff line change
@@ -93,13 +93,13 @@ public Object write(VirtualFrame frame, ByteList bytes) {
break;
}
} else {
writeBytes(frame, (byte) currentByte);
writeByte(frame, (byte) currentByte);
currentByte = 0;
}
}

if ((lengthToUse & 1) != 0) {
writeBytes(frame, (byte) (currentByte & 0xff));
writeByte(frame, (byte) (currentByte & 0xff));
}

return null;
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public WritePaddedBytesNode(RubyContext context, int padding) {
@Specialization
public Object write(VirtualFrame frame, ByteList bytes) {
for (int n = 0; n < padding - bytes.length(); n++) {
writeBytes(frame, (byte) ' ');
writeByte(frame, (byte) ' ');
}

writeBytes(frame, bytes);
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ public WriteUTF8CharacterNode(RubyContext context) {

@Specialization(guards = {"value >= 0", "value <= 0x7f"})
public Object writeSingleByte(VirtualFrame frame, long value) {
writeBytes(frame,
writeByte(frame,
(byte) value);
return null;
}
Original file line number Diff line number Diff line change
@@ -64,7 +64,13 @@ public PackNode parse(FormatTokenizer tokenizer) {
final PackNode node;

if (token instanceof ByteList) {
node = WriteBytesNodeGen.create(context, new LiteralBytesNode(context, (ByteList) token));
final ByteList byteList = (ByteList) token;

if (byteList.length() == 1) {
node = new WriteByteNode(context, (byte) byteList.get(0));
} else {
node = WriteBytesNodeGen.create(context, new LiteralBytesNode(context, byteList));
}
} else if (token instanceof FormatDirective) {
final FormatDirective directive = (FormatDirective) token;

0 comments on commit dfdfb54

Please sign in to comment.