summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKoichi Sasada <ko1@atdot.net>2022-07-29 16:02:10 +0900
committerKoichi Sasada <ko1@atdot.net>2022-08-01 17:48:05 +0900
commit5bbba76489628f4509495ebf4ba0a7aad4c0b560 (patch)
tree10cdc8fb1474b43122a132501feb05f4032ec271
parent1520936aa760e6b2747d31c37854f22a63591667 (diff)
respect current frame of `rb_eval_string`
`self` is nearest Ruby method's `self`. If there is no ruby frame, use toplevel `self` (`main`). https://bugs.ruby-lang.org/issues/18780
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/6199
-rw-r--r--ext/-test-/eval/eval.c13
-rw-r--r--ext/-test-/eval/extconf.rb2
-rw-r--r--test/-ext-/eval/test_eval.rb12
-rw-r--r--vm_eval.c5
4 files changed, 31 insertions, 1 deletions
diff --git a/ext/-test-/eval/eval.c b/ext/-test-/eval/eval.c
new file mode 100644
index 0000000000..983468fc34
--- /dev/null
+++ b/ext/-test-/eval/eval.c
@@ -0,0 +1,13 @@
+#include "ruby/ruby.h"
+
+static VALUE
+eval_string(VALUE self, VALUE str)
+{
+ return rb_eval_string(StringValueCStr(str));
+}
+
+void
+Init_eval(void)
+{
+ rb_define_global_function("rb_eval_string", eval_string, 1);
+}
diff --git a/ext/-test-/eval/extconf.rb b/ext/-test-/eval/extconf.rb
new file mode 100644
index 0000000000..cdbf6a8597
--- /dev/null
+++ b/ext/-test-/eval/extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile('-test-/eval')
diff --git a/test/-ext-/eval/test_eval.rb b/test/-ext-/eval/test_eval.rb
new file mode 100644
index 0000000000..27952996e2
--- /dev/null
+++ b/test/-ext-/eval/test_eval.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: false
+require 'test/unit'
+require "-test-/eval"
+
+class EvalTest < Test::Unit::TestCase
+ def test_rb_eval_string
+ a = 1
+ assert_equal [self, 1, __method__], rb_eval_string(%q{
+ [self, a, __method__]
+ })
+ end
+end
diff --git a/vm_eval.c b/vm_eval.c
index e490e4e32d..c7669cbb85 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -1828,7 +1828,10 @@ VALUE
ruby_eval_string_from_file(const char *str, const char *filename)
{
VALUE file = filename ? rb_str_new_cstr(filename) : 0;
- return eval_string_with_cref(rb_vm_top_self(), rb_str_new2(str), NULL, file, 1);
+ rb_execution_context_t *ec = GET_EC();
+ rb_control_frame_t *cfp = ec ? rb_vm_get_ruby_level_next_cfp(ec, ec->cfp) : NULL;
+ VALUE self = cfp ? cfp->self : rb_vm_top_self();
+ return eval_string_with_cref(self, rb_str_new2(str), NULL, file, 1);
}
VALUE