Skip to content

Commit

Permalink
[Truffle] String#num_bytes= and primitive :string_resize_capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisseaton committed Mar 14, 2015
1 parent cf89fd2 commit 4feca49
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 55 deletions.
6 changes: 0 additions & 6 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/append_tags.txt
@@ -1,8 +1,2 @@
fails:StringIO#<< when passed [Object] returns self
fails:StringIO#<< when passed [Object] writes the passed argument onto self
fails:StringIO#<< when passed [Object] writes the passed argument at the current position
fails:StringIO#<< when passed [Object] pads self with \000 when the current position is after the end
fails:StringIO#<< when passed [Object] taints self's String when the passed argument is tainted
fails:StringIO#<< when passed [Object] does not taint self when the passed argument is tainted
fails:StringIO#<< when passed [Object] updates self's position
fails:StringIO#<< when passed [Object] tries to convert the passed argument to a String using #to_s

This file was deleted.

3 changes: 0 additions & 3 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/print_tags.txt
@@ -1,10 +1,7 @@
fails:StringIO#print prints $_ when passed no arguments
fails:StringIO#print prints the passed arguments to self
fails:StringIO#print tries to convert the passed Object to a String using #to_s
fails:StringIO#print returns nil
fails:StringIO#print pads self with \000 when the current position is after the end
fails:StringIO#print honors the output record separator global
fails:StringIO#print updates the current position
fails:StringIO#print correctly updates the current position when honoring the output record separator global
fails:StringIO#print when in append mode appends the passed argument to the end of self
fails:StringIO#print when in append mode correctly updates self's position
3 changes: 0 additions & 3 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/printf_tags.txt
@@ -1,4 +1 @@
fails:StringIO#printf returns nil
fails:StringIO#printf pads self with \000 when the current position is after the end
fails:StringIO#printf performs format conversion
fails:StringIO#printf updates the current position
8 changes: 0 additions & 8 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/putc_tags.txt

This file was deleted.

Expand Up @@ -6,5 +6,4 @@ fails:StringIO#reopen when passed [Object, Integer] raises an Errno::EACCES when
fails:StringIO#reopen when passed [Object, Integer] raises a TypeError when trying to reopen self with a frozen String in truncate-mode
fails:StringIO#reopen when passed [Object, Integer] raises a RuntimeError when trying to reopen self with a frozen String in truncate-mode
fails:StringIO#reopen when passed [Object, Integer] does not raise IOError when passed a frozen String in read-mode
fails:StringIO#reopen reopens a stream when given a String argument
fails:StringIO#reopen does not truncate the content even when the StringIO argument is in the truncate mode
10 changes: 0 additions & 10 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/syswrite_tags.txt

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions spec/truffle/tags/rubysl/rubysl-stringio/spec/write_tags.txt

This file was deleted.

Expand Up @@ -2213,4 +2213,22 @@ public static ByteList swapcase(RubyString string) {
}
}

@CoreMethod(names = "_set_num_bytes", required = 1)
public abstract static class SetNumBytesNode extends CoreMethodNode {

public SetNumBytesNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public SetNumBytesNode(SetNumBytesNode prev) {
super(prev);
}

@Specialization
public RubyString setNumBytes(RubyString string, int count) {
string.getByteList().view(0, count);

This comment has been minimized.

Copy link
@eregon

eregon Mar 14, 2015

Member

This looks funny, but maybe it's just ByteList API?

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Mar 14, 2015

Author Contributor

Yeah - it's basically a setter for length with a funny name.

return string;
}
}

}
Expand Up @@ -624,8 +624,9 @@ public StringResizeCapacityPrimitiveNode(StringResizeCapacityPrimitiveNode prev)
}

@Specialization
public Object stringResizeCapacity(RubyString string, Object capacity) {
throw new UnsupportedOperationException("string_resize_capacity");
public RubyString stringResizeCapacity(RubyString string, int capacity) {
string.getByteList().ensure(capacity);
return string;
}

}
Expand Down
14 changes: 13 additions & 1 deletion truffle/src/main/ruby/core/shims.rb
Expand Up @@ -199,4 +199,16 @@ def [](index)
@array[index]
end
end
end
end

class String

def modify!

This comment has been minimized.

Copy link
@nirvdrum

nirvdrum Mar 14, 2015

Contributor

I'm pretty sure Rubinius does a CoW byte buffer like JRuby does. modify! would change the share level and make a new copy. See RubyString#modify for what they do.

This comment has been minimized.

Copy link
@chrisseaton

chrisseaton Mar 14, 2015

Author Contributor

So if we don't have COW yet we can make it a no-op?

# TODO CS 14-Mar-15 what does this do?
end

def num_bytes=(count)
_set_num_bytes count
end

end

7 comments on commit 4feca49

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nirvdrum check this is compatible with the way you are tackling String

@nirvdrum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had been avoiding these methods. But if they work, great.

@nirvdrum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And unrelated, but I think this is first synthetic method we've added to String, so we'll want to keep track of that for whatever cleanup @eregon has in mind.

@eregon
Copy link
Member

@eregon eregon commented on 4feca49 Mar 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the indirection with num_bytes/_set_num_bytes? Is it used with an explicit receiver?

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought a name like _set_num_bytes made it clearer it was synthetic - the leading underscore. If I implemented num_bytes= directly in Java and not in a shims file I think we'd forget.

@eregon
Copy link
Member

@eregon eregon commented on 4feca49 Mar 14, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you annotate the CoreMethod with @RubiniusOnly? I think it would be clear while keeping things simple.

@chrisseaton
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry I forgot about @RubiniusOnly - I should have used that.

Please sign in to comment.