ruby-changes:65105
From: Nobuyoshi <ko1@a...>
Date: Mon, 1 Feb 2021 19:54:48 +0900 (JST)
Subject: [ruby-changes:65105] 1cdae49d39 (master): Implement NameError::message#clone for Ractor
https://git.ruby-lang.org/ruby.git/commit/?id=1cdae49d39 From 1cdae49d39fbaef654df487f168d1fb14a146d59 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada <nobu@r...> Date: Mon, 1 Feb 2021 18:01:10 +0900 Subject: Implement NameError::message#clone for Ractor --- bootstraptest/test_ractor.rb | 13 +++++++++++++ error.c | 35 +++++++++++++++++++++++++++++++++-- test/objspace/test_objspace.rb | 9 +++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/bootstraptest/test_ractor.rb b/bootstraptest/test_ractor.rb index 0e3a6bc..b32a790 100644 --- a/bootstraptest/test_ractor.rb +++ b/bootstraptest/test_ractor.rb @@ -1350,4 +1350,17 @@ assert_equal "#{n}#{n}", %Q{ https://github.com/ruby/ruby/blob/trunk/bootstraptest/test_ractor.rb#L1350 }.map{|r| r.take}.join } +# NameError +assert_equal "ok", %q{ + begin + bar + rescue => err + end + begin + Ractor.new{} << err + rescue TypeError + 'ok' + end +} + end # if !ENV['GITHUB_WORKFLOW'] diff --git a/error.c b/error.c index ba0f258..d715ec1 100644 --- a/error.c +++ b/error.c @@ -1862,9 +1862,9 @@ static const rb_data_type_t name_err_mesg_data_type = { https://github.com/ruby/ruby/blob/trunk/error.c#L1862 /* :nodoc: */ static VALUE -rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method) +rb_name_err_mesg_init(VALUE klass, VALUE mesg, VALUE recv, VALUE method) { - VALUE result = TypedData_Wrap_Struct(rb_cNameErrorMesg, &name_err_mesg_data_type, 0); + VALUE result = TypedData_Wrap_Struct(klass, &name_err_mesg_data_type, 0); VALUE *ptr = ALLOC_N(VALUE, NAME_ERR_MESG_COUNT); ptr[NAME_ERR_MESG__MESG] = mesg; @@ -1876,6 +1876,35 @@ rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method) https://github.com/ruby/ruby/blob/trunk/error.c#L1876 /* :nodoc: */ static VALUE +rb_name_err_mesg_new(VALUE mesg, VALUE recv, VALUE method) +{ + return rb_name_err_mesg_init(rb_cNameErrorMesg, mesg, recv, method); +} + +/* :nodoc: */ +static VALUE +name_err_mesg_alloc(VALUE klass) +{ + return rb_name_err_mesg_init(klass, Qnil, Qnil, Qnil); +} + +/* :nodoc: */ +static VALUE +name_err_mesg_init_copy(VALUE obj1, VALUE obj2) +{ + VALUE *ptr1, *ptr2; + + if (obj1 == obj2) return obj1; + rb_obj_init_copy(obj1, obj2); + + TypedData_Get_Struct(obj1, VALUE, &name_err_mesg_data_type, ptr1); + TypedData_Get_Struct(obj2, VALUE, &name_err_mesg_data_type, ptr2); + MEMCPY(ptr1, ptr2, VALUE, NAME_ERR_MESG_COUNT); + return obj1; +} + +/* :nodoc: */ +static VALUE name_err_mesg_equal(VALUE obj1, VALUE obj2) { VALUE *ptr1, *ptr2; @@ -2802,6 +2831,8 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2831 rb_define_method(rb_eNameError, "receiver", name_err_receiver, 0); rb_define_method(rb_eNameError, "local_variables", name_err_local_variables, 0); rb_cNameErrorMesg = rb_define_class_under(rb_eNameError, "message", rb_cObject); + rb_define_alloc_func(rb_cNameErrorMesg, name_err_mesg_alloc); + rb_define_method(rb_cNameErrorMesg, "initialize_copy", name_err_mesg_init_copy, 1); rb_define_method(rb_cNameErrorMesg, "==", name_err_mesg_equal, 1); rb_define_method(rb_cNameErrorMesg, "to_str", name_err_mesg_to_str, 0); rb_define_method(rb_cNameErrorMesg, "_dump", name_err_mesg_dump, 1); diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb index 230c1d0..6b956e6 100644 --- a/test/objspace/test_objspace.rb +++ b/test/objspace/test_objspace.rb @@ -528,6 +528,15 @@ class TestObjSpace < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L528 children.each {|child| ObjectSpace.internal_class_of(child).itself} # this used to crash end + def test_name_error_message + begin + bar + rescue => err + _, m = ObjectSpace.reachable_objects_from(err) + end + assert_equal(m, m.clone) + end + def traverse_super_classes klass while klass klass = ObjectSpace.internal_super_of(klass) -- cgit v1.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/