diff options
Diffstat (limited to 'struct.c')
-rw-r--r-- | struct.c | 23 |
1 files changed, 14 insertions, 9 deletions
@@ -12,15 +12,13 @@ #include "ruby/ruby.h" VALUE rb_cStruct; +static ID id_members; static VALUE struct_alloc(VALUE); -VALUE -rb_struct_iv_get(VALUE c, const char *name) +static inline VALUE +struct_ivar_get(VALUE c, ID id) { - ID id; - - id = rb_intern(name); for (;;) { if (rb_ivar_defined(c, id)) return rb_ivar_get(c, id); @@ -31,9 +29,15 @@ rb_struct_iv_get(VALUE c, const char *name) } VALUE +rb_struct_iv_get(VALUE c, const char *name) +{ + return struct_ivar_get(c, rb_intern(name)); +} + +VALUE rb_struct_s_members(VALUE klass) { - VALUE members = rb_struct_iv_get(klass, "__members__"); + VALUE members = struct_ivar_get(klass, id_members); if (NIL_P(members)) { rb_raise(rb_eTypeError, "uninitialized struct"); @@ -193,7 +197,7 @@ make_struct(VALUE name, VALUE members, VALUE klass) } nstr = rb_define_class_under(klass, rb_id2name(id), klass); } - rb_iv_set(nstr, "__members__", members); + rb_ivar_set(nstr, id_members, members); rb_define_alloc_func(nstr, struct_alloc); rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1); @@ -248,7 +252,7 @@ rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_ rb_class_inherited(super, klass); } - rb_iv_set(klass, "__members__", members); + rb_ivar_set(klass, id_members, members); if (alloc) rb_define_alloc_func(klass, alloc); @@ -342,7 +346,7 @@ static size_t num_members(VALUE klass) { VALUE members; - members = rb_struct_iv_get(klass, "__members__"); + members = struct_ivar_get(klass, id_members); if (TYPE(members) != T_ARRAY) { rb_raise(rb_eTypeError, "broken members"); } @@ -907,4 +911,5 @@ Init_Struct(void) rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1); rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0); + id_members = rb_intern("__members__"); } |