summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-10-06 15:15:12 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-10-06 15:15:12 +0000
commitf433be6d9c9ca7010d2c4368030a09a76bb3b98b (patch)
treed01fd00f2f8484478658bab588709901875da88d /struct.c
parent16d052477b987fbe2db8ca68be2d4a5fb405514b (diff)
* io.c (rb_io_s_sysopen): preserve path in the buffer allocated by
ALLOCA_N() to prevent modification. [ruby-dev:24438] * io.c (rb_io_mode_flags): preserve append mode flag. [ruby-dev:24436] * io.c (rb_io_modenum_mode): do not use external output buffer. * string.c (rb_str_justify): differ pointer retrieval to prevent padding string modification. [ruby-dev:24434] * range.c (range_each_func): allow func to terminate loop by returning RANGE_EACH_BREAK. * range.c (member_i): use RANGE_EACH_BREAK. [ruby-talk:114959] * marshal.c (r_byte): retrieve pointer from string value for each time. [ruby-dev:24404] * marshal.c (r_bytes0): ditto. * enum.c (sort_by_i): re-entrance check added. [ruby-dev:24399] * io.c (io_read): should freeze all reading buffer. [ruby-dev:24400] * string.c (rb_str_sum): should use bignums when bits is greater than or equals to sizeof(long)*CHAR_BITS. [ruby-dev:24395] * eval.c (specific_eval): defer pointer retrieval to prevent unsafe sourcefile string modification. [ruby-dev:24382] * eval.c (specific_eval): defer pointer retrieval to prevent unsafe sourcefile string modification. [ruby-dev:24382] * string.c (rb_str_sum): wrong cast caused wrong result. [ruby-dev:24385] * enum.c (enum_sort_by): hide temporary array from ObjectSpace.each_object. [ruby-dev:24386] * string.c (rb_str_sum): check was done with false pointer. [ruby-dev:24383] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@7003 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/struct.c b/struct.c
index b5454b4bea..a82799de4d 100644
--- a/struct.c
+++ b/struct.c
@@ -33,8 +33,8 @@ rb_struct_iv_get(c, name)
}
}
-static VALUE
-struct_s_members(klass)
+VALUE
+rb_struct_s_members(klass)
VALUE klass;
{
VALUE members = rb_struct_iv_get(klass, "__members__");
@@ -45,15 +45,12 @@ struct_s_members(klass)
return members;
}
-static VALUE
-struct_members(s)
+VALUE
+rb_struct_members(s)
VALUE s;
{
- VALUE members = struct_s_members(rb_obj_class(s));
+ VALUE members = rb_struct_s_members(rb_obj_class(s));
- if (NIL_P(members)) {
- rb_bug("non-initialized struct");
- }
if (RSTRUCT(s)->len != RARRAY(members)->len) {
rb_raise(rb_eTypeError, "struct size differs (%d required %d given)",
RARRAY(members)->len, RSTRUCT(s)->len);
@@ -62,13 +59,13 @@ struct_members(s)
}
static VALUE
-rb_struct_s_members(klass)
+rb_struct_s_members_m(klass)
VALUE klass;
{
VALUE members, ary;
VALUE *p, *pend;
- members = struct_s_members(klass);
+ members = rb_struct_s_members(klass);
ary = rb_ary_new2(RARRAY(members)->len);
p = RARRAY(members)->ptr; pend = p + RARRAY(members)->len;
while (p < pend) {
@@ -92,10 +89,10 @@ rb_struct_s_members(klass)
*/
static VALUE
-rb_struct_members(obj)
+rb_struct_members_m(obj)
VALUE obj;
{
- return rb_struct_s_members(rb_obj_class(obj));
+ return rb_struct_s_members_m(rb_obj_class(obj));
}
VALUE
@@ -106,10 +103,7 @@ rb_struct_getmember(obj, id)
VALUE members, slot;
long i;
- members = rb_struct_iv_get(rb_obj_class(obj), "__members__");
- if (NIL_P(members)) {
- rb_bug("uninitialized struct");
- }
+ members = rb_struct_members(obj);
slot = ID2SYM(id);
for (i=0; i<RARRAY(members)->len; i++) {
if (RARRAY(members)->ptr[i] == slot) {
@@ -167,10 +161,7 @@ rb_struct_set(obj, val)
VALUE members, slot;
long i;
- members = rb_struct_iv_get(rb_obj_class(obj), "__members__");
- if (NIL_P(members)) {
- rb_bug("uninitialized struct");
- }
+ members = rb_struct_members(obj);
rb_struct_modify(obj);
for (i=0; i<RARRAY(members)->len; i++) {
slot = RARRAY(members)->ptr[i];
@@ -215,7 +206,7 @@ make_struct(name, members, klass)
rb_define_alloc_func(nstr, struct_alloc);
rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
rb_define_singleton_method(nstr, "[]", rb_class_new_instance, -1);
- rb_define_singleton_method(nstr, "members", rb_struct_s_members, 0);
+ rb_define_singleton_method(nstr, "members", rb_struct_s_members_m, 0);
for (i=0; i< RARRAY(members)->len; i++) {
ID id = SYM2ID(RARRAY(members)->ptr[i]);
if (i<10) {
@@ -315,12 +306,19 @@ rb_struct_s_def(argc, argv, klass)
id = rb_to_id(RARRAY(rest)->ptr[i]);
RARRAY(rest)->ptr[i] = ID2SYM(id);
}
- if (!NIL_P(name) && TYPE(name) != T_STRING) {
- id = rb_to_id(name);
- rb_ary_unshift(rest, ID2SYM(id));
- name = Qnil;
+ if (!NIL_P(name)) {
+ VALUE tmp = rb_check_string_type(name);
+
+ if (NIL_P(tmp)) {
+ id = rb_to_id(name);
+ rb_ary_unshift(rest, ID2SYM(id));
+ name = Qnil;
+ }
}
st = make_struct(name, rest, klass);
+ if (rb_block_given_p()) {
+ rb_mod_module_eval(0, 0, st);
+ }
return st;
}
@@ -456,7 +454,7 @@ rb_struct_each_pair(s)
VALUE members;
long i;
- members = struct_members(s);
+ members = rb_struct_members(s);
for (i=0; i<RSTRUCT(s)->len; i++) {
rb_yield_values(2, rb_ary_entry(members, i), RSTRUCT(s)->ptr[i]);
}
@@ -471,7 +469,7 @@ inspect_struct(s)
VALUE str, members;
long i;
- members = struct_members(s);
+ members = rb_struct_members(s);
str = rb_str_buf_new2("#<struct ");
rb_str_cat2(str, cname);
rb_str_cat2(str, " ");
@@ -537,6 +535,7 @@ rb_struct_to_a(s)
return rb_ary_new4(RSTRUCT(s)->len, RSTRUCT(s)->ptr);
}
+/* :nodoc: */
static VALUE
rb_struct_init_copy(copy, s)
VALUE copy, s;
@@ -561,7 +560,7 @@ rb_struct_aref_id(s, id)
VALUE members;
long i, len;
- members = struct_members(s);
+ members = rb_struct_members(s);
len = RARRAY(members)->len;
for (i=0; i<len; i++) {
if (SYM2ID(RARRAY(members)->ptr[i]) == id) {
@@ -620,7 +619,7 @@ rb_struct_aset_id(s, id, val)
VALUE members;
long i, len;
- members = struct_members(s);
+ members = rb_struct_members(s);
rb_struct_modify(s);
len = RARRAY(members)->len;
if (RSTRUCT(s)->len != RARRAY(members)->len) {
@@ -907,5 +906,5 @@ Init_Struct()
rb_define_method(rb_cStruct, "select", rb_struct_select, -1);
rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
- rb_define_method(rb_cStruct, "members", rb_struct_members, 0);
+ rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
}