static mrb_value mrb_random_srand(mrb_state *mrb, mrb_value self) { mrb_int argc; mrb_value *argv; mrb_value seed; mrb_get_args(mrb, "*", &argv, &argc); if (argc == 0) { seed = mrb_nil_value(); } else if (argc == 1) { seed = argv[0]; if (!mrb_nil_p(seed) && !mrb_fixnum_p(seed)) { seed = mrb_check_convert_type(mrb, seed, MRB_TT_FIXNUM, "Fixnum", "to_int"); } if (!mrb_nil_p(seed) && mrb_fixnum(seed) < 0) { seed = mrb_fixnum_value(0 - mrb_fixnum(seed)); } if (!mrb_fixnum_p(seed)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument type"); return mrb_nil_value(); } } else { mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%d for 0..1)", argc); return mrb_nil_value(); } seed = mrb_random_mt_srand(mrb, seed); mrb_value old_seed = mrb_gv_get(mrb, mrb_intern(mrb, RAND_SEED_KEY)); mrb_gv_set(mrb, mrb_intern(mrb, RAND_SEED_KEY), seed); return old_seed; }
mrb_value mrb_f_kill(mrb_state *mrb, mrb_value klass) { mrb_int pid; mrb_value *argv, sigo; int argc, sent, signo = 0; mrb_get_args(mrb, "oi*", &sigo, &pid, &argv, &argc); if (mrb_fixnum_p(sigo)) { signo = mrb_fixnum(sigo); } else { mrb_raisef(mrb, E_TYPE_ERROR, "bad signal type %s", mrb_obj_classname(mrb, sigo)); } sent = 0; if (kill(pid, signo) == -1) mrb_sys_fail(mrb, "kill"); sent++; while (argc-- > 0) { if (!mrb_fixnum_p(*argv)) { mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %s (expected Fixnum)", mrb_obj_classname(mrb, *argv)); } if (kill(mrb_fixnum(*argv), signo) == -1) mrb_sys_fail(mrb, "kill"); sent++; argv++; } return mrb_fixnum_value(sent); }
static int check_error(mrb_state *mrb) { /* Error check */ /* $ko_test and $kill_test should be 0 */ mrb_value ko_test = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$ko_test")); mrb_value kill_test = mrb_gv_get(mrb, mrb_intern_lit(mrb, "$kill_test")); return mrb_fixnum_p(ko_test) && mrb_fixnum(ko_test) == 0 && mrb_fixnum_p(kill_test) && mrb_fixnum(kill_test) == 0; }
/* * call-seq: * * num ** other -> num * * Raises <code>num</code> the <code>other</code> power. * * 2.0**3 #=> 8.0 */ static mrb_value num_pow(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_float d; mrb_get_args(mrb, "o", &y); d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y)); if (mrb_fixnum_p(x) && mrb_fixnum_p(y) && FIXABLE(d)) return mrb_fixnum_value((mrb_int)d); return mrb_float_value(mrb, d); }
mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method) { mrb_value v; if (mrb_fixnum_p(val)) return val; v = convert_type(mrb, val, "Integer", method, FALSE); if (mrb_nil_p(v) || !mrb_fixnum_p(v)) { return mrb_nil_value(); } return v; }
static mrb_value mrb_range_size(mrb_state *mrb, mrb_value range) { struct RRange *r = mrb_range_ptr(mrb, range); mrb_value beg, end; mrb_float beg_f, end_f; mrb_bool num_p = TRUE; mrb_bool excl; beg = r->edges->beg; end = r->edges->end; excl = r->excl; if (mrb_fixnum_p(beg)) { beg_f = (mrb_float)mrb_fixnum(beg); } else if (mrb_float_p(beg)) { beg_f = mrb_float(beg); } else { num_p = FALSE; } if (mrb_fixnum_p(end)) { end_f = (mrb_float)mrb_fixnum(end); } else if (mrb_float_p(end)) { end_f = mrb_float(end); } else { num_p = FALSE; } if (num_p) { mrb_float n = end_f - beg_f; mrb_float err = (fabs(beg_f) + fabs(end_f) + fabs(end_f-beg_f)) * MRB_FLOAT_EPSILON; if (err>0.5) err=0.5; if (excl) { if (n<=0) return mrb_fixnum_value(0); if (n<1) n = 0; else n = floor(n - err); } else { if (n<0) return mrb_fixnum_value(0); n = floor(n + err); } if (isinf(n+1)) return mrb_float_value(mrb, INFINITY); return mrb_fixnum_value(n+1); } return mrb_nil_value(); }
/* * call-seq: * * num ** other -> num * * Raises <code>num</code> the <code>other</code> power. * * 2.0**3 #=> 8.0 */ static mrb_value num_pow(mrb_state *mrb, mrb_value x) { mrb_value y; int both_int = FALSE; mrb_float d; mrb_get_args(mrb, "o", &y); if (mrb_fixnum_p(x) && mrb_fixnum_p(y)) both_int = TRUE; d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y)); if (both_int && FIXABLE(d)) return mrb_fixnum_value((mrb_int)d); return mrb_float_value(d); }
static mrb_value plus(mrb_state *mrb, mrb_value self) { mrb_value a, b; mrb_get_args(mrb, "oo", &a, &b); if (mrb_fixnum_p(a) && mrb_fixnum_p(b)) { mrb_int x = mrb_fixnum(a); mrb_int y = mrb_fixnum(b); return mrb_fixnum_value(x + y); } else if (mrb_string_p(a) && mrb_string_p(b)) { mrb_value s = mrb_str_plus(mrb, a, b); return s; } else { return mrb_nil_value(); } }
static mrb_int getButtonArg(mrb_state *mrb) { mrb_int num; mrb_value arg; mrb_get_args(mrb, "o", &arg); if (mrb_fixnum_p(arg)) { num = mrb_fixnum(arg); } else if (mrb_symbol_p(arg) && rgssVer >= 3) { mrb_value symHash = getMrbData(mrb)->buttoncodeHash; mrb_value numVal = mrb_hash_fetch(mrb, symHash, arg, mrb_fixnum_value(Input::None)); num = mrb_fixnum(numVal); } else { // FIXME: RMXP allows only few more types that // don't make sense (symbols in pre 3, floats) num = 0; } return num; }
bool rubyval_to_std_vector_string(mrb_state* mrb, mrb_value arg, std::vector<std::string>* ret, const char* funcName) { if (! mrb_array_p(arg)) { return false; } mrb_int len = mrb_ary_len(mrb, arg); for (mrb_int i = 0; i < len; i++) { mrb_value v = mrb_ary_ref(mrb, arg, i); std::string str = ""; if (mrb_fixnum_p(v)) { mrb_int val = mrb_fixnum(v); char *cstr = nullptr; sprintf(cstr, "%d", val); str = std::string(cstr); } else if (mrb_float_p(v)) { mrb_float val = mrb_float(v); char *cstr = nullptr; sprintf(cstr, "%f", val); str = std::string(cstr); } ret->push_back(str); } return true; }
static mrb_value ctx_array_reference(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; mrb_value mrb_id_or_name; grn_obj *object; mrb_get_args(mrb, "o", &mrb_id_or_name); if (mrb_nil_p(mrb_id_or_name)) { return mrb_nil_value(); } if (mrb_fixnum_p(mrb_id_or_name)) { grn_id id = mrb_fixnum(mrb_id_or_name); object = grn_ctx_at(ctx, id); } else { mrb_value mrb_name; mrb_name = mrb_convert_type(mrb, mrb_id_or_name, MRB_TT_STRING, "String", "to_str"); object = grn_ctx_get(ctx, RSTRING_PTR(mrb_name), RSTRING_LEN(mrb_name)); } return grn_mrb_value_from_grn_obj(mrb, object); }
static mrb_value initialize(mrb_state *mrb, mrb_value self) { mrb_value var; DATA_TYPE(self) = &mrb_pp_var_array_buffer_type; DATA_PTR(self) = mrb_pp_var_alloc(mrb); switch (mrb_get_args(mrb, "|o", &var)) { case 0: MRB_PP_VAR(self) = PPB(VarArrayBuffer)->Create(0); break; case 1: if (mrb_fixnum_p(var)) { MRB_PP_VAR(self) = PPB(VarArrayBuffer)->Create(mrb_fixnum(var)); break; } else if (!mrb_obj_is_kind_of(mrb, var, mrb_pp_var_class)) { mrb_raise(mrb, E_TYPE_ERROR, "argument must be a Fixnum or PP::Var"); } else if (!mrb_test(mrb_funcall(mrb, var, "is_array_buffer", 0))) { mrb_raisef(mrb, E_TYPE_ERROR, "%S is not an array buffer value", var); } MRB_PP_VAR(self) = MRB_PP_VAR(var); PPB(Var)->AddRef(MRB_PP_VAR(self)); break; default: mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); } return self; }
/* * call-seq: * fix.divmod(numeric) -> array * * See <code>Numeric#divmod</code>. */ static mrb_value fix_divmod(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); if (mrb_fixnum_p(y)) { mrb_int div, mod; if (mrb_fixnum(y) == 0) { return mrb_assoc_new(mrb, mrb_float_value(mrb, INFINITY), mrb_float_value(mrb, NAN)); } fixdivmod(mrb, mrb_fixnum(x), mrb_fixnum(y), &div, &mod); return mrb_assoc_new(mrb, mrb_fixnum_value(div), mrb_fixnum_value(mod)); } else { mrb_float div, mod; mrb_value a, b; flodivmod(mrb, (mrb_float)mrb_fixnum(x), mrb_to_flo(mrb, y), &div, &mod); a = mrb_float_value(mrb, (mrb_int)div); b = mrb_float_value(mrb, mod); return mrb_assoc_new(mrb, a, b); } }
static mrb_value fix_mod(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_int a; mrb_get_args(mrb, "o", &y); a = mrb_fixnum(x); if (mrb_fixnum_p(y)) { mrb_int b, mod; if ((b=mrb_fixnum(y)) == 0) { #ifdef MRB_WITHOUT_FLOAT /* ZeroDivisionError */ return mrb_fixnum_value(0); #else return mrb_float_value(mrb, NAN); #endif } fixdivmod(mrb, a, b, 0, &mod); return mrb_fixnum_value(mod); } #ifdef MRB_WITHOUT_FLOAT mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value"); #else else {
static mrb_value mrb_random_init(mrb_state *mrb, mrb_value self) { mrb_value seed; mt_state *t; /* avoid memory leaks */ t = (mt_state*)DATA_PTR(self); if (t) { mrb_free(mrb, t); } mrb_data_init(self, NULL, &mt_state_type); t = (mt_state *)mrb_malloc(mrb, sizeof(mt_state)); t->mti = N + 1; seed = get_opt(mrb); seed = mrb_random_mt_srand(mrb, t, seed); if (mrb_nil_p(seed)) { t->has_seed = FALSE; } else { mrb_assert(mrb_fixnum_p(seed)); t->has_seed = TRUE; t->seed = mrb_fixnum(seed); } mrb_data_init(self, t, &mt_state_type); return self; }
mrb_value mrb_ary_aset(mrb_state *mrb, mrb_value self) { mrb_value *argv; int argc; mrb_get_args(mrb, "*", &argv, &argc); switch(argc) { case 2: if (!mrb_fixnum_p(argv[0])) { /* Should we support Range object for 1st arg ? */ mrb_raise(mrb, E_TYPE_ERROR, "expected Fixnum for 1st argument"); } mrb_ary_set(mrb, self, mrb_fixnum(argv[0]), argv[1]); return argv[1]; case 3: mrb_ary_splice(mrb, self, mrb_fixnum(argv[0]), mrb_fixnum(argv[1]), argv[2]); return argv[2]; default: mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments"); return mrb_nil_value(); } }
mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int)) { mrb_int i, j, beg, len; mrb_value result; result = mrb_ary_new(mrb); for (i = 0; i < argc; ++i) { if (mrb_fixnum_p(argv[i])) { mrb_ary_push(mrb, result, func(mrb, obj, mrb_fixnum(argv[i]))); } else if (range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE)) { mrb_int const end = olen < beg + len ? olen : beg + len; for (j = beg; j < end; ++j) { mrb_ary_push(mrb, result, func(mrb, obj, j)); } for (; j < beg + len; ++j) { mrb_ary_push(mrb, result, mrb_nil_value()); } } else { mrb_raisef(mrb, E_TYPE_ERROR, "invalid values selector: %S", argv[i]); } } return result; }
mrb_value mrb_f_sleep(mrb_state *mrb, mrb_value klass) { int argc; mrb_value *argv; time_t beg, end; beg = time(0); mrb_get_args(mrb, "*", &argv, &argc); if (argc == 0) { sleep((32767<<16)+32767); } else if(argc == 1) { struct timeval tv; int n; if (mrb_fixnum_p(argv[0])) { tv.tv_sec = mrb_fixnum(argv[0]); tv.tv_usec = 0; } else { tv.tv_sec = mrb_float(argv[0]); tv.tv_usec = (mrb_float(argv[0]) - tv.tv_sec) * 1000000.0; } n = select(0, 0, 0, 0, &tv); if (n < 0) mrb_sys_fail(mrb, "mrb_f_sleep failed"); } else { mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong # of arguments"); } end = time(0) - beg; return mrb_fixnum_value(end); }
static mrb_bool r_gt(mrb_state *mrb, mrb_value a, mrb_value b) { mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* output :a < b => -1, a = b => 0, a > b => +1 */ return mrb_fixnum_p(r) && mrb_fixnum(r) == 1; }
static ssize_t mrb_wslay_event_recv_callback(wslay_event_context_ptr ctx, uint8_t *buf, size_t len, int flags, void *user_data) { mrb_wslay_user_data *data = (mrb_wslay_user_data *) user_data; mrb_state*mrb = data->mrb; int ai = mrb_gc_arena_save(mrb); struct mrb_jmpbuf *prev_jmp = mrb->jmp; struct mrb_jmpbuf c_jmp; mrb_int ret = -1; MRB_TRY(&c_jmp) { mrb->jmp = &c_jmp; mrb_value argv[2]; argv[0] = mrb_cptr_value(mrb, buf); argv[1] = mrb_fixnum_value(len); errno = 0; mrb_assert(mrb_type(data->recv_callback) == MRB_TT_PROC); mrb_value buf_obj = mrb_yield_argv(mrb, data->recv_callback, NELEMS(argv), argv); if (mrb_fixnum_p(buf_obj)) { ret = mrb_fixnum(buf_obj); } else { buf_obj = mrb_str_to_str(mrb, buf_obj); ret = RSTRING_LEN(buf_obj); if (ret < 0||ret > len) { mrb_raise(mrb, E_RANGE_ERROR, "returned buf doesn't fit"); } if (ret > 0) { memmove(buf, (uint8_t *) RSTRING_PTR(buf_obj), ret); } } mrb->jmp = prev_jmp; } MRB_CATCH(&c_jmp) { mrb->jmp = prev_jmp; if (mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EAGAIN"))|| mrb_obj_is_kind_of(mrb, mrb_obj_value(mrb->exc), mrb_class_get_under(mrb, mrb_module_get(mrb, "Errno"), "EWOULDBLOCK"))) { mrb->exc = NULL; wslay_event_set_error(ctx, WSLAY_ERR_WOULDBLOCK); } else { wslay_event_set_error(ctx, WSLAY_ERR_CALLBACK_FAILURE); } } MRB_END_EXC(&c_jmp); mrb_gc_arena_restore(mrb, ai); return ret; }
static mrb_value blender_setter(mrb_state *mrb, mrb_value self) { mrb_value a; mrb_value *e; mrb_get_args(mrb, "A", &a); if (RARRAY_LEN(a) != 3) { goto invalid; } e = RARRAY_PTR(a); if (mrb_fixnum_p(e[0]) && mrb_fixnum_p(e[1]) && mrb_fixnum_p(e[2])) { al_set_blender(mrb_fixnum(e[0]), mrb_fixnum(e[1]), mrb_fixnum(e[2])); return a; } invalid: mrb_raise(mrb, E_ALLEGRO_ERROR, "invalid blending mode"); return mrb_nil_value(); // unreachable }
/* * call-seq: * num.eql?(numeric) -> true or false * * Returns <code>true</code> if <i>num</i> and <i>numeric</i> are the * same type and have equal values. * * 1 == 1.0 #=> true * 1.eql?(1.0) #=> false * (1.0).eql?(1.0) #=> true */ static mrb_value fix_eql(mrb_state *mrb, mrb_value x) { mrb_value y; mrb_get_args(mrb, "o", &y); if (!mrb_fixnum_p(y)) return mrb_false_value(); return mrb_bool_value(mrb_fixnum(x) == mrb_fixnum(y)); }
grn_operator grn_mrb_value_to_operator(mrb_state *mrb, mrb_value mrb_op) { if (!mrb_fixnum_p(mrb_op)) { mrb_op = mrb_funcall(mrb, mrb_op, "value", 0); } return mrb_fixnum(mrb_op); }
VCL_INT vmod_exec_integer(VRT_CTX, struct vmod_priv *priv, VCL_STRING code) { mrb_state *mrb = (mrb_state*)priv->priv; mrb_value v = mrb_code_exec(mrb, code); if(!mrb_fixnum_p(v)) { return 0; } return mrb_fixnum(v); }
static mrb_value bit_coerce(mrb_state *mrb, mrb_value x) { while (!mrb_fixnum_p(x)) { if (mrb_float_p(x)) { mrb_raise(mrb, E_TYPE_ERROR, "can't convert Float into Integer"); } x = mrb_to_int(mrb, x); } return x; }
mrb_value mrb_num_div(mrb_state *mrb, mrb_value x, mrb_value y) { #ifdef MRB_WITHOUT_FLOAT if (!mrb_fixnum_p(y)) { mrb_raise(mrb, E_TYPE_ERROR, "non fixnum value"); } return mrb_fixnum_value(mrb_fixnum(x) / mrb_fixnum(y)); #else return mrb_float_value(mrb, mrb_to_flo(mrb, x) / mrb_to_flo(mrb, y)); #endif }
static mrb_bool r_ge(mrb_state *mrb, mrb_value a, mrb_value b) { mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */ /* output :a < b => -1, a = b => 0, a > b => +1 */ if (mrb_fixnum_p(r)) { mrb_int c = mrb_fixnum(r); if (c == 0 || c == 1) return TRUE; } return FALSE; }
static mrb_int aget_index(mrb_state *mrb, mrb_value index) { if (mrb_fixnum_p(index)) { return mrb_fixnum(index); } else { mrb_int i; mrb_get_args(mrb, "i", &i); return i; } }
/* * call-seq: * * num ** other -> num * * Raises <code>num</code> the <code>other</code> power. * * 2.0**3 #=> 8.0 */ static mrb_value num_pow(mrb_state *mrb, mrb_value x) { mrb_value y; #ifndef MRB_WITHOUT_FLOAT mrb_float d; #endif mrb_get_args(mrb, "o", &y); if (mrb_fixnum_p(x) && mrb_fixnum_p(y)) { /* try ipow() */ mrb_int base = mrb_fixnum(x); mrb_int exp = mrb_fixnum(y); mrb_int result = 1; if (exp < 0) #ifdef MRB_WITHOUT_FLOAT return mrb_fixnum_value(0); #else goto float_pow; #endif for (;;) { if (exp & 1) { if (mrb_int_mul_overflow(result, base, &result)) { #ifndef MRB_WITHOUT_FLOAT goto float_pow; #endif } } exp >>= 1; if (exp == 0) break; if (mrb_int_mul_overflow(base, base, &base)) { #ifndef MRB_WITHOUT_FLOAT goto float_pow; #endif } } return mrb_fixnum_value(result); }
static mrb_value mrb_to_integer(mrb_state *mrb, mrb_value val, const char *method) { mrb_value v; if (mrb_fixnum_p(val)) return val; v = convert_type(mrb, val, "Integer", method, TRUE); if (!mrb_obj_is_kind_of(mrb, v, mrb->fixnum_class)) { mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer (%S#%S gives %S)", val, val, mrb_str_new_cstr(mrb, method), v); } return v; }