diff options
| author | Yukihiro Matsumoto <[email protected]> | 2012-05-24 01:09:36 +0900 |
|---|---|---|
| committer | Yukihiro Matsumoto <[email protected]> | 2012-05-24 01:09:36 +0900 |
| commit | ad9e841c5359efdd37a132767e03ad7a2d5ae72e (patch) | |
| tree | 8e60edf2dce29e86d8b47d0b2e8dc8a2a2329ded /src/struct.c | |
| parent | 4f012f578f22cca67af8c633845464abc79913f4 (diff) | |
| download | mruby-ad9e841c5359efdd37a132767e03ad7a2d5ae72e.tar.gz mruby-ad9e841c5359efdd37a132767e03ad7a2d5ae72e.zip | |
made mrb_get_args() better (optinal args, type checks); close #173 #176
Diffstat (limited to 'src/struct.c')
| -rw-r--r-- | src/struct.c | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/src/struct.c b/src/struct.c index ff66b19a2..699825cff 100644 --- a/src/struct.c +++ b/src/struct.c @@ -533,19 +533,20 @@ mrb_value mrb_struct_init_copy(mrb_state *mrb, mrb_value copy) { mrb_value s; + mrb_get_args(mrb, "o", &s); - if (mrb_obj_equal(mrb, copy, s)) return copy; - //mrb_check_frozen(copy); - if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) { - mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); - } - if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { - mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); - } - memcpy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), sizeof(mrb_value)*RSTRUCT_LEN(copy)); + if (mrb_obj_equal(mrb, copy, s)) return copy; + //mrb_check_frozen(copy); + if (!mrb_obj_is_instance_of(mrb, s, mrb_obj_class(mrb, copy))) { + mrb_raise(mrb, E_TYPE_ERROR, "wrong argument class"); + } + if (RSTRUCT_LEN(copy) != RSTRUCT_LEN(s)) { + mrb_raise(mrb, E_TYPE_ERROR, "struct size mismatch"); + } + memcpy(RSTRUCT_PTR(copy), RSTRUCT_PTR(s), sizeof(mrb_value)*RSTRUCT_LEN(copy)); - return copy; + return copy; } static mrb_value @@ -668,24 +669,25 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s) long i; mrb_value idx; mrb_value val; + mrb_get_args(mrb, "oo", &idx, &val); - if (mrb_type(idx) == MRB_TT_STRING || mrb_type(idx) == MRB_TT_SYMBOL) { - return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); - } + if (mrb_type(idx) == MRB_TT_STRING || mrb_type(idx) == MRB_TT_SYMBOL) { + return mrb_struct_aset_id(mrb, s, mrb_to_id(mrb, idx), val); + } - i = mrb_fixnum(idx); - if (i < 0) i = RSTRUCT_LEN(s) + i; - if (i < 0) { - mrb_raise(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - if (RSTRUCT_LEN(s) <= i) { - mrb_raise(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", - i, RSTRUCT_LEN(s)); - } - mrb_struct_modify(s); - return RSTRUCT_PTR(s)[i] = val; + i = mrb_fixnum(idx); + if (i < 0) i = RSTRUCT_LEN(s) + i; + if (i < 0) { + mrb_raise(mrb, E_INDEX_ERROR, "offset %ld too small for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + if (RSTRUCT_LEN(s) <= i) { + mrb_raise(mrb, E_INDEX_ERROR, "offset %ld too large for struct(size:%ld)", + i, RSTRUCT_LEN(s)); + } + mrb_struct_modify(s); + return RSTRUCT_PTR(s)[i] = val; } static mrb_value |
