Skip to content

Commit c62fc35

Browse files
committed
Support #dup and #clone on foreign strings
1 parent a91ed96 commit c62fc35

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Compatibility:
2424
* Fix `IO.pipe` - allow overriding `IO.new` that is used to create new pipes (#2692, @andykonchin).
2525
* Fix exception message when there are missing or extra keyword arguments - it contains all the missing/extra keywords now (#1522, @andrykonchin).
2626
* Always terminate native strings with enough `\0` bytes (#2704, @eregon).
27+
* Support `#dup` and `#clone` on foreign strings (@eregon).
2728

2829
Performance:
2930

spec/truffle/interop/polyglot/foreign_string_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@java_string = Truffle::Interop.as_string("abc")
1515
@truffle_string = Truffle::Interop.as_truffle_string("abc")
1616
@foreign_string = Truffle::Debug.foreign_string("abc")
17+
@all = [@java_character, @java_string, @truffle_string, @foreign_string]
1718
end
1819

1920
it "are of the expected Java class" do
@@ -99,6 +100,33 @@
99100
@foreign_string.should == "abc"
100101
end
101102

103+
it "supports #+@ and then mutation" do
104+
@all.each do |foreign_string|
105+
copy = +foreign_string
106+
copy << "."
107+
copy.should == "#{foreign_string}."
108+
end
109+
end
110+
111+
it "supports #dup and then mutation" do
112+
@all.each do |foreign_string|
113+
copy = foreign_string.dup
114+
copy << "."
115+
copy.should == "#{foreign_string}."
116+
end
117+
end
118+
119+
it "supports #clone and then mutation" do
120+
@all.each do |foreign_string|
121+
foreign_string.clone.should.frozen?
122+
foreign_string.clone(freeze: true).should.frozen?
123+
124+
copy = foreign_string.clone(freeze: false)
125+
copy << "."
126+
copy.should == "#{foreign_string}."
127+
end
128+
end
129+
102130
it "has all Ruby String methods" do
103131
@java_character.should == "C"
104132
@java_string.capitalize.should == "Abc"

src/main/ruby/truffleruby/core/truffle/polyglot_methods.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ def clear(...)
128128
to_s.clear(...)
129129
end
130130

131+
def clone(...)
132+
to_s.clone(...)
133+
end
134+
131135
def codepoints(...)
132136
to_s.codepoints(...)
133137
end
@@ -180,6 +184,10 @@ def dump(...)
180184
to_s.dump(...)
181185
end
182186

187+
def dup(...)
188+
to_s.dup(...)
189+
end
190+
183191
def each_byte(...)
184192
to_s.each_byte(...)
185193
end

tool/generate-polyglot-methods.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ module Polyglot
1717
module StringTrait
1818
RUBY
1919

20-
string_methods = String.public_instance_methods(false).sort
20+
string_methods = String.public_instance_methods(false)
2121
# Already implemented
2222
string_methods -= %i[to_s to_str freeze frozen?]
23+
# Kernel methods which we want to handle by converting to a Ruby String first
24+
string_methods += %i[clone dup]
2325

24-
string_methods.each do |name|
26+
string_methods.sort.each do |name|
2527
code << <<-RUBY
2628
def #{name}(...)
2729
to_s.#{name}(...)

0 commit comments

Comments
 (0)