Skip to content

Commit

Permalink
Update for logic
Browse files Browse the repository at this point in the history
This doesn't pass test_string_mapped because we do not lock
String objects like MRI does and cannot raise an error when the
String owned by the Buffer is modified independently.
  • Loading branch information
headius committed Oct 20, 2023
1 parent 38f8458 commit 737b71a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/src/main/java/org/jruby/RubyIOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,27 @@ public RubyIOBuffer(Ruby runtime, RubyClass metaClass, ByteBuffer base, int size
@JRubyMethod(name = "for", meta = true)
public static IRubyObject rbFor(ThreadContext context, IRubyObject self, IRubyObject _string, Block block) {
RubyString string = _string.convertToString();
int flags = string.isFrozen() ? READONLY : 0;

// If the string is frozen, both code paths are okay.
// If the string is not frozen, if a block is not given, it must be frozen.
boolean isGiven = block.isGiven();
if (!isGiven) {
if (!block.isGiven()) {
// This internally returns the source string if it's already frozen.
string = string.newFrozen();
flags = READONLY;
} else {
if ((flags & READONLY) != READONLY) {
string.modify();
}
}

ByteList bytes = string.getByteList();
int size = bytes.realSize();
ByteBuffer wrap = ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), size);

RubyIOBuffer buffer = newBuffer(context.runtime, ByteBuffer.wrap(bytes.unsafeBytes(), bytes.begin(), size), size, READONLY);
RubyIOBuffer buffer = newBuffer(context.runtime, wrap, size, flags);

if (isGiven) {
if (block.isGiven()) {
return block.yieldSpecific(context, buffer);
}

Expand Down

0 comments on commit 737b71a

Please sign in to comment.