summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQuang-Minh Nguyen <nguyenquangminh0711@gmail.com>2020-09-18 06:02:14 (GMT)
committerKoichi Sasada <ko1@atdot.net>2020-09-20 14:10:44 (GMT)
commit398da71175ef9154be505155c0b1c0b2efb20445 (patch)
tree1a06a4e5c65e15c40f3a55649eedd2b8bc7d93f0
parentd5fa66156ab116df558448402b93c9c129b30291 (diff)
Validate name during initialization
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3555
-rw-r--r--bootstraptest/test_ractor.rb50
-rw-r--r--ractor.c10
2 files changed, 43 insertions, 17 deletions
diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb
index 4b6afc2..9cfd42c 100644
--- a/bootstraptest/test_ractor.rb
+++ b/bootstraptest/test_ractor.rb
@@ -8,6 +8,37 @@ assert_equal 'Ractor', %q{
Ractor.new{}.class
}
+# A Ractor can have a name
+assert_equal 'test-name', %q{
+ r = Ractor.new name: 'test-name' do
+ end
+ r.name
+}
+
+# If Ractor doesn't have a name, Ractor#name returns nil.
+assert_equal 'nil', %q{
+ r = Ractor.new do
+ end
+ r.name.inspect
+}
+
+# Raises exceptions if initialize with invalid name
+assert_equal 'no implicit conversion of Array into String', %q{
+ begin
+ r = Ractor.new(name: [{}]) {}
+ rescue TypeError => e
+ e.message
+ end
+}
+
+assert_equal 'ASCII incompatible encoding (UTF-16BE)', %q{
+ begin
+ r = Ractor.new(name: String.new('Invalid encoding', encoding: 'UTF-16BE')) {}
+ rescue ArgumentError => e
+ e.message
+ end
+}
+
# Ractor.new must call with a block
assert_equal "must be called with a block", %q{
begin
@@ -263,7 +294,7 @@ assert_equal 'false', %q{
r = Ractor.new obj do |msg|
msg.object_id
end
-
+
obj.object_id == r.take
}
@@ -360,7 +391,7 @@ assert_equal 'hello', %q{
str = r.take
begin
- r.take
+ r.take
rescue Ractor::RemoteError
str #=> "hello"
end
@@ -528,20 +559,6 @@ assert_equal '[1000, 3]', %q{
Ractor.new{ [A.size, H.size] }.take
}
-# A Ractor can have a name
-assert_equal 'test-name', %q{
- r = Ractor.new name: 'test-name' do
- end
- r.name
-}
-
-# If Ractor doesn't have a name, Ractor#name returns nil.
-assert_equal 'nil', %q{
- r = Ractor.new do
- end
- r.name.inspect
-}
-
###
### Synchronization tests
###
@@ -559,4 +576,3 @@ assert_equal "#{N}#{N}", %Q{
}
end # if !ENV['GITHUB_WORKFLOW']
-
diff --git a/ractor.c b/ractor.c
index 0ae0070..5562299 100644
--- a/ractor.c
+++ b/ractor.c
@@ -1310,6 +1310,16 @@ ractor_init(rb_ractor_t *r, VALUE name, VALUE loc)
rb_ractor_living_threads_init(r);
// naming
+ if (!NIL_P(name)) {
+ rb_encoding *enc;
+ StringValueCStr(name);
+ enc = rb_enc_get(name);
+ if (!rb_enc_asciicompat(enc)) {
+ rb_raise(rb_eArgError, "ASCII incompatible encoding (%s)",
+ rb_enc_name(enc));
+ }
+ name = rb_str_new_frozen(name);
+ }
r->name = name;
r->loc = loc;
}