diff --git a/eval.c b/eval.c index 24e6dac079..a61dfb1289 100644 --- a/eval.c +++ b/eval.c @@ -1777,6 +1777,16 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj) return obj; } +VALUE +rb_top_main_class(const char *method) +{ + VALUE klass = GET_THREAD()->top_wrapper; + + if (!klass) return rb_cObject; + rb_warning("main.%s in the wrapped load is effective only in wrapper module", method); + return klass; +} + /* * call-seq: * include(module, ...) -> self @@ -1789,13 +1799,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj) static VALUE top_include(int argc, VALUE *argv, VALUE self) { - rb_thread_t *th = GET_THREAD(); - - if (th->top_wrapper) { - rb_warning("main.include in the wrapped load is effective only in wrapper module"); - return rb_mod_include(argc, argv, th->top_wrapper); - } - return rb_mod_include(argc, argv, rb_cObject); + return rb_mod_include(argc, argv, rb_top_main_class("include")); } /* diff --git a/internal/eval.h b/internal/eval.h index 73bb656d96..e594d8516d 100644 --- a/internal/eval.h +++ b/internal/eval.h @@ -21,6 +21,7 @@ extern ID ruby_static_id_status; VALUE rb_refinement_module_get_refined_class(VALUE module); void rb_class_modify_check(VALUE); NORETURN(VALUE rb_f_raise(int argc, VALUE *argv)); +VALUE rb_top_main_class(const char *method); /* eval_error.c */ VALUE rb_get_backtrace(VALUE info); diff --git a/proc.c b/proc.c index c31017128e..fb60b073d5 100644 --- a/proc.c +++ b/proc.c @@ -2382,17 +2382,7 @@ rb_obj_define_method(int argc, VALUE *argv, VALUE obj) static VALUE top_define_method(int argc, VALUE *argv, VALUE obj) { - rb_thread_t *th = GET_THREAD(); - VALUE klass; - - klass = th->top_wrapper; - if (klass) { - rb_warning("main.define_method in the wrapped load is effective only in wrapper module"); - } - else { - klass = rb_cObject; - } - return rb_mod_define_method(argc, argv, klass); + return rb_mod_define_method(argc, argv, rb_top_main_class("define_method")); } /* diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index afbcae2e59..1b93a42e42 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -369,6 +369,26 @@ class TestRequire < Test::Unit::TestCase end end + def test_public_in_wrapped_load + Tempfile.create(["test_public_in_wrapped_load", ".rb"]) do |t| + t.puts "def foo; end", "public :foo" + t.close + assert_warning(/main\.public/) do + assert load(t.path, true) + end + end + end + + def test_private_in_wrapped_load + Tempfile.create(["test_private_in_wrapped_load", ".rb"]) do |t| + t.puts "def foo; end", "private :foo" + t.close + assert_warning(/main\.private/) do + assert load(t.path, true) + end + end + end + def test_load_scope bug1982 = '[ruby-core:25039] [Bug #1982]' Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| diff --git a/version.h b/version.h index ae323455f8..49b025fb31 100644 --- a/version.h +++ b/version.h @@ -11,7 +11,7 @@ # define RUBY_VERSION_MINOR RUBY_API_VERSION_MINOR #define RUBY_VERSION_TEENY 4 #define RUBY_RELEASE_DATE RUBY_RELEASE_YEAR_STR"-"RUBY_RELEASE_MONTH_STR"-"RUBY_RELEASE_DAY_STR -#define RUBY_PATCHLEVEL 195 +#define RUBY_PATCHLEVEL 196 #include "ruby/version.h" #include "ruby/internal/abi.h" diff --git a/vm_method.c b/vm_method.c index 98866a1c6c..17aeb16a66 100644 --- a/vm_method.c +++ b/vm_method.c @@ -2552,7 +2552,7 @@ rb_mod_private_method(int argc, VALUE *argv, VALUE obj) static VALUE top_public(int argc, VALUE *argv, VALUE _) { - return rb_mod_public(argc, argv, rb_cObject); + return rb_mod_public(argc, argv, rb_top_main_class("public")); } /* @@ -2572,7 +2572,7 @@ top_public(int argc, VALUE *argv, VALUE _) static VALUE top_private(int argc, VALUE *argv, VALUE _) { - return rb_mod_private(argc, argv, rb_cObject); + return rb_mod_private(argc, argv, rb_top_main_class("private")); } /* @@ -2585,7 +2585,7 @@ top_private(int argc, VALUE *argv, VALUE _) static VALUE top_ruby2_keywords(int argc, VALUE *argv, VALUE module) { - return rb_mod_ruby2_keywords(argc, argv, rb_cObject); + return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords")); } /*