Skip to content

Commit 5891878

Browse files
committed
[Bug #20342] Consider wrapped load in main methods
1 parent ef19234 commit 5891878

File tree

5 files changed

+36
-21
lines changed

5 files changed

+36
-21
lines changed

eval.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,16 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
17821782
return obj;
17831783
}
17841784

1785+
VALUE
1786+
rb_top_main_class(const char *method)
1787+
{
1788+
VALUE klass = GET_THREAD()->top_wrapper;
1789+
1790+
if (!klass) return rb_cObject;
1791+
rb_warning("main.%s in the wrapped load is effective only in wrapper module", method);
1792+
return klass;
1793+
}
1794+
17851795
/*
17861796
* call-seq:
17871797
* include(module, ...) -> self
@@ -1794,13 +1804,7 @@ rb_obj_extend(int argc, VALUE *argv, VALUE obj)
17941804
static VALUE
17951805
top_include(int argc, VALUE *argv, VALUE self)
17961806
{
1797-
rb_thread_t *th = GET_THREAD();
1798-
1799-
if (th->top_wrapper) {
1800-
rb_warning("main.include in the wrapped load is effective only in wrapper module");
1801-
return rb_mod_include(argc, argv, th->top_wrapper);
1802-
}
1803-
return rb_mod_include(argc, argv, rb_cObject);
1807+
return rb_mod_include(argc, argv, rb_top_main_class("include"));
18041808
}
18051809

18061810
/*

internal/eval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern ID ruby_static_id_status;
2121
VALUE rb_refinement_module_get_refined_class(VALUE module);
2222
void rb_class_modify_check(VALUE);
2323
NORETURN(VALUE rb_f_raise(int argc, VALUE *argv));
24+
VALUE rb_top_main_class(const char *method);
2425

2526
/* eval_error.c */
2627
VALUE rb_get_backtrace(VALUE info);

proc.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,17 +2331,7 @@ rb_obj_define_method(int argc, VALUE *argv, VALUE obj)
23312331
static VALUE
23322332
top_define_method(int argc, VALUE *argv, VALUE obj)
23332333
{
2334-
rb_thread_t *th = GET_THREAD();
2335-
VALUE klass;
2336-
2337-
klass = th->top_wrapper;
2338-
if (klass) {
2339-
rb_warning("main.define_method in the wrapped load is effective only in wrapper module");
2340-
}
2341-
else {
2342-
klass = rb_cObject;
2343-
}
2344-
return rb_mod_define_method(argc, argv, klass);
2334+
return rb_mod_define_method(argc, argv, rb_top_main_class("define_method"));
23452335
}
23462336

23472337
/*

test/ruby/test_require.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,26 @@ def test_require_in_wrapped_load
370370
end
371371
end
372372

373+
def test_public_in_wrapped_load
374+
Tempfile.create(["test_public_in_wrapped_load", ".rb"]) do |t|
375+
t.puts "def foo; end", "public :foo"
376+
t.close
377+
assert_warning(/main\.public/) do
378+
assert load(t.path, true)
379+
end
380+
end
381+
end
382+
383+
def test_private_in_wrapped_load
384+
Tempfile.create(["test_private_in_wrapped_load", ".rb"]) do |t|
385+
t.puts "def foo; end", "private :foo"
386+
t.close
387+
assert_warning(/main\.private/) do
388+
assert load(t.path, true)
389+
end
390+
end
391+
end
392+
373393
def test_load_scope
374394
bug1982 = '[ruby-core:25039] [Bug #1982]'
375395
Tempfile.create(["test_ruby_test_require", ".rb"]) {|t|

vm_method.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,7 @@ rb_mod_private_method(int argc, VALUE *argv, VALUE obj)
26852685
static VALUE
26862686
top_public(int argc, VALUE *argv, VALUE _)
26872687
{
2688-
return rb_mod_public(argc, argv, rb_cObject);
2688+
return rb_mod_public(argc, argv, rb_top_main_class("public"));
26892689
}
26902690

26912691
/*
@@ -2705,7 +2705,7 @@ top_public(int argc, VALUE *argv, VALUE _)
27052705
static VALUE
27062706
top_private(int argc, VALUE *argv, VALUE _)
27072707
{
2708-
return rb_mod_private(argc, argv, rb_cObject);
2708+
return rb_mod_private(argc, argv, rb_top_main_class("private"));
27092709
}
27102710

27112711
/*
@@ -2718,7 +2718,7 @@ top_private(int argc, VALUE *argv, VALUE _)
27182718
static VALUE
27192719
top_ruby2_keywords(int argc, VALUE *argv, VALUE module)
27202720
{
2721-
return rb_mod_ruby2_keywords(argc, argv, rb_cObject);
2721+
return rb_mod_ruby2_keywords(argc, argv, rb_top_main_class("ruby2_keywords"));
27222722
}
27232723

27242724
/*

0 commit comments

Comments
 (0)