Skip to content

Commit

Permalink
respect current frame of rb_eval_string
Browse files Browse the repository at this point in the history
`self` is nearest Ruby method's `self`.
If there is no ruby frame, use toplevel `self` (`main`).

https://bugs.ruby-lang.org/issues/18780
  • Loading branch information
ko1 committed Aug 1, 2022
1 parent 1520936 commit 5bbba76
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions ext/-test-/eval/eval.c
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 2 additions & 0 deletions ext/-test-/eval/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'mkmf'
create_makefile('-test-/eval')
12 changes: 12 additions & 0 deletions test/-ext-/eval/test_eval.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion vm_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5bbba76

Please sign in to comment.