From 45f61e38796c6097cd35db3410fcdc20379d167d Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Sat, 7 May 2022 17:34:17 +0900 Subject: [PATCH] class.c (mrb_get_args): remove `I` specifier (istruct); close #5706 --- mrbgems/mruby-random/src/random.c | 25 +++++++++++++++---- .../mruby-test-inline-struct/test/inline.c | 11 +++++--- src/class.c | 24 ------------------ 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c index f0e01c43c..bd126a93d 100644 --- a/mrbgems/mruby-random/src/random.c +++ b/mrbgems/mruby-random/src/random.c @@ -242,14 +242,22 @@ random_m_bytes(mrb_state *mrb, mrb_value self) static mrb_value mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary) { - mrb_int i, max; - rand_state *random; if (RARRAY_LEN(ary) > 1) { + mrb_int i, max; + mrb_value r; + rand_state *random; + struct RClass *c = mrb_class_get_id(mrb, ID_RANDOM_STRICT); - if (mrb_get_args(mrb, "|I", &random, c) == 0) { + if (mrb_get_args(mrb, "|o", &r) == 0) { random = random_default_state(mrb); } + else if (mrb_obj_is_kind_of(mrb, r, c)){ + random = (rand_state*)mrb_istruct_ptr(r); + } + else { + mrb_raise(mrb, E_TYPE_ERROR, "Random object required"); + } mrb_ary_modify(mrb, mrb_ary_ptr(ary)); max = RARRAY_LEN(ary); for (i = RARRAY_LEN(ary) - 1; i > 0; i--) { @@ -304,13 +312,20 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary) { mrb_int n = 0; mrb_bool given; + mrb_value r; rand_state *random; mrb_int len; struct RClass *c = mrb_class_get_id(mrb, ID_RANDOM_STRICT); - if (mrb_get_args(mrb, "|i?I", &n, &given, &random, c) < 2) { + if (mrb_get_args(mrb, "|i?o", &n, &given, &r) < 2) { random = random_default_state(mrb); } + else if (mrb_obj_is_kind_of(mrb, r, c)){ + random = (rand_state*)mrb_istruct_ptr(r); + } + else { + mrb_raise(mrb, E_TYPE_ERROR, "Random object required"); + } len = RARRAY_LEN(ary); if (!given) { /* pick one element */ switch (len) { @@ -387,7 +402,7 @@ void mrb_mruby_random_gem_init(mrb_state *mrb) mrb_define_method(mrb, mrb->kernel_module, "srand", random_f_srand, MRB_ARGS_OPT(1)); random = mrb_define_class(mrb, "Random", mrb->object_class); - mrb_const_set(mrb, mrb_obj_value(mrb->object_class), ID_RANDOM_STRICT, mrb_obj_value(random)); // for mrb_get_args() + mrb_const_set(mrb, mrb_obj_value(mrb->object_class), ID_RANDOM_STRICT, mrb_obj_value(random)); // for class check MRB_SET_INSTANCE_TT(random, MRB_TT_ISTRUCT); mrb_define_class_method(mrb, random, "rand", random_f_rand, MRB_ARGS_OPT(1)); mrb_define_class_method(mrb, random, "srand", random_f_srand, MRB_ARGS_OPT(1)); diff --git a/mrbgems/mruby-test-inline-struct/test/inline.c b/mrbgems/mruby-test-inline-struct/test/inline.c index 7537bffc6..a0058554f 100644 --- a/mrbgems/mruby-test-inline-struct/test/inline.c +++ b/mrbgems/mruby-test-inline-struct/test/inline.c @@ -56,10 +56,15 @@ istruct_test_test_receive(mrb_state *mrb, mrb_value self) static mrb_value istruct_test_test_receive_direct(mrb_state *mrb, mrb_value self) { - char *ptr; + mrb_value is; struct RClass *klass = mrb_class_get(mrb, "InlineStructTest"); - mrb_get_args(mrb, "I", &ptr, klass); - return mrb_bool_value(ptr[0] == 's'); + mrb_get_args(mrb, "o", &is); + if (mrb_obj_is_kind_of(mrb, is, klass)) { + char *ptr = mrb_istruct_ptr(is);; + return mrb_bool_value(ptr[0] == 's'); + } + mrb_raise(mrb, E_TYPE_ERROR, "InlineStructTest"); + return mrb_false_value(); } static mrb_value diff --git a/src/class.c b/src/class.c index 2331b53f2..3c2033251 100644 --- a/src/class.c +++ b/src/class.c @@ -914,7 +914,6 @@ mrb_block_given_p(mrb_state *mrb) b: boolean [mrb_bool] n: String/Symbol [mrb_sym] d: data [void*,mrb_data_type const] 2nd argument will be used to check data type so it won't be modified; when ! follows, the value may be nil - I: inline struct [void*,struct RClass] I! gives NULL for nil &: block [mrb_value] &! raises exception if no block given *: rest argument [const mrb_value*,mrb_int] The rest of the arguments as an array; *! avoid copy of the stack |: optional Following arguments are optional @@ -1162,29 +1161,6 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) } } break; - case 'I': - { - void* *p; - struct RClass *klass; - - p = va_arg(ap, void**); - klass = va_arg(ap, struct RClass*); - if (pickarg) { - if (altmode && mrb_nil_p(*pickarg)) { - *p = NULL; - } - else { - if (!mrb_obj_is_kind_of(mrb, *pickarg, klass)) { - mrb_raisef(mrb, E_TYPE_ERROR, "%v is not a %C", *pickarg, klass); - } - if (!mrb_istruct_p(*pickarg)) { - mrb_raisef(mrb, E_TYPE_ERROR, "%v is not inline struct", *pickarg); - } - *p = mrb_istruct_ptr(*pickarg); - } - } - } - break; #ifndef MRB_NO_FLOAT case 'f': {