summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-random
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-08-14 15:28:38 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-08-14 15:28:38 +0900
commit40c4c5d344f44f38c777f2e23dfe8bd7b219660c (patch)
treea684ee92fbfd8968811868534bd144a17e44007a /mrbgems/mruby-random
parent8e5e96239c4e4b4725dbff14ce9f1019e3058de8 (diff)
downloadmruby-40c4c5d344f44f38c777f2e23dfe8bd7b219660c.tar.gz
mruby-40c4c5d344f44f38c777f2e23dfe8bd7b219660c.zip
random.c: use `I` specifier for `mrb_get_args()`; ref #5530
Diffstat (limited to 'mrbgems/mruby-random')
-rw-r--r--mrbgems/mruby-random/src/random.c55
1 files changed, 4 insertions, 51 deletions
diff --git a/mrbgems/mruby-random/src/random.c b/mrbgems/mruby-random/src/random.c
index a1f44f105..27fd98f82 100644
--- a/mrbgems/mruby-random/src/random.c
+++ b/mrbgems/mruby-random/src/random.c
@@ -149,14 +149,6 @@ get_opt(mrb_state* mrb)
return arg;
}
-static void
-random_check(mrb_state *mrb, mrb_value random) {
- struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Random));
- if (!mrb_obj_is_kind_of(mrb, random, c) || !mrb_istruct_p(random)) {
- mrb_raise(mrb, E_TYPE_ERROR, "Random instance required");
- }
-}
-
static mrb_value
random_default(mrb_state *mrb) {
struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Random));
@@ -227,47 +219,13 @@ static mrb_value
mrb_ary_shuffle_bang(mrb_state *mrb, mrb_value ary)
{
mrb_int i, max;
- mrb_value r = mrb_nil_value();
rand_state *random;
- /*
- * MSC compiler bug generating invalid instructions with optimization
- * enabled. MSC errantly uses a hardcoded value with optimizations on
- * when using a fixed value from a union.
- * Creating a temp volatile variable and reassigning back to the original
- * value tricks the compiler to not perform this optimization;
- */
-#if defined _MSC_VER && _MSC_VER >= 1923
- /* C++ will not cast away volatile easily, so we cannot do something like
- * volatile mrb_value rr = r; r = (mrb_value)rr; with C++.
- * That cast does work with C.
- * We also have to trick the compiler to not optimize away the const_cast entirely
- * by creating and manipulating an intermediate volatile pointer.
- */
- volatile mrb_value *v_r;
- volatile mrb_int ii;
- mrb_value *p_r;
- v_r = &r;
- ii = 2;
- v_r = v_r + 2;
-#if defined __cplusplus
- p_r = const_cast<mrb_value*>(v_r - ii);
-#else
- p_r = (mrb_value*)v_r - ii;
-#endif
- r = *p_r;
-#endif
-
if (RARRAY_LEN(ary) > 1) {
- mrb_get_args(mrb, "|o", &r);
-
- if (mrb_nil_p(r)) {
+ struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Random));
+ if (mrb_get_args(mrb, "|I", &random, c) == 0) {
random = random_default_state(mrb);
}
- else {
- random_check(mrb, r);
- random = random_ptr(r);
- }
mrb_ary_modify(mrb, mrb_ary_ptr(ary));
max = RARRAY_LEN(ary);
for (i = RARRAY_LEN(ary) - 1; i > 0; i--) {
@@ -322,18 +280,13 @@ mrb_ary_sample(mrb_state *mrb, mrb_value ary)
{
mrb_int n = 0;
mrb_bool given;
- mrb_value r = mrb_nil_value();
rand_state *random;
mrb_int len;
+ struct RClass *c = mrb_class_get_id(mrb, MRB_SYM(Random));
- mrb_get_args(mrb, "|i?o", &n, &given, &r);
- if (mrb_nil_p(r)) {
+ if (mrb_get_args(mrb, "|i?I", &n, &given, &random, c) < 2) {
random = random_default_state(mrb);
}
- else {
- random_check(mrb, r);
- random = random_ptr(r);
- }
len = RARRAY_LEN(ary);
if (!given) { /* pick one element */
switch (len) {