Skip to content

Commit

Permalink
class.c (mrb_get_args): remove I specifier (istruct); close mruby#5706
Browse files Browse the repository at this point in the history
  • Loading branch information
matz committed May 7, 2022
1 parent 457abf4 commit 45f61e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
25 changes: 20 additions & 5 deletions mrbgems/mruby-random/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -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--) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
11 changes: 8 additions & 3 deletions mrbgems/mruby-test-inline-struct/test/inline.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 0 additions & 24 deletions src/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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':
{
Expand Down

0 comments on commit 45f61e3

Please sign in to comment.