static mrb_value mrb_int_chr(mrb_state *mrb, mrb_value x) { mrb_int chr; char c; chr = mrb_fixnum(x); if (chr >= (1 << CHAR_BIT)) { mrb_raisef(mrb, E_RANGE_ERROR, "%" PRIdMRB_INT " out of char range", chr); } c = (char)chr; return mrb_str_new(mrb, &c, 1); }
static mrb_value struct_members(mrb_state *mrb, mrb_value s) { mrb_value members = struct_s_members(mrb, mrb_obj_class(mrb, s)); if (!mrb_array_p(s)) { mrb_raise(mrb, E_TYPE_ERROR, "corrupted struct"); } if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) { mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%S required %S given)", mrb_fixnum_value(RARRAY_LEN(members)), mrb_fixnum_value(RSTRUCT_LEN(s))); } return members; }
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_value type = inspect_type(mrb, val); mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S to Integer (%S#%S gives %S)", type, type, mrb_str_new_cstr(mrb, method), inspect_type(mrb, v)); } return v; }
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)) { const char *cname = mrb_obj_classname(mrb, val); mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %s to Integer (%s#%s gives %s)", cname, cname, method, mrb_obj_classname(mrb, v)); } return v; }
struct mrb_ffi_type* rclass_to_mrb_ffi_type(mrb_state *mrb, struct RClass *cls) { struct RClass *cls_ = cls; while(cls) { mrb_value ffi_type = mrb_obj_iv_get(mrb, (struct RObject*)cls, mrb_intern(mrb, "@ffi_type")); if(mrb_test(ffi_type)) { return (struct mrb_ffi_type*)DATA_PTR(ffi_type); } cls = cls->super; } mrb_raisef(mrb, E_TYPE_ERROR, "%s cannot convert to C value", mrb_class_name(mrb, cls_)); return NULL; }
MRB_API void mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) { iv_tbl *t = obj->iv; if (MRB_FROZEN_P(obj)) { mrb_raisef(mrb, E_RUNTIME_ERROR, "can't modify frozen %S", mrb_obj_value(obj)); } if (!t) { t = obj->iv = iv_new(mrb); } mrb_write_barrier(mrb, (struct RBasic*)obj); iv_put(mrb, t, sym, v); }
/*! * Ensures a class can be derived from super. * * \param super a reference to an object. * \exception TypeError if \a super is not a Class or \a super is a singleton class. */ void mrb_check_inheritable(mrb_state *mrb, struct RClass *super) { if (super->tt != MRB_TT_CLASS) { mrb_raisef(mrb, E_TYPE_ERROR, "superclass must be a Class (%s given)", mrb_obj_classname(mrb, mrb_obj_value(super))); } if (super->tt == MRB_TT_SCLASS) { mrb_raise(mrb, E_TYPE_ERROR, "can't make subclass of singleton class"); } if (super == mrb->class_class) { mrb_raise(mrb, E_TYPE_ERROR, "can't make subclass of Class"); } }
static mrb_value convert_type(mrb_state *mrb, mrb_value val, const char *tname, const char *method, mrb_bool raise) { mrb_sym m = 0; m = mrb_intern_cstr(mrb, method); if (!mrb_respond_to(mrb, val, m)) { if (raise) { mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %S into %S", inspect_type(mrb, val), mrb_str_new_cstr(mrb, tname)); } return mrb_nil_value(); } return mrb_funcall_argv(mrb, val, m, 0, 0); }
/* * Document-method: Integer#chr * call-seq: * int.chr -> string * * Returns a string containing the character represented by the +int+'s value * according to +encoding+. * * 65.chr #=> "A" * 230.chr #=> "\xE6" */ static mrb_value mrb_int_chr(mrb_state *mrb, mrb_value x) { mrb_int chr; char c; chr = to_int(mrb, x); if (chr >= (1 << CHAR_BIT)) { mrb_raisef(mrb, E_RANGE_ERROR, "%S out of char range", x); } c = (char)chr; return mrb_str_new(mrb, &c, 1); }
static mrb_value replace_contents(mrb_state *mrb, mrb_value self) { mrb_value image; mrb_get_args(mrb, "o", &image); if (!mrb_obj_is_instance_of(mrb, image, mrb_pp_image_data_class)) { mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a PP::Image object", image); } PPB(Graphics2D)->ReplaceContents(MRB_PP_RESOURCE(self), MRB_PP_RESOURCE(image)); return mrb_nil_value(); }
static mrb_value flush(mrb_state *mrb, mrb_value self) { mrb_value cc; int32_t ret; mrb_get_args(mrb, "o", &cc); if (!mrb_obj_is_instance_of(mrb, cc, mrb_pp_completion_callback_class)) { mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a PP::CompletionCallback object", cc); } ret = PPB(Graphics2D)->Flush(MRB_PP_RESOURCE(self), MRB_PP_COMPLETION_CALLBACK(cc)); return mrb_fixnum_value(ret); }
void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t) { const struct types *type = builtin_types; struct RString *s; enum mrb_vtype xt; xt = mrb_type(x); if ((xt != t) || (xt == MRB_TT_DATA)) { while (type->type < MRB_TT_MAXDEFINE) { if (type->type == t) { const char *etype; if (mrb_nil_p(x)) { etype = "nil"; } else if (mrb_type(x) == MRB_TT_FIXNUM) { etype = "Fixnum"; } else if (mrb_type(x) == MRB_TT_SYMBOL) { etype = "Symbol"; } else if (mrb_special_const_p(x)) { s = mrb_str_ptr(mrb_obj_as_string(mrb, x)); etype = s->ptr; } else { etype = mrb_obj_classname(mrb, x); } mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %s (expected %s)", etype, type->name); } type++; } mrb_raisef(mrb, E_TYPE_ERROR, "unknown type 0x%x (0x%x given)", t, mrb_type(x)); } }
static int mrb_io_modestr_to_flags(mrb_state *mrb, const char *mode) { int flags = 0; const char *m = mode; switch (*m++) { case 'r': flags |= FMODE_READABLE; break; case 'w': flags |= FMODE_WRITABLE | FMODE_CREATE | FMODE_TRUNC; break; case 'a': flags |= FMODE_WRITABLE | FMODE_APPEND | FMODE_CREATE; break; default: mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %S", mrb_str_new_cstr(mrb, mode)); } while (*m) { switch (*m++) { case 'b': flags |= FMODE_BINMODE; break; case '+': flags |= FMODE_READWRITE; break; case ':': /* XXX: PASSTHROUGH*/ default: mrb_raisef(mrb, E_ARGUMENT_ERROR, "illegal access mode %S", mrb_str_new_cstr(mrb, mode)); } } return flags; }
grn_obj * grn_mrb_value_to_bulk(mrb_state *mrb, mrb_value mrb_value_, grn_obj *bulk) { grn_ctx *ctx = (grn_ctx *)mrb->ud; switch (mrb_type(mrb_value_)) { case MRB_TT_FALSE : if (mrb_nil_p(mrb_value_)) { grn_obj_reinit(ctx, bulk, GRN_DB_VOID, 0); } else { grn_obj_reinit(ctx, bulk, GRN_DB_BOOL, 0); GRN_BOOL_SET(ctx, bulk, GRN_FALSE); } break; case MRB_TT_TRUE : grn_obj_reinit(ctx, bulk, GRN_DB_BOOL, 0); GRN_BOOL_SET(ctx, bulk, GRN_TRUE); break; case MRB_TT_FIXNUM : grn_obj_reinit(ctx, bulk, GRN_DB_INT64, 0); GRN_INT64_SET(ctx, bulk, mrb_fixnum(mrb_value_)); break; case MRB_TT_SYMBOL : { const char *name; mrb_int name_length; grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); name = mrb_sym2name_len(mrb, mrb_symbol(mrb_value_), &name_length); GRN_TEXT_SET(ctx, bulk, name, name_length); } break; case MRB_TT_FLOAT : grn_obj_reinit(ctx, bulk, GRN_DB_FLOAT, 0); GRN_FLOAT_SET(ctx, bulk, mrb_float(mrb_value_)); break; case MRB_TT_STRING : grn_obj_reinit(ctx, bulk, GRN_DB_TEXT, 0); GRN_TEXT_SET(ctx, bulk, RSTRING_PTR(mrb_value_), RSTRING_LEN(mrb_value_)); break; default : mrb_raisef(mrb, E_ARGUMENT_ERROR, "unsupported object to convert to bulk: %S", mrb_value_); break; } return bulk; }
static mrb_value mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val) { mrb_value members, *ptr, *ptr_members; mrb_int i, len; members = mrb_struct_members(mrb, s); len = RARRAY_LEN(members); if (RSTRUCT_LEN(s) != len) { mrb_raisef(mrb, E_TYPE_ERROR, "struct size differs (%S required %S given)", mrb_fixnum_value(len), mrb_fixnum_value(RSTRUCT_LEN(s))); } ptr = RSTRUCT_PTR(s); ptr_members = RARRAY_PTR(members); for (i=0; i<len; i++) { if (mrb_symbol(ptr_members[i]) == id) { ptr[i] = val; return val; } } mrb_raisef(mrb, E_INDEX_ERROR, "no member '%S' in struct", mrb_sym2str(mrb, id)); return val; /* not reach */ }
mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj) { struct RBasic *p; mrb_value dup; if (mrb_special_const_p(obj)) { mrb_raisef(mrb, E_TYPE_ERROR, "can't dup %S", obj); } p = mrb_obj_alloc(mrb, mrb_type(obj), mrb_obj_class(mrb, obj)); dup = mrb_obj_value(p); init_copy(mrb, dup, obj); return dup; }
static mrb_value mrb_env_getenv(mrb_state *mrb, mrb_value name) { if (mrb_type(name) != MRB_TT_STRING) { mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %s into String", mrb_obj_classname(mrb, name)); return mrb_nil_value(); } char *nam = mrb_string_value_ptr(mrb, name); char *env = getenv(nam); if (env == NULL) { return mrb_nil_value(); } return mrb_str_new2(mrb, env); }
static mrb_value thread_set_limit(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; mrb_int limit; mrb_get_args(mrb, "i", &limit); if (limit < 1) { mrb_raisef(mrb, E_ARGUMENT_ERROR, "thread limit must be 1 or larger: %S", mrb_fixnum_value(limit)); } grn_thread_set_limit_with_ctx(ctx, limit); return mrb_nil_value(); }
mrb_value mrb_mraa_uart_write(mrb_state *mrb, mrb_value self) { mraa_uart_context uart; mraa_result_t result; char *string; size_t l = 0; mrb_get_args(mrb, "s", &string, &l); uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type); result = mraa_uart_write(uart, string, l); if (result < 0) { mrb_raisef(mrb, E_RUNTIME_ERROR, "Could not write (err %S)", mrb_fixnum_value(result)); } return mrb_fixnum_value(result); }
MRB_API void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t) { const struct types *type = builtin_types; enum mrb_vtype xt; xt = mrb_type(x); if ((xt != t) || (xt == MRB_TT_DATA)) { while (type->type < MRB_TT_MAXDEFINE) { if (type->type == t) { const char *etype; if (mrb_nil_p(x)) { etype = "nil"; } else if (mrb_fixnum_p(x)) { etype = "Fixnum"; } else if (mrb_type(x) == MRB_TT_SYMBOL) { etype = "Symbol"; } else if (mrb_immediate_p(x)) { etype = RSTRING_PTR(mrb_obj_as_string(mrb, x)); } else { etype = mrb_obj_classname(mrb, x); } mrb_raisef(mrb, E_TYPE_ERROR, "wrong argument type %S (expected %S)", mrb_str_new_cstr(mrb, etype), mrb_str_new_cstr(mrb, type->name)); } type++; } mrb_raisef(mrb, E_TYPE_ERROR, "unknown type %S (%S given)", mrb_fixnum_value(t), mrb_fixnum_value(mrb_type(x))); } }
static mrb_value get_sym_or_str_arg(mrb_state *mrb) { mrb_value sym_or_str; mrb_get_args(mrb, "o", &sym_or_str); if (mrb_symbol_p(sym_or_str) || mrb_string_p(sym_or_str)) { return sym_or_str; } else { mrb_value obj = mrb_funcall(mrb, sym_or_str, "inspect", 0); mrb_raisef(mrb, E_TYPE_ERROR, "%S is not a symbol", obj); return mrb_nil_value(); } }
static mrb_value mrb_struct_aset(mrb_state *mrb, mrb_value s) { mrb_int i; mrb_value idx; mrb_value val; mrb_get_args(mrb, "oo", &idx, &val); if (mrb_string_p(idx)) { mrb_value sym = mrb_check_intern_str(mrb, idx); if (mrb_nil_p(sym)) { mrb_name_error(mrb, mrb_intern_str(mrb, idx), "no member '%S' in struct", idx); } idx = sym; } if (mrb_symbol_p(idx)) { return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val); } i = mrb_int(mrb, idx); if (i < 0) i = RSTRUCT_LEN(s) + i; if (i < 0) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too small for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } if (RSTRUCT_LEN(s) <= i) { mrb_raisef(mrb, E_INDEX_ERROR, "offset %S too large for struct(size:%S)", mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s))); } mrb_struct_modify(mrb, s); return RSTRUCT_PTR(s)[i] = val; }
static mrb_value make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass) { mrb_value nstr, *ptr_members; mrb_sym id; mrb_int i, len; struct RClass *c; if (mrb_nil_p(name)) { c = mrb_class_new(mrb, klass); } else { /* old style: should we warn? */ name = mrb_str_to_str(mrb, name); id = mrb_to_id(mrb, name); if (!mrb_is_const_id(id)) { mrb_raisef(mrb, E_NAME_ERROR, "identifier %s needs to be constant", mrb_string_value_ptr(mrb, name)); } if (mrb_const_defined_at(mrb, klass, id)) { mrb_warn("redefining constant Struct::%s", mrb_string_value_ptr(mrb, name)); //?rb_mod_remove_const(klass, mrb_sym2name(mrb, id)); } c = mrb_define_class_under(mrb, klass, RSTRING_PTR(name), klass); } MRB_SET_INSTANCE_TT(c, MRB_TT_ARRAY); nstr = mrb_obj_value(c); mrb_iv_set(mrb, nstr, mrb_intern2(mrb, "__members__", 11), members); mrb_define_class_method(mrb, c, "new", mrb_instance_new, ARGS_ANY()); mrb_define_class_method(mrb, c, "[]", mrb_instance_new, ARGS_ANY()); mrb_define_class_method(mrb, c, "members", mrb_struct_s_members_m, ARGS_NONE()); //RSTRUCT(nstr)->basic.c->super = c->c; ptr_members = RARRAY_PTR(members); len = RARRAY_LEN(members); for (i=0; i< len; i++) { mrb_sym id = mrb_symbol(ptr_members[i]); if (mrb_is_local_id(id) || mrb_is_const_id(id)) { if (i < N_REF_FUNC) { mrb_define_method_id(mrb, c, id, ref_func[i], ARGS_NONE()); } else { mrb_define_method_id(mrb, c, id, mrb_struct_ref, ARGS_NONE()); } mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, ARGS_REQ(1)); } } return nstr; }
TMO mrb_timeout_value(mrb_state *mrb, mrb_value tmo) { if (mrb_fixnum_p(tmo)) { return (TMO)mrb_fixnum(tmo); } if (mrb_symbol_p(tmo)) { if (mrb_symbol(tmo) == mrb_intern_lit(mrb, "polling")) { return (TMO)TMO_POL; } if (mrb_symbol(tmo) == mrb_intern_lit(mrb, "forever")) { return (TMO)TMO_FEVR; } } mrb_raisef(mrb, E_TYPE_ERROR, "wrong timeout value (%S)", tmo); }
/* * call-seq: * obj.clone -> an_object * * Produces a shallow copy of <i>obj</i>---the instance variables of * <i>obj</i> are copied, but not the objects they reference. Copies * the frozen state of <i>obj</i>. See also the discussion * under <code>Object#dup</code>. * * class Klass * attr_accessor :str * end * s1 = Klass.new #=> #<Klass:0x401b3a38> * s1.str = "Hello" #=> "Hello" * s2 = s1.clone #=> #<Klass:0x401b3998 @str="Hello"> * s2.str[1,4] = "i" #=> "i" * s1.inspect #=> "#<Klass:0x401b3a38 @str=\"Hi\">" * s2.inspect #=> "#<Klass:0x401b3998 @str=\"Hi\">" * * This method may have class-specific behavior. If so, that * behavior will be documented under the #+initialize_copy+ method of * the class. * * Some Class(True False Nil Symbol Fixnum Float) Object cannot clone. */ mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self) { struct RObject *p; mrb_value clone; if (mrb_special_const_p(self)) { mrb_raisef(mrb, E_TYPE_ERROR, "can't clone %S", self); } p = (struct RObject*)mrb_obj_alloc(mrb, mrb_type(self), mrb_obj_class(mrb, self)); p->c = mrb_singleton_class_clone(mrb, self); clone = mrb_obj_value(p); init_copy(mrb, clone, self); return clone; }
/* * call-seq: * LED.color = col # => nil * * Light LED. * * Parameters: * +col+ LED color * LED::OFF * LED::RED * LED::GREEN * LED::ORANGE * * Returns nil. */ static mrb_value mrb_led_color(mrb_state *mrb, mrb_value self) { mrb_value cmap = mrb_const_get(mrb, self, mrb_intern_lit(mrb, "COLOR")); mrb_sym col; mrb_value colv; mrb_get_args(mrb, "n", &col); colv = mrb_hash_get(mrb, cmap, mrb_symbol_value(col)); if (mrb_nil_p(colv)) { mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid color :%S", mrb_sym2str(mrb, col)); } ev3_led_set_color(mrb_fixnum(colv)); return mrb_nil_value(); }
struct RProc* mrb_method_search(mrb_state *mrb, struct RClass* c, mrb_sym mid) { struct RProc *m; m = mrb_method_search_vm(mrb, &c, mid); if (!m) { mrb_value inspect = mrb_funcall(mrb, mrb_obj_value(c), "inspect", 0); if (RSTRING_LEN(inspect) > 64) { inspect = mrb_any_to_s(mrb, mrb_obj_value(c)); } mrb_raisef(mrb, E_NAME_ERROR, "undefined method '%s' for class %s", mrb_sym2name(mrb, mid), RSTRING_PTR(inspect)); } return m; }
MRB_API void mrb_obj_iv_set(mrb_state *mrb, struct RObject *obj, mrb_sym sym, mrb_value v) { iv_tbl *t; if (MRB_FROZEN_P(obj)) { mrb_raisef(mrb, E_FROZEN_ERROR, "can't modify frozen %S", mrb_obj_value(obj)); } assign_class_name(mrb, obj, sym, v); if (!obj->iv) { obj->iv = iv_new(mrb); } t = obj->iv; iv_put(mrb, t, sym, v); mrb_write_barrier(mrb, (struct RBasic*)obj); }
static mrb_value mrb_str_aref_m(mrb_state *mrb, mrb_value str) { mrb_value a1, a2; int argc; argc = mrb_get_args(mrb, "o|o", &a1, &a2); if (argc == 2) { regexp_check(mrb, a1); return str_substr(mrb, str, mrb_fixnum(a1), mrb_fixnum(a2)); } if (argc != 1) { mrb_raisef(mrb, E_ARGUMENT_ERROR, "wrong number of arguments (%S for 1)", mrb_fixnum_value(argc)); } return mrb_str_aref(mrb, str, a1); }
mrb_value mrb_redis_select(mrb_state *mrb, mrb_value self) { mrb_value database; mrb_get_args(mrb, "o", &database); redisContext *rc = mrb_redis_get_context(mrb, self); if (mrb_type(database) != MRB_TT_FIXNUM) { mrb_raisef(mrb, E_TYPE_ERROR, "type mismatch: %S given", database); } redisReply *rs = redisCommand(rc, "SELECT %d", mrb_fixnum(database)); freeReplyObject(rs); return self; }