* class.c (rb_define_class_id_under, rb_define_module_id_under):

new functions to define a nested class/module with non-ascii
  name.

* struct.c (make_struct): use name with encoding.

* struct.c (inspect_struct): ditto.  [ruby-core:24849]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2009-08-12 06:32:21 +00:00
Родитель c00428ed9e
Коммит 31f4a82942
5 изменённых файлов: 48 добавлений и 20 удалений

Просмотреть файл

@ -1,3 +1,13 @@
Wed Aug 12 15:32:16 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* class.c (rb_define_class_id_under, rb_define_module_id_under):
new functions to define a nested class/module with non-ascii
name.
* struct.c (make_struct): use name with encoding.
* struct.c (inspect_struct): ditto. [ruby-core:24849]
Wed Aug 12 Wed Aug 12 14:54:34 2009 Koichi Sasada <ko1@atdot.net> Wed Aug 12 Wed Aug 12 14:54:34 2009 Koichi Sasada <ko1@atdot.net>
* insns.def, vm.c, vm_insnhelper.c, vm_insnhelper.h: check * insns.def, vm.c, vm_insnhelper.c, vm_insnhelper.h: check

28
class.c
Просмотреть файл

@ -359,26 +359,30 @@ rb_define_class(const char *name, VALUE super)
VALUE VALUE
rb_define_class_under(VALUE outer, const char *name, VALUE super) rb_define_class_under(VALUE outer, const char *name, VALUE super)
{ {
VALUE klass; return rb_define_class_id_under(outer, rb_intern(name), super);
ID id; }
VALUE
rb_define_class_id_under(VALUE outer, ID id, VALUE super)
{
VALUE klass;
id = rb_intern(name);
if (rb_const_defined_at(outer, id)) { if (rb_const_defined_at(outer, id)) {
klass = rb_const_get_at(outer, id); klass = rb_const_get_at(outer, id);
if (TYPE(klass) != T_CLASS) { if (TYPE(klass) != T_CLASS) {
rb_raise(rb_eTypeError, "%s is not a class", name); rb_raise(rb_eTypeError, "%s is not a class", rb_id2name(id));
} }
if (rb_class_real(RCLASS_SUPER(klass)) != super) { if (rb_class_real(RCLASS_SUPER(klass)) != super) {
rb_name_error(id, "%s is already defined", name); rb_name_error(id, "%s is already defined", rb_id2name(id));
} }
return klass; return klass;
} }
if (!super) { if (!super) {
rb_warn("no super class for `%s::%s', Object assumed", rb_warn("no super class for `%s::%s', Object assumed",
rb_class2name(outer), name); rb_class2name(outer), rb_id2name(id));
} }
klass = rb_define_class_id(id, super); klass = rb_define_class_id(id, super);
rb_set_class_path(klass, outer, name); rb_set_class_path_string(klass, outer, rb_id2str(id));
rb_const_set(outer, id, klass); rb_const_set(outer, id, klass);
rb_class_inherited(super, klass); rb_class_inherited(super, klass);
@ -429,10 +433,14 @@ rb_define_module(const char *name)
VALUE VALUE
rb_define_module_under(VALUE outer, const char *name) rb_define_module_under(VALUE outer, const char *name)
{ {
VALUE module; return rb_define_module_id_under(outer, rb_intern(name));
ID id; }
VALUE
rb_define_module_under(VALUE outer, ID id)
{
VALUE module;
id = rb_intern(name);
if (rb_const_defined_at(outer, id)) { if (rb_const_defined_at(outer, id)) {
module = rb_const_get_at(outer, id); module = rb_const_get_at(outer, id);
if (TYPE(module) == T_MODULE) if (TYPE(module) == T_MODULE)

Просмотреть файл

@ -152,8 +152,10 @@ VALUE rb_make_metaclass(VALUE, VALUE);
void rb_check_inheritable(VALUE); void rb_check_inheritable(VALUE);
VALUE rb_class_inherited(VALUE, VALUE); VALUE rb_class_inherited(VALUE, VALUE);
VALUE rb_define_class_id(ID, VALUE); VALUE rb_define_class_id(ID, VALUE);
VALUE rb_define_class_id_under(VALUE, ID, VALUE);
VALUE rb_module_new(void); VALUE rb_module_new(void);
VALUE rb_define_module_id(ID); VALUE rb_define_module_id(ID);
VALUE rb_define_module_id_under(VALUE, ID);
VALUE rb_mod_included_modules(VALUE); VALUE rb_mod_included_modules(VALUE);
VALUE rb_mod_include_p(VALUE, VALUE); VALUE rb_mod_include_p(VALUE, VALUE);
VALUE rb_mod_ancestors(VALUE); VALUE rb_mod_ancestors(VALUE);

Просмотреть файл

@ -196,7 +196,7 @@ make_struct(VALUE name, VALUE members, VALUE klass)
rb_warn("redefining constant Struct::%s", StringValuePtr(name)); rb_warn("redefining constant Struct::%s", StringValuePtr(name));
rb_mod_remove_const(klass, ID2SYM(id)); rb_mod_remove_const(klass, ID2SYM(id));
} }
nstr = rb_define_class_under(klass, rb_id2name(id), klass); nstr = rb_define_class_id_under(klass, id, klass);
} }
rb_ivar_set(nstr, id_members, members); rb_ivar_set(nstr, id_members, members);
@ -496,21 +496,19 @@ rb_struct_each_pair(VALUE s)
static VALUE static VALUE
inspect_struct(VALUE s, VALUE dummy, int recur) inspect_struct(VALUE s, VALUE dummy, int recur)
{ {
const char *cname = rb_class2name(rb_obj_class(s)); VALUE cname = rb_class_name(rb_obj_class(s));
VALUE str, members; VALUE members, str = rb_str_new2("#<struct ");
long i; long i;
char first = RSTRING_PTR(cname)[0];
if (recur || first != '#') {
rb_str_append(str, cname);
}
if (recur) { if (recur) {
return rb_sprintf("#<struct %s:...>", cname); return rb_str_cat2(str, ":...>");
} }
members = rb_struct_members(s); members = rb_struct_members(s);
if (cname[0] == '#') {
str = rb_str_new2("#<struct ");
}
else {
str = rb_sprintf("#<struct %s ", cname);
}
for (i=0; i<RSTRUCT_LEN(s); i++) { for (i=0; i<RSTRUCT_LEN(s); i++) {
VALUE slot; VALUE slot;
ID id; ID id;
@ -518,6 +516,9 @@ inspect_struct(VALUE s, VALUE dummy, int recur)
if (i > 0) { if (i > 0) {
rb_str_cat2(str, ", "); rb_str_cat2(str, ", ");
} }
else if (first != '#') {
rb_str_cat2(str, " ");
}
slot = RARRAY_PTR(members)[i]; slot = RARRAY_PTR(members)[i];
id = SYM2ID(slot); id = SYM2ID(slot);
if (rb_is_local_id(id) || rb_is_const_id(id)) { if (rb_is_local_id(id) || rb_is_const_id(id)) {

Просмотреть файл

@ -212,4 +212,11 @@ class TestStruct < Test::Unit::TestCase
Struct.new(0) Struct.new(0)
} }
end end
def test_nonascii
struct_test = Struct.new("R\u{e9}sum\u{e9}", :"r\u{e9}sum\u{e9}")
assert_equal(Struct.const_get("R\u{e9}sum\u{e9}"), struct_test, '[ruby-core:24849]')
a = struct_test.new(42)
assert_equal("#<struct Struct::R\u{e9}sum\u{e9} r\u{e9}sum\u{e9}=42>", a.inspect, '[ruby-core:24849]')
end
end end