Implement Ruby2.7's frozen strings from `Module#name`
MODIFY
mrbgems/mruby-class-ext/test/module.rb
mrbgems/mruby-class-ext/test/module.rb
+def assert_module_name(exp, mod)
+ assert "assert_module_name" do
+ act = mod.__send__(:name)
+ assert_equal exp, act
+ assert_predicate act, :frozen?
+ end
+end
+
assert 'Module#<' do
a = Class.new
b = Class.new(a)
@@ -58,17 +66,17 @@ assert 'Module#name' do
const_set :SetInner, Class.new
end
- assert_equal 'Outer', Outer.name
- assert_equal 'Outer::Inner', Outer::Inner.name
- assert_equal 'Outer::SetInner', Outer::SetInner.name
+ assert_module_name 'Outer', Outer
+ assert_module_name 'Outer::Inner', Outer::Inner
+ assert_module_name 'Outer::SetInner', Outer::SetInner
outer = Module.new do
const_set :SetInner, Class.new
end
Object.const_set :SetOuter, outer
- assert_equal 'SetOuter', SetOuter.name
- assert_equal 'SetOuter::SetInner', SetOuter::SetInner.name
+ assert_module_name 'SetOuter', SetOuter
+ assert_module_name 'SetOuter::SetInner', SetOuter::SetInner
mod = Module.new
cls = Class.new

MODIFY
src/class.c
src/class.c
@@ -76,6 +76,7 @@ mrb_class_name_class(mrb_state *mrb, struct RClass *outer, struct RClass *c, mrb
name = mrb_str_dup(mrb, name);
mrb_str_cat_cstr(mrb, name, "::");
mrb_str_cat_cstr(mrb, name, mrb_sym_name(mrb, id));
+ MRB_SET_FROZEN_FLAG(mrb_obj_ptr(name));
}
mrb_obj_iv_set_force(mrb, (struct RObject*)c, nsym, name);
}
@@ -1712,7 +1713,7 @@ mrb_class_path(mrb_state *mrb, struct RClass *c)
/* toplevel class/module */
return mrb_sym_str(mrb, mrb_symbol(path));
}
- return mrb_str_dup(mrb, path);
+ return path;
}
MRB_API struct RClass*

MODIFY
src/variable.c
src/variable.c
@@ -1123,7 +1123,7 @@ mrb_class_find_path(mrb_state *mrb, struct RClass *c)
iv_del(mrb, c->iv, mrb_intern_lit(mrb, "__outer__"), NULL);
iv_put(mrb, c->iv, mrb_intern_lit(mrb, "__classname__"), path);
mrb_field_write_barrier_value(mrb, (struct RBasic*)c, path);
- path = mrb_str_dup(mrb, path);
+ MRB_SET_FROZEN_FLAG(mrb_obj_ptr(path));
}
return path;
}

Implement Ruby2.7's frozen strings from `Module#name`