Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Introduce Namespace#eval #13701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions namespace.c
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,23 @@ rb_namespace_require_relative(VALUE namespace, VALUE fname)
return rb_ensure(rb_require_relative_entrypoint, fname, namespace_both_pop, (VALUE)&arg);
}

static VALUE
rb_namespace_eval_string(VALUE str)
{
return rb_eval_string(RSTRING_PTR(str));
}

static VALUE
rb_namespace_eval(VALUE namespace, VALUE str)
{
rb_thread_t *th = GET_THREAD();

StringValue(str);

namespace_push(th, namespace);
return rb_ensure(rb_namespace_eval_string, str, namespace_pop, (VALUE)th);
}

static int namespace_experimental_warned = 0;

void
Expand Down Expand Up @@ -1061,6 +1078,7 @@ Init_Namespace(void)
rb_define_method(rb_cNamespace, "load", rb_namespace_load, -1);
rb_define_method(rb_cNamespace, "require", rb_namespace_require, 1);
rb_define_method(rb_cNamespace, "require_relative", rb_namespace_require_relative, 1);
rb_define_method(rb_cNamespace, "eval", rb_namespace_eval, 1);

rb_define_method(rb_cNamespace, "inspect", rb_namespace_inspect, 0);

Expand Down
82 changes: 82 additions & 0 deletions test/ruby/test_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -533,4 +533,86 @@ def test_load_path_and_loaded_features
assert !$LOADED_FEATURES.include?(File.join(namespace_dir, 'blank1.rb'))
assert !$LOADED_FEATURES.include?(File.join(namespace_dir, 'blank2.rb'))
end

def test_eval_basic
pend unless Namespace.enabled?

# Test basic evaluation
result = @n.eval("1 + 1")
assert_equal 2, result

# Test string evaluation
result = @n.eval("'hello ' + 'world'")
assert_equal "hello world", result
end

def test_eval_with_constants
pend unless Namespace.enabled?

# Define a constant in the namespace via eval
@n.eval("TEST_CONST = 42")
assert_equal 42, @n::TEST_CONST

# Constant should not be visible in main namespace
assert_raise(NameError) { TEST_CONST }
end

def test_eval_with_classes
pend unless Namespace.enabled?

# Define a class in the namespace via eval
@n.eval("class TestClass; def hello; 'from namespace'; end; end")

# Class should be accessible in the namespace
instance = @n::TestClass.new
assert_equal "from namespace", instance.hello

# Class should not be visible in main namespace
assert_raise(NameError) { TestClass }
end

def test_eval_isolation
pend unless Namespace.enabled?

# Create another namespace
n2 = Namespace.new

# Define different constants in each namespace
@n.eval("ISOLATION_TEST = 'first'")
n2.eval("ISOLATION_TEST = 'second'")

# Each namespace should have its own constant
assert_equal "first", @n::ISOLATION_TEST
assert_equal "second", n2::ISOLATION_TEST

# Constants should not interfere with each other
assert_not_equal @n::ISOLATION_TEST, n2::ISOLATION_TEST
end

def test_eval_with_variables
pend unless Namespace.enabled?

# Test local variable access (should work within the eval context)
result = @n.eval("x = 10; y = 20; x + y")
assert_equal 30, result
end

def test_eval_error_handling
pend unless Namespace.enabled?

# Test syntax error
assert_raise(SyntaxError) { @n.eval("1 +") }

# Test name error
assert_raise(NameError) { @n.eval("undefined_variable") }

# Test that namespace is properly restored after error
begin
@n.eval("raise RuntimeError, 'test error'")
rescue RuntimeError
# Should be able to continue using the namespace
result = @n.eval("2 + 2")
assert_equal 4, result
end
end
end
Loading