Skip to content

Commit

Permalink
[Bug #20342] Consider wrapped load in main methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Apr 4, 2024
1 parent ef19234 commit 5891878
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
18 changes: 11 additions & 7 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,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
Expand All @@ -1794,13 +1804,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"));
}

/*
Expand Down
1 change: 1 addition & 0 deletions internal/eval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 1 addition & 11 deletions proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2331,17 +2331,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"));
}

/*
Expand Down
20 changes: 20 additions & 0 deletions test/ruby/test_require.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,26 @@ def test_require_in_wrapped_load
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|
Expand Down
6 changes: 3 additions & 3 deletions vm_method.c
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,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"));
}

/*
Expand All @@ -2705,7 +2705,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"));
}

/*
Expand All @@ -2718,7 +2718,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"));
}

/*
Expand Down

0 comments on commit 5891878

Please sign in to comment.